70 |
70 |
71 def _prepare(self, maillocation): |
71 def _prepare(self, maillocation): |
72 self._setID() |
72 self._setID() |
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, dcvers, 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', 'sieve', 'all', None]: |
78 if not service in (None, 'all', 'imap', 'pop3', 'sieve', 'smtp'): |
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 sieve_col = 'sieve' if dcvers > 11 else 'managesieve' |
85 if service in ['smtp', 'pop3', 'imap', 'sieve']: |
85 if service in ('smtp', 'pop3', 'imap'): |
86 dbc.execute( |
86 sql = 'UPDATE users SET %s = %s WHERE uid = %d' % (service, state, |
87 "UPDATE users SET %s=%s WHERE local_part='%s' AND gid=%s" |
87 self._uid) |
88 % (service, state, self._addr._localpart, self._gid)) |
88 elif service == 'sieve': |
89 elif state: |
89 sql = 'UPDATE users SET %s = %s WHERE uid = %d' % (sieve_col, |
90 # TODO |
90 state, self._uid) |
91 # add dovecotvers check |
91 else: |
92 dbc.execute("UPDATE users SET smtp = TRUE, pop3 = TRUE,\ |
92 sql = 'UPDATE users SET smtp = %(s)s, pop3 = %(s)s, imap = %(s)s,\ |
93 imap = TRUE, managesieve = TRUE WHERE local_part = %s AND gid = %s", |
93 %(col)s = %(s)s WHERE uid = %(uid)d' % { |
94 self._addr._localpart, self._gid) |
94 's': state, 'col': sieve_col, 'uid': self._uid} |
95 else: |
95 dbc = self._dbh.cursor() |
96 dbc.execute("UPDATE users SET smtp = FALSE, pop3 = FALSE,\ |
96 dbc.execute(sql) |
97 imap = FALSE, managesieve = FALSE WHERE local_part = %s AND gid = %s", |
|
98 self._addr._localpart, self._gid) |
|
99 if dbc.rowcount > 0: |
97 if dbc.rowcount > 0: |
100 self._dbh.commit() |
98 self._dbh.commit() |
101 dbc.close() |
99 dbc.close() |
102 |
100 |
103 def __aliaseCount(self): |
101 def __aliaseCount(self): |
122 if directory == 'domain': |
120 if directory == 'domain': |
123 return '%s' % self._base |
121 return '%s' % self._base |
124 elif directory == 'home': |
122 elif directory == 'home': |
125 return '%s/%i' % (self._base, self._uid) |
123 return '%s/%i' % (self._base, self._uid) |
126 |
124 |
127 def enable(self, service=None): |
125 def enable(self, dcvers, service=None): |
128 self._switchState(True, service) |
126 self._switchState(True, dcvers, service) |
129 |
127 |
130 def disable(self, service=None): |
128 def disable(self, dcvers, service=None): |
131 self._switchState(False, service) |
129 self._switchState(False, dcvers, service) |
132 |
130 |
133 def save(self, maillocation, smtp, pop3, imap, sieve): |
131 def save(self, maillocation, dcvers, smtp, pop3, imap, sieve): |
134 if self._uid < 1: |
132 if self._uid < 1: |
|
133 sieve_col = 'sieve' if dcvers > 11 else 'managesieve' |
135 self._prepare(maillocation) |
134 self._prepare(maillocation) |
|
135 sql = "INSERT INTO users (local_part, passwd, uid, gid, mid, tid,\ |
|
136 smtp, pop3, imap, %s) VALUES ('%s', '%s', %d, %d, %d, %d, %s, %s, %s, %s)" % ( |
|
137 sieve_col, self._addr._localpart, self._passwd, self._uid, |
|
138 self._gid, self._mid, self._tid, smtp, pop3, imap, sieve) |
136 dbc = self._dbh.cursor() |
139 dbc = self._dbh.cursor() |
137 # TODO |
140 dbc.execute(sql) |
138 # add dovecotvers check |
|
139 dbc.execute("""INSERT INTO users (local_part, passwd, uid, gid,\ |
|
140 mid, tid, smtp, pop3, imap, managesieve)\ |
|
141 VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""", |
|
142 self._addr._localpart, self._passwd, self._uid, self._gid, |
|
143 self._mid, self._tid, smtp, pop3, imap, sieve) |
|
144 self._dbh.commit() |
141 self._dbh.commit() |
145 dbc.close() |
142 dbc.close() |
146 else: |
143 else: |
147 raise AccE(_(u'The account »%s« already exists.') % self._addr, |
144 raise AccE(_(u'The account »%s« already exists.') % self._addr, |
148 ERR.ACCOUNT_EXISTS) |
145 ERR.ACCOUNT_EXISTS) |
166 gid=%s", value, self._addr._localpart, self._gid) |
163 gid=%s", value, self._addr._localpart, self._gid) |
167 if dbc.rowcount > 0: |
164 if dbc.rowcount > 0: |
168 self._dbh.commit() |
165 self._dbh.commit() |
169 dbc.close() |
166 dbc.close() |
170 |
167 |
171 def getInfo(self): |
168 def getInfo(self, dcvers): |
172 dbc = self._dbh.cursor() |
169 sieve_col = 'sieve' if dcvers > 11 else 'managesieve' |
173 # TODO |
170 sql = 'SELECT name, uid, gid, mid, tid, smtp, pop3, imap, %s\ |
174 # add dovecotvers check |
171 FROM users WHERE uid = %d' % (sieve_col, self._uid) |
175 dbc.execute("SELECT name, uid, gid, mid, tid, smtp, pop3, imap, \ |
172 dbc = self._dbh.cursor() |
176 managesieve FROM users WHERE local_part=%s AND gid=%s", |
173 dbc.execute(sql) |
177 self._addr._localpart, self._gid) |
|
178 info = dbc.fetchone() |
174 info = dbc.fetchone() |
179 dbc.close() |
175 dbc.close() |
180 if info is None: |
176 if info is None: |
181 raise AccE(_(u"The account »%s« doesn't exists.") % self._addr, |
177 raise AccE(_(u"The account »%s« doesn't exists.") % self._addr, |
182 ERR.NO_SUCH_ACCOUNT) |
178 ERR.NO_SUCH_ACCOUNT) |
183 else: |
179 else: |
184 keys = ['name', 'uid', 'gid', 'maildir', 'transport', 'smtp', |
180 keys = ['name', 'uid', 'gid', 'maildir', 'transport', 'smtp', |
185 'pop3', 'imap', 'sieve'] |
181 'pop3', 'imap', sieve_col] |
186 info = dict(zip(keys, info)) |
182 info = dict(zip(keys, info)) |
187 for service in ['smtp', 'pop3', 'imap', 'sieve']: |
183 for service in ('smtp', 'pop3', 'imap', sieve_col): |
188 if bool(info[service]): |
184 if bool(info[service]): |
189 info[service] = _('enabled') |
185 info[service] = _('enabled') |
190 else: |
186 else: |
191 info[service] = _('disabled') |
187 info[service] = _('disabled') |
192 info['address'] = self._addr |
188 info['address'] = self._addr |