024b93cee59267fea14d680e8a9c50dd4b18a420
[plcapi.git] / PLC / Methods / AdmGetPersonRoles.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.Auth import Auth
6
7 class AdmGetPersonRoles(Method):
8     """
9     Deprecated. See GetPersons.
10
11     Return the roles that the specified person has as a struct:
12
13     {'10': 'admin', '30': 'user', '20': 'pi', '40': 'tech'}
14
15     Admins can get the roles for any user. PIs can only get the roles
16     for members of their sites. All others may only get their own
17     roles.
18
19     Note that because of XML-RPC marshalling limitations, the keys to
20     this struct are string representations of the integer role
21     identifiers.
22     """
23
24     status = "deprecated"
25
26     roles = ['admin', 'pi', 'user', 'tech']
27
28     accepts = [
29         Auth(),
30         Mixed(Person.fields['person_id'],
31               Person.fields['email'])
32         ]
33
34     returns = dict
35
36     def call(self, auth, person_id_or_email):
37         # Get account information
38         persons = Persons(self.api, [person_id_or_email])
39         if not persons:
40             raise PLCInvalidArgument, "No such account"
41
42         person = persons[0]
43
44         # Authenticated function
45         assert self.caller is not None
46
47         # Check if we can view this account
48         if not self.caller.can_view(person):
49             raise PLCPermissionDenied, "Not allowed to view specified account"
50
51         # Stringify the keys!
52         role_ids = map(str, person['role_ids'])
53         roles = person['roles']
54
55         return dict(zip(role_ids, roles))