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