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