svn keywords
[plcapi.git] / PLC / Methods / AddRoleToPerson.py
1 # $Id$
2 # $URL$
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 Auth
8 from PLC.Roles import Role, Roles
9
10 class AddRoleToPerson(Method):
11     """
12     Grants the specified role to the person.
13     
14     PIs can only grant the tech and user roles to users and techs at
15     their sites. Admins can grant any role to any user.
16
17     Returns 1 if successful, faults otherwise.
18     """
19
20     roles = ['admin', 'pi']
21
22     accepts = [
23         Auth(),
24         Mixed(Role.fields['role_id'],
25               Role.fields['name']),
26         Mixed(Person.fields['person_id'],
27               Person.fields['email']),
28         ]
29
30     returns = Parameter(int, '1 if successful')
31
32     def call(self, auth, role_id_or_name, person_id_or_email):
33         # Get role
34         roles = Roles(self.api, [role_id_or_name])
35         if not roles:
36             raise PLCInvalidArgument, "Invalid role '%s'" % unicode(role_id_or_name)
37         role = roles[0]
38
39         # Get account information
40         persons = Persons(self.api, [person_id_or_email])
41         if not persons:
42             raise PLCInvalidArgument, "No such account"
43         person = persons[0]
44
45         if person['peer_id'] is not None:
46             raise PLCInvalidArgument, "Not a local account"
47
48         # Authenticated function
49         assert self.caller is not None
50
51         # Check if we can update this account
52         if not self.caller.can_update(person):
53             raise PLCPermissionDenied, "Not allowed to update specified account"
54
55         # Can only grant lesser (higher) roles to others
56         if 'admin' not in self.caller['roles'] and \
57            role['role_id'] <= min(self.caller['role_ids']):
58             raise PLCInvalidArgument, "Not allowed to grant that role"
59
60         if role['role_id'] not in person['role_ids']:
61             person.add_role(role)
62
63         self.event_objects = {'Person': [person['person_id']],
64                               'Role': [role['role_id']]}
65         self.message = "Role %d granted to person %d" % \
66                        (role['role_id'], person['person_id'])
67
68         return 1