b238d77427e017f9d18b99943cb001cc389446bd
[plcapi.git] / PLC / Methods / VerifyPerson.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 time
9
10 class VerifyPerson(Method):
11     """
12     Check that the verification_key is valid for a specified person
13     and not expired. 
14
15     Returns 1 if the verification key if valid.
16     """
17
18     roles = ['admin', 'pi', 'user', 'tech']
19
20     accepts = [
21         AnonymousAuth(),
22         Mixed(Person.fields['person_id'],
23               Person.fields['email']),
24         Person.fields['verification_key']
25         ]
26
27     returns = Parameter(int, '1 if verification_key is valid')
28
29     def call(self, auth, person_id_or_email, verification_key):
30
31         # Get account information
32         persons = Persons(self.api, [person_id_or_email])
33         if not persons:
34             raise PLCInvalidArgument, "No such account"
35
36         person = persons[0]
37
38         # make sure verification key matches
39         if not person['verification_key']:
40             raise PLCInvalidArgument, "Invalid key"
41         if person['verification_key'] != verification_key:
42             raise PLCInvalidArgument, "Invalid key"
43
44         # make sure key is not expired
45         if not person['verification_expires']:
46             raise PLCInvalidArgument, "Invalid key"
47         expires = str(person['verification_expires'])
48         if time.strptime(expires, "%Y-%m-%d %H:%M:%S") < \
49            time.gmtime(time.time()):
50             raise PLCInvalidArgument, "Invalid key"
51         
52         # Logging variables
53         self.object_ids = [person['person_id']]
54         self.message = 'Verification key check on perons %d.' % \
55                 (person['person_id'])
56  
57         return 1