X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FMethods%2FUpdatePerson.py;h=0795151020bcf3583bf28c1541ac6fbe4285e4a7;hb=ceda06160467b19df343fd8f40822eba6ae6079c;hp=a79a46124447899de38c7bd17edef03fdc113035;hpb=40d33ca5c3e174e3ac07e3519c07e43825b4c797;p=plcapi.git diff --git a/PLC/Methods/UpdatePerson.py b/PLC/Methods/UpdatePerson.py index a79a461..0795151 100644 --- a/PLC/Methods/UpdatePerson.py +++ b/PLC/Methods/UpdatePerson.py @@ -1,19 +1,23 @@ +# $Id$ +# $URL$ from PLC.Faults import * from PLC.Method import Method from PLC.Parameter import Parameter, Mixed from PLC.Persons import Person, Persons -from PLC.Auth import PasswordAuth +from PLC.Auth import Auth +from PLC.sendmail import sendmail + +related_fields = Person.related_fields.keys() +can_update = lambda (field, value): field in \ + ['first_name', 'last_name', 'title', 'email', + 'password', 'phone', 'url', 'bio', 'accepted_aup', + 'enabled'] + related_fields class UpdatePerson(Method): """ - Updates a person. Only the fields specified in update_fields are + Updates a person. Only the fields specified in person_fields are updated, all other fields are left untouched. - To remove a value without setting a new one in its place (for - example, to remove an address from the person), specify -1 for int - and double fields and 'null' for string fields. first_name and - last_name cannot be unset. - Users and techs can only update themselves. PIs can only update themselves and other non-PIs at their sites. @@ -22,38 +26,28 @@ class UpdatePerson(Method): roles = ['admin', 'pi', 'user', 'tech'] - can_update = lambda (field, value): field in \ - ['first_name', 'last_name', 'title', 'email', - 'password', 'phone', 'url', 'bio', 'accepted_aup'] - update_fields = dict(filter(can_update, Person.fields.items())) + person_fields = dict(filter(can_update, Person.fields.items() + Person.related_fields.items())) accepts = [ - PasswordAuth(), + Auth(), Mixed(Person.fields['person_id'], Person.fields['email']), - update_fields + person_fields ] returns = Parameter(int, '1 if successful') - def call(self, auth, person_id_or_email, update_fields): - if filter(lambda field: field not in self.update_fields, update_fields): - raise PLCInvalidArgument, "Invalid field specified" - - # XML-RPC cannot marshal None, so we need special values to - # represent "unset". - for key, value in update_fields.iteritems(): - if value == -1 or value == "null": - if key in ['first_name', 'last_name']: - raise PLCInvalidArgument, "first_name and last_name cannot be unset" - update_fields[key] = None + def call(self, auth, person_id_or_email, person_fields): + person_fields = dict(filter(can_update, person_fields.items())) # Get account information persons = Persons(self.api, [person_id_or_email]) if not persons: raise PLCInvalidArgument, "No such account" + person = persons[0] - person = persons.values()[0] + if person['peer_id'] is not None: + raise PLCInvalidArgument, "Not a local account" # Authenticated function assert self.caller is not None @@ -62,7 +56,37 @@ class UpdatePerson(Method): if not self.caller.can_update(person): raise PLCPermissionDenied, "Not allowed to update specified account" - person.update(update_fields) + # Make requested associations + for field in related_fields: + if field in person_fields: + person.associate(auth, field, person_fields[field]) + person_fields.pop(field) + + person.update(person_fields) + person.update_last_updated(False) person.sync() + if 'enabled' in person_fields: + To = [("%s %s" % (person['first_name'], person['last_name']), person['email'])] + Cc = [] + if person['enabled']: + Subject = "%s account enabled" % (self.api.config.PLC_NAME) + 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) + else: + Subject = "%s account disabled" % (self.api.config.PLC_NAME) + Body = "Your %s account has been disabled. Please contact your PI or PlanetLab support for more information" % (self.api.config.PLC_NAME) + sendmail(self.api, To = To, Cc = Cc, Subject = Subject, Body = Body) + + + # Logging variables + self.event_objects = {'Person': [person['person_id']]} + + # Redact password + if 'password' in person_fields: + person_fields['password'] = "Removed by API" + self.message = 'Person %d updated: %s.' % \ + (person['person_id'], person_fields.keys()) + if 'enabled' in person_fields: + self.message += ' Person enabled' + return 1