1 | ## $Id: userscontainer.py 12915 2015-05-08 07:09:08Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
4 | ## This program is free software; you can redistribute it and/or modify |
---|
5 | ## it under the terms of the GNU General Public License as published by |
---|
6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
7 | ## (at your option) any later version. |
---|
8 | ## |
---|
9 | ## This program is distributed in the hope that it will be useful, |
---|
10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | ## GNU General Public License for more details. |
---|
13 | ## |
---|
14 | ## You should have received a copy of the GNU General Public License |
---|
15 | ## along with this program; if not, write to the Free Software |
---|
16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | ## |
---|
18 | """Users container for the Kofa portal. |
---|
19 | """ |
---|
20 | import grok |
---|
21 | from zope.event import notify |
---|
22 | from waeup.kofa.authentication import Account |
---|
23 | from waeup.kofa.interfaces import IUsersContainer |
---|
24 | from waeup.kofa.utils.logger import Logger |
---|
25 | from waeup.kofa.utils.batching import ExporterBase |
---|
26 | from waeup.kofa.interfaces import ICSVExporter |
---|
27 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
28 | |
---|
29 | |
---|
30 | class UsersContainer(grok.Container, Logger): |
---|
31 | """A container for officers. |
---|
32 | |
---|
33 | See interfaces.py and users.txt for extensive description. |
---|
34 | """ |
---|
35 | grok.implements(IUsersContainer) |
---|
36 | grok.require('waeup.manageUsers') |
---|
37 | |
---|
38 | def addUser(self, name, password, title=None, |
---|
39 | description=None, email=None, phone=None, |
---|
40 | public_name= None, roles=[]): |
---|
41 | """Add a new Account instance, created from parameters. |
---|
42 | """ |
---|
43 | if title is None: |
---|
44 | title = name |
---|
45 | self[name] = Account(name=name, password=password, title=title, |
---|
46 | description=description, public_name=public_name, |
---|
47 | email=email, phone=phone, roles=roles) |
---|
48 | |
---|
49 | def addAccount(self, account): |
---|
50 | """Add the account passed. |
---|
51 | """ |
---|
52 | self[account.name] = account |
---|
53 | |
---|
54 | def delUser(self, name): |
---|
55 | """Delete user, if an account with the given name exists. |
---|
56 | |
---|
57 | Do not complain, if the name does not exist. |
---|
58 | """ |
---|
59 | if name in self.keys(): |
---|
60 | del self[name] |
---|
61 | |
---|
62 | |
---|
63 | class UserExporter(grok.GlobalUtility, ExporterBase): |
---|
64 | """The User Exporter exports all user accounts. It iterates over all |
---|
65 | objects of the ``users`` container. |
---|
66 | """ |
---|
67 | grok.implements(ICSVExporter) |
---|
68 | grok.name('users') |
---|
69 | |
---|
70 | fields = ('name', 'title', 'public_name', 'description', |
---|
71 | 'email', 'phone', 'roles', 'local_roles', 'password') |
---|
72 | title = _(u'Users') |
---|
73 | |
---|
74 | def mangle_value(self, value, name, context=None): |
---|
75 | """The mangler determines the local roles each user has and computes |
---|
76 | a Python expression like: |
---|
77 | |
---|
78 | ``{u'waeup.local.ClearanceOfficer': [u'faculties/ABC/', u'faculties/DEF/']}`` |
---|
79 | """ |
---|
80 | if name == 'local_roles' and context is not None: |
---|
81 | local_roles = context.getLocalRoles() |
---|
82 | value = {} |
---|
83 | for role in local_roles.keys(): |
---|
84 | objects = local_roles[role] |
---|
85 | object_list = [] |
---|
86 | for object in objects: |
---|
87 | obj= object |
---|
88 | path = '' |
---|
89 | while obj.__class__.__name__ != 'University': |
---|
90 | path = '%s/' % obj.__name__ + path |
---|
91 | obj = obj.__parent__ |
---|
92 | object_list.append(path) |
---|
93 | value[role] = object_list |
---|
94 | return super( |
---|
95 | UserExporter, self).mangle_value( |
---|
96 | value, name, context=context) |
---|
97 | |
---|
98 | def export(self, users, filepath=None): |
---|
99 | """Export `users`, an iterable, as CSV file. |
---|
100 | |
---|
101 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
102 | """ |
---|
103 | writer, outfile = self.get_csv_writer(filepath) |
---|
104 | for user in users: |
---|
105 | self.write_item(user, writer) |
---|
106 | return self.close_outfile(filepath, outfile) |
---|
107 | |
---|
108 | def export_all(self, site, filepath=None): |
---|
109 | """Export users into filepath as CSV data. |
---|
110 | |
---|
111 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
112 | """ |
---|
113 | users = site.get('users', {}) |
---|
114 | return self.export(users.values(), filepath) |
---|