VirtualMailManager/Account.py
changeset 20 55146c78b3fb
parent 19 bf9a03c476fc
child 24 48ea255e8a85
equal deleted inserted replaced
19:bf9a03c476fc 20:55146c78b3fb
    81 
    81 
    82     def _prepare(self, maillocation):
    82     def _prepare(self, maillocation):
    83         self._setID()
    83         self._setID()
    84         self._mid = MailLocation(self._dbh, maillocation=maillocation).getID()
    84         self._mid = MailLocation(self._dbh, maillocation=maillocation).getID()
    85 
    85 
    86     def _switchState(self, state):
    86     def _switchState(self, state, service):
    87         if not isinstance(state, bool):
    87         if not isinstance(state, bool):
    88             return False
    88             return False
       
    89         if not service in ['smtp', 'pop3', 'imap', 'managesieve', 'all', None]:
       
    90             raise VMMAccountException(("Unknown service »%s«" % service,
       
    91                 ERR.UNKNOWN_SERVICE))
    89         if self._uid < 1:
    92         if self._uid < 1:
    90             raise VMMAccountException(("Account doesn't exists",
    93             raise VMMAccountException(("Account doesn't exists",
    91                 ERR.NO_SUCH_ACCOUNT))
    94                 ERR.NO_SUCH_ACCOUNT))
    92         dbc = self._dbh.cursor()
    95         dbc = self._dbh.cursor()
    93         dbc.execute("""UPDATE users SET disabled=%s WHERE local_part=%s\
    96         if service in ['smtp', 'pop3', 'imap', 'managesieve']:
    94  AND gid=%s""", state, self._localpart, self._gid)
    97             dbc.execute(
       
    98                     "UPDATE users SET %s=%s WHERE local_part='%s' AND gid=%s"
       
    99                     % (service, state, self._localpart, self._gid))
       
   100         elif state:
       
   101             dbc.execute("UPDATE users SET smtp = TRUE, pop3 = TRUE,\
       
   102  imap = TRUE, managesieve = TRUE WHERE local_part = %s AND gid = %s",
       
   103                 self._localpart, self._gid)
       
   104         else:
       
   105             dbc.execute("UPDATE users SET smtp = FALSE, pop3 = FALSE,\
       
   106  imap = FALSE, managesieve = FALSE WHERE local_part = %s AND gid = %s",
       
   107                 self._localpart, self._gid)
    95         if dbc.rowcount > 0:
   108         if dbc.rowcount > 0:
    96             self._dbh.commit()
   109             self._dbh.commit()
    97         dbc.close()
   110         dbc.close()
    98 
   111 
    99     def getUID(self):
   112     def getUID(self):
   106         if directory == 'domain':
   119         if directory == 'domain':
   107             return '%s' % self._base
   120             return '%s' % self._base
   108         elif directory == 'home':
   121         elif directory == 'home':
   109             return '%s/%i' % (self._base, self._uid)
   122             return '%s/%i' % (self._base, self._uid)
   110 
   123 
   111     def enable(self):
   124     def enable(self, service=None):
   112         self._switchState(False)
   125         self._switchState(True, service)
   113 
   126 
   114     def disable(self):
   127     def disable(self, service=None):
   115         self._switchState(True)
   128         self._switchState(False, service)
   116 
   129 
   117     def save(self, maillocation):
   130     def save(self, maillocation, smtp, pop3, imap, managesieve):
   118         if self._uid < 1:
   131         if self._uid < 1:
   119             self._prepare(maillocation)
   132             self._prepare(maillocation)
   120             dbc = self._dbh.cursor()
   133             dbc = self._dbh.cursor()
   121             dbc.execute("""INSERT INTO users (local_part, passwd, uid, gid,\
   134             dbc.execute("""INSERT INTO users (local_part, passwd, uid, gid,\
   122  mid, tid) VALUES (%s, %s, %s, %s, %s, %s)""", self._localpart, self._passwd,
   135  mid, tid, smtp, pop3, imap, managesieve)\
   123                     self._uid, self._gid, self._mid, self._tid)
   136  VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
       
   137                 self._localpart, self._passwd, self._uid, self._gid, self._mid,
       
   138                 self._tid, smtp, pop3, imap, managesieve)
   124             self._dbh.commit()
   139             self._dbh.commit()
   125             dbc.close()
   140             dbc.close()
   126         else:
   141         else:
   127             raise VMMAccountException(('Account already exists.',
   142             raise VMMAccountException(('Account already exists.',
   128                 ERR.ACCOUNT_EXISTS))
   143                 ERR.ACCOUNT_EXISTS))
   148             self._dbh.commit()
   163             self._dbh.commit()
   149         dbc.close()
   164         dbc.close()
   150 
   165 
   151     def getInfo(self):
   166     def getInfo(self):
   152         dbc = self._dbh.cursor()
   167         dbc = self._dbh.cursor()
   153         dbc.execute("SELECT name, uid, gid, mid, tid, disabled FROM users\
   168         dbc.execute("SELECT name, uid, gid, mid, tid, smtp, pop3, imap, \
   154  WHERE local_part=%s AND gid=%s", self._localpart, self._gid)
   169  managesieve FROM users WHERE local_part=%s AND gid=%s",
       
   170             self._localpart, self._gid)
   155         info = dbc.fetchone()
   171         info = dbc.fetchone()
   156         dbc.close()
   172         dbc.close()
   157         if info is None:
   173         if info is None:
   158             raise VMMAccountException(("Account doesn't exists",
   174             raise VMMAccountException(("Account doesn't exists",
   159                 ERR.NO_SUCH_ACCOUNT))
   175                 ERR.NO_SUCH_ACCOUNT))
   160         else:
   176         else:
   161             keys = ['name', 'uid', 'gid', 'maildir', 'transport', 'disabled']
   177             keys = ['name', 'uid', 'gid', 'maildir', 'transport', 'smtp',
       
   178                     'pop3', 'imap', 'managesieve']
   162             info = dict(zip(keys, info))
   179             info = dict(zip(keys, info))
   163             if bool(info['disabled']):
   180             for service in ['smtp', 'pop3', 'imap', 'managesieve']:
   164                 info['disabled'] = 'Yes'
   181                 if bool(info[service]):
   165             else:
   182                     info[service] = 'enabled'
   166                 info['disabled'] = 'No'
   183                 else:
       
   184                     info[service] = 'disabled'
   167             info['address'] = self._addr
   185             info['address'] = self._addr
   168             info['maildir'] = '%s/%s/%s' % (self._base, info['uid'],
   186             info['maildir'] = '%s/%s/%s' % (self._base, info['uid'],
   169                     MailLocation(self._dbh,
   187                     MailLocation(self._dbh,
   170                         mid=info['maildir']).getMailLocation())
   188                         mid=info['maildir']).getMailLocation())
   171             info['transport'] = Transport(self._dbh,
   189             info['transport'] = Transport(self._dbh,