X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;ds=sidebyside;f=PLC%2FMethods%2FUpdatePerson.py;h=8f74fc0442da8b52b6951cc2218e6e3a81572c30;hb=cafad33fd1bfcec52ff875987ff840a1c8566dbe;hp=36cb2c256085adf259ec74b93f6ddde774935f93;hpb=682e1251201c658ef45e38b0761fe79e787f2e3e;p=plcapi.git diff --git a/PLC/Methods/UpdatePerson.py b/PLC/Methods/UpdatePerson.py index 36cb2c2..8f74fc0 100644 --- a/PLC/Methods/UpdatePerson.py +++ b/PLC/Methods/UpdatePerson.py @@ -2,22 +2,18 @@ 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 +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'] + 'enabled'] + related_fields class UpdatePerson(Method): """ 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. @@ -27,13 +23,13 @@ class UpdatePerson(Method): roles = ['admin', 'pi', 'user', 'tech'] - 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') @@ -45,8 +41,10 @@ class UpdatePerson(Method): 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 @@ -54,8 +52,26 @@ class UpdatePerson(Method): # Check if we can update this account if not self.caller.can_update(person): raise PLCPermissionDenied, "Not allowed to update specified account" + + # 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() + + # 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