- added logging variable 'object_type'
[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     object_type = 'Person'
33
34     def call(self, auth, person_id_or_email, key_ids):
35         # Get account information
36         persons = Persons(self.api, [person_id_or_email])
37         if not persons:
38             raise PLCInvalidArgument, "No such account"
39         person = persons[0]
40
41         if person['peer_id'] is not None:
42             raise PLCInvalidArgument, "Not a local account"
43
44         if 'admin' not in self.caller['roles']:
45             if self.caller['person_id'] != person['person_id']:
46                 raise PLCPermissionDenied, "Not allowed to update specified account"
47
48         key_ids = set(key_ids).intersection(person['key_ids'])
49         if not key_ids:
50             return 1
51
52         # Get associated key details
53         keys = Keys(self.api, key_ids)
54
55         for key in keys:
56             key.delete()
57
58         return 1