[7193] | 1 | ## $Id: authentication.py 14670 2017-04-06 12:20:41Z uli $ |
---|
| 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 | ## |
---|
[7819] | 18 | """Authentication for Kofa. |
---|
[4073] | 19 | """ |
---|
| 20 | import grok |
---|
[10055] | 21 | import time |
---|
[7169] | 22 | from zope.event import notify |
---|
[5900] | 23 | from zope.component import getUtility, getUtilitiesFor |
---|
[8973] | 24 | from zope.component.interfaces import IFactory |
---|
[10055] | 25 | from zope.interface import Interface, implementedBy |
---|
[8756] | 26 | from zope.schema import getFields |
---|
[7169] | 27 | from zope.securitypolicy.interfaces import ( |
---|
| 28 | IPrincipalRoleMap, IPrincipalRoleManager) |
---|
[7233] | 29 | from zope.pluggableauth.factories import Principal |
---|
[6610] | 30 | from zope.pluggableauth.plugins.session import SessionCredentialsPlugin |
---|
[14667] | 31 | from zope.pluggableauth.plugins.httpplugins import ( |
---|
[14669] | 32 | HTTPBasicAuthCredentialsPlugin) |
---|
[6610] | 33 | from zope.pluggableauth.interfaces import ( |
---|
[14669] | 34 | ICredentialsPlugin, IAuthenticatorPlugin, IAuthenticatedPrincipalFactory, |
---|
| 35 | AuthenticatedPrincipalCreated) |
---|
[7233] | 36 | from zope.publisher.interfaces import IRequest |
---|
[6610] | 37 | from zope.password.interfaces import IPasswordManager |
---|
[4129] | 38 | from zope.securitypolicy.principalrole import principalRoleManager |
---|
[14669] | 39 | from waeup.kofa.interfaces import ( |
---|
| 40 | ILocalRoleSetEvent, IUserAccount, IAuthPluginUtility, IPasswordValidator, |
---|
| 41 | IKofaPrincipal, IKofaPrincipalInfo, IKofaPluggable, IBatchProcessor, |
---|
| 42 | IGNORE_MARKER, IFailedLoginInfo) |
---|
[8973] | 43 | from waeup.kofa.utils.batching import BatchProcessor |
---|
[12190] | 44 | from waeup.kofa.permissions import get_all_roles |
---|
[4073] | 45 | |
---|
[14669] | 46 | |
---|
[4073] | 47 | def setup_authentication(pau): |
---|
| 48 | """Set up plugguble authentication utility. |
---|
| 49 | |
---|
| 50 | Sets up an IAuthenticatorPlugin and |
---|
| 51 | ICredentialsPlugin (for the authentication mechanism) |
---|
[5900] | 52 | |
---|
| 53 | Then looks for any external utilities that want to modify the PAU. |
---|
[4073] | 54 | """ |
---|
[14667] | 55 | pau.credentialsPlugins = ( |
---|
[14669] | 56 | 'No Challenge if Authenticated', |
---|
| 57 | 'xmlrpc-credentials', |
---|
| 58 | 'credentials') |
---|
[6672] | 59 | pau.authenticatorPlugins = ('users',) |
---|
[4073] | 60 | |
---|
[5900] | 61 | # Give any third-party code and subpackages a chance to modify the PAU |
---|
| 62 | auth_plugin_utilities = getUtilitiesFor(IAuthPluginUtility) |
---|
[5901] | 63 | for name, util in auth_plugin_utilities: |
---|
[5900] | 64 | util.register(pau) |
---|
| 65 | |
---|
[14669] | 66 | |
---|
[6673] | 67 | def get_principal_role_manager(): |
---|
| 68 | """Get a role manager for principals. |
---|
| 69 | |
---|
| 70 | If we are currently 'in a site', return the role manager for the |
---|
| 71 | portal or the global rolemanager else. |
---|
| 72 | """ |
---|
| 73 | portal = grok.getSite() |
---|
| 74 | if portal is not None: |
---|
| 75 | return IPrincipalRoleManager(portal) |
---|
| 76 | return principalRoleManager |
---|
| 77 | |
---|
[14669] | 78 | |
---|
| 79 | class KofaSessionCredentialsPlugin( |
---|
| 80 | grok.GlobalUtility, SessionCredentialsPlugin): |
---|
[14667] | 81 | """Session plugin that picks usernames/passwords from fields in webforms. |
---|
| 82 | """ |
---|
[4073] | 83 | grok.provides(ICredentialsPlugin) |
---|
| 84 | grok.name('credentials') |
---|
| 85 | |
---|
| 86 | loginpagename = 'login' |
---|
| 87 | loginfield = 'form.login' |
---|
| 88 | passwordfield = 'form.password' |
---|
| 89 | |
---|
[14667] | 90 | |
---|
| 91 | class KofaXMLRPCCredentialsPlugin( |
---|
| 92 | grok.GlobalUtility, HTTPBasicAuthCredentialsPlugin): |
---|
| 93 | """Plugin that picks useranams/passwords from basic-auth headers. |
---|
| 94 | |
---|
| 95 | As XMLRPC requests send/post their authentication credentials in HTTP |
---|
| 96 | basic-auth headers, we need a plugin that can handle this. |
---|
| 97 | |
---|
| 98 | This plugin, however, does no challenging. If a user does not provide |
---|
| 99 | basic-auth infos, we will not ask for some. This is correct as we plan to |
---|
| 100 | communicate with machines. |
---|
| 101 | |
---|
| 102 | This plugin is planned to be used in "PluggableAuthenitications" registered |
---|
| 103 | with `University` instances. |
---|
| 104 | """ |
---|
| 105 | grok.provides(ICredentialsPlugin) |
---|
| 106 | grok.name('xmlrpc-credentials') |
---|
| 107 | |
---|
| 108 | def challenge(self, request): |
---|
| 109 | """XMLRPC is for machines. No need to challenge. |
---|
| 110 | """ |
---|
| 111 | return False |
---|
| 112 | |
---|
| 113 | def logout(self, request): |
---|
| 114 | """Basic auth does not provide any logout possibility. |
---|
| 115 | """ |
---|
| 116 | return False |
---|
| 117 | |
---|
| 118 | |
---|
[7819] | 119 | class KofaPrincipalInfo(object): |
---|
| 120 | """An implementation of IKofaPrincipalInfo. |
---|
[4073] | 121 | |
---|
[7819] | 122 | A Kofa principal info is created with id, login, title, description, |
---|
[8757] | 123 | phone, email, public_name and user_type. |
---|
[7233] | 124 | """ |
---|
[7819] | 125 | grok.implements(IKofaPrincipalInfo) |
---|
[7233] | 126 | |
---|
[10055] | 127 | def __init__(self, id, title, description, email, phone, public_name, |
---|
| 128 | user_type): |
---|
[4073] | 129 | self.id = id |
---|
| 130 | self.title = title |
---|
| 131 | self.description = description |
---|
[7233] | 132 | self.email = email |
---|
| 133 | self.phone = phone |
---|
[8757] | 134 | self.public_name = public_name |
---|
[7240] | 135 | self.user_type = user_type |
---|
[4073] | 136 | self.credentialsPlugin = None |
---|
| 137 | self.authenticatorPlugin = None |
---|
| 138 | |
---|
[10055] | 139 | def __eq__(self, obj): |
---|
| 140 | default = object() |
---|
| 141 | result = [] |
---|
| 142 | for name in ('id', 'title', 'description', 'email', 'phone', |
---|
| 143 | 'public_name', 'user_type', 'credentialsPlugin', |
---|
| 144 | 'authenticatorPlugin'): |
---|
| 145 | result.append( |
---|
| 146 | getattr(self, name) == getattr(obj, name, default)) |
---|
| 147 | return False not in result |
---|
| 148 | |
---|
[14669] | 149 | |
---|
[7819] | 150 | class KofaPrincipal(Principal): |
---|
[7233] | 151 | """A portal principal. |
---|
| 152 | |
---|
[10055] | 153 | Kofa principals provide an extra `email`, `phone`, `public_name` |
---|
| 154 | and `user_type` attribute extending ordinary principals. |
---|
[7233] | 155 | """ |
---|
| 156 | |
---|
[7819] | 157 | grok.implements(IKofaPrincipal) |
---|
[7233] | 158 | |
---|
| 159 | def __init__(self, id, title=u'', description=u'', email=u'', |
---|
[8757] | 160 | phone=None, public_name=u'', user_type=u'', prefix=None): |
---|
[7233] | 161 | self.id = id |
---|
[7239] | 162 | if prefix is not None: |
---|
| 163 | self.id = '%s.%s' % (prefix, self.id) |
---|
[7233] | 164 | self.title = title |
---|
| 165 | self.description = description |
---|
| 166 | self.groups = [] |
---|
| 167 | self.email = email |
---|
| 168 | self.phone = phone |
---|
[8757] | 169 | self.public_name = public_name |
---|
[7240] | 170 | self.user_type = user_type |
---|
[7233] | 171 | |
---|
| 172 | def __repr__(self): |
---|
[7819] | 173 | return 'KofaPrincipal(%r)' % self.id |
---|
[7233] | 174 | |
---|
[14669] | 175 | |
---|
[7819] | 176 | class AuthenticatedKofaPrincipalFactory(grok.MultiAdapter): |
---|
| 177 | """Creates 'authenticated' Kofa principals. |
---|
[7233] | 178 | |
---|
[7819] | 179 | Adapts (principal info, request) to a KofaPrincipal instance. |
---|
[7233] | 180 | |
---|
| 181 | This adapter is used by the standard PAU to transform |
---|
[7819] | 182 | KofaPrincipalInfos into KofaPrincipal instances. |
---|
[7233] | 183 | """ |
---|
[7819] | 184 | grok.adapts(IKofaPrincipalInfo, IRequest) |
---|
[7233] | 185 | grok.implements(IAuthenticatedPrincipalFactory) |
---|
| 186 | |
---|
| 187 | def __init__(self, info, request): |
---|
| 188 | self.info = info |
---|
| 189 | self.request = request |
---|
| 190 | |
---|
| 191 | def __call__(self, authentication): |
---|
[7819] | 192 | principal = KofaPrincipal( |
---|
[7233] | 193 | self.info.id, |
---|
| 194 | self.info.title, |
---|
| 195 | self.info.description, |
---|
| 196 | self.info.email, |
---|
| 197 | self.info.phone, |
---|
[8757] | 198 | self.info.public_name, |
---|
[7240] | 199 | self.info.user_type, |
---|
[7239] | 200 | authentication.prefix, |
---|
[7233] | 201 | ) |
---|
| 202 | notify( |
---|
| 203 | AuthenticatedPrincipalCreated( |
---|
| 204 | authentication, principal, self.info, self.request)) |
---|
| 205 | return principal |
---|
| 206 | |
---|
[14669] | 207 | |
---|
[10055] | 208 | class FailedLoginInfo(grok.Model): |
---|
| 209 | grok.implements(IFailedLoginInfo) |
---|
| 210 | |
---|
| 211 | def __init__(self, num=0, last=None): |
---|
| 212 | self.num = num |
---|
| 213 | self.last = last |
---|
| 214 | return |
---|
| 215 | |
---|
| 216 | def as_tuple(self): |
---|
| 217 | return (self.num, self.last) |
---|
| 218 | |
---|
| 219 | def set_values(self, num=0, last=None): |
---|
| 220 | self.num, self.last = num, last |
---|
| 221 | self._p_changed = True |
---|
| 222 | pass |
---|
| 223 | |
---|
| 224 | def increase(self): |
---|
| 225 | self.set_values(num=self.num + 1, last=time.time()) |
---|
| 226 | pass |
---|
| 227 | |
---|
| 228 | def reset(self): |
---|
| 229 | self.set_values(num=0, last=None) |
---|
| 230 | pass |
---|
| 231 | |
---|
[14669] | 232 | |
---|
[4073] | 233 | class Account(grok.Model): |
---|
[10055] | 234 | """Kofa user accounts store infos about a user. |
---|
| 235 | |
---|
| 236 | Beside the usual data and an (encrypted) password, accounts also |
---|
| 237 | have a persistent attribute `failed_logins` which is an instance |
---|
| 238 | of `waeup.kofa.authentication.FailedLoginInfo`. |
---|
| 239 | |
---|
| 240 | This attribute can be manipulated directly (set new value, |
---|
| 241 | increase values, or reset). |
---|
| 242 | """ |
---|
[4109] | 243 | grok.implements(IUserAccount) |
---|
[4129] | 244 | |
---|
[4125] | 245 | def __init__(self, name, password, title=None, description=None, |
---|
[14669] | 246 | email=None, phone=None, public_name=None, roles=[]): |
---|
[4073] | 247 | self.name = name |
---|
[4087] | 248 | if title is None: |
---|
| 249 | title = name |
---|
| 250 | self.title = title |
---|
| 251 | self.description = description |
---|
[7221] | 252 | self.email = email |
---|
[7233] | 253 | self.phone = phone |
---|
[8756] | 254 | self.public_name = public_name |
---|
[12926] | 255 | self.suspended = False |
---|
[4073] | 256 | self.setPassword(password) |
---|
[7636] | 257 | self.setSiteRolesForPrincipal(roles) |
---|
[10055] | 258 | |
---|
[6180] | 259 | # We don't want to share this dict with other accounts |
---|
| 260 | self._local_roles = dict() |
---|
[10055] | 261 | self.failed_logins = FailedLoginInfo() |
---|
[4073] | 262 | |
---|
| 263 | def setPassword(self, password): |
---|
[8343] | 264 | passwordmanager = getUtility(IPasswordManager, 'SSHA') |
---|
[4073] | 265 | self.password = passwordmanager.encodePassword(password) |
---|
| 266 | |
---|
| 267 | def checkPassword(self, password): |
---|
[13394] | 268 | try: |
---|
| 269 | blocker = grok.getSite()['configuration'].maintmode_enabled_by |
---|
| 270 | if blocker and blocker != self.name: |
---|
| 271 | return False |
---|
| 272 | except (TypeError, KeyError): # in unit tests |
---|
| 273 | pass |
---|
[8344] | 274 | if not isinstance(password, basestring): |
---|
| 275 | return False |
---|
| 276 | if not self.password: |
---|
| 277 | # unset/empty passwords do never match |
---|
| 278 | return False |
---|
[14669] | 279 | if self.suspended: |
---|
[12926] | 280 | return False |
---|
[8343] | 281 | passwordmanager = getUtility(IPasswordManager, 'SSHA') |
---|
[4073] | 282 | return passwordmanager.checkPassword(self.password, password) |
---|
| 283 | |
---|
[7177] | 284 | def getSiteRolesForPrincipal(self): |
---|
[6673] | 285 | prm = get_principal_role_manager() |
---|
[4129] | 286 | roles = [x[0] for x in prm.getRolesForPrincipal(self.name) |
---|
| 287 | if x[0].startswith('waeup.')] |
---|
| 288 | return roles |
---|
[5055] | 289 | |
---|
[7177] | 290 | def setSiteRolesForPrincipal(self, roles): |
---|
[6673] | 291 | prm = get_principal_role_manager() |
---|
[7177] | 292 | old_roles = self.getSiteRolesForPrincipal() |
---|
[7658] | 293 | if sorted(old_roles) == sorted(roles): |
---|
| 294 | return |
---|
[4129] | 295 | for role in old_roles: |
---|
| 296 | # Remove old roles, not to be set now... |
---|
| 297 | if role.startswith('waeup.') and role not in roles: |
---|
| 298 | prm.unsetRoleForPrincipal(role, self.name) |
---|
| 299 | for role in roles: |
---|
[7658] | 300 | # Convert role to ASCII string to be in line with the |
---|
| 301 | # event handler |
---|
| 302 | prm.assignRoleToPrincipal(str(role), self.name) |
---|
| 303 | return |
---|
[4129] | 304 | |
---|
[7177] | 305 | roles = property(getSiteRolesForPrincipal, setSiteRolesForPrincipal) |
---|
[4129] | 306 | |
---|
[6180] | 307 | def getLocalRoles(self): |
---|
| 308 | return self._local_roles |
---|
| 309 | |
---|
| 310 | def notifyLocalRoleChanged(self, obj, role_id, granted=True): |
---|
| 311 | objects = self._local_roles.get(role_id, []) |
---|
| 312 | if granted and obj not in objects: |
---|
| 313 | objects.append(obj) |
---|
| 314 | if not granted and obj in objects: |
---|
| 315 | objects.remove(obj) |
---|
| 316 | self._local_roles[role_id] = objects |
---|
| 317 | if len(objects) == 0: |
---|
| 318 | del self._local_roles[role_id] |
---|
[6182] | 319 | self._p_changed = True |
---|
[6180] | 320 | return |
---|
| 321 | |
---|
[14669] | 322 | |
---|
[4073] | 323 | class UserAuthenticatorPlugin(grok.GlobalUtility): |
---|
[6625] | 324 | grok.implements(IAuthenticatorPlugin) |
---|
[4073] | 325 | grok.provides(IAuthenticatorPlugin) |
---|
| 326 | grok.name('users') |
---|
| 327 | |
---|
| 328 | def authenticateCredentials(self, credentials): |
---|
| 329 | if not isinstance(credentials, dict): |
---|
| 330 | return None |
---|
| 331 | if not ('login' in credentials and 'password' in credentials): |
---|
| 332 | return None |
---|
| 333 | account = self.getAccount(credentials['login']) |
---|
| 334 | if account is None: |
---|
| 335 | return None |
---|
[10055] | 336 | # The following shows how 'time penalties' could be enforced |
---|
| 337 | # on failed logins. First three failed logins are 'for |
---|
| 338 | # free'. After that the user has to wait for 1, 2, 4, 8, 16, |
---|
| 339 | # 32, ... seconds before a login can succeed. |
---|
| 340 | # There are, however, some problems to discuss, before we |
---|
| 341 | # really use this in all authenticators. |
---|
| 342 | |
---|
| 343 | #num, last = account.failed_logins.as_tuple() |
---|
| 344 | #if (num > 2) and (time.time() < (last + 2**(num-3))): |
---|
| 345 | # # tried login while account still blocked due to previous |
---|
| 346 | # # login errors. |
---|
| 347 | # return None |
---|
[4073] | 348 | if not account.checkPassword(credentials['password']): |
---|
[10055] | 349 | #account.failed_logins.increase() |
---|
[4073] | 350 | return None |
---|
[7819] | 351 | return KofaPrincipalInfo( |
---|
[7315] | 352 | id=account.name, |
---|
| 353 | title=account.title, |
---|
| 354 | description=account.description, |
---|
| 355 | email=account.email, |
---|
| 356 | phone=account.phone, |
---|
[8757] | 357 | public_name=account.public_name, |
---|
[7315] | 358 | user_type=u'user') |
---|
[4073] | 359 | |
---|
| 360 | def principalInfo(self, id): |
---|
| 361 | account = self.getAccount(id) |
---|
| 362 | if account is None: |
---|
| 363 | return None |
---|
[7819] | 364 | return KofaPrincipalInfo( |
---|
[7315] | 365 | id=account.name, |
---|
| 366 | title=account.title, |
---|
| 367 | description=account.description, |
---|
| 368 | email=account.email, |
---|
| 369 | phone=account.phone, |
---|
[8757] | 370 | public_name=account.public_name, |
---|
[7315] | 371 | user_type=u'user') |
---|
[4073] | 372 | |
---|
| 373 | def getAccount(self, login): |
---|
[4087] | 374 | # ... look up the account object and return it ... |
---|
[7172] | 375 | userscontainer = self.getUsersContainer() |
---|
| 376 | if userscontainer is None: |
---|
[4087] | 377 | return |
---|
[7172] | 378 | return userscontainer.get(login, None) |
---|
[4087] | 379 | |
---|
| 380 | def addAccount(self, account): |
---|
[7172] | 381 | userscontainer = self.getUsersContainer() |
---|
| 382 | if userscontainer is None: |
---|
[4087] | 383 | return |
---|
| 384 | # XXX: complain if name already exists... |
---|
[7172] | 385 | userscontainer.addAccount(account) |
---|
[4087] | 386 | |
---|
[4091] | 387 | def addUser(self, name, password, title=None, description=None): |
---|
[7172] | 388 | userscontainer = self.getUsersContainer() |
---|
| 389 | if userscontainer is None: |
---|
[4091] | 390 | return |
---|
[7172] | 391 | userscontainer.addUser(name, password, title, description) |
---|
[5055] | 392 | |
---|
[7172] | 393 | def getUsersContainer(self): |
---|
[4087] | 394 | site = grok.getSite() |
---|
[4743] | 395 | return site['users'] |
---|
[6203] | 396 | |
---|
[14669] | 397 | |
---|
[7147] | 398 | class PasswordValidator(grok.GlobalUtility): |
---|
| 399 | |
---|
[14669] | 400 | grok.implements(IPasswordValidator) |
---|
[7147] | 401 | |
---|
[14669] | 402 | def validate_password(self, pw, pw_repeat): |
---|
| 403 | errors = [] |
---|
| 404 | if len(pw) < 3: |
---|
| 405 | errors.append('Password must have at least 3 chars.') |
---|
| 406 | if pw != pw_repeat: |
---|
| 407 | errors.append('Passwords do not match.') |
---|
| 408 | return errors |
---|
[7147] | 409 | |
---|
[14669] | 410 | |
---|
[7169] | 411 | class LocalRoleSetEvent(object): |
---|
| 412 | |
---|
| 413 | grok.implements(ILocalRoleSetEvent) |
---|
| 414 | |
---|
| 415 | def __init__(self, object, role_id, principal_id, granted=True): |
---|
| 416 | self.object = object |
---|
| 417 | self.role_id = role_id |
---|
| 418 | self.principal_id = principal_id |
---|
| 419 | self.granted = granted |
---|
| 420 | |
---|
[14669] | 421 | |
---|
[6203] | 422 | @grok.subscribe(IUserAccount, grok.IObjectRemovedEvent) |
---|
[6839] | 423 | def handle_account_removed(account, event): |
---|
[9313] | 424 | """When an account is removed, local and global roles might |
---|
| 425 | have to be deleted. |
---|
[6203] | 426 | """ |
---|
| 427 | local_roles = account.getLocalRoles() |
---|
| 428 | principal = account.name |
---|
[9313] | 429 | |
---|
[6203] | 430 | for role_id, object_list in local_roles.items(): |
---|
| 431 | for object in object_list: |
---|
| 432 | try: |
---|
| 433 | role_manager = IPrincipalRoleManager(object) |
---|
| 434 | except TypeError: |
---|
[9313] | 435 | # No Account object, no role manager, no local roles to remove |
---|
[6203] | 436 | continue |
---|
| 437 | role_manager.unsetRoleForPrincipal(role_id, principal) |
---|
[9313] | 438 | role_manager = IPrincipalRoleManager(grok.getSite()) |
---|
| 439 | roles = account.getSiteRolesForPrincipal() |
---|
| 440 | for role_id in roles: |
---|
| 441 | role_manager.unsetRoleForPrincipal(role_id, principal) |
---|
[6203] | 442 | return |
---|
[7169] | 443 | |
---|
[14669] | 444 | |
---|
[7169] | 445 | @grok.subscribe(IUserAccount, grok.IObjectAddedEvent) |
---|
| 446 | def handle_account_added(account, event): |
---|
| 447 | """When an account is added, the local owner role and the global |
---|
[7185] | 448 | AcademicsOfficer role must be set. |
---|
[7169] | 449 | """ |
---|
[7173] | 450 | # We set the local Owner role |
---|
| 451 | role_manager_account = IPrincipalRoleManager(account) |
---|
| 452 | role_manager_account.assignRoleToPrincipal( |
---|
[7169] | 453 | 'waeup.local.Owner', account.name) |
---|
[7185] | 454 | # We set the global AcademicsOfficer role |
---|
[7173] | 455 | site = grok.getSite() |
---|
| 456 | role_manager_site = IPrincipalRoleManager(site) |
---|
| 457 | role_manager_site.assignRoleToPrincipal( |
---|
[7185] | 458 | 'waeup.AcademicsOfficer', account.name) |
---|
[7173] | 459 | # Finally we have to notify the user account that the local role |
---|
[7169] | 460 | # of the same object has changed |
---|
| 461 | notify(LocalRoleSetEvent( |
---|
| 462 | account, 'waeup.local.Owner', account.name, granted=True)) |
---|
| 463 | return |
---|
| 464 | |
---|
[14669] | 465 | |
---|
[7169] | 466 | @grok.subscribe(Interface, ILocalRoleSetEvent) |
---|
| 467 | def handle_local_role_changed(obj, event): |
---|
| 468 | site = grok.getSite() |
---|
| 469 | if site is None: |
---|
| 470 | return |
---|
| 471 | users = site.get('users', None) |
---|
| 472 | if users is None: |
---|
| 473 | return |
---|
| 474 | if event.principal_id not in users.keys(): |
---|
| 475 | return |
---|
| 476 | user = users[event.principal_id] |
---|
| 477 | user.notifyLocalRoleChanged(event.object, event.role_id, event.granted) |
---|
| 478 | return |
---|
| 479 | |
---|
[14669] | 480 | |
---|
[7169] | 481 | @grok.subscribe(Interface, grok.IObjectRemovedEvent) |
---|
| 482 | def handle_local_roles_on_obj_removed(obj, event): |
---|
| 483 | try: |
---|
| 484 | role_map = IPrincipalRoleMap(obj) |
---|
| 485 | except TypeError: |
---|
| 486 | # no map, no roles to remove |
---|
| 487 | return |
---|
| 488 | for local_role, user_name, setting in role_map.getPrincipalsAndRoles(): |
---|
| 489 | notify(LocalRoleSetEvent( |
---|
[14669] | 490 | obj, local_role, user_name, granted=False)) |
---|
[7315] | 491 | return |
---|
[8756] | 492 | |
---|
[14669] | 493 | |
---|
[8973] | 494 | class UserAccountFactory(grok.GlobalUtility): |
---|
| 495 | """A factory for user accounts. |
---|
| 496 | |
---|
| 497 | This factory is only needed for imports. |
---|
| 498 | """ |
---|
| 499 | grok.implements(IFactory) |
---|
| 500 | grok.name(u'waeup.UserAccount') |
---|
| 501 | title = u"Create a user.", |
---|
| 502 | description = u"This factory instantiates new user account instances." |
---|
| 503 | |
---|
| 504 | def __call__(self, *args, **kw): |
---|
| 505 | return Account(name=None, password='') |
---|
| 506 | |
---|
| 507 | def getInterfaces(self): |
---|
| 508 | return implementedBy(Account) |
---|
| 509 | |
---|
[14669] | 510 | |
---|
[8973] | 511 | class UserProcessor(BatchProcessor): |
---|
[14669] | 512 | """The User Processor processes user accounts, i.e. `Account` objects in |
---|
| 513 | the ``users`` container. |
---|
[12869] | 514 | |
---|
| 515 | The `roles` columns must contain Python list |
---|
| 516 | expressions like ``['waeup.PortalManager', 'waeup.ImportManager']``. |
---|
| 517 | |
---|
| 518 | The processor does not import local roles. These can be imported |
---|
| 519 | by means of batch processors in the academic section. |
---|
[8973] | 520 | """ |
---|
| 521 | grok.implements(IBatchProcessor) |
---|
| 522 | grok.provides(IBatchProcessor) |
---|
| 523 | grok.context(Interface) |
---|
| 524 | util_name = 'userprocessor' |
---|
| 525 | grok.name(util_name) |
---|
| 526 | |
---|
| 527 | name = u'User Processor' |
---|
| 528 | iface = IUserAccount |
---|
| 529 | |
---|
[14669] | 530 | location_fields = ['name', ] |
---|
[8973] | 531 | factory_name = 'waeup.UserAccount' |
---|
| 532 | |
---|
| 533 | mode = None |
---|
| 534 | |
---|
| 535 | def parentsExist(self, row, site): |
---|
| 536 | return 'users' in site.keys() |
---|
| 537 | |
---|
| 538 | def entryExists(self, row, site): |
---|
| 539 | return row['name'] in site['users'].keys() |
---|
| 540 | |
---|
| 541 | def getParent(self, row, site): |
---|
| 542 | return site['users'] |
---|
| 543 | |
---|
| 544 | def getEntry(self, row, site): |
---|
| 545 | if not self.entryExists(row, site): |
---|
| 546 | return None |
---|
| 547 | parent = self.getParent(row, site) |
---|
| 548 | return parent.get(row['name']) |
---|
| 549 | |
---|
| 550 | def addEntry(self, obj, row, site): |
---|
| 551 | parent = self.getParent(row, site) |
---|
| 552 | parent.addAccount(obj) |
---|
| 553 | return |
---|
| 554 | |
---|
[13182] | 555 | def delEntry(self, row, site): |
---|
| 556 | user = self.getEntry(row, site) |
---|
| 557 | if user is not None: |
---|
| 558 | parent = self.getParent(row, site) |
---|
[14669] | 559 | grok.getSite().logger.info( |
---|
| 560 | '%s - %s - User removed' % (self.name, row['name'])) |
---|
[13182] | 561 | del parent[user.name] |
---|
| 562 | pass |
---|
| 563 | |
---|
[9706] | 564 | def updateEntry(self, obj, row, site, filename): |
---|
[9312] | 565 | """Update obj to the values given in row. |
---|
| 566 | """ |
---|
| 567 | changed = [] |
---|
| 568 | for key, value in row.items(): |
---|
[14669] | 569 | if key == 'roles': |
---|
[9312] | 570 | # We cannot simply set the roles attribute here because |
---|
| 571 | # we can't assure that the name attribute is set before |
---|
| 572 | # the roles attribute is set. |
---|
| 573 | continue |
---|
| 574 | # Skip fields to be ignored. |
---|
| 575 | if value == IGNORE_MARKER: |
---|
| 576 | continue |
---|
| 577 | if not hasattr(obj, key): |
---|
| 578 | continue |
---|
| 579 | setattr(obj, key, value) |
---|
| 580 | changed.append('%s=%s' % (key, value)) |
---|
| 581 | roles = row.get('roles', IGNORE_MARKER) |
---|
| 582 | if roles not in ('', IGNORE_MARKER): |
---|
| 583 | evalvalue = eval(roles) |
---|
| 584 | if isinstance(evalvalue, list): |
---|
| 585 | setattr(obj, 'roles', evalvalue) |
---|
| 586 | changed.append('roles=%s' % roles) |
---|
| 587 | # Log actions... |
---|
| 588 | items_changed = ', '.join(changed) |
---|
[14669] | 589 | grok.getSite().logger.info( |
---|
| 590 | '%s - %s - %s - updated: %s' % ( |
---|
| 591 | self.name, filename, row['name'], items_changed)) |
---|
[9312] | 592 | return |
---|
[8973] | 593 | |
---|
[12190] | 594 | def checkConversion(self, row, mode='ignore'): |
---|
| 595 | """Validates all values in row. |
---|
| 596 | """ |
---|
| 597 | errs, inv_errs, conv_dict = super( |
---|
| 598 | UserProcessor, self).checkConversion(row, mode=mode) |
---|
| 599 | # We need to check if roles exist. |
---|
[13181] | 600 | roles = row.get('roles', IGNORE_MARKER) |
---|
[12190] | 601 | all_roles = [i[0] for i in get_all_roles()] |
---|
| 602 | if roles not in ('', IGNORE_MARKER): |
---|
| 603 | evalvalue = eval(roles) |
---|
| 604 | for role in evalvalue: |
---|
| 605 | if role not in all_roles: |
---|
[14669] | 606 | errs.append(('roles', 'invalid role')) |
---|
[12190] | 607 | return errs, inv_errs, conv_dict |
---|
| 608 | |
---|
[14669] | 609 | |
---|
[8756] | 610 | class UsersPlugin(grok.GlobalUtility): |
---|
| 611 | """A plugin that updates users. |
---|
| 612 | """ |
---|
| 613 | grok.implements(IKofaPluggable) |
---|
| 614 | grok.name('users') |
---|
| 615 | |
---|
| 616 | deprecated_attributes = [] |
---|
| 617 | |
---|
| 618 | def setup(self, site, name, logger): |
---|
| 619 | return |
---|
| 620 | |
---|
| 621 | def update(self, site, name, logger): |
---|
| 622 | users = site['users'] |
---|
| 623 | items = getFields(IUserAccount).items() |
---|
| 624 | for user in users.values(): |
---|
| 625 | # Add new attributes |
---|
| 626 | for i in items: |
---|
[14669] | 627 | if not hasattr(user, i[0]): |
---|
| 628 | setattr(user, i[0], i[1].missing_value) |
---|
[8756] | 629 | logger.info( |
---|
| 630 | 'UsersPlugin: %s attribute %s added.' % ( |
---|
[14669] | 631 | user.name, i[0])) |
---|
[10055] | 632 | if not hasattr(user, 'failed_logins'): |
---|
| 633 | # add attribute `failed_logins`... |
---|
| 634 | user.failed_logins = FailedLoginInfo() |
---|
| 635 | logger.info( |
---|
| 636 | 'UsersPlugin: attribute failed_logins added.') |
---|
[8756] | 637 | # Remove deprecated attributes |
---|
| 638 | for i in self.deprecated_attributes: |
---|
| 639 | try: |
---|
[14669] | 640 | delattr(user, i) |
---|
[8756] | 641 | logger.info( |
---|
| 642 | 'UsersPlugin: %s attribute %s deleted.' % ( |
---|
[14669] | 643 | user.name, i)) |
---|
[8756] | 644 | except AttributeError: |
---|
| 645 | pass |
---|
[10055] | 646 | return |
---|
[14670] | 647 | |
---|
| 648 | |
---|
| 649 | class UpdatePAUPlugin(grok.GlobalUtility): |
---|
| 650 | """A plugin that updates a local PAU. |
---|
| 651 | |
---|
| 652 | We insert an 'xmlrpc-credentials' PAU-plugin into a sites PAU if it is not |
---|
| 653 | present already. There must be 'credentials' plugin registered already. |
---|
| 654 | |
---|
| 655 | XXX: This Plugin fixes a shortcoming of waeup.kofa 1.5. Sites created or |
---|
| 656 | updated afterwards do not need this plugin and it should be removed. |
---|
| 657 | """ |
---|
| 658 | grok.implements(IKofaPluggable) |
---|
| 659 | grok.name('site-pluggable-auth') |
---|
| 660 | |
---|
| 661 | def setup(self, site, name, logger): |
---|
| 662 | return |
---|
| 663 | |
---|
| 664 | def update(self, site, name, logger): |
---|
| 665 | pau = site.getSiteManager()['PluggableAuthentication'] |
---|
| 666 | if 'xmlrpc-credentials' in pau.credentialsPlugins: |
---|
| 667 | return |
---|
| 668 | plugins = list(pau.credentialsPlugins) |
---|
| 669 | plugins.insert(plugins.index('credentials'), 'xmlrpc-credentials') |
---|
| 670 | pau.credentialsPlugins = tuple(plugins) |
---|