Merge remote-tracking branch 'origin/pycurl' into planetlab-4_0-branch
[plcapi.git] / trunk / PLC / Methods / AdmDeleteAllPersonKeys.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 AdmDeleteAllPersonKeys(Method):
9     """
10     Deprecated. Functionality can be implemented with GetPersons and
11     DeleteKey.
12
13     Deletes all of the keys associated with an account. Non-admins may
14     only delete their own keys.
15
16     Non-admins may only delete their own keys.
17
18     Returns 1 if successful, faults otherwise.
19     """
20
21     status = "deprecated"
22
23     roles = ['admin', 'pi', 'tech', 'user']
24
25     accepts = [
26         Auth(),
27         Mixed(Person.fields['person_id'],
28               Person.fields['email'])
29         ]
30
31     returns = Parameter(int, '1 if successful')
32
33     def call(self, auth, person_id_or_email):
34         # Get account information
35         persons = Persons(self.api, [person_id_or_email])
36         if not persons:
37             raise PLCInvalidArgument, "No such account"
38
39         person = persons[0]
40
41         if 'admin' not in self.caller['roles']:
42             if self.caller['person_id'] != person['person_id']:
43                 raise PLCPermissionDenied, "Not allowed to update specified account"
44
45         key_ids = person['key_ids']
46         if not key_ids:
47             return 1
48
49         # Get associated key details
50         keys = Keys(self.api, key_ids)
51
52         for key in keys:
53             key.delete()
54
55         return 1