VirtualMailManager/Account.py
changeset 55 15c873f94ba6
parent 48 0d5f58f8b8f5
child 65 5506433db9a3
equal deleted inserted replaced
54:1fc1f82c662f 55:15c873f94ba6
    11 __author__ = 'Pascal Volk <p.volk@veb-it.de>'
    11 __author__ = 'Pascal Volk <p.volk@veb-it.de>'
    12 __version__ = VERSION
    12 __version__ = VERSION
    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 as AccE
    17 from Domain import Domain
    17 from Domain import Domain
    18 from Transport import Transport
    18 from Transport import Transport
    19 from MailLocation import MailLocation
    19 from MailLocation import MailLocation
    20 import VirtualMailManager as VMM
    20 import VirtualMailManager as VMM
    21 import constants.ERROR as ERR
    21 import constants.ERROR as ERR
    34         self._tid = 0
    34         self._tid = 0
    35         self._passwd = password
    35         self._passwd = password
    36         self._setAddr()
    36         self._setAddr()
    37         self._exists()
    37         self._exists()
    38         if self._isAlias():
    38         if self._isAlias():
    39             raise VMMAccountException(
    39             raise AccE(_(u"There is already an alias with the address »%s«.") %\
    40             _(u"There is already an alias with the address »%s«.") % address,
    40                     address, ERR.ALIAS_EXISTS)
    41                 ERR.ALIAS_EXISTS)
       
    42 
    41 
    43     def _exists(self):
    42     def _exists(self):
    44         dbc = self._dbh.cursor()
    43         dbc = self._dbh.cursor()
    45         dbc.execute("SELECT uid, mid, tid FROM users \
    44         dbc.execute("SELECT uid, mid, tid FROM users \
    46 WHERE gid=%s AND local_part=%s",
    45 WHERE gid=%s AND local_part=%s",
    67     def _setAddr(self):
    66     def _setAddr(self):
    68         self._localpart, d = self._addr.split('@')
    67         self._localpart, d = self._addr.split('@')
    69         dom = Domain(self._dbh, d)
    68         dom = Domain(self._dbh, d)
    70         self._gid = dom.getID()
    69         self._gid = dom.getID()
    71         if self._gid == 0:
    70         if self._gid == 0:
    72             raise VMMAccountException(_(u"Domain »%s« doesn't exist.") % d,
    71             raise AccE(_(u"The domain »%s« doesn't exist yet.") % d,
    73                 ERR.NO_SUCH_DOMAIN)
    72                 ERR.NO_SUCH_DOMAIN)
    74         self._base = dom.getDir()
    73         self._base = dom.getDir()
    75         self._tid = dom.getTransportID()
    74         self._tid = dom.getTransportID()
    76 
    75 
    77     def _setID(self):
    76     def _setID(self):
    86 
    85 
    87     def _switchState(self, state, service):
    86     def _switchState(self, state, service):
    88         if not isinstance(state, bool):
    87         if not isinstance(state, bool):
    89             return False
    88             return False
    90         if not service in ['smtp', 'pop3', 'imap', 'managesieve', 'all', None]:
    89         if not service in ['smtp', 'pop3', 'imap', 'managesieve', 'all', None]:
    91             raise VMMAccountException(_(u"Unknown service »%s«.") % service,
    90             raise AccE(_(u"Unknown service »%s«.") % service,
    92                 ERR.UNKNOWN_SERVICE)
    91                     ERR.UNKNOWN_SERVICE)
    93         if self._uid < 1:
    92         if self._uid < 1:
    94             raise VMMAccountException(_(u"The account »%s« doesn't exists.") %
    93             raise AccE(_(u"The account »%s« doesn't exists.") % self._addr,
    95                 self._addr, ERR.NO_SUCH_ACCOUNT)
    94                     ERR.NO_SUCH_ACCOUNT)
    96         dbc = self._dbh.cursor()
    95         dbc = self._dbh.cursor()
    97         if service in ['smtp', 'pop3', 'imap', 'managesieve']:
    96         if service in ['smtp', 'pop3', 'imap', 'managesieve']:
    98             dbc.execute(
    97             dbc.execute(
    99                     "UPDATE users SET %s=%s WHERE local_part='%s' AND gid=%s"
    98                     "UPDATE users SET %s=%s WHERE local_part='%s' AND gid=%s"
   100                     % (service, state, self._localpart, self._gid))
    99                     % (service, state, self._localpart, self._gid))
   141                 self._localpart, self._passwd, self._uid, self._gid, self._mid,
   140                 self._localpart, self._passwd, self._uid, self._gid, self._mid,
   142                 self._tid, smtp, pop3, imap, managesieve)
   141                 self._tid, smtp, pop3, imap, managesieve)
   143             self._dbh.commit()
   142             self._dbh.commit()
   144             dbc.close()
   143             dbc.close()
   145         else:
   144         else:
   146             raise VMMAccountException(_(u'The account »%s« already exists.') %
   145             raise AccE(_(u'The account »%s« already exists.') % self._addr,
   147                 self._addr, ERR.ACCOUNT_EXISTS)
   146                     ERR.ACCOUNT_EXISTS)
   148        
   147        
   149     def modify(self, what, value):
   148     def modify(self, what, value):
   150         if self._uid == 0:
   149         if self._uid == 0:
   151             raise VMMAccountException(_(u"The account »%s« doesn't exists.") %
   150             raise AccE(_(u"The account »%s« doesn't exists.") % self._addr,
   152                 self._addr, ERR.NO_SUCH_ACCOUNT)
   151                     ERR.NO_SUCH_ACCOUNT)
   153         if what not in ['name', 'password', 'transport']:
   152         if what not in ['name', 'password', 'transport']:
   154             return False
   153             return False
   155         dbc = self._dbh.cursor()
   154         dbc = self._dbh.cursor()
   156         if what == 'password':
   155         if what == 'password':
   157             dbc.execute("UPDATE users SET passwd=%s WHERE local_part=%s AND\
   156             dbc.execute("UPDATE users SET passwd=%s WHERE local_part=%s AND\
   173  managesieve FROM users WHERE local_part=%s AND gid=%s",
   172  managesieve FROM users WHERE local_part=%s AND gid=%s",
   174             self._localpart, self._gid)
   173             self._localpart, self._gid)
   175         info = dbc.fetchone()
   174         info = dbc.fetchone()
   176         dbc.close()
   175         dbc.close()
   177         if info is None:
   176         if info is None:
   178             raise VMMAccountException(_(u"The account »%s« doesn't exists.") %
   177             raise AccE(_(u"The account »%s« doesn't exists.") % self._addr,
   179                 self._addr, ERR.NO_SUCH_ACCOUNT)
   178                     ERR.NO_SUCH_ACCOUNT)
   180         else:
   179         else:
   181             keys = ['name', 'uid', 'gid', 'maildir', 'transport', 'smtp',
   180             keys = ['name', 'uid', 'gid', 'maildir', 'transport', 'smtp',
   182                     'pop3', 'imap', 'managesieve']
   181                     'pop3', 'imap', 'managesieve']
   183             info = dict(zip(keys, info))
   182             info = dict(zip(keys, info))
   184             for service in ['smtp', 'pop3', 'imap', 'managesieve']:
   183             for service in ['smtp', 'pop3', 'imap', 'managesieve']:
   201                     self._gid, self._localpart)
   200                     self._gid, self._localpart)
   202             if dbc.rowcount > 0:
   201             if dbc.rowcount > 0:
   203                 self._dbh.commit()
   202                 self._dbh.commit()
   204             dbc.close()
   203             dbc.close()
   205         else:
   204         else:
   206             raise VMMAccountException(_(u"The account »%s« doesn't exists.") %
   205             raise AccE(_(u"The account »%s« doesn't exists.") % self._addr,
   207                 self._addr, ERR.NO_SUCH_ACCOUNT)
   206                     ERR.NO_SUCH_ACCOUNT)
   208 
   207 
   209 
   208 
   210 def getAccountByID(uid, dbh):
   209 def getAccountByID(uid, dbh):
   211     try:
   210     try:
   212         uid = long(uid)
   211         uid = long(uid)
   213     except ValueError:
   212     except ValueError:
   214         raise VMMAccountException(_(u'uid must be an int/long.'),
   213         raise AccE(_(u'uid must be an int/long.'), ERR.INVALID_AGUMENT)
   215             ERR.INVALID_AGUMENT)
       
   216     if uid < 1:
   214     if uid < 1:
   217         raise VMMAccountException(_(u'uid must be greater than 0.'),
   215         raise AccE(_(u'uid must be greater than 0.'), ERR.INVALID_AGUMENT)
   218             ERR.INVALID_AGUMENT)
       
   219     dbc = dbh.cursor()
   216     dbc = dbh.cursor()
   220     dbc.execute("SELECT local_part||'@'|| domain_name.domainname AS address,\
   217     dbc.execute("SELECT local_part||'@'|| domain_name.domainname AS address,\
   221  uid, users.gid FROM users LEFT JOIN domain_name ON (domain_name.gid \
   218  uid, users.gid FROM users LEFT JOIN domain_name ON (domain_name.gid \
   222  = users.gid AND is_primary) WHERE uid = %s;", uid)
   219  = users.gid AND is_primary) WHERE uid = %s;", uid)
   223     info = dbc.fetchone()
   220     info = dbc.fetchone()
   224     dbc.close()
   221     dbc.close()
   225     if info is None:
   222     if info is None:
   226         raise VMMAccountException(
   223         raise AccE(_(u"There is no account with the UID »%d«.") % uid, 
   227             _(u"There is no account with the UID »%d«.") % uid,
   224                 ERR.NO_SUCH_ACCOUNT)
   228             ERR.NO_SUCH_ACCOUNT)
       
   229     keys = ['address', 'uid', 'gid']
   225     keys = ['address', 'uid', 'gid']
   230     info = dict(zip(keys, info))
   226     info = dict(zip(keys, info))
   231     return info
   227     return info
   232 
   228