Initial checkin of new API implementation
[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 PasswordAuth
6
7 class AdmGetPersonRoles(Method):
8     """
9     Return the roles that the specified person has as a struct:
10
11     {'10': 'admin', '30': 'user', '20': 'pi', '40': 'tech'}
12
13     Admins can get the roles for any user. PIs can only get the roles
14     for members of their sites. All others may only get their own
15     roles.
16
17     Note that because of XML-RPC marshalling limitations, the keys to
18     this struct are string representations of the integer role
19     identifiers.
20     """
21
22     roles = ['admin', 'pi', 'user', 'tech']
23
24     accepts = [
25         PasswordAuth(),
26         Mixed(Person.fields['person_id'],
27               Person.fields['email'])
28         ]
29
30     returns = dict
31
32     # Stupid return type, and can get now roles through
33     # AdmGetPersons().
34     status = "useless"
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.values()[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))