X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FMethods%2FAddPersonKey.py;h=5c28b773c319932ce114c5e0e3d3a1ef4233e59d;hb=d51a2803a4fb08e542e2debb4b49983066e5aaf4;hp=edad2cbdb0e70b6f29f8f2ccac703e727ff88039;hpb=f8e0c71d96dce99eff25e5bb6bdf941468e897a2;p=plcapi.git diff --git a/PLC/Methods/AddPersonKey.py b/PLC/Methods/AddPersonKey.py index edad2cb..5c28b77 100644 --- a/PLC/Methods/AddPersonKey.py +++ b/PLC/Methods/AddPersonKey.py @@ -3,7 +3,9 @@ from PLC.Method import Method from PLC.Parameter import Parameter, Mixed from PLC.Keys import Key, Keys from PLC.Persons import Person, Persons -from PLC.Auth import PasswordAuth +from PLC.Auth import Auth + +can_update = lambda field_value: field_value[0] in ['key_type','key'] class AddPersonKey(Method): """ @@ -16,38 +18,42 @@ class AddPersonKey(Method): roles = ['admin', 'pi', 'tech', 'user'] + key_fields = dict(list(filter(can_update, list(Key.fields.items())))) + accepts = [ - PasswordAuth(), + Auth(), Mixed(Person.fields['person_id'], Person.fields['email']), - Key.fields['key_type'], - Key.fields['key'] + key_fields ] returns = Parameter(int, 'New key_id (> 0) if successful') - event_type = 'Add' - object_type = 'Key' - object_ids = [] + def call(self, auth, person_id_or_email, key_fields): + key_fields = dict(list(filter(can_update, list(key_fields.items())))) - def call(self, auth, person_id_or_email, key_type, key_value): # Get account details - persons = Persons(self.api, [person_id_or_email]).values() + persons = Persons(self.api, [person_id_or_email]) if not persons: - raise PLCInvalidArgument, "No such account" + raise PLCInvalidArgument("No such account") person = persons[0] - # If we are not admin, make sure caller is adding a key to their account + if person['peer_id'] is not None: + raise PLCInvalidArgument("Not a local account") + + # If we are not admin, make sure caller is adding a key to their account if 'admin' not in self.caller['roles']: if person['person_id'] != self.caller['person_id']: - raise PLCPermissionDenied, "You may only modify your own keys" + raise PLCPermissionDenied("You may only modify your own keys") - key = Key(self.api) - key['person_id'] = person['person_id'] - key['key_type'] = key_type - key['key'] = key_value + key = Key(self.api, key_fields) key.sync(commit = False) person.add_key(key, commit = True) - self.object_ids = [key['key_id']] + + # Logging variables + self.event_objects = {'Person': [person['person_id']], + 'Key': [key['key_id']]} + self.message = 'Key %d added to person %d' % \ + (key['key_id'], person['person_id']) return key['key_id']