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