Ignore:
Timestamp:
4 Sep 2011, 23:35:58 (13 years ago)
Author:
uli
Message:

Add support for assigning roles to student accounts and let password setter create unicode passwords.

Location:
main/waeup.sirp/trunk/src/waeup/sirp/students
Files:
2 edited

Legend:

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

    r6674 r6680  
    2727from zope.password.interfaces import IPasswordManager
    2828from zope.pluggableauth.interfaces import IAuthenticatorPlugin
    29 from waeup.sirp.authentication import PrincipalInfo
     29from waeup.sirp.authentication import PrincipalInfo, get_principal_role_manager
    3030from waeup.sirp.interfaces import IAuthPluginUtility, IUserAccount
    3131from waeup.sirp.students.interfaces import IStudent
     
    5353        return self.title
    5454
    55     @property
    56     def roles(self):
    57         return getattr(self.context, 'roles', None)
     55    def _get_roles(self):
     56        prm = get_principal_role_manager()
     57        roles = [x[0] for x in prm.getRolesForPrincipal(self.name)
     58                 if x[0].startswith('waeup.')]
     59        return roles
     60
     61    def _set_roles(self, roles):
     62        """Set roles for principal denoted by this account.
     63        """
     64        prm = get_principal_role_manager()
     65        old_roles = self.roles
     66        for role in old_roles:
     67            # Remove old roles, not to be set now...
     68            if role.startswith('waeup.') and role not in roles:
     69                prm.unsetRoleForPrincipal(role, self.name)
     70        for role in roles:
     71            prm.assignRoleToPrincipal(role, self.name)
     72        return
     73
     74    roles = property(_get_roles, _set_roles)
    5875
    5976    def setPassword(self, password):
    6077        """Set a password (LDAP-compatible) SSHA encoded.
    6178
    62         We do not store passwords in plaintext.
     79        We do not store passwords in plaintext. Encrypted password is
     80        stored as unicode string.
    6381        """
    6482        passwordmanager = getUtility(IPasswordManager, 'SSHA')
    65         self.context.password = passwordmanager.encodePassword(password)
     83        self.context.password = u'%s' % (
     84            passwordmanager.encodePassword(password))
    6685
    6786    def checkPassword(self, password):
     
    7190            return False
    7291        passwordmanager = getUtility(IPasswordManager, 'SSHA')
    73         return passwordmanager.checkPassword(self.context.password, password)
     92        return passwordmanager.checkPassword(
     93            self.context.password.encode('utf-8'), # turn unicode into bytes
     94            password)
    7495
    7596class StudentsAuthenticatorPlugin(grok.GlobalUtility):
  • main/waeup.sirp/trunk/src/waeup/sirp/students/tests/test_authentication.py

    r6668 r6680  
    2121##
    2222import unittest
     23from zope.authentication.interfaces import IAuthentication
    2324from zope.component import provideUtility, queryUtility, getGlobalSiteManager
    2425from zope.interface.verify import verifyClass, verifyObject
     
    2627from zope.password.interfaces import IPasswordManager
    2728from zope.pluggableauth import PluggableAuthentication
     29from zope.securitypolicy.role import Role
     30from zope.securitypolicy.interfaces import IRole, Allow
     31from waeup.sirp.authentication import get_principal_role_manager
    2832from waeup.sirp.interfaces import IAuthPluginUtility, IUserAccount
    2933from waeup.sirp.students.authentication import (
     
    6266    password = None
    6367
     68
     69class MinimalPAU(PluggableAuthentication):
     70    def getPrincipal(self, id):
     71        return 'faked principal'
     72
    6473class StudentAccountTests(unittest.TestCase):
    6574
     
    6776        self.fake_stud = FakeStudent()
    6877        self.account = StudentAccount(self.fake_stud)
     78
     79        # We provide a minimal PAU
     80        pau = MinimalPAU()
     81        provideUtility(pau, IAuthentication)
     82
     83        # We register a role
     84        test_role = Role('waeup.test.Role', 'Testing Role')
     85        provideUtility(test_role, IRole, name='waeup.test.Role')
     86
    6987        # We have to setup a password manager utility manually as we
    7088        # have no functional test. In functional tests this would
     
    7694
    7795    def tearDown(self):
    78         # Clear up the SSHA utility
    79         ssha_manager = queryUtility(
    80             IPasswordManager, name='SSHA', default=None)
    81         if ssha_manager is not None:
    82             gsm = getGlobalSiteManager()
    83             gsm.unregisterUtility(ssha_manager)
     96        self.account.roles = [] # make sure roles are reset
     97        gsm = getGlobalSiteManager()
     98        to_clean = []
     99        # Clear up utilities registered in setUp
     100        to_clean.append(
     101            (IPasswordManager, queryUtility(
     102                    IPasswordManager, name='SSHA', default=None)))
     103        to_clean.append(
     104            (IAuthentication, queryUtility(IAuthentication, default=None)))
     105        to_clean.append(
     106            (IRole, queryUtility(IRole, name='test.Role', default=None)))
     107        for iface, elem in to_clean:
     108            if elem is not None:
     109                gsm.unregisterUtility(elem, iface)
    84110        return
    85111
     
    95121        # we do not store plaintext passwords
    96122        self.assertTrue(self.fake_stud.password != 'secret')
     123        # passwords are stored as unicode
     124        self.assertTrue(isinstance(self.fake_stud.password, unicode))
    97125        return
    98126
     
    107135        self.assertEqual(result3, True)
    108136        return
     137
     138    def test_role_set(self):
     139        # make sure we can set roles for principals denoted by account
     140        prm = get_principal_role_manager()
     141        self.assertEqual(prm.getPrincipalsAndRoles(), [])
     142        self.account.roles = ['waeup.test.Role']
     143        self.assertEqual(
     144            prm.getPrincipalsAndRoles(),
     145            [('waeup.test.Role', 'test_stud', Allow)])
     146        return
     147
     148    def test_role_get(self):
     149        # make sure we can get roles set for an account
     150        self.assertEqual(self.account.roles, [])
     151        self.account.roles = ['waeup.test.Role',] # set a role
     152        self.assertEqual(self.account.roles, ['waeup.test.Role'])
     153        return
Note: See TracChangeset for help on using the changeset viewer.