This commit was manufactured by cvs2svn to create branch
[plcapi.git] / PLC / Methods / AdmIsPersonInRole.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 from PLC.Roles import Role, Roles
7
8 class AdmIsPersonInRole(Method):
9     """
10     Deprecated. Functionality can be implemented with GetPersons.
11
12     Returns 1 if the specified account has the specified role, 0
13     otherwise. This function differs from AdmGetPersonRoles() in that
14     any authorized user can call it. It is currently restricted to
15     verifying PI roles.
16     """
17
18     status = "deprecated"
19
20     roles = ['admin', 'pi', 'user', 'tech']
21
22     accepts = [
23         Auth(),
24         Mixed(Person.fields['person_id'],
25               Person.fields['email']),
26         Mixed(Parameter(int, "Role identifier"),
27               Parameter(str, "Role name"))
28         ]
29
30     returns = Parameter(int, "1 if account has role, 0 otherwise")
31
32     object_type = 'Person'
33
34     def call(self, auth, person_id_or_email, role_id_or_name):
35         # This is a totally fucked up function. I have no idea why it
36         # exists or who calls it, but here is how it is supposed to
37         # work.
38
39         # Only allow PI roles to be checked
40         roles = {}
41         for role in Roles(self.api):
42             roles[role['role_id']] = role['name']
43             roles[role['name']] = role['role_id']
44
45         if role_id_or_name not in roles:
46             raise PLCInvalidArgument, "Invalid role identifier or name"
47
48         if isinstance(role_id_or_name, int):
49             role_id = role_id_or_name
50         else:
51             role_id = roles[role_id_or_name]
52
53         if roles[role_id] != "pi":
54             raise PLCInvalidArgument, "Only the PI role may be checked"
55
56         # Get account information
57         persons = Persons(self.api, [person_id_or_email])
58
59         # Rather than raise an error, and indicate whether or not
60         # the person is real, return 0.
61         if not persons:
62             return 0
63
64         person = persons[0]
65
66         if role_id in person['role_ids']:
67             return 1
68
69         return 0