Changeset 7169 for main/waeup.sirp/trunk


Ignore:
Timestamp:
22 Nov 2011, 14:35:12 (13 years ago)
Author:
Henrik Bettermann
Message:

Put all user component related stuff into the authentication module to avoid errors due to circular imports. The users module now only contains the usercontainer components.

Location:
main/waeup.sirp/trunk/src/waeup/sirp
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/authentication.py

    r7147 r7169  
    22"""
    33import grok
     4from zope.event import notify
    45from zope.component import getUtility, getUtilitiesFor
     6from zope.interface import Interface
     7from zope.securitypolicy.interfaces import (
     8    IPrincipalRoleMap, IPrincipalRoleManager)
    59from zope.pluggableauth.plugins.session import SessionCredentialsPlugin
    610from zope.pluggableauth.interfaces import (
    711        ICredentialsPlugin, IAuthenticatorPlugin, IPrincipalInfo)
    812from zope.password.interfaces import IPasswordManager
    9 from zope.securitypolicy.interfaces import IPrincipalRoleManager
    1013from zope.securitypolicy.principalrole import principalRoleManager
    11 from waeup.sirp.interfaces import (
     14from waeup.sirp.interfaces import (ILocalRoleSetEvent,
    1215    IUserAccount, IAuthPluginUtility, IPasswordValidator)
    1316
     
    184187       return errors
    185188
     189class LocalRoleSetEvent(object):
     190
     191    grok.implements(ILocalRoleSetEvent)
     192
     193    def __init__(self, object, role_id, principal_id, granted=True):
     194        self.object = object
     195        self.role_id = role_id
     196        self.principal_id = principal_id
     197        self.granted = granted
     198
    186199@grok.subscribe(IUserAccount, grok.IObjectRemovedEvent)
    187200def handle_account_removed(account, event):
     
    199212            role_manager.unsetRoleForPrincipal(role_id, principal)
    200213    return
     214
     215@grok.subscribe(IUserAccount, grok.IObjectAddedEvent)
     216def handle_account_added(account, event):
     217    """When an account is added, the local owner role and the global
     218    PortalUser rolemust be set.
     219    """
     220    # First we have to set the local owner role of the account object
     221    role_manager = IPrincipalRoleManager(account)
     222    role_manager.assignRoleToPrincipal(
     223        'waeup.local.Owner', account.name)
     224    # Then we have to notify the user account that the local role
     225    # of the same object has changed
     226    notify(LocalRoleSetEvent(
     227        account, 'waeup.local.Owner', account.name, granted=True))
     228    return
     229
     230@grok.subscribe(Interface, ILocalRoleSetEvent)
     231def handle_local_role_changed(obj, event):
     232    site = grok.getSite()
     233    if site is None:
     234        return
     235    users = site.get('users', None)
     236    if users is None:
     237        return
     238    role_id = event.role_id
     239    if event.principal_id not in users.keys():
     240        return
     241    user = users[event.principal_id]
     242    user.notifyLocalRoleChanged(event.object, event.role_id, event.granted)
     243    return
     244
     245@grok.subscribe(Interface, grok.IObjectRemovedEvent)
     246def handle_local_roles_on_obj_removed(obj, event):
     247    try:
     248        role_map = IPrincipalRoleMap(obj)
     249    except TypeError:
     250        # no map, no roles to remove
     251        return
     252    for local_role, user_name, setting in role_map.getPrincipalsAndRoles():
     253        notify(LocalRoleSetEvent(
     254                obj, local_role, user_name, granted=False))
     255    return
  • main/waeup.sirp/trunk/src/waeup/sirp/authentication.txt

    r7163 r7169  
    7171because we do not neccessarily know where Alice lives:
    7272
    73   >>> from waeup.sirp.users import LocalRoleSetEvent
     73  >>> from waeup.sirp.authentication import LocalRoleSetEvent
    7474  >>> from zope.event import notify
    7575  >>> notify(LocalRoleSetEvent(chalet, 'BigBoss', 'alice',
  • main/waeup.sirp/trunk/src/waeup/sirp/browser/pages.py

    r7165 r7169  
    4040from waeup.sirp.university.catalog import search
    4141from waeup.sirp.university.vocabularies import course_levels
    42 from waeup.sirp.users import LocalRoleSetEvent
     42from waeup.sirp.authentication import LocalRoleSetEvent
    4343from waeup.sirp.widgets.restwidget import ReSTDisplayWidget
    4444from waeup.sirp.authentication import get_principal_role_manager
  • main/waeup.sirp/trunk/src/waeup/sirp/users.py

    r7163 r7169  
    1 """Users (principals) for the WAeUP portal.
     1"""User container for the WAeUP portal.
    22"""
    33import grok
    44from zope.event import notify
    5 from zope.interface import Interface
    6 from zope.securitypolicy.interfaces import (
    7     IPrincipalRoleMap, IPrincipalRoleManager)
    85from waeup.sirp.authentication import Account
    9 from waeup.sirp.interfaces import (
    10     IUserContainer, ILocalRoleSetEvent, IUserAccount)
     6from waeup.sirp.interfaces import IUserContainer
    117
    128class UserContainer(grok.Container):
     
    3935        if name in self.keys():
    4036            del self[name]
    41 
    42 class LocalRoleSetEvent(object):
    43 
    44     grok.implements(ILocalRoleSetEvent)
    45 
    46     def __init__(self, object, role_id, principal_id, granted=True):
    47         self.object = object
    48         self.role_id = role_id
    49         self.principal_id = principal_id
    50         self.granted = granted
    51 
    52 @grok.subscribe(Interface, ILocalRoleSetEvent)
    53 def handle_local_role_changed(obj, event):
    54     site = grok.getSite()
    55     if site is None:
    56         return
    57     users = site.get('users', None)
    58     if users is None:
    59         return
    60     role_id = event.role_id
    61     if event.principal_id not in users.keys():
    62         return
    63     user = users[event.principal_id]
    64     user.notifyLocalRoleChanged(event.object, event.role_id, event.granted)
    65     return
    66 
    67 @grok.subscribe(Interface, grok.IObjectRemovedEvent)
    68 def handle_local_roles_on_obj_removed(obj, event):
    69     try:
    70         role_map = IPrincipalRoleMap(obj)
    71     except TypeError:
    72         # no map, no roles to remove
    73         return
    74     for local_role, user_name, setting in role_map.getPrincipalsAndRoles():
    75         notify(LocalRoleSetEvent(
    76                 obj, local_role, user_name, granted=False))
    77     return
    78 
    79 @grok.subscribe(IUserAccount, grok.IObjectAddedEvent)
    80 def handle_user_added(account, event):
    81     """If an account is added the local owner role must be set.
    82     """
    83     # First we have to set the local owner role of the account object
    84     role_manager = IPrincipalRoleManager(account)
    85     role_manager.assignRoleToPrincipal(
    86         'waeup.local.Owner', account.name)
    87     # Then we have to notify the user account that the local role
    88     # of the same object has changed
    89     notify(LocalRoleSetEvent(
    90         account, 'waeup.local.Owner', account.name, granted=True))
    91     return
  • main/waeup.sirp/trunk/src/waeup/sirp/users.txt

    r5140 r7169  
    1 User for the WAeUP portal
    2 *************************
     1User container for the WAeUP portal
     2***********************************
    33
    44.. :doctest:
     
    1010    ...   PlacelessSetup)
    1111    >>> PlacelessSetup().setUp()
    12 
    13 User Containers
    14 ===============
    1512
    1613We can create a user container which will hold the useraccounts for
Note: See TracChangeset for help on using the changeset viewer.