f887feb2a8635f140dc8bfd786c99a950260ea76
[plcapi.git] / PLC / Methods / DeleteKey.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 class DeleteKey(Method):
9     """
10     Deletes a key.
11
12     Non-admins may only delete their own keys.
13
14     Returns 1 if successful, faults otherwise.
15     """
16
17     roles = ['admin', 'pi', 'tech', 'user']
18
19     accepts = [
20         Auth(),
21         Key.fields['key_id'],
22         ]
23
24     returns = Parameter(int, '1 if successful')
25
26
27     def call(self, auth, key_id):
28         # Get associated key details
29         keys = Keys(self.api, [key_id])
30         if not keys:
31             raise PLCInvalidArgument, "No such key"
32         key = keys[0]
33
34         if key['peer_id'] is not None:
35             raise PLCInvalidArgument, "Not a local key"
36
37         if 'admin' not in self.caller['roles']:
38             if key['key_id'] not in self.caller['key_ids']:
39                 raise PLCPermissionDenied, "Key must be associated with your account"
40
41         key.delete()
42         
43         # Logging variables
44         self.event_objects = {'Key': [key['key_id']]}
45         self.message = 'Key %d deleted' % key['key_id']
46
47         return 1