VirtualMailManager/Account.py
changeset 113 e35755191ff3
parent 102 485d3f7d6981
child 114 e671210b04b8
equal deleted inserted replaced
112:d1f345f91e1c 113:e35755191ff3
    73         self._mid = MailLocation(self._dbh, maillocation=maillocation).getID()
    73         self._mid = MailLocation(self._dbh, maillocation=maillocation).getID()
    74 
    74 
    75     def _switchState(self, state, service):
    75     def _switchState(self, state, service):
    76         if not isinstance(state, bool):
    76         if not isinstance(state, bool):
    77             return False
    77             return False
    78         if not service in ['smtp', 'pop3', 'imap', 'managesieve', 'all', None]:
    78         if not service in ['smtp', 'pop3', 'imap', 'sieve', 'all', None]:
    79             raise AccE(_(u"Unknown service »%s«.") % service,
    79             raise AccE(_(u"Unknown service »%s«.") % service,
    80                     ERR.UNKNOWN_SERVICE)
    80                     ERR.UNKNOWN_SERVICE)
    81         if self._uid < 1:
    81         if self._uid < 1:
    82             raise AccE(_(u"The account »%s« doesn't exists.") % self._addr,
    82             raise AccE(_(u"The account »%s« doesn't exists.") % self._addr,
    83                     ERR.NO_SUCH_ACCOUNT)
    83                     ERR.NO_SUCH_ACCOUNT)
    84         dbc = self._dbh.cursor()
    84         dbc = self._dbh.cursor()
    85         if service in ['smtp', 'pop3', 'imap', 'managesieve']:
    85         if service in ['smtp', 'pop3', 'imap', 'sieve']:
    86             dbc.execute(
    86             dbc.execute(
    87                     "UPDATE users SET %s=%s WHERE local_part='%s' AND gid=%s"
    87                     "UPDATE users SET %s=%s WHERE local_part='%s' AND gid=%s"
    88                     % (service, state, self._addr._localpart, self._gid))
    88                     % (service, state, self._addr._localpart, self._gid))
    89         elif state:
    89         elif state:
       
    90             # TODO
       
    91             # add dovecotvers check
    90             dbc.execute("UPDATE users SET smtp = TRUE, pop3 = TRUE,\
    92             dbc.execute("UPDATE users SET smtp = TRUE, pop3 = TRUE,\
    91  imap = TRUE, managesieve = TRUE WHERE local_part = %s AND gid = %s",
    93  imap = TRUE, managesieve = TRUE WHERE local_part = %s AND gid = %s",
    92                 self._addr._localpart, self._gid)
    94                 self._addr._localpart, self._gid)
    93         else:
    95         else:
    94             dbc.execute("UPDATE users SET smtp = FALSE, pop3 = FALSE,\
    96             dbc.execute("UPDATE users SET smtp = FALSE, pop3 = FALSE,\
   126         self._switchState(True, service)
   128         self._switchState(True, service)
   127 
   129 
   128     def disable(self, service=None):
   130     def disable(self, service=None):
   129         self._switchState(False, service)
   131         self._switchState(False, service)
   130 
   132 
   131     def save(self, maillocation, smtp, pop3, imap, managesieve):
   133     def save(self, maillocation, smtp, pop3, imap, sieve):
   132         if self._uid < 1:
   134         if self._uid < 1:
   133             self._prepare(maillocation)
   135             self._prepare(maillocation)
   134             dbc = self._dbh.cursor()
   136             dbc = self._dbh.cursor()
       
   137             # TODO
       
   138             # add dovecotvers check
   135             dbc.execute("""INSERT INTO users (local_part, passwd, uid, gid,\
   139             dbc.execute("""INSERT INTO users (local_part, passwd, uid, gid,\
   136  mid, tid, smtp, pop3, imap, managesieve)\
   140  mid, tid, smtp, pop3, imap, managesieve)\
   137  VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
   141  VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
   138                 self._addr._localpart, self._passwd, self._uid, self._gid,
   142                 self._addr._localpart, self._passwd, self._uid, self._gid,
   139                 self._mid, self._tid, smtp, pop3, imap, managesieve)
   143                 self._mid, self._tid, smtp, pop3, imap, sieve)
   140             self._dbh.commit()
   144             self._dbh.commit()
   141             dbc.close()
   145             dbc.close()
   142         else:
   146         else:
   143             raise AccE(_(u'The account »%s« already exists.') % self._addr,
   147             raise AccE(_(u'The account »%s« already exists.') % self._addr,
   144                     ERR.ACCOUNT_EXISTS)
   148                     ERR.ACCOUNT_EXISTS)
   164             self._dbh.commit()
   168             self._dbh.commit()
   165         dbc.close()
   169         dbc.close()
   166 
   170 
   167     def getInfo(self):
   171     def getInfo(self):
   168         dbc = self._dbh.cursor()
   172         dbc = self._dbh.cursor()
       
   173         # TODO
       
   174         # add dovecotvers check
   169         dbc.execute("SELECT name, uid, gid, mid, tid, smtp, pop3, imap, \
   175         dbc.execute("SELECT name, uid, gid, mid, tid, smtp, pop3, imap, \
   170  managesieve FROM users WHERE local_part=%s AND gid=%s",
   176  managesieve FROM users WHERE local_part=%s AND gid=%s",
   171             self._addr._localpart, self._gid)
   177             self._addr._localpart, self._gid)
   172         info = dbc.fetchone()
   178         info = dbc.fetchone()
   173         dbc.close()
   179         dbc.close()
   174         if info is None:
   180         if info is None:
   175             raise AccE(_(u"The account »%s« doesn't exists.") % self._addr,
   181             raise AccE(_(u"The account »%s« doesn't exists.") % self._addr,
   176                     ERR.NO_SUCH_ACCOUNT)
   182                     ERR.NO_SUCH_ACCOUNT)
   177         else:
   183         else:
   178             keys = ['name', 'uid', 'gid', 'maildir', 'transport', 'smtp',
   184             keys = ['name', 'uid', 'gid', 'maildir', 'transport', 'smtp',
   179                     'pop3', 'imap', 'managesieve']
   185                     'pop3', 'imap', 'sieve']
   180             info = dict(zip(keys, info))
   186             info = dict(zip(keys, info))
   181             for service in ['smtp', 'pop3', 'imap', 'managesieve']:
   187             for service in ['smtp', 'pop3', 'imap', 'sieve']:
   182                 if bool(info[service]):
   188                 if bool(info[service]):
   183                     info[service] = _('enabled')
   189                     info[service] = _('enabled')
   184                 else:
   190                 else:
   185                     info[service] = _('disabled')
   191                     info[service] = _('disabled')
   186             info['address'] = self._addr
   192             info['address'] = self._addr