Changeset 4129


Ignore:
Timestamp:
15 May 2009, 11:17:34 (16 years ago)
Author:
uli
Message:

Handle roles more robust.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • waeup/branches/ulif-rewrite/src/waeup/authentication.py

    r4125 r4129  
    1313from zope.component import getUtility
    1414from zope.interface import Interface
     15from zope.securitypolicy.interfaces import IPrincipalRoleManager
     16from zope.securitypolicy.principalrole import principalRoleManager
    1517from waeup.interfaces import IWAeUPObject, IUserAccount
    1618from waeup.viewlets import Index, MainArea, LeftSidebar
     
    7880class Account(grok.Model):
    7981    grok.implements(IUserAccount)
    80    
     82
     83
    8184    def __init__(self, name, password, title=None, description=None,
    8285                 roles = []):
     
    8992        self.description = description
    9093        self.setPassword(password)
    91         self.roles = roles
     94        self.setRoles(roles)
    9295
    9396    def setPassword(self, password):
     
    99102        return passwordmanager.checkPassword(self.password, password)
    100103
    101  
     104    def getRoles(self):
     105        prm = self._getPrincipalRoleManager()
     106        roles = [x[0] for x in prm.getRolesForPrincipal(self.name)
     107                 if x[0].startswith('waeup.')]
     108        return roles
     109   
     110    def setRoles(self, roles):
     111        prm = self._getPrincipalRoleManager()
     112       
     113        old_roles = self.getRoles()
     114        for role in old_roles:
     115            # Remove old roles, not to be set now...
     116            if role.startswith('waeup.') and role not in roles:
     117                prm.unsetRoleForPrincipal(role, self.name)
     118
     119        for role in roles:
     120            prm.assignRoleToPrincipal(role, self.name)
     121
     122    roles = property(getRoles, setRoles)
     123
     124    def _getPrincipalRoleManager(self):
     125        portal = grok.getSite()
     126        if portal is not None:
     127            return IPrincipalRoleManager(portal)
     128        return principalRoleManager
     129       
     130   
    102131class UserAuthenticatorPlugin(grok.GlobalUtility):
    103132    grok.provides(IAuthenticatorPlugin)
Note: See TracChangeset for help on using the changeset viewer.