1506e9fcbb763276994519a7ceb8d49d3a9c936c
[plcapi.git] / PLC / Methods / DeleteRoleFromPerson.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 DeleteRoleFromPerson(Method):
11     """
12     Deletes the specified role from the person.
13     
14     PIs can only revoke the tech and user roles from users and techs
15     at their sites. ins can revoke any role from 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 revoke lesser (higher) roles from others
56         if 'admin' not in self.caller['roles'] and \
57            role['role_id'] <= min(self.caller['role_ids']):
58             raise PLCPermissionDenied, "Not allowed to revoke that role"
59
60         if role['role_id'] in person['role_ids']:
61             person.remove_role(role)
62         
63         # Logging variables
64         self.event_objects = {'Person': [person['person_id']],
65                               'Role': [role['role_id']]}        
66         self.message = "Role %d revoked from person %d" % \
67                        (role['role_id'], person['person_id'])
68
69         return 1