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
8 can_update = lambda (field, value): field in \
9 ['first_name', 'last_name', 'title', 'email',
10 'password', 'phone', 'url', 'bio', 'accepted_aup',
13 class UpdatePerson(Method):
15 Updates a person. Only the fields specified in person_fields are
16 updated, all other fields are left untouched.
18 Users and techs can only update themselves. PIs can only update
19 themselves and other non-PIs at their sites.
21 Returns 1 if successful, faults otherwise.
24 roles = ['admin', 'pi', 'user', 'tech']
26 person_fields = dict(filter(can_update, Person.fields.items() ))
30 Mixed(Person.fields['person_id'],
31 Person.fields['email']),
35 returns = Parameter(int, '1 if successful')
37 def call(self, auth, person_id_or_email, person_fields):
38 person_fields = dict(filter(can_update, person_fields.items()))
40 # Get account information
41 persons = Persons(self.api, [person_id_or_email])
43 raise PLCInvalidArgument, "No such account"
46 if person['peer_id'] is not None:
47 raise PLCInvalidArgument, "Not a local account"
49 # Authenticated function
50 assert self.caller is not None
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"
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)
62 person.update(person_fields)
63 person.update_last_updated(False)
66 if 'enabled' in person_fields:
67 To = [("%s %s" % (person['first_name'], person['last_name']), person['email'])]
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)
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)
79 self.event_objects = {'Person': [person['person_id']]}
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'