- key no longer has person_id
[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 PasswordAuth
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         PasswordAuth(),
20         Key.fields['key_id'],
21         ]
22
23     returns = Parameter(int, '1 if successful')
24
25     def call(self, auth, key_id):
26         # Get associated key details
27         keys = Keys(self.api, [key_id]).values()
28         if not keys:
29             raise PLCInvalidArgument, "No such key"
30         key = keys[0]
31
32         if 'admin' not in self.caller['roles']:
33             if key['key_id'] not in self.caller['key_ids']:
34                 raise PLCPermissionDenied, "Key must be associated with your account"
35
36         key.delete()
37
38         return 1