1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Keys import Key, Keys
5 from PLC.Persons import Person, Persons
6 from PLC.Auth import Auth
8 can_update = lambda (field, value): field not in ['key_id']
10 class AddPersonKey(Method):
12 Adds a new key to the specified account.
14 Non-admins can only modify their own keys.
16 Returns the new key_id (> 0) if successful, faults otherwise.
19 roles = ['admin', 'pi', 'tech', 'user']
21 key_fields = dict(filter(can_update, Key.fields.items()))
25 Mixed(Person.fields['person_id'],
26 Person.fields['email']),
30 returns = Parameter(int, 'New key_id (> 0) if successful')
32 object_type = 'Person'
35 def call(self, auth, person_id_or_email, key_fields):
36 key_fields = dict(filter(can_update, key_fields.items()))
39 persons = Persons(self.api, [person_id_or_email])
41 raise PLCInvalidArgument, "No such account"
44 if person['peer_id'] is not None:
45 raise PLCInvalidArgument, "Not a local account"
47 # If we are not admin, make sure caller is adding a key to their account
48 if 'admin' not in self.caller['roles']:
49 if person['person_id'] != self.caller['person_id']:
50 raise PLCPermissionDenied, "You may only modify your own keys"
52 key = Key(self.api, key_fields)
53 key.sync(commit = False)
54 person.add_key(key, commit = True)
57 self.object_ids = [person['person_id'], key['key_id']]
58 self.message = 'Key %d added to person %d' % \
59 (key['key_id'], person['person_id'])