3 from PLC.Faults import *
4 from PLC.Method import Method
5 from PLC.Parameter import Parameter, Mixed
6 from PLC.Persons import Person, Persons
7 from PLC.Auth import Auth
8 from PLC.sendmail import sendmail
10 related_fields = Person.related_fields.keys()
11 can_update = lambda (field, value): field in \
12 ['first_name', 'last_name', 'title', 'email',
13 'password', 'phone', 'url', 'bio', 'accepted_aup',
14 'enabled'] + related_fields
16 class UpdatePerson(Method):
18 Updates a person. Only the fields specified in person_fields are
19 updated, all other fields are left untouched.
21 Users and techs can only update themselves. PIs can only update
22 themselves and other non-PIs at their sites.
24 Returns 1 if successful, faults otherwise.
27 roles = ['admin', 'pi', 'user', 'tech']
29 person_fields = dict(filter(can_update, Person.fields.items() + Person.related_fields.items()))
33 Mixed(Person.fields['person_id'],
34 Person.fields['email']),
38 returns = Parameter(int, '1 if successful')
40 def call(self, auth, person_id_or_email, person_fields):
41 person_fields = dict(filter(can_update, person_fields.items()))
43 # Get account information
44 persons = Persons(self.api, [person_id_or_email])
46 raise PLCInvalidArgument, "No such account"
49 if person['peer_id'] is not None:
50 raise PLCInvalidArgument, "Not a local account"
52 # Authenticated function
53 assert self.caller is not None
55 # Check if we can update this account
56 if not self.caller.can_update(person):
57 raise PLCPermissionDenied, "Not allowed to update specified account"
59 # Make requested associations
60 for field in related_fields:
61 if field in person_fields:
62 person.associate(auth, field, person_fields[field])
63 person_fields.pop(field)
65 person.update(person_fields)
66 person.update_last_updated(False)
69 if 'enabled' in person_fields:
70 To = [("%s %s" % (person['first_name'], person['last_name']), person['email'])]
73 Subject = "%s account enabled" % (self.api.config.PLC_NAME)
74 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)
76 Subject = "%s account disabled" % (self.api.config.PLC_NAME)
77 Body = "Your %s account has been disabled. Please contact your PI or PlanetLab support for more information" % (self.api.config.PLC_NAME)
78 sendmail(self.api, To = To, Cc = Cc, Subject = Subject, Body = Body)
82 self.event_objects = {'Person': [person['person_id']]}
85 if 'password' in person_fields:
86 person_fields['password'] = "Removed by API"
87 self.message = 'Person %d updated: %s.' % \
88 (person['person_id'], person_fields.keys())
89 if 'enabled' in person_fields:
90 self.message += ' Person enabled'