UpdatePerson.py
[plcapi.git] / PLC / Methods / UpdatePerson.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.Auth import PasswordAuth
6
7 can_update = lambda (field, value): field in \
8              ['first_name', 'last_name', 'title', 'email',
9               'password', 'phone', 'url', 'bio', 'accepted_aup',
10               'enabled']
11
12 class UpdatePerson(Method):
13     """
14     Updates a person. Only the fields specified in person_fields are
15     updated, all other fields are left untouched.
16
17     To remove a value without setting a new one in its place (for
18     example, to remove an address from the person), specify -1 for int
19     and double fields and 'null' for string fields. first_name and
20     last_name cannot be unset.
21     
22     Users and techs can only update themselves. PIs can only update
23     themselves and other non-PIs at their sites.
24
25     Returns 1 if successful, faults otherwise.
26     """
27
28     roles = ['admin', 'pi', 'user', 'tech']
29
30     update_fields = dict(filter(can_update, Person.fields.items()))
31
32     accepts = [
33         PasswordAuth(),
34         Mixed(Person.fields['person_id'],
35               Person.fields['email']),
36         update_fields
37         ]
38
39     returns = Parameter(int, '1 if successful')
40
41     def call(self, auth, person_id_or_email, person_fields):
42         person_fields = dict(filter(can_update, person_fields.items()))
43
44         # Get account information
45         persons = Persons(self.api, [person_id_or_email])
46         if not persons:
47             raise PLCInvalidArgument, "No such account"
48
49         person = persons.values()[0]
50
51         # Authenticated function
52         assert self.caller is not None
53
54         # Check if we can update this account
55         if not self.caller.can_update(person):
56             raise PLCPermissionDenied, "Not allowed to update specified account"
57
58         person.update(person_fields)
59         person.sync()
60
61         return 1