fd24eefc06ac1992da5fda260d34db5d6dffc03c
[plcapi.git] / PLC / Methods / AdmDeletePersonKeys.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Persons import Person, Persons
5 from PLC.Keys import Key, Keys
6 from PLC.Auth import Auth
7
8 class AdmDeletePersonKeys(Method):
9     """
10     Deprecated. Functionality can be implemented with GetPersons and
11     DeleteKey.
12
13     Deletes the specified keys. Non-admins may only delete their own
14     keys.
15
16     Returns 1 if successful, faults otherwise.
17     """
18
19     status = "deprecated"
20
21     roles = ['admin', 'pi', 'tech', 'user']
22
23     accepts = [
24         Auth(),
25         Mixed(Person.fields['person_id'],
26               Person.fields['email']),
27         [Key.fields['key_id']]
28         ]
29
30     returns = Parameter(int, '1 if successful')
31
32     def call(self, auth, person_id_or_email, key_ids):
33         # Get account information
34         persons = Persons(self.api, [person_id_or_email])
35         if not persons:
36             raise PLCInvalidArgument, "No such account"
37         person = persons[0]
38
39         if person['peer_id'] is not None:
40             raise PLCInvalidArgument, "Not a local account"
41
42         if 'admin' not in self.caller['roles']:
43             if self.caller['person_id'] != person['person_id']:
44                 raise PLCPermissionDenied, "Not allowed to update specified account"
45
46         key_ids = set(key_ids).intersection(person['key_ids'])
47         if not key_ids:
48             return 1
49
50         # Get associated key details
51         keys = Keys(self.api, key_ids)
52
53         for key in keys:
54             key.delete()
55
56         return 1