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

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

Rebuild applicants package (1st part). Applicants now have an applicant_id and a password and can use the regular login page to enter the portal.

Add user_type attribute to SIRPPrincipal objects.

Add some permissions in students package.

Some tests are still missing and will be re-added soon.

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