2 from PLC.Faults import *
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.Persons import Person, Persons
6 from PLC.Auth import Auth
7 from PLC.sendmail import sendmail
9 related_fields = Person.related_fields.keys()
10 can_update = lambda (field, value): field in \
11 ['first_name', 'last_name', 'title', 'email',
12 'password', 'phone', 'url', 'bio', 'accepted_aup',
13 'enabled'] + related_fields
15 class UpdatePerson(Method):
17 Updates a person. Only the fields specified in person_fields are
18 updated, all other fields are left untouched.
20 Users and techs can only update themselves. PIs can only update
21 themselves and other non-PIs at their sites.
23 Returns 1 if successful, faults otherwise.
26 roles = ['admin', 'pi', 'user', 'tech']
28 person_fields = dict(filter(can_update, Person.fields.items() + Person.related_fields.items()))
32 Mixed(Person.fields['person_id'],
33 Person.fields['email']),
37 returns = Parameter(int, '1 if successful')
39 def call(self, auth, person_id_or_email, person_fields):
40 person_fields = dict(filter(can_update, person_fields.items()))
42 # Get account information
43 persons = Persons(self.api, [person_id_or_email])
45 raise PLCInvalidArgument, "No such account"
48 if person['peer_id'] is not None:
49 raise PLCInvalidArgument, "Not a local account"
51 # Authenticated function
52 assert self.caller is not None
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"
58 # Make requested associations
59 for field in related_fields:
60 if field in person_fields:
61 person.associate(auth, field, person_fields[field])
62 person_fields.pop(field)
64 person.update(person_fields)
65 person.update_last_updated(False)
68 if 'enabled' in person_fields:
69 To = [("%s %s" % (person['first_name'], person['last_name']), person['email'])]
72 Subject = "%s account enabled" % (self.api.config.PLC_NAME)
73 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)
75 Subject = "%s account disabled" % (self.api.config.PLC_NAME)
76 Body = "Your %s account has been disabled. Please contact your PI or PlanetLab support for more information" % (self.api.config.PLC_NAME)
77 sendmail(self.api, To = To, Cc = Cc, Subject = Subject, Body = Body)
81 self.event_objects = {'Person': [person['person_id']]}
84 if 'password' in person_fields:
85 person_fields['password'] = "Removed by API"
86 self.message = 'Person %d updated: %s.' % \
87 (person['person_id'], person_fields.keys())
88 if 'enabled' in person_fields:
89 self.message += ' Person enabled'