This commit was manufactured by cvs2svn to create branch
[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 Auth
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     Users and techs can only update themselves. PIs can only update
18     themselves and other non-PIs at their sites.
19
20     Returns 1 if successful, faults otherwise.
21     """
22
23     roles = ['admin', 'pi', 'user', 'tech']
24
25     person_fields = dict(filter(can_update, Person.fields.items()))
26
27     accepts = [
28         Auth(),
29         Mixed(Person.fields['person_id'],
30               Person.fields['email']),
31         person_fields
32         ]
33
34     returns = Parameter(int, '1 if successful')
35
36     object_type = 'Person'
37
38     def call(self, auth, person_id_or_email, person_fields):
39         person_fields = dict(filter(can_update, person_fields.items()))
40
41         # Get account information
42         persons = Persons(self.api, [person_id_or_email])
43         if not persons:
44             raise PLCInvalidArgument, "No such account"
45         person = persons[0]
46
47         if person['peer_id'] is not None:
48             raise PLCInvalidArgument, "Not a local account"
49
50         # Authenticated function
51         assert self.caller is not None
52
53         # Check if we can update this account
54         if not self.caller.can_update(person):
55             raise PLCPermissionDenied, "Not allowed to update specified account"
56
57         person.update(person_fields)
58         person.sync()
59         
60         # Logging variables
61         self.object_ids = [person['person_id']]
62
63         # Redact password
64         if 'password' in person_fields:
65             person_fields['password'] = "Removed by API"
66         self.message = 'Person %d updated: %s.' % \
67                        (person['person_id'], person_fields.keys())
68         if 'enabled' in person_fields:
69             self.message += ' Person enabled'   
70
71         return 1