source: main/waeup.sirp/trunk/src/waeup/sirp/applicants/authentication.py @ 7781

Last change on this file since 7781 was 7364, checked in by Henrik Bettermann, 13 years ago

Define two different (convenience?) methods: fullname and display_fullname. The first one is a hyphen-separated string of all name parts and is meant for indexing only. The second one uses the SIRPUtils fullname method and is meant for displaying the fullname in the UI. It can be easily customized according to the requirements of the school.

  • Property svn:keywords set to Id
File size: 3.4 KB
Line 
1## $Id: authentication.py 7364 2011-12-17 12:54:39Z henrik $
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##
18"""
19Authenticate applicants.
20"""
21import grok
22from zope.pluggableauth.interfaces import IAuthenticatorPlugin
23from waeup.sirp.interfaces import IAuthPluginUtility, IUserAccount
24from waeup.sirp.applicants.interfaces import IApplicant
25from waeup.sirp.students.authentication import (
26    StudentAccount, StudentsAuthenticatorPlugin)
27
28class ApplicantAccount(StudentAccount):
29    """An adapter to turn applicant objects into accounts on-the-fly.
30    """
31    grok.context(IApplicant)
32    grok.implements(IUserAccount)
33
34    @property
35    def name(self):
36        return self.context.applicant_id
37
38    @property
39    def title(self):
40        return self.context.display_fullname
41
42    @property
43    def user_type(self):
44        return u'applicant'
45
46class ApplicantsAuthenticatorPlugin(StudentsAuthenticatorPlugin):
47    grok.implements(IAuthenticatorPlugin)
48    grok.provides(IAuthenticatorPlugin)
49    grok.name('applicants')
50
51    def getAccount(self, login):
52        """Look up a applicant identified by `login`. Returns an account.
53
54        First we split the login name into the container part and
55        the application number part. Then we simply look up the key under which
56        the applicant is stored in the respective applicants cointainer of
57        the portal.
58
59        Returns not an applicant but an account object adapted from any
60        applicant found.
61
62        If no such applicant exists, ``None`` is returned.
63        """
64        site = grok.getSite()
65        if site is None:
66            return None
67        applicantsroot = site.get('applicants', None)
68        if applicantsroot is None:
69            return None
70        try:
71            container, application_number = login.split('_')
72        except ValueError:
73            return None
74        applicantscontainer = applicantsroot.get(container,None)
75        if applicantscontainer is None:
76            return None
77        applicant = applicantscontainer.get(application_number, None)
78        if applicant is None:
79            return None
80        return IUserAccount(applicant)
81
82class ApplicantsAuthenticatorSetup(grok.GlobalUtility):
83    """Register or unregister applicant authentication for a PAU.
84
85    This piece is called when a new site is created.
86    """
87    grok.implements(IAuthPluginUtility)
88    grok.name('applicants_auth_setup')
89
90    def register(self, pau):
91        plugins = list(pau.authenticatorPlugins)
92        plugins.append('applicants')
93        pau.authenticatorPlugins = tuple(plugins)
94        return pau
95
96    def unregister(self, pau):
97        plugins = [x for x in pau.authenticatorPlugins
98                   if x != 'applicants']
99        pau.authenticatorPlugins = tuple(plugins)
100        return pau
Note: See TracBrowser for help on using the repository browser.