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
8 class AddRoleToPerson(Method):
10 Grants the specified role to the person.
12 PIs can only grant the tech and user roles to users and techs at
13 their sites. Admins can grant any role to any user.
15 Returns 1 if successful, faults otherwise.
18 roles = ['admin', 'pi']
22 Mixed(Role.fields['role_id'],
24 Mixed(Person.fields['person_id'],
25 Person.fields['email']),
28 returns = Parameter(int, '1 if successful')
31 object_type = 'Person'
34 def call(self, auth, role_id_or_name, person_id_or_email):
37 for role_id, role in Roles(self.api).iteritems():
38 roles[role_id] = role['name']
39 roles[role['name']] = role_id
41 if role_id_or_name not in roles:
42 raise PLCInvalidArgument, "Invalid role identifier or name"
44 if isinstance(role_id_or_name, int):
45 role_id = role_id_or_name
47 role_id = roles[role_id_or_name]
49 # Get account information
50 persons = Persons(self.api, [person_id_or_email])
52 raise PLCInvalidArgument, "No such account"
54 person = persons.values()[0]
56 # Authenticated function
57 assert self.caller is not None
59 # Check if we can update this account
60 if not self.caller.can_update(person):
61 raise PLCPermissionDenied, "Not allowed to update specified account"
63 # Can only grant lesser (higher) roles to others
64 if 'admin' not in self.caller['roles'] and \
65 role_id <= min(self.caller['role_ids']):
66 raise PLCInvalidArgument, "Not allowed to grant that role"
68 if role_id not in person['role_ids']:
69 person.add_role(role_id)
71 self.object_ids = [person['person_id']]