Initial checkin of new API implementation
[plcapi.git] / PLC / Methods / AdmIsPersonInRole.py
1 from types import StringTypes
2
3 from PLC.Faults import *
4 from PLC.Method import Method
5 from PLC.Parameter import Parameter, Mixed
6 from PLC.Persons import Person, Persons
7 from PLC.Auth import PasswordAuth
8 from PLC.Roles import Roles
9
10 class AdmIsPersonInRole(Method):
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     roles = ['admin', 'pi', 'user', 'tech']
19
20     accepts = [
21         PasswordAuth(),
22         Mixed(Person.fields['person_id'],
23               Person.fields['email']),
24         Roles.fields['role_id']
25         ]
26
27     returns = Parameter(int, "1 if account has role, 0 otherwise")
28
29     status = "useless"
30
31     def call(self, auth, person_id_or_email, role_id):
32         # This is a totally fucked up function. I have no idea why it
33         # exists or who calls it, but here is how it is supposed to
34         # work.
35
36         # Only allow PI roles to be checked
37         roles = Roles(self.api)
38         if not roles.has_key(role_id) or roles[role_id] != "pi":
39             raise PLCInvalidArgument, "Only the PI role may be checked"
40
41         # Get account information
42         persons = Persons(self.api, [person_id_or_email])
43
44         # Rather than raise an error, and indicate whether or not
45         # the person is real, return 0.
46         if not persons:
47             return 0
48
49         person = persons.values()[0]
50
51         if role_id in person['role_ids']:
52             return 1
53
54         return 0