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 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
14 class UpdatePerson(Method):
16 Updates a person. Only the fields specified in person_fields are
17 updated, all other fields are left untouched.
19 Users and techs can only update themselves. PIs can only update
20 themselves and other non-PIs at their sites.
22 Returns 1 if successful, faults otherwise.
25 roles = ['admin', 'pi', 'user', 'tech']
27 person_fields = dict(filter(can_update, Person.fields.items() + Person.related_fields.items()))
31 Mixed(Person.fields['person_id'],
32 Person.fields['email']),
36 returns = Parameter(int, '1 if successful')
38 def call(self, auth, person_id_or_email, person_fields):
39 person_fields = dict(filter(can_update, person_fields.items()))
41 # Get account information
42 persons = Persons(self.api, [person_id_or_email])
44 raise PLCInvalidArgument, "No such account"
47 if person['peer_id'] is not None:
48 raise PLCInvalidArgument, "Not a local account"
50 # Authenticated function
51 assert self.caller is not None
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"
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)
63 person.update(person_fields)
64 person.update_last_updated(False)
67 if 'enabled' in person_fields:
68 To = [("%s %s" % (person['first_name'], person['last_name']), person['email'])]
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)
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)
80 self.event_objects = {'Person': [person['person_id']]}
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'