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