VirtualMailManager/Account.py
branchv0.6.x
changeset 290 e2785e04f92e
parent 276 f2ecfe0a0e09
child 293 a77b67673aa6
equal deleted inserted replaced
289:142f188f7552 290:e2785e04f92e
    64 
    64 
    65     def _load(self):
    65     def _load(self):
    66         """Load 'uid', 'mid' and 'tid' from the database and set _new to
    66         """Load 'uid', 'mid' and 'tid' from the database and set _new to
    67         `False` - if the user could be found. """
    67         `False` - if the user could be found. """
    68         dbc = self._dbh.cursor()
    68         dbc = self._dbh.cursor()
    69         dbc.execute(
    69         dbc.execute('SELECT uid, mid, tid FROM users WHERE gid = %s AND '
    70             "SELECT uid, mid, tid FROM users WHERE gid=%s AND local_part=%s",
    70                     'local_part = %s', self._domain.gid, self._addr.localpart)
    71                     self._domain.gid, self._addr.localpart)
       
    72         result = dbc.fetchone()
    71         result = dbc.fetchone()
    73         dbc.close()
    72         dbc.close()
    74         if result:
    73         if result:
    75             self._uid, self._mid, _tid = result
    74             self._uid, self._mid, _tid = result
    76             if _tid != self._transport.tid:
    75             if _tid != self._transport.tid:
    88     def _prepare(self, maillocation):
    87     def _prepare(self, maillocation):
    89         """Check and set different attributes - before we store the
    88         """Check and set different attributes - before we store the
    90         information in the database.
    89         information in the database.
    91         """
    90         """
    92         if maillocation.dovecot_version > cfg_dget('misc.dovecot_version'):
    91         if maillocation.dovecot_version > cfg_dget('misc.dovecot_version'):
    93             raise AErr(_("The mail_location prefix '%(prefix)s' requires \
    92             raise AErr(_(u"The mail_location prefix '%(prefix)s' requires "
    94 Dovecot >= v%(version)s") % {'prefix': maillocation.prefix,
    93                          u"Dovecot >= v%(version)s") %
    95                        'version': version_str(maillocation.dovecot_version)},
    94                        {'prefix': maillocation.prefix,
       
    95                         'version': version_str(maillocation.dovecot_version)},
    96                        INVALID_MAIL_LOCATION)
    96                        INVALID_MAIL_LOCATION)
    97         if not maillocation.postfix and \
    97         if not maillocation.postfix and \
    98           self._transport.transport.lower() in ('virtual:', 'virtual'):
    98           self._transport.transport.lower() in ('virtual:', 'virtual'):
    99             raise AErr(_(u"Invalid transport '%(transport)s' for mail_location\
    99             raise AErr(_(u"Invalid transport '%(transport)s' for mail_location"
   100  prefix '%(prefix)s'") % {'transport': self._transport,
   100                          u" prefix '%(prefix)s'") %
   101                        'prefix': maillocation.prefix},
   101                        {'transport': self._transport,
   102                        INVALID_MAIL_LOCATION)
   102                         'prefix': maillocation.prefix}, INVALID_MAIL_LOCATION)
   103         self._mid = maillocation.mid
   103         self._mid = maillocation.mid
   104         self._set_uid()
   104         self._set_uid()
   105 
   105 
   106     def _switch_state(self, state, service):
   106     def _switch_state(self, state, service):
   107         """Switch the state of the Account's services on or off. See
   107         """Switch the state of the Account's services on or off. See
   321             info['mail_location'] = MailLocation(mid=self._mid).mail_location
   321             info['mail_location'] = MailLocation(mid=self._mid).mail_location
   322             info['transport'] = self._transport.transport
   322             info['transport'] = self._transport.transport
   323             info['uid'] = self._uid
   323             info['uid'] = self._uid
   324             return info
   324             return info
   325         # nearly impossibleā€½
   325         # nearly impossibleā€½
   326         raise AErr(_(u"Couldn't fetch information for account: '%s'") \
   326         raise AErr(_(u"Couldn't fetch information for account: '%s'") %
   327                    % self._addr, NO_SUCH_ACCOUNT)
   327                    self._addr, NO_SUCH_ACCOUNT)
   328 
   328 
   329     def get_aliases(self):
   329     def get_aliases(self):
   330         """Return a list with all alias e-mail addresses, whose destination
   330         """Return a list with all alias e-mail addresses, whose destination
   331         is the address of the Account."""
   331         is the address of the Account."""
   332         self._chk_state()
   332         self._chk_state()
   333         dbc = self._dbh.cursor()
   333         dbc = self._dbh.cursor()
   334         dbc.execute("SELECT address ||'@'|| domainname FROM alias, domain_name\
   334         dbc.execute("SELECT address ||'@'|| domainname FROM alias, "
   335  WHERE destination = %s AND domain_name.gid = alias.gid\
   335                     "domain_name WHERE destination = %s AND domain_name.gid = "
   336  AND domain_name.is_primary ORDER BY address", str(self._addr))
   336                     "alias.gid AND domain_name.is_primary ORDER BY address",
       
   337                     str(self._addr))
   337         addresses = dbc.fetchall()
   338         addresses = dbc.fetchall()
   338         dbc.close()
   339         dbc.close()
   339         aliases = []
   340         aliases = []
   340         if addresses:
   341         if addresses:
   341             aliases = [alias[0] for alias in addresses]
   342             aliases = [alias[0] for alias in addresses]
   363             self._dbh.commit()
   364             self._dbh.commit()
   364         else:  # check first for aliases
   365         else:  # check first for aliases
   365             a_count = self._count_aliases()
   366             a_count = self._count_aliases()
   366             if a_count > 0:
   367             if a_count > 0:
   367                 dbc.close()
   368                 dbc.close()
   368                 raise AErr(_(u"There are %(count)d aliases with the \
   369                 raise AErr(_(u"There are %(count)d aliases with the "
   369 destination address '%(address)s'.") % \
   370                              u"destination address '%(address)s'.") %
   370                            {'count': a_count, 'address': self._addr},
   371                            {'count': a_count, 'address': self._addr},
   371                            ALIAS_PRESENT)
   372                            ALIAS_PRESENT)
   372             dbc.execute('DELETE FROM users WHERE uid = %s', self._uid)
   373             dbc.execute('DELETE FROM users WHERE uid = %s', self._uid)
   373             self._dbh.commit()
   374             self._dbh.commit()
   374         dbc.close()
   375         dbc.close()
   396     except ValueError:
   397     except ValueError:
   397         raise AErr(_(u'UID must be an int/long.'), INVALID_AGUMENT)
   398         raise AErr(_(u'UID must be an int/long.'), INVALID_AGUMENT)
   398     if uid < 1:
   399     if uid < 1:
   399         raise AErr(_(u'UID must be greater than 0.'), INVALID_AGUMENT)
   400         raise AErr(_(u'UID must be greater than 0.'), INVALID_AGUMENT)
   400     dbc = dbh.cursor()
   401     dbc = dbh.cursor()
   401     dbc.execute("SELECT local_part||'@'|| domain_name.domainname AS address,\
   402     dbc.execute("SELECT local_part||'@'|| domain_name.domainname AS address, "
   402  uid, users.gid FROM users LEFT JOIN domain_name ON (domain_name.gid \
   403                 "uid, users.gid FROM users LEFT JOIN domain_name ON "
   403  = users.gid AND is_primary) WHERE uid = %s;", uid)
   404                 "(domain_name.gid = users.gid AND is_primary) WHERE uid = %s",
       
   405                 uid)
   404     info = dbc.fetchone()
   406     info = dbc.fetchone()
   405     dbc.close()
   407     dbc.close()
   406     if not info:
   408     if not info:
   407         raise AErr(_(u"There is no account with the UID '%d'.") % uid,
   409         raise AErr(_(u"There is no account with the UID '%d'.") % uid,
   408                    NO_SUCH_ACCOUNT)
   410                    NO_SUCH_ACCOUNT)