VirtualMailManager/Account.py
changeset 121 7ccc05774118
parent 115 21f264a88ab2
child 128 cf8116625866
equal deleted inserted replaced
120:928659c8ee9f 121:7ccc05774118
    12 from MailLocation import MailLocation
    12 from MailLocation import MailLocation
    13 from EmailAddress import EmailAddress
    13 from EmailAddress import EmailAddress
    14 import VirtualMailManager as VMM
    14 import VirtualMailManager as VMM
    15 import constants.ERROR as ERR
    15 import constants.ERROR as ERR
    16 
    16 
    17 class Account:
    17 class Account(object):
    18     """Class to manage e-mail accounts."""
    18     """Class to manage e-mail accounts."""
       
    19     __slots__ = ('_addr','_base','_gid','_mid','_passwd','_tid','_uid','_dbh')
    19     def __init__(self, dbh, address, password=None):
    20     def __init__(self, dbh, address, password=None):
    20         self._dbh = dbh
    21         self._dbh = dbh
    21         self._base = None
    22         self._base = None
    22         if isinstance(address, EmailAddress):
    23         if isinstance(address, EmailAddress):
    23             self._addr = address
    24             self._addr = address
   150                     ERR.NO_SUCH_ACCOUNT)
   151                     ERR.NO_SUCH_ACCOUNT)
   151         if what not in ['name', 'password', 'transport']:
   152         if what not in ['name', 'password', 'transport']:
   152             return False
   153             return False
   153         dbc = self._dbh.cursor()
   154         dbc = self._dbh.cursor()
   154         if what == 'password':
   155         if what == 'password':
   155             dbc.execute("UPDATE users SET passwd=%s WHERE local_part=%s AND\
   156             dbc.execute('UPDATE users SET passwd = %s WHERE uid = %s',
   156  gid=%s", value, self._addr._localpart, self._gid)
   157                     value, self._uid)
   157         elif what == 'transport':
   158         elif what == 'transport':
   158             self._tid = Transport(self._dbh, transport=value).getID()
   159             self._tid = Transport(self._dbh, transport=value).getID()
   159             dbc.execute("UPDATE users SET tid=%s WHERE local_part=%s AND\
   160             dbc.execute('UPDATE users SET tid = %s WHERE uid = %s',
   160  gid=%s", self._tid, self._addr._localpart, self._gid)
   161                     self._tid, self._uid)
   161         else:
   162         else:
   162             dbc.execute("UPDATE users SET name=%s WHERE local_part=%s AND\
   163             dbc.execute('UPDATE users SET name = %s WHERE uid = %s',
   163  gid=%s", value, self._addr._localpart, self._gid)
   164                     value, self._uid)
   164         if dbc.rowcount > 0:
   165         if dbc.rowcount > 0:
   165             self._dbh.commit()
   166             self._dbh.commit()
   166         dbc.close()
   167         dbc.close()
   167 
   168 
   168     def getInfo(self, dcvers):
   169     def getInfo(self, dcvers):
   210         if self._uid < 1:
   211         if self._uid < 1:
   211             raise AccE(_(u"The account »%s« doesn't exists.") % self._addr,
   212             raise AccE(_(u"The account »%s« doesn't exists.") % self._addr,
   212                     ERR.NO_SUCH_ACCOUNT)
   213                     ERR.NO_SUCH_ACCOUNT)
   213         dbc = self._dbh.cursor()
   214         dbc = self._dbh.cursor()
   214         if delalias == 'delalias':
   215         if delalias == 'delalias':
   215             dbc.execute("DELETE FROM users WHERE gid=%s AND local_part=%s",
   216             dbc.execute('DELETE FROM users WHERE uid= %s', self._uid)
   216                     self._gid, self._addr._localpart)
       
   217             u_rc = dbc.rowcount
   217             u_rc = dbc.rowcount
   218             # delete also all aliases where the destination address is the same
   218             # delete also all aliases where the destination address is the same
   219             # as for this account.
   219             # as for this account.
   220             dbc.execute("DELETE FROM alias WHERE destination = %s",
   220             dbc.execute("DELETE FROM alias WHERE destination = %s",
   221                     str(self._addr))
   221                     str(self._addr))
   222             if u_rc > 0 or dbc.rowcount > 0:
   222             if u_rc > 0 or dbc.rowcount > 0:
   223                 self._dbh.commit()
   223                 self._dbh.commit()
   224         else: # check first for aliases
   224         else: # check first for aliases
   225             a_count = self.__aliaseCount()
   225             a_count = self.__aliaseCount()
   226             if a_count == 0:
   226             if a_count == 0:
   227                 dbc.execute("DELETE FROM users WHERE gid=%s AND local_part=%s",
   227                 dbc.execute('DELETE FROM users WHERE uid = %s', self._uid)
   228                         self._gid, self._addr._localpart)
       
   229                 if dbc.rowcount > 0:
   228                 if dbc.rowcount > 0:
   230                     self._dbh.commit()
   229                     self._dbh.commit()
   231             else:
   230             else:
   232                 dbc.close()
   231                 dbc.close()
   233                 raise AccE(
   232                 raise AccE(