[8846] | 1 | ## $Id: mandate.py 14221 2016-10-20 21:13:17Z 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 |
---|
[8847] | 22 | import hashlib |
---|
[8849] | 23 | import os |
---|
[8846] | 24 | from datetime import datetime, timedelta |
---|
| 25 | from grok import index |
---|
[11949] | 26 | from waeup.ikoba.interfaces import IUserAccount |
---|
| 27 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
| 28 | from waeup.ikoba.mandates.interfaces import IMandate |
---|
[8846] | 29 | |
---|
[8849] | 30 | class Mandate(grok.Model): |
---|
| 31 | """This is a mandate. |
---|
[8846] | 32 | """ |
---|
| 33 | grok.implements(IMandate) |
---|
| 34 | grok.provides(IMandate) |
---|
[8849] | 35 | grok.baseclass() |
---|
[8846] | 36 | |
---|
[8849] | 37 | def __init__(self, days=1, mandate_id=None): |
---|
[8846] | 38 | super(Mandate, self).__init__() |
---|
| 39 | self.creation_date = datetime.utcnow() # offset-naive datetime |
---|
| 40 | delta = timedelta(days=days) |
---|
| 41 | self.expires = datetime.utcnow() + delta |
---|
[8847] | 42 | if mandate_id is None: |
---|
[8849] | 43 | mandate_id = os.urandom(20) |
---|
| 44 | mandate_id = hashlib.md5(mandate_id).hexdigest() |
---|
[8847] | 45 | self.mandate_id = mandate_id |
---|
[8846] | 46 | self.params = {} |
---|
| 47 | return |
---|
| 48 | |
---|
[8849] | 49 | def execute(self): |
---|
| 50 | return _('Nothing to do.') |
---|
| 51 | |
---|
[8857] | 52 | class PasswordMandate(Mandate): |
---|
[8860] | 53 | """This is a mandate which can set a password. |
---|
[8849] | 54 | """ |
---|
| 55 | |
---|
[8857] | 56 | def _setPassword(self): |
---|
[8858] | 57 | user = self.params.get('user', None) |
---|
[8846] | 58 | pwd = self.params.get('password', None) |
---|
[8859] | 59 | if not None in (user, pwd): |
---|
[8858] | 60 | try: |
---|
| 61 | IUserAccount(user).setPassword(pwd) |
---|
| 62 | return True |
---|
| 63 | except: |
---|
| 64 | return False |
---|
[8846] | 65 | return False |
---|
| 66 | |
---|
| 67 | def execute(self): |
---|
| 68 | msg = _('Wrong mandate parameters.') |
---|
[14221] | 69 | redirect_path = '' |
---|
[8846] | 70 | if self.expires < datetime.utcnow(): |
---|
| 71 | msg = _('Mandate expired.') |
---|
[14221] | 72 | elif self._setPassword(): |
---|
[11681] | 73 | msg = _('Password has been successfully set. ' |
---|
| 74 | 'Login with your new password.') |
---|
[8860] | 75 | username = IUserAccount(self.params['user']).name |
---|
| 76 | grok.getSite().logger.info( |
---|
| 77 | 'PasswordMandate used: %s ' % username) |
---|
[14221] | 78 | redirect_path = 'login' |
---|
[8846] | 79 | del self.__parent__[self.mandate_id] |
---|
[14221] | 80 | return msg, redirect_path |
---|