bug fixes
[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 from PLC.sendmail import sendmail
7
8 can_update = lambda (field, value): field in \
9              ['first_name', 'last_name', 'title', 'email',
10               'password', 'phone', 'url', 'bio', 'accepted_aup',
11               'enabled'] 
12
13 class UpdatePerson(Method):
14     """
15     Updates a person. Only the fields specified in person_fields are
16     updated, all other fields are left untouched.
17
18     Users and techs can only update themselves. PIs can only update
19     themselves and other non-PIs at their sites.
20
21     Returns 1 if successful, faults otherwise.
22     """
23
24     roles = ['admin', 'pi', 'user', 'tech']
25
26     person_fields = dict(filter(can_update, Person.fields.items() ))
27
28     accepts = [
29         Auth(),
30         Mixed(Person.fields['person_id'],
31               Person.fields['email']),
32         person_fields
33         ]
34
35     returns = Parameter(int, '1 if successful')
36
37     def call(self, auth, person_id_or_email, person_fields):
38         person_fields = dict(filter(can_update, person_fields.items()))
39
40         # Get account information
41         persons = Persons(self.api, [person_id_or_email])
42         if not persons:
43             raise PLCInvalidArgument, "No such account"
44         person = persons[0]
45
46         if person['peer_id'] is not None:
47             raise PLCInvalidArgument, "Not a local account"
48
49         # Authenticated function
50         assert self.caller is not None
51
52         # Check if we can update this account
53         if not self.caller.can_update(person):
54             raise PLCPermissionDenied, "Not allowed to update specified account"
55
56         # Make requested associations
57         for field in related_fields:
58             if field in person_fields:
59                 person.associate(auth, field, person_fields[field])
60                 person_fields.pop(field)
61
62         person.update(person_fields)
63         person.update_last_updated(False)
64         person.sync()
65
66         if 'enabled' in person_fields:
67             To = [("%s %s" % (person['first_name'], person['last_name']), person['email'])]
68             Cc = []
69             if person['enabled']:
70                 Subject = "%s account enabled" % (self.api.config.PLC_NAME)
71                 Body = "Your %s account has been enabled. Please visit %s to access your account." % (self.api.config.PLC_NAME, self.api.config.PLC_WWW_HOST)
72             else:
73                 Subject = "%s account disabled" % (self.api.config.PLC_NAME)
74                 Body = "Your %s account has been disabled. Please contact your PI or PlanetLab support for more information" % (self.api.config.PLC_NAME)
75             sendmail(self.api, To = To, Cc = Cc, Subject = Subject, Body = Body)
76
77
78         # Logging variables
79         self.event_objects = {'Person': [person['person_id']]}
80
81         # Redact password
82         if 'password' in person_fields:
83             person_fields['password'] = "Removed by API"
84         self.message = 'Person %d updated: %s.' % \
85                        (person['person_id'], person_fields.keys())
86         if 'enabled' in person_fields:
87             self.message += ' Person enabled'
88
89         return 1