get rid of svn keywords once and for good
[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 related_fields = Person.related_fields.keys()
9 can_update = lambda (field, value): field in \
10              ['first_name', 'last_name', 'title', 'email',
11               'password', 'phone', 'url', 'bio', 'accepted_aup',
12               'enabled'] + related_fields
13
14 class UpdatePerson(Method):
15     """
16     Updates a person. Only the fields specified in person_fields are
17     updated, all other fields are left untouched.
18
19     Users and techs can only update themselves. PIs can only update
20     themselves and other non-PIs at their sites.
21
22     Returns 1 if successful, faults otherwise.
23     """
24
25     roles = ['admin', 'pi', 'user', 'tech']
26
27     person_fields = dict(filter(can_update, Person.fields.items() + Person.related_fields.items()))
28
29     accepts = [
30         Auth(),
31         Mixed(Person.fields['person_id'],
32               Person.fields['email']),
33         person_fields
34         ]
35
36     returns = Parameter(int, '1 if successful')
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         # Make requested associations
58         for field in related_fields:
59             if field in person_fields:
60                 person.associate(auth, field, person_fields[field])
61                 person_fields.pop(field)
62
63         person.update(person_fields)
64         person.update_last_updated(False)
65         person.sync()
66
67         if 'enabled' in person_fields:
68             To = [("%s %s" % (person['first_name'], person['last_name']), person['email'])]
69             Cc = []
70             if person['enabled']:
71                 Subject = "%s account enabled" % (self.api.config.PLC_NAME)
72                 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)
73             else:
74                 Subject = "%s account disabled" % (self.api.config.PLC_NAME)
75                 Body = "Your %s account has been disabled. Please contact your PI or PlanetLab support for more information" % (self.api.config.PLC_NAME)
76             sendmail(self.api, To = To, Cc = Cc, Subject = Subject, Body = Body)
77
78
79         # Logging variables
80         self.event_objects = {'Person': [person['person_id']]}
81
82         # Redact password
83         if 'password' in person_fields:
84             person_fields['password'] = "Removed by API"
85         self.message = 'Person %d updated: %s.' % \
86                        (person['person_id'], person_fields.keys())
87         if 'enabled' in person_fields:
88             self.message += ' Person enabled'
89
90         return 1