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 | """ |
---|
19 | Authenticate applicants. |
---|
20 | """ |
---|
21 | import grok |
---|
22 | from zope.component import getUtility |
---|
23 | from zope.password.interfaces import IPasswordManager |
---|
24 | from zope.pluggableauth.interfaces import ( |
---|
25 | IAuthenticatorPlugin, ICredentialsPlugin) |
---|
26 | from zope.pluggableauth.plugins.session import ( |
---|
27 | SessionCredentialsPlugin, SessionCredentials) |
---|
28 | from zope.publisher.interfaces.http import IHTTPRequest |
---|
29 | from zope.session.interfaces import ISession |
---|
30 | from waeup.sirp.authentication import SIRPPrincipalInfo, get_principal_role_manager |
---|
31 | from waeup.sirp.interfaces import ( |
---|
32 | IAuthPluginUtility, IUserAccount, IPasswordValidator) |
---|
33 | from waeup.sirp.applicants.interfaces import IApplicant |
---|
34 | from waeup.sirp.students.authentication import ( |
---|
35 | StudentAccount, StudentsAuthenticatorPlugin) |
---|
36 | |
---|
37 | class 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 | |
---|
55 | class 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 | |
---|
91 | class 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 |
---|