author | Pascal Volk <neverseen@users.sourceforge.net> |
Thu, 15 Apr 2010 03:05:01 +0000 | |
branch | v0.6.x |
changeset 245 | f9a6b6701cf9 |
parent 236 | 084331dd1e4c |
child 246 | 481280686789 |
permissions | -rw-r--r-- |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
1 |
# -*- coding: UTF-8 -*- |
162
0ac9ef587769
Updated copyright notices to include the year 2010.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
156
diff
changeset
|
2 |
# Copyright (c) 2007 - 2010, Pascal Volk |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
3 |
# See COPYING for distribution information. |
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
4 |
|
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
5 |
""" |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
6 |
VirtualMailManager.Account |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
7 |
|
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
8 |
Virtual Mail Manager's Account class to manage e-mail accounts. |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
9 |
""" |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
10 |
|
185
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
162
diff
changeset
|
11 |
from VirtualMailManager.Domain import Domain |
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
162
diff
changeset
|
12 |
from VirtualMailManager.EmailAddress import EmailAddress |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
13 |
from VirtualMailManager.Transport import Transport |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
14 |
from VirtualMailManager.constants.ERROR import \ |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
15 |
ACCOUNT_EXISTS, ACCOUNT_MISSING_PASSWORD, ALIAS_EXISTS, ALIAS_PRESENT, \ |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
16 |
INVALID_AGUMENT, NO_SUCH_ACCOUNT, NO_SUCH_DOMAIN, RELOCATED_EXISTS, \ |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
17 |
UNKNOWN_MAILLOCATION_NAME, UNKNOWN_SERVICE |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
18 |
from VirtualMailManager.errors import AccountError as AErr |
228
a7b000ca4ac9
VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents:
227
diff
changeset
|
19 |
from VirtualMailManager.maillocation import MailLocation, known_format |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
20 |
from VirtualMailManager.pycompat import all |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
21 |
|
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
22 |
|
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
23 |
_ = lambda msg: msg |
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
24 |
|
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
25 |
|
121
7ccc05774118
Converted to new-style class, added __slots__, updated queries.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
115
diff
changeset
|
26 |
class Account(object): |
3
a9b44e04bf01
* VirtualMailManager/Account.py:
Pascal Volk <neverseen@users.sourceforge.net>
parents:
0
diff
changeset
|
27 |
"""Class to manage e-mail accounts.""" |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
28 |
__slots__ = ('_addr', '_domain', '_mid', '_new', '_passwd', '_tid', '_uid', |
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
29 |
'_dbh') |
216
0c8c053b451c
Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
30 |
|
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
31 |
def __init__(self, dbh, address): |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
32 |
"""Creates a new Account instance. |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
33 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
34 |
When an account with the given *address* could be found in the |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
35 |
database all relevant data will be loaded. |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
36 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
37 |
Arguments: |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
38 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
39 |
`dbh` : pyPgSQL.PgSQL.Connection |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
40 |
A database connection for the database access. |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
41 |
`address` : basestring |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
42 |
The e-mail address of the (new) Account. |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
43 |
""" |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
44 |
if not isinstance(address, EmailAddress): |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
45 |
raise TypeError("Argument 'address' is not an EmailAddress") |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
46 |
self._addr = address |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
47 |
self._dbh = dbh |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
48 |
self._domain = Domain(self._dbh, self._addr.domainname) |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
49 |
if not self._domain.gid: |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
50 |
raise AErr(_(u"The domain '%s' doesn't exist.") % |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
51 |
self._addr.domainname, NO_SUCH_DOMAIN) |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
52 |
self._uid = 0 |
9
e3d3dbeb5b84
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
8
diff
changeset
|
53 |
self._mid = 0 |
e3d3dbeb5b84
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
8
diff
changeset
|
54 |
self._tid = 0 |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
55 |
self._passwd = None |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
56 |
self._new = True |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
57 |
self._load() |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
58 |
|
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
59 |
def _load(self): |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
60 |
"""Load 'uid', 'mid' and 'tid' from the database and set _new to |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
61 |
`False` - if the user could be found. """ |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
62 |
dbc = self._dbh.cursor() |
216
0c8c053b451c
Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
63 |
dbc.execute( |
0c8c053b451c
Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
64 |
"SELECT uid, mid, tid FROM users WHERE gid=%s AND local_part=%s", |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
65 |
self._domain.gid, self._addr.localpart) |
9
e3d3dbeb5b84
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
8
diff
changeset
|
66 |
result = dbc.fetchone() |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
67 |
dbc.close() |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
68 |
if result: |
9
e3d3dbeb5b84
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
8
diff
changeset
|
69 |
self._uid, self._mid, self._tid = result |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
70 |
self._new = False |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
71 |
|
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
72 |
def _set_uid(self): |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
73 |
"""Set the unique ID for the new Account.""" |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
74 |
assert self._uid == 0 |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
75 |
dbc = self._dbh.cursor() |
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
76 |
dbc.execute("SELECT nextval('users_uid')") |
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
77 |
self._uid = dbc.fetchone()[0] |
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
78 |
dbc.close() |
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
79 |
|
9
e3d3dbeb5b84
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
8
diff
changeset
|
80 |
def _prepare(self, maillocation): |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
81 |
"""Check and set different attributes - before we store the |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
82 |
information in the database.""" |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
83 |
if not known_format(maillocation): |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
84 |
raise AErr(_(u'Unknown mail_location mailbox format: %r') % |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
85 |
maillocation, UNKNOWN_MAILLOCATION_NAME) |
228
a7b000ca4ac9
VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents:
227
diff
changeset
|
86 |
self._mid = MailLocation(format=maillocation).mid |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
87 |
if not self._tid: |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
88 |
self._tid = self._domain.tid |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
89 |
self._set_uid() |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
90 |
|
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
91 |
def _switch_state(self, state, dcvers, service): |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
92 |
"""Switch the state of the Account's services on or off. See |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
93 |
Account.enable()/Account.disable() for more information.""" |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
94 |
self._chk_state() |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
95 |
if service not in (None, 'all', 'imap', 'pop3', 'sieve', 'smtp'): |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
96 |
raise AErr(_(u"Unknown service: '%s'.") % service, UNKNOWN_SERVICE) |
134
6ca3d22e5dd0
Fixed a Python2.4.4 SyntaxError
Pascal Volk <neverseen@users.sourceforge.net>
parents:
133
diff
changeset
|
97 |
if dcvers > 11: |
6ca3d22e5dd0
Fixed a Python2.4.4 SyntaxError
Pascal Volk <neverseen@users.sourceforge.net>
parents:
133
diff
changeset
|
98 |
sieve_col = 'sieve' |
6ca3d22e5dd0
Fixed a Python2.4.4 SyntaxError
Pascal Volk <neverseen@users.sourceforge.net>
parents:
133
diff
changeset
|
99 |
else: |
6ca3d22e5dd0
Fixed a Python2.4.4 SyntaxError
Pascal Volk <neverseen@users.sourceforge.net>
parents:
133
diff
changeset
|
100 |
sieve_col = 'managesieve' |
115
21f264a88ab2
Added sieve/managesieve switching stuff,
Pascal Volk <neverseen@users.sourceforge.net>
parents:
114
diff
changeset
|
101 |
if service in ('smtp', 'pop3', 'imap'): |
21f264a88ab2
Added sieve/managesieve switching stuff,
Pascal Volk <neverseen@users.sourceforge.net>
parents:
114
diff
changeset
|
102 |
sql = 'UPDATE users SET %s = %s WHERE uid = %d' % (service, state, |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
103 |
self._uid) |
115
21f264a88ab2
Added sieve/managesieve switching stuff,
Pascal Volk <neverseen@users.sourceforge.net>
parents:
114
diff
changeset
|
104 |
elif service == 'sieve': |
21f264a88ab2
Added sieve/managesieve switching stuff,
Pascal Volk <neverseen@users.sourceforge.net>
parents:
114
diff
changeset
|
105 |
sql = 'UPDATE users SET %s = %s WHERE uid = %d' % (sieve_col, |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
106 |
state, |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
107 |
self._uid) |
115
21f264a88ab2
Added sieve/managesieve switching stuff,
Pascal Volk <neverseen@users.sourceforge.net>
parents:
114
diff
changeset
|
108 |
else: |
21f264a88ab2
Added sieve/managesieve switching stuff,
Pascal Volk <neverseen@users.sourceforge.net>
parents:
114
diff
changeset
|
109 |
sql = 'UPDATE users SET smtp = %(s)s, pop3 = %(s)s, imap = %(s)s,\ |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
110 |
%(col)s = %(s)s WHERE uid = %(uid)d' % \ |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
111 |
{'s': state, 'col': sieve_col, 'uid': self._uid} |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
112 |
dbc = self._dbh.cursor() |
115
21f264a88ab2
Added sieve/managesieve switching stuff,
Pascal Volk <neverseen@users.sourceforge.net>
parents:
114
diff
changeset
|
113 |
dbc.execute(sql) |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
114 |
if dbc.rowcount > 0: |
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
115 |
self._dbh.commit() |
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
116 |
dbc.close() |
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
117 |
|
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
118 |
def _count_aliases(self): |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
119 |
"""Count all alias addresses where the destination address is the |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
120 |
address of the Account.""" |
66
995d538a6eb5
* 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
65
diff
changeset
|
121 |
dbc = self._dbh.cursor() |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
122 |
sql = "SELECT COUNT(destination) FROM alias WHERE destination = '%s'"\ |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
123 |
% self._addr |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
124 |
dbc.execute(sql) |
66
995d538a6eb5
* 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
65
diff
changeset
|
125 |
a_count = dbc.fetchone()[0] |
995d538a6eb5
* 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
65
diff
changeset
|
126 |
dbc.close() |
995d538a6eb5
* 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
65
diff
changeset
|
127 |
return a_count |
995d538a6eb5
* 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
65
diff
changeset
|
128 |
|
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
129 |
def _chk_state(self): |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
130 |
"""Raise an AccountError if the Account is new - not yet saved in the |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
131 |
database.""" |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
132 |
if self._new: |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
133 |
raise AErr(_(u"The account '%s' doesn't exist.") % self._addr, |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
134 |
NO_SUCH_ACCOUNT) |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
135 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
136 |
@property |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
137 |
def domain_directory(self): |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
138 |
"""The directory of the domain the Account belongs to.""" |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
139 |
return self._domain.directory |
38
c44ea4526546
* 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
34
diff
changeset
|
140 |
|
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
141 |
@property |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
142 |
def gid(self): |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
143 |
"""The Account's group ID.""" |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
144 |
return self._domain.gid |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
145 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
146 |
@property |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
147 |
def home(self): |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
148 |
"""The Account's home directory.""" |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
149 |
return '%s/%s' % (self._domain.directory, self._uid) |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
150 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
151 |
@property |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
152 |
def uid(self): |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
153 |
"""The Account's unique ID.""" |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
154 |
return self._uid |
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
155 |
|
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
156 |
def set_password(self, password): |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
157 |
"""Set a password for the new Account. |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
158 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
159 |
If you want to update the password of an existing Account use |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
160 |
Account.modify(). |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
161 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
162 |
Argument: |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
163 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
164 |
`password` : basestring |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
165 |
The hashed password for the new Account.""" |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
166 |
self._passwd = password |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
167 |
|
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
168 |
def set_transport(self, transport): |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
169 |
"""Set the transport for the new Account. |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
170 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
171 |
If you want to update the transport of an existing Account use |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
172 |
Account.modify(). |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
173 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
174 |
Argument: |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
175 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
176 |
`transport` : basestring |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
177 |
The string representation of the transport, e.g.: 'dovecot:' |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
178 |
""" |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
179 |
self._tid = Transport(self._dbh, transport=transport).tid |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
180 |
|
115
21f264a88ab2
Added sieve/managesieve switching stuff,
Pascal Volk <neverseen@users.sourceforge.net>
parents:
114
diff
changeset
|
181 |
def enable(self, dcvers, service=None): |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
182 |
"""Enable a/all service/s for the Account. |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
183 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
184 |
Possible values for the *service* are: 'imap', 'pop3', 'sieve' and |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
185 |
'smtp'. When all services should be enabled, use 'all' or the |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
186 |
default value `None`. |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
187 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
188 |
Arguments: |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
189 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
190 |
`dcvers` : int |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
191 |
The concatenated major and minor version number from |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
192 |
`dovecot --version`. |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
193 |
`service` : basestring |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
194 |
The name of a service ('imap', 'pop3', 'smtp', 'sieve'), 'all' |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
195 |
or `None`. |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
196 |
""" |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
197 |
self._switch_state(True, dcvers, service) |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
198 |
|
115
21f264a88ab2
Added sieve/managesieve switching stuff,
Pascal Volk <neverseen@users.sourceforge.net>
parents:
114
diff
changeset
|
199 |
def disable(self, dcvers, service=None): |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
200 |
"""Disable a/all service/s for the Account. |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
201 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
202 |
For more information see: Account.enable().""" |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
203 |
self._switch_state(False, dcvers, service) |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
204 |
|
115
21f264a88ab2
Added sieve/managesieve switching stuff,
Pascal Volk <neverseen@users.sourceforge.net>
parents:
114
diff
changeset
|
205 |
def save(self, maillocation, dcvers, smtp, pop3, imap, sieve): |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
206 |
"""Save the new Account in the database. |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
207 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
208 |
Arguments: |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
209 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
210 |
`maillocation` : basestring |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
211 |
The mailbox format of the mail_location: 'maildir', 'mbox', |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
212 |
'dbox' or 'mdbox'. |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
213 |
`dcvers` : int |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
214 |
The concatenated major and minor version number from |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
215 |
`dovecot --version`. |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
216 |
`smtp, pop3, imap, sieve` : bool |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
217 |
Indicates if the user of the Account should be able to use this |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
218 |
services. |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
219 |
""" |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
220 |
if not self._new: |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
221 |
raise AErr(_(u"The account '%s' already exists.") % self._addr, |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
222 |
ACCOUNT_EXISTS) |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
223 |
if not self._passwd: |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
224 |
raise AErr(_(u"No password set for '%s'.") % self._addr, |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
225 |
ACCOUNT_MISSING_PASSWORD) |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
226 |
assert all(isinstance(service, bool) for service in (smtp, pop3, imap, |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
227 |
sieve)) |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
228 |
if dcvers > 11: |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
229 |
sieve_col = 'sieve' |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
230 |
else: |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
231 |
sieve_col = 'managesieve' |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
232 |
self._prepare(maillocation) |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
233 |
sql = "INSERT INTO users (local_part, passwd, uid, gid, mid, tid,\ |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
234 |
smtp, pop3, imap, %s) VALUES ('%s', '%s', %d, %d, %d, %d, %s, %s, %s, %s)" % ( |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
235 |
sieve_col, self._addr.localpart, self._passwd, self._uid, |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
236 |
self._domain.gid, self._mid, self._tid, smtp, pop3, imap, sieve) |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
237 |
dbc = self._dbh.cursor() |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
238 |
dbc.execute(sql) |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
239 |
self._dbh.commit() |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
240 |
dbc.close() |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
241 |
self._new = False |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
242 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
243 |
def modify(self, field, value): |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
244 |
"""Update the Account's *field* to the new *value*. |
128
cf8116625866
Converted VirtualMailManager and Postconf to new-style classes.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
121
diff
changeset
|
245 |
|
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
246 |
Possible values for *filed* are: 'name', 'password' and |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
247 |
'transport'. *value* is the *field*'s new value. |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
248 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
249 |
Arguments: |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
250 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
251 |
`field` : basestring |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
252 |
The attribute name: 'name', 'password' or 'transport' |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
253 |
`value` : basestring |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
254 |
The new value of the attribute. The password is expected as a |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
255 |
hashed password string. |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
256 |
""" |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
257 |
if field not in ('name', 'password', 'transport'): |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
258 |
raise AErr(_(u"Unknown field: '%s'") % field, INVALID_AGUMENT) |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
259 |
self._chk_state() |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
260 |
dbc = self._dbh.cursor() |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
261 |
if field == 'password': |
121
7ccc05774118
Converted to new-style class, added __slots__, updated queries.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
115
diff
changeset
|
262 |
dbc.execute('UPDATE users SET passwd = %s WHERE uid = %s', |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
263 |
value, self._uid) |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
264 |
elif field == 'transport': |
235
9d3405ed08e5
VMM/Transport: Renamed attribute/property {,_}id to {,_}tid.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
228
diff
changeset
|
265 |
self._tid = Transport(self._dbh, transport=value).tid |
121
7ccc05774118
Converted to new-style class, added __slots__, updated queries.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
115
diff
changeset
|
266 |
dbc.execute('UPDATE users SET tid = %s WHERE uid = %s', |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
267 |
self._tid, self._uid) |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
268 |
else: |
121
7ccc05774118
Converted to new-style class, added __slots__, updated queries.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
115
diff
changeset
|
269 |
dbc.execute('UPDATE users SET name = %s WHERE uid = %s', |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
270 |
value, self._uid) |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
271 |
if dbc.rowcount > 0: |
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
272 |
self._dbh.commit() |
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
273 |
dbc.close() |
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
274 |
|
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
275 |
def get_info(self, dcvers): |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
276 |
"""Returns a dict with some information about the Account. |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
277 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
278 |
The keys of the dict are: 'address', 'gid', 'home', 'imap' |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
279 |
'mail_location', 'name', 'pop3', 'sieve', 'smtp', transport' and |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
280 |
'uid'. |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
281 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
282 |
Argument: |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
283 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
284 |
`dcvers` : int |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
285 |
The concatenated major and minor version number from |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
286 |
`dovecot --version`. |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
287 |
""" |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
288 |
self._chk_state() |
134
6ca3d22e5dd0
Fixed a Python2.4.4 SyntaxError
Pascal Volk <neverseen@users.sourceforge.net>
parents:
133
diff
changeset
|
289 |
if dcvers > 11: |
6ca3d22e5dd0
Fixed a Python2.4.4 SyntaxError
Pascal Volk <neverseen@users.sourceforge.net>
parents:
133
diff
changeset
|
290 |
sieve_col = 'sieve' |
6ca3d22e5dd0
Fixed a Python2.4.4 SyntaxError
Pascal Volk <neverseen@users.sourceforge.net>
parents:
133
diff
changeset
|
291 |
else: |
6ca3d22e5dd0
Fixed a Python2.4.4 SyntaxError
Pascal Volk <neverseen@users.sourceforge.net>
parents:
133
diff
changeset
|
292 |
sieve_col = 'managesieve' |
115
21f264a88ab2
Added sieve/managesieve switching stuff,
Pascal Volk <neverseen@users.sourceforge.net>
parents:
114
diff
changeset
|
293 |
sql = 'SELECT name, uid, gid, mid, tid, smtp, pop3, imap, %s\ |
21f264a88ab2
Added sieve/managesieve switching stuff,
Pascal Volk <neverseen@users.sourceforge.net>
parents:
114
diff
changeset
|
294 |
FROM users WHERE uid = %d' % (sieve_col, self._uid) |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
295 |
dbc = self._dbh.cursor() |
115
21f264a88ab2
Added sieve/managesieve switching stuff,
Pascal Volk <neverseen@users.sourceforge.net>
parents:
114
diff
changeset
|
296 |
dbc.execute(sql) |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
297 |
info = dbc.fetchone() |
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
298 |
dbc.close() |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
299 |
if info: |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
300 |
keys = ('name', 'uid', 'gid', 'mid', 'transport', 'smtp', |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
301 |
'pop3', 'imap', sieve_col) |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
302 |
info = dict(zip(keys, info)) |
115
21f264a88ab2
Added sieve/managesieve switching stuff,
Pascal Volk <neverseen@users.sourceforge.net>
parents:
114
diff
changeset
|
303 |
for service in ('smtp', 'pop3', 'imap', sieve_col): |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
304 |
if info[service]: |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
305 |
# TP: A service (pop3/imap) is enabled/usable for a user |
41
fbcb7e314510
* 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
38
diff
changeset
|
306 |
info[service] = _('enabled') |
20
55146c78b3fb
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
19
diff
changeset
|
307 |
else: |
156
a849843115e9
Added comments for the Translation Project. Updated PO template.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
155
diff
changeset
|
308 |
# TP: A service (pop3/imap) isn't enabled/usable for a user |
41
fbcb7e314510
* 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
38
diff
changeset
|
309 |
info[service] = _('disabled') |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
310 |
info['address'] = self._addr |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
311 |
info['home'] = '%s/%s' % (self._domain.directory, info['uid']) |
227
87db9f1f95ea
VMM/Account: Adjusted to changes in maillocation.MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
225
diff
changeset
|
312 |
info['mail_location'] = MailLocation(mid=info['mid']).mail_location |
9
e3d3dbeb5b84
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
8
diff
changeset
|
313 |
info['transport'] = Transport(self._dbh, |
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
314 |
tid=info['transport']).transport |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
315 |
del info['mid'] |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
316 |
return info |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
317 |
# nearly impossible‽ |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
318 |
raise AErr(_(u"Couldn't fetch information for account: '%s'") \ |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
319 |
% self._addr, NO_SUCH_ACCOUNT) |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
320 |
|
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
321 |
def get_aliases(self): |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
322 |
"""Return a list with all alias e-mail addresses, whose destination |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
323 |
is the address of the Account.""" |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
324 |
self._chk_state() |
90
1734eb5101c6
* 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
82
diff
changeset
|
325 |
dbc = self._dbh.cursor() |
1734eb5101c6
* 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
82
diff
changeset
|
326 |
dbc.execute("SELECT address ||'@'|| domainname FROM alias, domain_name\ |
1734eb5101c6
* 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
82
diff
changeset
|
327 |
WHERE destination = %s AND domain_name.gid = alias.gid\ |
145
ead2a7e9f8be
Sort alias addresses in Account.getAliases() / vmm userinfo.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
144
diff
changeset
|
328 |
AND domain_name.is_primary ORDER BY address", str(self._addr)) |
90
1734eb5101c6
* 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
82
diff
changeset
|
329 |
addresses = dbc.fetchall() |
1734eb5101c6
* 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
82
diff
changeset
|
330 |
dbc.close() |
1734eb5101c6
* 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
82
diff
changeset
|
331 |
aliases = [] |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
332 |
if addresses: |
144
4c6aa6c29dd7
Small optimizations in Account.getAliases() and Alias.getInfo().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
138
diff
changeset
|
333 |
aliases = [alias[0] for alias in addresses] |
90
1734eb5101c6
* 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
82
diff
changeset
|
334 |
return aliases |
1734eb5101c6
* 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
82
diff
changeset
|
335 |
|
66
995d538a6eb5
* 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
65
diff
changeset
|
336 |
def delete(self, delalias): |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
337 |
"""Delete the Account from the database. |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
338 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
339 |
Argument: |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
340 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
341 |
`delalias` : basestring |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
342 |
if the values of delalias is 'delalias', all aliases, which |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
343 |
points to the Account, will be also deleted.""" |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
344 |
self._chk_state() |
66
995d538a6eb5
* 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
65
diff
changeset
|
345 |
dbc = self._dbh.cursor() |
995d538a6eb5
* 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
65
diff
changeset
|
346 |
if delalias == 'delalias': |
121
7ccc05774118
Converted to new-style class, added __slots__, updated queries.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
115
diff
changeset
|
347 |
dbc.execute('DELETE FROM users WHERE uid= %s', self._uid) |
65
5506433db9a3
* 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
55
diff
changeset
|
348 |
# delete also all aliases where the destination address is the same |
5506433db9a3
* 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
55
diff
changeset
|
349 |
# as for this account. |
114
e671210b04b8
Fixed a libpq.OperationalError in Account.delete()
Pascal Volk <neverseen@users.sourceforge.net>
parents:
113
diff
changeset
|
350 |
dbc.execute("DELETE FROM alias WHERE destination = %s", |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
351 |
str(self._addr)) |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
352 |
self._dbh.commit() |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
353 |
else: # check first for aliases |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
354 |
a_count = self._count_aliases() |
66
995d538a6eb5
* 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
65
diff
changeset
|
355 |
if a_count == 0: |
121
7ccc05774118
Converted to new-style class, added __slots__, updated queries.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
115
diff
changeset
|
356 |
dbc.execute('DELETE FROM users WHERE uid = %s', self._uid) |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
357 |
self._dbh.commit() |
66
995d538a6eb5
* 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
65
diff
changeset
|
358 |
else: |
995d538a6eb5
* 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
65
diff
changeset
|
359 |
dbc.close() |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
360 |
raise AErr(_(u"There are %(count)d aliases with the \ |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
361 |
destination address '%(address)s'.") % \ |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
362 |
{'count': a_count, 'address': self._addr}, |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
363 |
ALIAS_PRESENT) |
66
995d538a6eb5
* 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
65
diff
changeset
|
364 |
dbc.close() |
19
bf9a03c476fc
* 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
17
diff
changeset
|
365 |
|
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
366 |
|
19
bf9a03c476fc
* 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
17
diff
changeset
|
367 |
def getAccountByID(uid, dbh): |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
368 |
"""Search an Account by its UID. |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
369 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
370 |
Argument: |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
371 |
|
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
372 |
`uid` : long |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
373 |
The Account unique ID. |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
374 |
`dbh` : pyPgSQL.PgSQL.Connection |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
375 |
a database connection for the database access. |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
376 |
""" |
19
bf9a03c476fc
* 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
17
diff
changeset
|
377 |
try: |
bf9a03c476fc
* 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
17
diff
changeset
|
378 |
uid = long(uid) |
bf9a03c476fc
* 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
17
diff
changeset
|
379 |
except ValueError: |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
380 |
raise AErr(_(u'UID must be an int/long.'), INVALID_AGUMENT) |
19
bf9a03c476fc
* 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
17
diff
changeset
|
381 |
if uid < 1: |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
382 |
raise AErr(_(u'UID must be greater than 0.'), INVALID_AGUMENT) |
19
bf9a03c476fc
* 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
17
diff
changeset
|
383 |
dbc = dbh.cursor() |
43
92a6132940f5
* 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
41
diff
changeset
|
384 |
dbc.execute("SELECT local_part||'@'|| domain_name.domainname AS address,\ |
92a6132940f5
* 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
41
diff
changeset
|
385 |
uid, users.gid FROM users LEFT JOIN domain_name ON (domain_name.gid \ |
92a6132940f5
* 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
41
diff
changeset
|
386 |
= users.gid AND is_primary) WHERE uid = %s;", uid) |
19
bf9a03c476fc
* 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
17
diff
changeset
|
387 |
info = dbc.fetchone() |
bf9a03c476fc
* 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
17
diff
changeset
|
388 |
dbc.close() |
245
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
389 |
if not info: |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
390 |
raise AErr(_(u"There is no account with the UID '%d'.") % uid, |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
391 |
NO_SUCH_ACCOUNT) |
f9a6b6701cf9
VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
236
diff
changeset
|
392 |
info = dict(zip(('address', 'uid', 'gid'), info)) |
19
bf9a03c476fc
* 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
17
diff
changeset
|
393 |
return info |
bf9a03c476fc
* 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
17
diff
changeset
|
394 |
|
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
395 |
|
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
396 |
del _ |