VirtualMailManager/Account.py
changeset 9 e3d3dbeb5b84
parent 8 7e3ce56f49e6
child 17 fe9be0081e5f
equal deleted inserted replaced
8:7e3ce56f49e6 9:e3d3dbeb5b84
    13 __revision__ = 'rev '+'$Rev$'.split()[1]
    13 __revision__ = 'rev '+'$Rev$'.split()[1]
    14 __date__ = '$Date$'.split()[1]
    14 __date__ = '$Date$'.split()[1]
    15 
    15 
    16 from Exceptions import VMMAccountException
    16 from Exceptions import VMMAccountException
    17 from Domain import Domain
    17 from Domain import Domain
       
    18 from Transport import Transport
       
    19 from MailLocation import MailLocation
    18 import constants.ERROR as ERR
    20 import constants.ERROR as ERR
    19 
    21 
    20 class Account:
    22 class Account:
    21     """Class to manage e-mail accounts."""
    23     """Class to manage e-mail accounts."""
    22     def __init__(self, dbh, basedir, address, password=None):
    24     def __init__(self, dbh, address, password=None):
    23         self._dbh = dbh
    25         self._dbh = dbh
    24         self._base = basedir
       
    25         self._base = None
    26         self._base = None
    26         self._addr = address
    27         self._addr = address
    27         self._localpart = None
    28         self._localpart = None
    28         self._name = None
    29         self._name = None
    29         self._uid = 0
    30         self._uid = 0
    30         self._gid = 0
    31         self._gid = 0
       
    32         self._mid = 0
       
    33         self._tid = 0
    31         self._passwd = password
    34         self._passwd = password
    32         self._home = None
       
    33         self._setAddr(address)
    35         self._setAddr(address)
    34         self._exists()
    36         self._exists()
    35         if self._isAlias():
    37         if self._isAlias():
    36             raise VMMAccountException(
    38             raise VMMAccountException(
    37             ('There is already an alias with address »%s«' % address,
    39             ('There is already an alias with address »%s«' % address,
    38                 ERR.ALIAS_EXISTS))
    40                 ERR.ALIAS_EXISTS))
    39 
    41 
    40     def _exists(self):
    42     def _exists(self):
    41         dbc = self._dbh.cursor()
    43         dbc = self._dbh.cursor()
    42         dbc.execute("SELECT uid FROM users WHERE gid=%s AND local_part=%s",
    44         dbc.execute("SELECT uid, mid, tid FROM users \
       
    45 WHERE gid=%s AND local_part=%s",
    43                 self._gid, self._localpart)
    46                 self._gid, self._localpart)
    44         uid = dbc.fetchone()
    47         result = dbc.fetchone()
    45         dbc.close()
    48         dbc.close()
    46         if uid is not None:
    49         if result is not None:
    47             self._uid = uid[0]
    50             self._uid, self._mid, self._tid = result
    48             return True
    51             return True
    49         else:
    52         else:
    50             return False
    53             return False
    51 
    54 
    52     def _isAlias(self):
    55     def _isAlias(self):
    53         dbc = self._dbh.cursor()
    56         dbc = self._dbh.cursor()
    54         dbc.execute("SELECT id FROM alias WHERE gid=%s AND address=%s",
    57         dbc.execute("SELECT gid FROM alias WHERE gid=%s AND address=%s",
    55                 self._gid, self._localpart)
    58                 self._gid, self._localpart)
    56         aid = dbc.fetchone()
    59         gid = dbc.fetchone()
    57         dbc.close()
    60         dbc.close()
    58         if aid is not None:
    61         if gid is not None:
    59             return True
    62             return True
    60         else:
    63         else:
    61             return False
    64             return False
    62 
    65 
    63     def _setAddr(self, address):
    66     def _setAddr(self, address):
    64         self._localpart, d = address.split('@')
    67         self._localpart, d = address.split('@')
    65         dom = Domain(self._dbh, d, self._base)
    68         dom = Domain(self._dbh, d)
    66         self._gid = dom.getID()
    69         self._gid = dom.getID()
    67         self._base = dom.getDir()
       
    68         if self._gid == 0:
    70         if self._gid == 0:
    69             raise VMMAccountException(("Domain »%s« doesn't exist." % d,
    71             raise VMMAccountException(("Domain »%s« doesn't exist." % d,
    70                 ERR.NO_SUCH_DOMAIN))
    72                 ERR.NO_SUCH_DOMAIN))
       
    73         self._base = dom.getDir()
       
    74         self._tid = dom.getTransportID()
    71 
    75 
    72     def _setID(self):
    76     def _setID(self):
    73         dbc = self._dbh.cursor()
    77         dbc = self._dbh.cursor()
    74         dbc.execute("SELECT nextval('users_uid')")
    78         dbc.execute("SELECT nextval('users_uid')")
    75         self._uid = dbc.fetchone()[0]
    79         self._uid = dbc.fetchone()[0]
    76         dbc.close()
    80         dbc.close()
    77 
    81 
    78     def _prepare(self):
    82     def _prepare(self, maillocation):
    79         self._setID()
    83         self._setID()
    80         self._home = "%i" % self._uid
    84         self._mid = MailLocation(self._dbh, maillocation=maillocation).getID()
    81 
    85 
    82     def _switchState(self, state):
    86     def _switchState(self, state):
    83         if not isinstance(state, bool):
    87         if not isinstance(state, bool):
    84             return False
    88             return False
    85         if self._uid < 1:
    89         if self._uid < 1:
   108         self._switchState(False)
   112         self._switchState(False)
   109 
   113 
   110     def disable(self):
   114     def disable(self):
   111         self._switchState(True)
   115         self._switchState(True)
   112 
   116 
   113     def save(self, mail):
   117     def save(self, maillocation):
   114         if self._uid < 1:
   118         if self._uid < 1:
   115             self._prepare()
   119             self._prepare(maillocation)
   116             dbc = self._dbh.cursor()
   120             dbc = self._dbh.cursor()
   117             dbc.execute("""INSERT INTO users (local_part, passwd, uid, gid,\
   121             dbc.execute("""INSERT INTO users (local_part, passwd, uid, gid,\
   118  home, mail) VALUES (%s, %s, %s, %s, %s, %s)""", self._localpart,
   122  mid, tid) VALUES (%s, %s, %s, %s, %s, %s)""", self._localpart, self._passwd,
   119                     self._passwd, self._uid, self._gid, self._home, mail)
   123                     self._uid, self._gid, self._mid, self._tid )
   120             self._dbh.commit()
   124             self._dbh.commit()
   121             dbc.close()
   125             dbc.close()
   122         else:
   126         else:
   123             raise VMMAccountException(('Account already exists.',
   127             raise VMMAccountException(('Account already exists.',
   124                 ERR.ACCOUNT_EXISTS))
   128                 ERR.ACCOUNT_EXISTS))
   140             self._dbh.commit()
   144             self._dbh.commit()
   141         dbc.close()
   145         dbc.close()
   142 
   146 
   143     def getInfo(self):
   147     def getInfo(self):
   144         dbc = self._dbh.cursor()
   148         dbc = self._dbh.cursor()
   145         dbc.execute("SELECT name, uid, gid, home, mail, disabled FROM users\
   149         dbc.execute("SELECT name, uid, gid, mid, tid, disabled FROM users\
   146  WHERE local_part=%s AND gid=%s", self._localpart, self._gid)
   150  WHERE local_part=%s AND gid=%s", self._localpart, self._gid)
   147         info = dbc.fetchone()
   151         info = dbc.fetchone()
   148         dbc.close()
   152         dbc.close()
   149         if info is None:
   153         if info is None:
   150             raise VMMAccountException(("Account doesn't exists",
   154             raise VMMAccountException(("Account doesn't exists",
   151                 ERR.NO_SUCH_ACCOUNT))
   155                 ERR.NO_SUCH_ACCOUNT))
   152         else:
   156         else:
   153             keys = ['name', 'uid', 'gid', 'home', 'mail', 'disabled']
   157             keys = ['name', 'uid', 'gid', 'maildir', 'transport', 'disabled']
   154             info = dict(zip(keys, info))
   158             info = dict(zip(keys, info))
   155             if bool(info['disabled']):
   159             if bool(info['disabled']):
   156                 info['disabled'] = 'Yes'
   160                 info['disabled'] = 'Yes'
   157             else:
   161             else:
   158                 info['disabled'] = 'No'
   162                 info['disabled'] = 'No'
   159             info['address'] = self._addr
   163             info['address'] = self._addr
   160             info['home'] = '%s/%s' % (self._base, info['home'])
   164             info['maildir'] = '%s/%s/%s' % (self._base, info['uid'],
       
   165                     MailLocation(self._dbh,
       
   166                         mid=info['maildir']).getMailLocation())
       
   167             info['transport'] = Transport(self._dbh,
       
   168                     tid=info['transport']).getTransport()
   161             return info
   169             return info
   162 
   170 
   163     def delete(self):
   171     def delete(self):
   164         if self._uid > 0:
   172         if self._uid > 0:
   165             dbc = self._dbh.cursor()
   173             dbc = self._dbh.cursor()