[7192] | 1 | ## $Id: authentication.py 7811 2012-03-08 19:00:51Z uli $ |
---|
[7240] | 2 | ## |
---|
[7192] | 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
[5431] | 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. |
---|
[7240] | 8 | ## |
---|
[5431] | 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. |
---|
[7240] | 13 | ## |
---|
[5431] | 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 | """ |
---|
[7240] | 19 | Authenticate applicants. |
---|
| 20 | """ |
---|
[5431] | 21 | import grok |
---|
[7255] | 22 | from zope.pluggableauth.interfaces import IAuthenticatorPlugin |
---|
[7811] | 23 | from waeup.kofa.interfaces import IAuthPluginUtility, IUserAccount |
---|
| 24 | from waeup.kofa.applicants.interfaces import IApplicant |
---|
| 25 | from waeup.kofa.students.authentication import ( |
---|
[7240] | 26 | StudentAccount, StudentsAuthenticatorPlugin) |
---|
[5431] | 27 | |
---|
[7240] | 28 | class ApplicantAccount(StudentAccount): |
---|
| 29 | """An adapter to turn applicant objects into accounts on-the-fly. |
---|
[5441] | 30 | """ |
---|
[7240] | 31 | grok.context(IApplicant) |
---|
| 32 | grok.implements(IUserAccount) |
---|
[5431] | 33 | |
---|
[7240] | 34 | @property |
---|
| 35 | def name(self): |
---|
| 36 | return self.context.applicant_id |
---|
[5431] | 37 | |
---|
[7240] | 38 | @property |
---|
| 39 | def title(self): |
---|
[7364] | 40 | return self.context.display_fullname |
---|
[5431] | 41 | |
---|
[7240] | 42 | @property |
---|
| 43 | def user_type(self): |
---|
| 44 | return u'applicant' |
---|
[5441] | 45 | |
---|
[7240] | 46 | class ApplicantsAuthenticatorPlugin(StudentsAuthenticatorPlugin): |
---|
| 47 | grok.implements(IAuthenticatorPlugin) |
---|
[5431] | 48 | grok.provides(IAuthenticatorPlugin) |
---|
| 49 | grok.name('applicants') |
---|
| 50 | |
---|
[7240] | 51 | def getAccount(self, login): |
---|
| 52 | """Look up a applicant identified by `login`. Returns an account. |
---|
[5909] | 53 | |
---|
[7240] | 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. |
---|
[5909] | 58 | |
---|
[7240] | 59 | Returns not an applicant but an account object adapted from any |
---|
| 60 | applicant found. |
---|
[5909] | 61 | |
---|
[7240] | 62 | If no such applicant exists, ``None`` is returned. |
---|
[5909] | 63 | """ |
---|
[7240] | 64 | site = grok.getSite() |
---|
| 65 | if site is None: |
---|
[5431] | 66 | return None |
---|
[7240] | 67 | applicantsroot = site.get('applicants', None) |
---|
| 68 | if applicantsroot is None: |
---|
[5431] | 69 | return None |
---|
[7240] | 70 | try: |
---|
| 71 | container, application_number = login.split('_') |
---|
| 72 | except ValueError: |
---|
[5446] | 73 | return None |
---|
[7240] | 74 | applicantscontainer = applicantsroot.get(container,None) |
---|
| 75 | if applicantscontainer is None: |
---|
[6409] | 76 | return None |
---|
[7240] | 77 | applicant = applicantscontainer.get(application_number, None) |
---|
| 78 | if applicant is None: |
---|
[6409] | 79 | return None |
---|
[7240] | 80 | return IUserAccount(applicant) |
---|
[5431] | 81 | |
---|
[7235] | 82 | class ApplicantsAuthenticatorSetup(grok.GlobalUtility): |
---|
[7240] | 83 | """Register or unregister applicant authentication for a PAU. |
---|
[5904] | 84 | |
---|
[7240] | 85 | This piece is called when a new site is created. |
---|
[5902] | 86 | """ |
---|
[7063] | 87 | grok.implements(IAuthPluginUtility) |
---|
[5902] | 88 | grok.name('applicants_auth_setup') |
---|
| 89 | |
---|
| 90 | def register(self, pau): |
---|
[7240] | 91 | plugins = list(pau.authenticatorPlugins) |
---|
| 92 | plugins.append('applicants') |
---|
[6661] | 93 | pau.authenticatorPlugins = tuple(plugins) |
---|
[5903] | 94 | return pau |
---|
| 95 | |
---|
[5902] | 96 | def unregister(self, pau): |
---|
[7240] | 97 | plugins = [x for x in pau.authenticatorPlugins |
---|
| 98 | if x != 'applicants'] |
---|
| 99 | pau.authenticatorPlugins = tuple(plugins) |
---|
[5903] | 100 | return pau |
---|