VirtualMailManager/Account.py
changeset 47 191d5a5adc4a
parent 43 92a6132940f5
child 48 0d5f58f8b8f5
equal deleted inserted replaced
46:7ece710c559d 47:191d5a5adc4a
    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 import gettext
       
    17 
       
    18 from Exceptions import VMMAccountException
    16 from Exceptions import VMMAccountException
    19 from Domain import Domain
    17 from Domain import Domain
    20 from Transport import Transport
    18 from Transport import Transport
    21 from MailLocation import MailLocation
    19 from MailLocation import MailLocation
       
    20 import VirtualMailManager as VMM
    22 import constants.ERROR as ERR
    21 import constants.ERROR as ERR
    23 
       
    24 gettext.bindtextdomain('vmm', '/usr/local/share/locale')
       
    25 gettext.textdomain('vmm')
       
    26 _ = gettext.gettext
       
    27 
    22 
    28 class Account:
    23 class Account:
    29     """Class to manage e-mail accounts."""
    24     """Class to manage e-mail accounts."""
    30     def __init__(self, dbh, address, password=None):
    25     def __init__(self, dbh, address, password=None):
    31         self._dbh = dbh
    26         self._dbh = dbh
    32         self._base = None
    27         self._base = None
    33         self._addr = address
    28         self._addr = VMM.VirtualMailManager.chkEmailAddress(address)
    34         self._localpart = None
    29         self._localpart = None
    35         self._name = None
    30         self._name = None
    36         self._uid = 0
    31         self._uid = 0
    37         self._gid = 0
    32         self._gid = 0
    38         self._mid = 0
    33         self._mid = 0
    40         self._passwd = password
    35         self._passwd = password
    41         self._setAddr()
    36         self._setAddr()
    42         self._exists()
    37         self._exists()
    43         if self._isAlias():
    38         if self._isAlias():
    44             raise VMMAccountException(
    39             raise VMMAccountException(
    45             (_(u"There is already an alias with address '%s'") % address,
    40             (_(u"There is already an alias with the address »%s«.") % address,
    46                 ERR.ALIAS_EXISTS))
    41                 ERR.ALIAS_EXISTS))
    47 
    42 
    48     def _exists(self):
    43     def _exists(self):
    49         dbc = self._dbh.cursor()
    44         dbc = self._dbh.cursor()
    50         dbc.execute("SELECT uid, mid, tid FROM users \
    45         dbc.execute("SELECT uid, mid, tid FROM users \
    72     def _setAddr(self):
    67     def _setAddr(self):
    73         self._localpart, d = self._addr.split('@')
    68         self._localpart, d = self._addr.split('@')
    74         dom = Domain(self._dbh, d)
    69         dom = Domain(self._dbh, d)
    75         self._gid = dom.getID()
    70         self._gid = dom.getID()
    76         if self._gid == 0:
    71         if self._gid == 0:
    77             raise VMMAccountException((_(u"Domain '%s' doesn't exist.") % d,
    72             raise VMMAccountException((_(u"Domain »%s« doesn't exist.") % d,
    78                 ERR.NO_SUCH_DOMAIN))
    73                 ERR.NO_SUCH_DOMAIN))
    79         self._base = dom.getDir()
    74         self._base = dom.getDir()
    80         self._tid = dom.getTransportID()
    75         self._tid = dom.getTransportID()
    81 
    76 
    82     def _setID(self):
    77     def _setID(self):
    91 
    86 
    92     def _switchState(self, state, service):
    87     def _switchState(self, state, service):
    93         if not isinstance(state, bool):
    88         if not isinstance(state, bool):
    94             return False
    89             return False
    95         if not service in ['smtp', 'pop3', 'imap', 'managesieve', 'all', None]:
    90         if not service in ['smtp', 'pop3', 'imap', 'managesieve', 'all', None]:
    96             raise VMMAccountException((_(u"Unknown service '%s'") % service,
    91             raise VMMAccountException((_(u"Unknown service »%s«.") % service,
    97                 ERR.UNKNOWN_SERVICE))
    92                 ERR.UNKNOWN_SERVICE))
    98         if self._uid < 1:
    93         if self._uid < 1:
    99             raise VMMAccountException((_("Account doesn't exists"),
    94             raise VMMAccountException((_(u"The account »%s« doesn't exists.") %
   100                 ERR.NO_SUCH_ACCOUNT))
    95                 self._addr, ERR.NO_SUCH_ACCOUNT))
   101         dbc = self._dbh.cursor()
    96         dbc = self._dbh.cursor()
   102         if service in ['smtp', 'pop3', 'imap', 'managesieve']:
    97         if service in ['smtp', 'pop3', 'imap', 'managesieve']:
   103             dbc.execute(
    98             dbc.execute(
   104                     "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"
   105                     % (service, state, self._localpart, self._gid))
   100                     % (service, state, self._localpart, self._gid))
   146                 self._localpart, self._passwd, self._uid, self._gid, self._mid,
   141                 self._localpart, self._passwd, self._uid, self._gid, self._mid,
   147                 self._tid, smtp, pop3, imap, managesieve)
   142                 self._tid, smtp, pop3, imap, managesieve)
   148             self._dbh.commit()
   143             self._dbh.commit()
   149             dbc.close()
   144             dbc.close()
   150         else:
   145         else:
   151             raise VMMAccountException((_('Account already exists.'),
   146             raise VMMAccountException((_(u'The account »%s« already exists.') %
   152                 ERR.ACCOUNT_EXISTS))
   147                 self._addr, ERR.ACCOUNT_EXISTS))
   153        
   148        
   154     def modify(self, what, value):
   149     def modify(self, what, value):
   155         if self._uid == 0:
   150         if self._uid == 0:
   156             raise VMMAccountException((_("Account doesn't exists"),
   151             raise VMMAccountException((_(u"The account »%s« doesn't exists.") %
   157                 ERR.NO_SUCH_ACCOUNT))
   152                 self._addr, ERR.NO_SUCH_ACCOUNT))
   158         if what not in ['name', 'password', 'transport']:
   153         if what not in ['name', 'password', 'transport']:
   159             return False
   154             return False
   160         dbc = self._dbh.cursor()
   155         dbc = self._dbh.cursor()
   161         if what == 'password':
   156         if what == 'password':
   162             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\
   178  managesieve FROM users WHERE local_part=%s AND gid=%s",
   173  managesieve FROM users WHERE local_part=%s AND gid=%s",
   179             self._localpart, self._gid)
   174             self._localpart, self._gid)
   180         info = dbc.fetchone()
   175         info = dbc.fetchone()
   181         dbc.close()
   176         dbc.close()
   182         if info is None:
   177         if info is None:
   183             raise VMMAccountException((_("Account doesn't exists"),
   178             raise VMMAccountException((_(u"The account »%s« doesn't exists.") %
   184                 ERR.NO_SUCH_ACCOUNT))
   179                 self._addr, ERR.NO_SUCH_ACCOUNT))
   185         else:
   180         else:
   186             keys = ['name', 'uid', 'gid', 'maildir', 'transport', 'smtp',
   181             keys = ['name', 'uid', 'gid', 'maildir', 'transport', 'smtp',
   187                     'pop3', 'imap', 'managesieve']
   182                     'pop3', 'imap', 'managesieve']
   188             info = dict(zip(keys, info))
   183             info = dict(zip(keys, info))
   189             for service in ['smtp', 'pop3', 'imap', 'managesieve']:
   184             for service in ['smtp', 'pop3', 'imap', 'managesieve']:
   206                     self._gid, self._localpart)
   201                     self._gid, self._localpart)
   207             if dbc.rowcount > 0:
   202             if dbc.rowcount > 0:
   208                 self._dbh.commit()
   203                 self._dbh.commit()
   209             dbc.close()
   204             dbc.close()
   210         else:
   205         else:
   211             raise VMMAccountException((_("Account doesn't exists"),
   206             raise VMMAccountException((_(u"The account »%s« doesn't exists.") %
   212                 ERR.NO_SUCH_ACCOUNT))
   207                 self._addr, ERR.NO_SUCH_ACCOUNT))
   213 
   208 
   214 
   209 
   215 def getAccountByID(uid, dbh):
   210 def getAccountByID(uid, dbh):
   216     try:
   211     try:
   217         uid = long(uid)
   212         uid = long(uid)
   218     except ValueError:
   213     except ValueError:
   219         raise VMMAccountException((_('uid must be an int/long.'),
   214         raise VMMAccountException((_(u'uid must be an int/long.'),
   220             ERR.INVALID_AGUMENT))
   215             ERR.INVALID_AGUMENT))
   221     if uid < 1:
   216     if uid < 1:
   222         raise VMMAccountException((_('uid must be greater than 0.'),
   217         raise VMMAccountException((_(u'uid must be greater than 0.'),
   223             ERR.INVALID_AGUMENT))
   218             ERR.INVALID_AGUMENT))
   224     dbc = dbh.cursor()
   219     dbc = dbh.cursor()
   225     dbc.execute("SELECT local_part||'@'|| domain_name.domainname AS address,\
   220     dbc.execute("SELECT local_part||'@'|| domain_name.domainname AS address,\
   226  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 \
   227  = users.gid AND is_primary) WHERE uid = %s;", uid)
   222  = users.gid AND is_primary) WHERE uid = %s;", uid)
   228     info = dbc.fetchone()
   223     info = dbc.fetchone()
   229     dbc.close()
   224     dbc.close()
   230     if info is None:
   225     if info is None:
   231         raise VMMAccountException((_("Account doesn't exists"),
   226         raise VMMAccountException((
       
   227             _(u"There is no account with the UID »%d«.") % uid,
   232             ERR.NO_SUCH_ACCOUNT))
   228             ERR.NO_SUCH_ACCOUNT))
   233     keys = ['address', 'uid', 'gid']
   229     keys = ['address', 'uid', 'gid']
   234     info = dict(zip(keys, info))
   230     info = dict(zip(keys, info))
   235     return info
   231     return info
   236 
   232