1 | ## $Id: mandate.py 16550 2021-07-14 06:40:53Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2012 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 | These are the mandates. |
---|
20 | """ |
---|
21 | import grok |
---|
22 | import hashlib |
---|
23 | import os |
---|
24 | from urllib import urlencode |
---|
25 | from datetime import datetime, timedelta |
---|
26 | from grok import index |
---|
27 | from waeup.kofa.interfaces import IUserAccount |
---|
28 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
29 | from waeup.kofa.mandates.interfaces import IMandate |
---|
30 | |
---|
31 | class Mandate(grok.Model): |
---|
32 | """This is a mandate. |
---|
33 | """ |
---|
34 | grok.implements(IMandate) |
---|
35 | grok.provides(IMandate) |
---|
36 | grok.baseclass() |
---|
37 | |
---|
38 | REDIRECT_WITH_MANDATE_ID = False |
---|
39 | |
---|
40 | def __init__(self, days=1, mandate_id=None): |
---|
41 | super(Mandate, self).__init__() |
---|
42 | self.creation_date = datetime.utcnow() # offset-naive datetime |
---|
43 | delta = timedelta(days=days) |
---|
44 | self.expires = datetime.utcnow() + delta |
---|
45 | if mandate_id is None: |
---|
46 | mandate_id = os.urandom(20) |
---|
47 | mandate_id = hashlib.md5(mandate_id).hexdigest() |
---|
48 | self.mandate_id = mandate_id |
---|
49 | self.params = {} |
---|
50 | return |
---|
51 | |
---|
52 | def execute(self): |
---|
53 | return _('Nothing to do.') |
---|
54 | |
---|
55 | class PasswordMandate(Mandate): |
---|
56 | """This is a mandate which can set a password. |
---|
57 | """ |
---|
58 | |
---|
59 | def _setPassword(self): |
---|
60 | user = self.params.get('user', None) |
---|
61 | pwd = self.params.get('password', None) |
---|
62 | if not None in (user, pwd): |
---|
63 | try: |
---|
64 | IUserAccount(user).setPassword(pwd) |
---|
65 | return True |
---|
66 | except: |
---|
67 | return False |
---|
68 | return False |
---|
69 | |
---|
70 | def execute(self): |
---|
71 | msg = _('Wrong mandate parameters.') |
---|
72 | redirect_path = '' |
---|
73 | if self.expires < datetime.utcnow(): |
---|
74 | msg = _('Mandate expired.') |
---|
75 | elif self._setPassword(): |
---|
76 | msg = _('Password has been successfully set. ' |
---|
77 | 'Login with your new password.') |
---|
78 | username = IUserAccount(self.params['user']).name |
---|
79 | password = self.params['password'] |
---|
80 | grok.getSite().logger.info( |
---|
81 | 'PasswordMandate used: %s ' % username) |
---|
82 | args = {'login':username, 'password':password} |
---|
83 | redirect_path = '/login?%s' % urlencode(args) |
---|
84 | del self.__parent__[self.mandate_id] |
---|
85 | return msg, redirect_path |
---|
86 | |
---|
87 | class ParentsPasswordMandate(Mandate): |
---|
88 | |
---|
89 | def _setPassword(self): |
---|
90 | student = self.params.get('student', None) |
---|
91 | pwd = self.params.get('password', None) |
---|
92 | if not None in (student, pwd): |
---|
93 | try: |
---|
94 | student.setParentsPassword(pwd) |
---|
95 | return True |
---|
96 | except: |
---|
97 | return False |
---|
98 | return False |
---|
99 | |
---|
100 | def execute(self): |
---|
101 | msg = _('Wrong mandate parameters.') |
---|
102 | redirect_path = '' |
---|
103 | if self.expires < datetime.utcnow(): |
---|
104 | msg = _('Mandate expired.') |
---|
105 | elif self._setPassword(): |
---|
106 | msg = _('Parents password has been successfully set. ' |
---|
107 | 'Login with your new parents password.') |
---|
108 | grok.getSite().logger.info( |
---|
109 | 'ParentsPasswordMandate used: %s ' |
---|
110 | % self.params['student'].student_id) |
---|
111 | redirect_path = '/login' |
---|
112 | del self.__parent__[self.mandate_id] |
---|
113 | return msg, redirect_path |
---|
114 | |
---|
115 | class RefereeReportMandate(Mandate): |
---|
116 | """This is a mandate which can unlock a `RefereeReportAddFormPage`. |
---|
117 | The mandate is not automatically deleted. |
---|
118 | """ |
---|
119 | |
---|
120 | REDIRECT_WITH_MANDATE_ID = True |
---|
121 | |
---|
122 | def execute(self): |
---|
123 | if self.expires < datetime.utcnow(): |
---|
124 | return _('Mandate expired.'), '' |
---|
125 | redirect_path = self.params.get('redirect_path', None) |
---|
126 | name = self.params.get('name', None) |
---|
127 | email = self.params.get('email', None) |
---|
128 | if None in (redirect_path, name, email): |
---|
129 | return _('Wrong mandate parameters.'), '' |
---|
130 | return None, redirect_path |
---|