Changeset 7233 for main/waeup.sirp/trunk


Ignore:
Timestamp:
28 Nov 2011, 21:04:57 (13 years ago)
Author:
Henrik Bettermann
Message:

Implement SIRPPrincipalInfo and SIRPPrincipal classes which provide ordinary principals with an extra email and phone attribute.

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

Legend:

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

    r7221 r7233  
    2424from zope.securitypolicy.interfaces import (
    2525    IPrincipalRoleMap, IPrincipalRoleManager)
     26from zope.pluggableauth.factories import Principal
    2627from zope.pluggableauth.plugins.session import SessionCredentialsPlugin
    2728from zope.pluggableauth.interfaces import (
    28         ICredentialsPlugin, IAuthenticatorPlugin, IPrincipalInfo)
     29        ICredentialsPlugin, IAuthenticatorPlugin,
     30        IAuthenticatedPrincipalFactory, AuthenticatedPrincipalCreated)
     31from zope.publisher.interfaces import IRequest
    2932from zope.password.interfaces import IPasswordManager
    3033from zope.securitypolicy.principalrole import principalRoleManager
    3134from waeup.sirp.interfaces import (ILocalRoleSetEvent,
    32     IUserAccount, IAuthPluginUtility, IPasswordValidator)
     35    IUserAccount, IAuthPluginUtility, IPasswordValidator,
     36    ISIRPPrincipal, ISIRPPrincipalInfo)
    3337
    3438def setup_authentication(pau):
     
    6872    passwordfield = 'form.password'
    6973
    70 class PrincipalInfo(object):
    71     grok.implements(IPrincipalInfo)
    72 
    73     def __init__(self, id, title, description):
     74class SIRPPrincipalInfo(object):
     75    """An implementation of ISIRPPrincipalInfo.
     76
     77    A SIRP principal info is created with id, login, title, description,
     78    phone and email.
     79    """
     80    grok.implements(ISIRPPrincipalInfo)
     81
     82    def __init__(self, id, title, description, email, phone):
    7483        self.id = id
    7584        self.title = title
    7685        self.description = description
     86        self.email = email
     87        self.phone = phone
    7788        self.credentialsPlugin = None
    7889        self.authenticatorPlugin = None
    7990
     91class SIRPPrincipal(Principal):
     92    """A portal principal.
     93
     94    SIRP principals provide an extra `email` and `phone`
     95    attribute extending ordinary principals.
     96    """
     97
     98    grok.implements(ISIRPPrincipal)
     99
     100    def __init__(self, id, title=u'', description=u'', email=u'',
     101                 phone=None, prefix=None):
     102        self.id = id
     103        self.title = title
     104        self.description = description
     105        self.groups = []
     106        self.email = email
     107        self.phone = phone
     108
     109    def __repr__(self):
     110        return 'SIRPPrincipal(%r)' % self.id
     111
     112class AuthenticatedSIRPPrincipalFactory(grok.MultiAdapter):
     113    """Creates 'authenticated' SIRP principals.
     114
     115    Adapts (principal info, request) to a SIRPPrincipal instance.
     116
     117    This adapter is used by the standard PAU to transform
     118    PrincipalInfos into Principal instances.
     119    """
     120    grok.adapts(ISIRPPrincipalInfo, IRequest)
     121    grok.implements(IAuthenticatedPrincipalFactory)
     122
     123    def __init__(self, info, request):
     124        self.info = info
     125        self.request = request
     126
     127    def __call__(self, authentication):
     128        principal = SIRPPrincipal(
     129            self.info.id,
     130            self.info.title,
     131            self.info.description,
     132            self.info.email,
     133            self.info.phone,
     134            )
     135        notify(
     136            AuthenticatedPrincipalCreated(
     137                authentication, principal, self.info, self.request))
     138        return principal
     139
    80140class Account(grok.Model):
    81141    grok.implements(IUserAccount)
     
    84144
    85145    def __init__(self, name, password, title=None, description=None,
    86                  email=None, roles = []):
     146                 email=None, phone=None, roles = []):
    87147        self.name = name
    88148        if title is None:
     
    93153        self.description = description
    94154        self.email = email
     155        self.phone = phone
    95156        self.setPassword(password)
    96157        #self.setSiteRolesForPrincipal(roles)
     
    155216        if not account.checkPassword(credentials['password']):
    156217            return None
    157         return PrincipalInfo(id=account.name,
     218        return SIRPPrincipalInfo(id=account.name,
    158219                             title=account.title,
    159                              description=account.description)
     220                             description=account.description,
     221                             email=account.email,
     222                             phone=account.phone)
    160223
    161224    def principalInfo(self, id):
     
    163226        if account is None:
    164227            return None
    165         return PrincipalInfo(id=account.name,
     228        return SIRPPrincipalInfo(id=account.name,
    166229                             title=account.title,
    167                              description=account.description)
     230                             description=account.description,
     231                             email=account.email,
     232                             phone=account.phone)
    168233
    169234    def getAccount(self, login):
  • main/waeup.sirp/trunk/src/waeup/sirp/browser/browser.txt

    r7229 r7233  
    128128  >>> browser.getControl(name="control_password").value = 'secret'
    129129  >>> browser.getControl(name="form.email").value = 'xx@yy.zz'
     130  >>> browser.getControl(name="form.phone").value = '1234'
    130131  >>> browser.getControl("Add user").click()
    131132  >>> print browser.contents
     
    162163  >>> browser.getControl(name="control_password").value = 'secret'
    163164  >>> browser.getControl(name="form.email").value = 'xx@yy.zz'
     165  >>> browser.getControl(name="form.phone").value = '1234'
    164166  >>> browser.getControl("Add user").click()
    165167  >>> 'The userid chosen already exists' in browser.contents
  • main/waeup.sirp/trunk/src/waeup/sirp/browser/pages.py

    r7232 r7233  
    382382        title = data['title']
    383383        email = data['email']
     384        phone = data['phone']
    384385        description = data['description']
    385386        #password = data['password']
     
    396397        try:
    397398            self.context.addUser(name, password, title=title, email=email,
    398                                  description=description, roles=roles)
     399                                 phone=phone, description=description,
     400                                 roles=roles)
    399401        except KeyError:
    400402            self.status = self.flash('The userid chosen already exists '
  • main/waeup.sirp/trunk/src/waeup/sirp/interfaces.py

    r7225 r7233  
    2323from zc.sourcefactory.basic import BasicSourceFactory
    2424from zope import schema
     25from zope.pluggableauth.interfaces import IPrincipalInfo
     26from zope.security.interfaces import IGroupClosureAwarePrincipal as IPrincipal
    2527from zope.component import getUtility
    2628from zope.component.interfaces import IObjectEvent
     
    232234        required = True,)
    233235
     236class ISIRPPrincipalInfo(IPrincipalInfo):
     237    """Infos about principals that are users of WAeUP SIRP.
     238    """
     239    email = Attribute("The email address of a user")
     240    phone = Attribute("The phone number of a user")
     241
     242
     243class ISIRPPrincipal(IPrincipal):
     244    """A principle for WAeUP SIRP.
     245
     246    This interface extends zope.security.interfaces.IPrincipal and
     247    requires also an `id` and other attributes defined there.
     248    """
     249
     250    email = schema.TextLine(
     251        title = u'Email',
     252        description = u'',
     253        required=False,)
     254
     255    phone = schema.Int(
     256        title = u'Phone',
     257        description = u'',
     258        required=False,)
    234259
    235260class IUserAccount(IWAeUPObject):
     
    255280        required = True,
    256281        constraint=validate_email,
     282        )
     283
     284    phone = schema.Int(
     285        title = u'Phone',
     286        default = None,
     287        required = True,
    257288        )
    258289
  • main/waeup.sirp/trunk/src/waeup/sirp/students/authentication.py

    r7221 r7233  
    2828from zope.publisher.interfaces.http import IHTTPRequest
    2929from zope.session.interfaces import ISession
    30 from waeup.sirp.authentication import PrincipalInfo, get_principal_role_manager
     30from waeup.sirp.authentication import SIRPPrincipalInfo, get_principal_role_manager
    3131from waeup.sirp.interfaces import (
    3232    IAuthPluginUtility, IUserAccount, IPasswordValidator)
     
    5454    def email(self):
    5555        return self.context.email
     56
     57    @property
     58    def phone(self):
     59        return self.context.phone
    5660
    5761    @property
     
    127131        if not account.checkPassword(credentials['password']):
    128132            return None
    129         return PrincipalInfo(id=account.name,
     133        return SIRPPrincipalInfo(id=account.name,
    130134                             title=account.title,
    131                              description=account.description)
     135                             description=account.description,
     136                             email=account.email,
     137                             phone=account.phone)
    132138
    133139    def principalInfo(self, id):
    134140        """Get a principal identified by `id`.
    135141
    136         This one is required by IAuthenticatorPlugin.
    137         """
    138         account = self.getAccount(id)
    139         if account is None:
    140             return None
    141         return PrincipalInfo(id=account.name,
    142                              title=account.title,
    143                              description=account.description)
     142        This one is required by IAuthenticatorPlugin but not needed here
     143        (see respective docstring in applicants package).
     144        """
     145        return None
    144146
    145147    def getAccount(self, login):
  • main/waeup.sirp/trunk/src/waeup/sirp/students/tests/test_authentication.py

    r7221 r7233  
    6464    password = None
    6565    email = None
     66    phone = None
    6667
    6768
  • main/waeup.sirp/trunk/src/waeup/sirp/tests/test_authentication.py

    r7194 r7233  
    2424from waeup.sirp.testing import FunctionalTestCase, FunctionalLayer
    2525from waeup.sirp.authentication import (
    26     UserAuthenticatorPlugin, Account, PrincipalInfo, get_principal_role_manager)
     26    UserAuthenticatorPlugin, Account, SIRPPrincipalInfo, get_principal_role_manager)
    2727
    2828class FakeSite(grok.Site, grok.Container):
     
    6363        result2 = plugin.authenticateCredentials(
    6464            dict(login='bob', password='nonsense'))
    65         self.assertTrue(isinstance(result1, PrincipalInfo))
     65        self.assertTrue(isinstance(result1, SIRPPrincipalInfo))
    6666        self.assertTrue(result2 is None)
    6767        return
     
    7272        result1 = plugin.principalInfo('bob')
    7373        result2 = plugin.principalInfo('manfred')
    74         self.assertTrue(isinstance(result1, PrincipalInfo))
     74        self.assertTrue(isinstance(result1, SIRPPrincipalInfo))
    7575        self.assertTrue(result2 is None)
    7676        return
  • main/waeup.sirp/trunk/src/waeup/sirp/userscontainer.py

    r7221 r7233  
    3232
    3333    def addUser(self, name, password, title=None,
    34                 description=None, email=None, roles=[]):
     34                description=None, email=None, phone=None, roles=[]):
    3535        """Add a new Account instance, created from parameters.
    3636        """
     
    3939        #if description is None:
    4040        #    description = title
    41         self[name] = Account(name, password, title, description, email, roles)
     41        self[name] = Account(name, password, title, description,
     42                             email, phone, roles)
    4243
    4344    def addAccount(self, account):
Note: See TracChangeset for help on using the changeset viewer.