- moved email notification code to Person class. call send_initiate_password_reset_em...
[plcapi.git] / PLC / Methods / InitiateResetPassword.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Persons import Person, Persons
5 from PLC.Messages import Message, Messages
6 from PLC.Auth import AnonymousAuth
7
8 import os
9 import time
10 from random import Random
11 import string
12
13 def create_random_string():
14     """
15     create and return a random string.
16     """
17     random = Random()
18     pool = string.letters + string.digits
19     key = [random.choice(pool) for i in range(32)]
20     random.shuffle(key) 
21     key = ''.join(key)
22         
23     return key    
24
25 class InitiateResetPassword(Method):
26     """
27     start the reset password procedure. this sends the user an email
28     they can use to go to the web interface to finish the reset of their
29     password.
30
31     the password is not modified yet. A random link to a password reset page
32     is created, and set to expire in 24 hours.
33
34     Returns 1 if successful, faults otherwise.
35     """
36
37     roles = ['admin', 'pi', 'user', 'tech']
38
39     accepts = [
40         AnonymousAuth(),
41         Mixed(Person.fields['person_id'],
42               Person.fields['email'])
43         ]
44
45     returns = Parameter(int, '1 if successful')
46
47     def call(self, auth, person_id_or_email):
48
49         # Get account information
50         persons = Persons(self.api, [person_id_or_email])
51         if not persons:
52             raise PLCInvalidArgument, "No such account"
53         
54         # update the verification key for this person in the db
55         person = persons[0]
56         verification_key = create_random_string()
57         person['verification_key'] = verification_key
58         person['verification_expires'] = \
59             time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(time.time()+86400))
60         person.sync()
61         
62         # send notification email
63         person.send_initiate_password_reset_email()
64
65         # Logging variables
66         self.object_ids = [person['person_id']]
67         self.message = 'Initiated password reset for person %d.' % \
68                 (person['person_id'])
69  
70         return 1