- Change .py files to use 4-space indents and no hard tab characters.
[plcapi.git] / PLC / Methods / UpdateKey.py
1 # $Id$
2 # $URL$
3 from PLC.Faults import *
4 from PLC.Method import Method
5 from PLC.Parameter import Parameter, Mixed
6 from PLC.Keys import Key, Keys
7 from PLC.Auth import Auth
8
9 can_update = lambda (field, value): field in \
10              ['key_type', 'key']
11
12 class UpdateKey(Method):
13     """
14     Updates the parameters of an existing key with the values in
15     key_fields.
16
17     Non-admins may only update their own keys.
18
19     Returns 1 if successful, faults otherwise.
20     """
21
22     roles = ['admin', 'pi', 'tech', 'user']
23
24     key_fields = dict(filter(can_update, Key.fields.items()))
25
26     accepts = [
27         Auth(),
28         Key.fields['key_id'],
29         key_fields
30         ]
31
32     returns = Parameter(int, '1 if successful')
33
34     def call(self, auth, key_id, key_fields):
35         key_fields = dict(filter(can_update, key_fields.items()))
36
37         # Get key information
38         keys = Keys(self.api, [key_id])
39         if not keys:
40             raise PLCInvalidArgument, "No such key"
41         key = keys[0]
42
43         if key['peer_id'] is not None:
44             raise PLCInvalidArgument, "Not a local key"
45
46         if 'admin' not in self.caller['roles']:
47             if key['key_id'] not in self.caller['key_ids']:
48                 raise PLCPermissionDenied, "Key must be associated with one of your accounts"
49
50         key.update(key_fields)
51         key.sync()
52
53         # Logging variables
54         self.event_objects = {'Key': [key['key_id']]}
55         self.message = 'key %d updated: %s' % \
56                 (key['key_id'], ", ".join(key_fields.keys()))
57         return 1