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.Auth import PasswordAuth
7 class UpdateKey(Method):
9 Updates the parameters of an existing key with the values in
12 Non-admins may only update their own keys.
14 Returns 1 if successful, faults otherwise.
17 roles = ['admin', 'pi', 'tech', 'user']
19 can_update = lambda (field, value): field in \
21 update_fields = dict(filter(can_update, Key.fields.items()))
29 returns = Parameter(int, '1 if successful')
31 def call(self, auth, key_id, key_fields):
32 # Make sure only valid fields are specified
33 if filter(lambda field: field not in self.update_fields, key_fields):
34 raise PLCInvalidArgument, "Invalid field specified"
37 keys = Keys(self.api, [key_id]).values()
39 raise PLCInvalidArgument, "No such key"
42 if 'admin' not in self.caller['roles']:
43 if key['key_id'] not in self.caller['key_ids']:
44 raise PLCPermissionDenied, "Key must be associated with one of your accounts"
45 assert key['person_id'] == self.caller['person_id']
47 key.update(key_fields)