[7192] | 1 | ## $Id: authentication.py 7240 2011-11-30 23:13:26Z henrik $ |
---|
[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 |
---|
[7240] | 22 | from zope.component import getUtility |
---|
| 23 | from zope.password.interfaces import IPasswordManager |
---|
[5431] | 24 | from zope.pluggableauth.interfaces import ( |
---|
[7240] | 25 | IAuthenticatorPlugin, ICredentialsPlugin) |
---|
| 26 | from zope.pluggableauth.plugins.session import ( |
---|
| 27 | SessionCredentialsPlugin, SessionCredentials) |
---|
[5431] | 28 | from zope.publisher.interfaces.http import IHTTPRequest |
---|
| 29 | from zope.session.interfaces import ISession |
---|
[7240] | 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) |
---|
[5431] | 36 | |
---|
[7240] | 37 | class ApplicantAccount(StudentAccount): |
---|
| 38 | """An adapter to turn applicant objects into accounts on-the-fly. |
---|
[5441] | 39 | """ |
---|
[7240] | 40 | grok.context(IApplicant) |
---|
| 41 | grok.implements(IUserAccount) |
---|
[5431] | 42 | |
---|
[7240] | 43 | @property |
---|
| 44 | def name(self): |
---|
| 45 | return self.context.applicant_id |
---|
[5431] | 46 | |
---|
[7240] | 47 | @property |
---|
| 48 | def title(self): |
---|
| 49 | return self.context.fullname |
---|
[5431] | 50 | |
---|
[7240] | 51 | @property |
---|
| 52 | def user_type(self): |
---|
| 53 | return u'applicant' |
---|
[5441] | 54 | |
---|
[7240] | 55 | class ApplicantsAuthenticatorPlugin(StudentsAuthenticatorPlugin): |
---|
| 56 | grok.implements(IAuthenticatorPlugin) |
---|
[5431] | 57 | grok.provides(IAuthenticatorPlugin) |
---|
| 58 | grok.name('applicants') |
---|
| 59 | |
---|
[7240] | 60 | def getAccount(self, login): |
---|
| 61 | """Look up a applicant identified by `login`. Returns an account. |
---|
[5909] | 62 | |
---|
[7240] | 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. |
---|
[5909] | 67 | |
---|
[7240] | 68 | Returns not an applicant but an account object adapted from any |
---|
| 69 | applicant found. |
---|
[5909] | 70 | |
---|
[7240] | 71 | If no such applicant exists, ``None`` is returned. |
---|
[5909] | 72 | """ |
---|
[7240] | 73 | site = grok.getSite() |
---|
| 74 | if site is None: |
---|
[5431] | 75 | return None |
---|
[7240] | 76 | applicantsroot = site.get('applicants', None) |
---|
| 77 | if applicantsroot is None: |
---|
[5431] | 78 | return None |
---|
[7240] | 79 | try: |
---|
| 80 | container, application_number = login.split('_') |
---|
| 81 | except ValueError: |
---|
[5446] | 82 | return None |
---|
[7240] | 83 | applicantscontainer = applicantsroot.get(container,None) |
---|
| 84 | if applicantscontainer is None: |
---|
[6409] | 85 | return None |
---|
[7240] | 86 | applicant = applicantscontainer.get(application_number, None) |
---|
| 87 | if applicant is None: |
---|
[6409] | 88 | return None |
---|
[7240] | 89 | return IUserAccount(applicant) |
---|
[5431] | 90 | |
---|
[7235] | 91 | class ApplicantsAuthenticatorSetup(grok.GlobalUtility): |
---|
[7240] | 92 | """Register or unregister applicant authentication for a PAU. |
---|
[5904] | 93 | |
---|
[7240] | 94 | This piece is called when a new site is created. |
---|
[5902] | 95 | """ |
---|
[7063] | 96 | grok.implements(IAuthPluginUtility) |
---|
[5902] | 97 | grok.name('applicants_auth_setup') |
---|
| 98 | |
---|
| 99 | def register(self, pau): |
---|
[7240] | 100 | plugins = list(pau.authenticatorPlugins) |
---|
| 101 | plugins.append('applicants') |
---|
[6661] | 102 | pau.authenticatorPlugins = tuple(plugins) |
---|
[5903] | 103 | return pau |
---|
| 104 | |
---|
[5902] | 105 | def unregister(self, pau): |
---|
[7240] | 106 | plugins = [x for x in pau.authenticatorPlugins |
---|
| 107 | if x != 'applicants'] |
---|
| 108 | pau.authenticatorPlugins = tuple(plugins) |
---|
[5903] | 109 | return pau |
---|