VirtualMailManager/Account.py
changeset 133 2d5c4745efec
parent 128 cf8116625866
child 134 6ca3d22e5dd0
equal deleted inserted replaced
132:fa22bd13b4d1 133:2d5c4745efec
    31         self._passwd = password
    31         self._passwd = password
    32         self._setAddr()
    32         self._setAddr()
    33         self._exists()
    33         self._exists()
    34         if self._uid < 1 and VMM.VirtualMailManager.aliasExists(self._dbh,
    34         if self._uid < 1 and VMM.VirtualMailManager.aliasExists(self._dbh,
    35                 self._addr):
    35                 self._addr):
    36             raise AccE(_(u"There is already an alias with the address »%s«.") %\
    36             raise AccE(_(u"There is already an alias with the address “%s”.") %\
    37                     self._addr, ERR.ALIAS_EXISTS)
    37                     self._addr, ERR.ALIAS_EXISTS)
    38         if self._uid < 1 and VMM.VirtualMailManager.relocatedExists(self._dbh,
    38         if self._uid < 1 and VMM.VirtualMailManager.relocatedExists(self._dbh,
    39                 self._addr):
    39                 self._addr):
    40             raise AccE(
    40             raise AccE(
    41               _(u"There is already a relocated user with the address »%s«.") %\
    41               _(u"There is already a relocated user with the address “%s”.") %\
    42                     self._addr, ERR.RELOCATED_EXISTS)
    42                     self._addr, ERR.RELOCATED_EXISTS)
    43 
    43 
    44     def _exists(self):
    44     def _exists(self):
    45         dbc = self._dbh.cursor()
    45         dbc = self._dbh.cursor()
    46         dbc.execute("SELECT uid, mid, tid FROM users \
    46         dbc.execute("SELECT uid, mid, tid FROM users \
    56 
    56 
    57     def _setAddr(self):
    57     def _setAddr(self):
    58         dom = Domain(self._dbh, self._addr._domainname)
    58         dom = Domain(self._dbh, self._addr._domainname)
    59         self._gid = dom.getID()
    59         self._gid = dom.getID()
    60         if self._gid == 0:
    60         if self._gid == 0:
    61             raise AccE(_(u"The domain »%s« doesn't exist yet.") %\
    61             raise AccE(_(u"The domain “%s” doesn't exist yet.") %\
    62                     self._addr._domainname, ERR.NO_SUCH_DOMAIN)
    62                     self._addr._domainname, ERR.NO_SUCH_DOMAIN)
    63         self._base = dom.getDir()
    63         self._base = dom.getDir()
    64         self._tid = dom.getTransportID()
    64         self._tid = dom.getTransportID()
    65 
    65 
    66     def _setID(self):
    66     def _setID(self):
    75 
    75 
    76     def _switchState(self, state, dcvers, service):
    76     def _switchState(self, state, dcvers, service):
    77         if not isinstance(state, bool):
    77         if not isinstance(state, bool):
    78             return False
    78             return False
    79         if not service in (None, 'all', 'imap', 'pop3', 'sieve', 'smtp'):
    79         if not service in (None, 'all', 'imap', 'pop3', 'sieve', 'smtp'):
    80             raise AccE(_(u"Unknown service »%s«.") % service,
    80             raise AccE(_(u"Unknown service “%s”.") % service,
    81                     ERR.UNKNOWN_SERVICE)
    81                     ERR.UNKNOWN_SERVICE)
    82         if self._uid < 1:
    82         if self._uid < 1:
    83             raise AccE(_(u"The account »%s« doesn't exists.") % self._addr,
    83             raise AccE(_(u"The account “%s” doesn't exists.") % self._addr,
    84                     ERR.NO_SUCH_ACCOUNT)
    84                     ERR.NO_SUCH_ACCOUNT)
    85         sieve_col = 'sieve' if dcvers > 11 else 'managesieve'
    85         sieve_col = 'sieve' if dcvers > 11 else 'managesieve'
    86         if service in ('smtp', 'pop3', 'imap'):
    86         if service in ('smtp', 'pop3', 'imap'):
    87             sql = 'UPDATE users SET %s = %s WHERE uid = %d' % (service, state,
    87             sql = 'UPDATE users SET %s = %s WHERE uid = %d' % (service, state,
    88                     self._uid)
    88                     self._uid)
   140             dbc = self._dbh.cursor()
   140             dbc = self._dbh.cursor()
   141             dbc.execute(sql)
   141             dbc.execute(sql)
   142             self._dbh.commit()
   142             self._dbh.commit()
   143             dbc.close()
   143             dbc.close()
   144         else:
   144         else:
   145             raise AccE(_(u'The account »%s« already exists.') % self._addr,
   145             raise AccE(_(u'The account “%s” already exists.') % self._addr,
   146                     ERR.ACCOUNT_EXISTS)
   146                     ERR.ACCOUNT_EXISTS)
   147 
   147 
   148     def modify(self, what, value):
   148     def modify(self, what, value):
   149         if self._uid == 0:
   149         if self._uid == 0:
   150             raise AccE(_(u"The account »%s« doesn't exists.") % self._addr,
   150             raise AccE(_(u"The account “%s” doesn't exists.") % self._addr,
   151                     ERR.NO_SUCH_ACCOUNT)
   151                     ERR.NO_SUCH_ACCOUNT)
   152         if what not in ['name', 'password', 'transport']:
   152         if what not in ['name', 'password', 'transport']:
   153             return False
   153             return False
   154         dbc = self._dbh.cursor()
   154         dbc = self._dbh.cursor()
   155         if what == 'password':
   155         if what == 'password':
   173         dbc = self._dbh.cursor()
   173         dbc = self._dbh.cursor()
   174         dbc.execute(sql)
   174         dbc.execute(sql)
   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 AccE(_(u"The account »%s« doesn't exists.") % self._addr,
   178             raise AccE(_(u"The account “%s” doesn't exists.") % self._addr,
   179                     ERR.NO_SUCH_ACCOUNT)
   179                     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', sieve_col]
   182                     'pop3', 'imap', sieve_col]
   183             info = dict(zip(keys, info))
   183             info = dict(zip(keys, info))
   207                 aliases.append(alias[0])
   207                 aliases.append(alias[0])
   208         return aliases
   208         return aliases
   209 
   209 
   210     def delete(self, delalias):
   210     def delete(self, delalias):
   211         if self._uid < 1:
   211         if self._uid < 1:
   212             raise AccE(_(u"The account »%s« doesn't exists.") % self._addr,
   212             raise AccE(_(u"The account “%s” doesn't exists.") % self._addr,
   213                     ERR.NO_SUCH_ACCOUNT)
   213                     ERR.NO_SUCH_ACCOUNT)
   214         dbc = self._dbh.cursor()
   214         dbc = self._dbh.cursor()
   215         if delalias == 'delalias':
   215         if delalias == 'delalias':
   216             dbc.execute('DELETE FROM users WHERE uid= %s', self._uid)
   216             dbc.execute('DELETE FROM users WHERE uid= %s', self._uid)
   217             u_rc = dbc.rowcount
   217             u_rc = dbc.rowcount
   229                     self._dbh.commit()
   229                     self._dbh.commit()
   230             else:
   230             else:
   231                 dbc.close()
   231                 dbc.close()
   232                 raise AccE(
   232                 raise AccE(
   233                   _(u"There are %(count)d aliases with the destination address\
   233                   _(u"There are %(count)d aliases with the destination address\
   234  »%(address)s«.") %{'count': a_count, 'address': self._addr}, ERR.ALIAS_PRESENT)
   234  “%(address)s”.") %{'count': a_count, 'address': self._addr}, ERR.ALIAS_PRESENT)
   235         dbc.close()
   235         dbc.close()
   236 
   236 
   237 def getAccountByID(uid, dbh):
   237 def getAccountByID(uid, dbh):
   238     try:
   238     try:
   239         uid = long(uid)
   239         uid = long(uid)
   246  uid, users.gid FROM users LEFT JOIN domain_name ON (domain_name.gid \
   246  uid, users.gid FROM users LEFT JOIN domain_name ON (domain_name.gid \
   247  = users.gid AND is_primary) WHERE uid = %s;", uid)
   247  = users.gid AND is_primary) WHERE uid = %s;", uid)
   248     info = dbc.fetchone()
   248     info = dbc.fetchone()
   249     dbc.close()
   249     dbc.close()
   250     if info is None:
   250     if info is None:
   251         raise AccE(_(u"There is no account with the UID »%d«.") % uid,
   251         raise AccE(_(u"There is no account with the UID “%d”.") % uid,
   252                 ERR.NO_SUCH_ACCOUNT)
   252                 ERR.NO_SUCH_ACCOUNT)
   253     keys = ['address', 'uid', 'gid']
   253     keys = ['address', 'uid', 'gid']
   254     info = dict(zip(keys, info))
   254     info = dict(zip(keys, info))
   255     return info
   255     return info
   256 
   256