- remove is_blacklisted from fields that can be updated
[plcapi.git] / PLC / Methods / UpdateKey.py
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
6
7 class UpdateKey(Method):
8     """
9     Updates the parameters of an existing key with the values in
10     key_fields.
11
12     Non-admins may only update their own keys.
13
14     Returns 1 if successful, faults otherwise.
15     """
16
17     roles = ['admin', 'pi', 'tech', 'user']
18
19     can_update = lambda (field, value): field in \
20                  ['key_type', 'key']
21     update_fields = dict(filter(can_update, Key.fields.items()))
22
23     accepts = [
24         PasswordAuth(),
25         Key.fields['key_id'],
26         update_fields
27         ]
28
29     returns = Parameter(int, '1 if successful')
30
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"
35
36         # Get key information
37         keys = Keys(self.api, [key_id]).values()
38         if not keys:
39             raise PLCInvalidArgument, "No such key"
40         key = keys[0]
41
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']
46
47         key.update(key_fields)
48         key.sync()
49
50         return 1