[7193] | 1 | ## $Id: userscontainer.py 12220 2014-12-14 05:04:52Z 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 | ## |
---|
[11949] | 18 | """Users container for the Ikoba portal. |
---|
[4089] | 19 | """ |
---|
| 20 | import grok |
---|
[6180] | 21 | from zope.event import notify |
---|
[11949] | 22 | from waeup.ikoba.authentication import Account |
---|
| 23 | from waeup.ikoba.interfaces import IUsersContainer |
---|
| 24 | from waeup.ikoba.utils.logger import Logger |
---|
| 25 | from waeup.ikoba.utils.batching import ExporterBase |
---|
| 26 | from waeup.ikoba.interfaces import ICSVExporter |
---|
| 27 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
[4089] | 28 | |
---|
[8787] | 29 | |
---|
[7653] | 30 | class UsersContainer(grok.Container, Logger): |
---|
[4089] | 31 | """A container for principals. |
---|
| 32 | |
---|
| 33 | See interfaces.py and users.txt for extensive description. |
---|
| 34 | """ |
---|
[7172] | 35 | grok.implements(IUsersContainer) |
---|
[4089] | 36 | grok.require('waeup.manageUsers') |
---|
| 37 | |
---|
[7221] | 38 | def addUser(self, name, password, title=None, |
---|
[8757] | 39 | description=None, email=None, phone=None, |
---|
| 40 | public_name= None, roles=[]): |
---|
[4638] | 41 | """Add a new Account instance, created from parameters. |
---|
| 42 | """ |
---|
[4089] | 43 | if title is None: |
---|
| 44 | title = name |
---|
[7197] | 45 | #if description is None: |
---|
| 46 | # description = title |
---|
[7636] | 47 | self[name] = Account(name=name, password=password, title=title, |
---|
[8757] | 48 | description=description, public_name=public_name, |
---|
[7636] | 49 | email=email, phone=phone, roles=roles) |
---|
[4089] | 50 | |
---|
| 51 | def addAccount(self, account): |
---|
[4638] | 52 | """Add the account passed. |
---|
| 53 | """ |
---|
[4089] | 54 | self[account.name] = account |
---|
[6180] | 55 | |
---|
[4089] | 56 | def delUser(self, name): |
---|
[4638] | 57 | """Delete user, if an account with the given name exists. |
---|
| 58 | |
---|
| 59 | Do not complain, if the name does not exist. |
---|
| 60 | """ |
---|
[4089] | 61 | if name in self.keys(): |
---|
| 62 | del self[name] |
---|
[8787] | 63 | |
---|
| 64 | |
---|
[12087] | 65 | class UserExporter(grok.GlobalUtility, ExporterBase): |
---|
[8986] | 66 | """Exporter for user accounts. |
---|
[8787] | 67 | """ |
---|
| 68 | grok.implements(ICSVExporter) |
---|
| 69 | grok.name('users') |
---|
| 70 | |
---|
| 71 | #: Fieldnames considered by this exporter |
---|
| 72 | fields = ('name', 'title', 'public_name', 'description', |
---|
| 73 | 'email', 'phone', 'roles', 'local_roles', 'password') |
---|
| 74 | |
---|
| 75 | #: The title under which this exporter will be displayed |
---|
| 76 | title = _(u'Users') |
---|
| 77 | |
---|
| 78 | def mangle_value(self, value, name, context=None): |
---|
| 79 | """Treat location values special. |
---|
| 80 | """ |
---|
[8986] | 81 | |
---|
[8787] | 82 | if name == 'local_roles' and context is not None: |
---|
| 83 | local_roles = context.getLocalRoles() |
---|
| 84 | value = {} |
---|
| 85 | for role in local_roles.keys(): |
---|
| 86 | objects = local_roles[role] |
---|
| 87 | object_list = [] |
---|
| 88 | for object in objects: |
---|
[10648] | 89 | obj= object |
---|
| 90 | path = '' |
---|
[11954] | 91 | while obj.__class__.__name__ != 'Company': |
---|
[10648] | 92 | path = '%s/' % obj.__name__ + path |
---|
| 93 | obj = obj.__parent__ |
---|
| 94 | object_list.append(path) |
---|
[8787] | 95 | value[role] = object_list |
---|
| 96 | return super( |
---|
[12087] | 97 | UserExporter, self).mangle_value( |
---|
[8787] | 98 | value, name, context=context) |
---|
| 99 | |
---|
| 100 | def export(self, users, filepath=None): |
---|
| 101 | """Export `users`, an iterable, as CSV file. |
---|
| 102 | |
---|
| 103 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 104 | """ |
---|
| 105 | writer, outfile = self.get_csv_writer(filepath) |
---|
| 106 | for user in users: |
---|
| 107 | self.write_item(user, writer) |
---|
| 108 | return self.close_outfile(filepath, outfile) |
---|
| 109 | |
---|
| 110 | def export_all(self, site, filepath=None): |
---|
| 111 | """Export users into filepath as CSV data. |
---|
| 112 | |
---|
| 113 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 114 | """ |
---|
| 115 | users = site.get('users', {}) |
---|
| 116 | return self.export(users.values(), filepath) |
---|