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 | """ |
---|
19 | Authenticate applicants. |
---|
20 | """ |
---|
21 | import grok |
---|
22 | from zope.pluggableauth.interfaces import IAuthenticatorPlugin |
---|
23 | from waeup.sirp.interfaces import IAuthPluginUtility, IUserAccount |
---|
24 | from waeup.sirp.applicants.interfaces import IApplicant |
---|
25 | from waeup.sirp.students.authentication import ( |
---|
26 | StudentAccount, StudentsAuthenticatorPlugin) |
---|
27 | |
---|
28 | class 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 | |
---|
46 | class 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 | |
---|
82 | class 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 |
---|