remove simplejson dependency
[plcapi.git] / PLC / Methods / DeleteRoleFromTagType.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.TagTypes import TagType, TagTypes
5 from PLC.Auth import Auth
6 from PLC.Roles import Role, Roles
7
8 class DeleteRoleFromTagType(Method):
9     """
10     Delete the specified role from the tagtype so that 
11     users with that role can no longer tweak the tag.
12
13     Only admins can call this method
14
15     Returns 1 if successful, faults otherwise.
16     """
17
18     roles = ['admin']
19
20     accepts = [
21         Auth(),
22         Mixed(Role.fields['role_id'],
23               Role.fields['name']),
24         Mixed(TagType.fields['tag_type_id'],
25               TagType.fields['tagname']),
26         ]
27
28     returns = Parameter(int, '1 if successful')
29
30     def call(self, auth, role_id_or_name, tag_type_id_or_tagname):
31         # Get role
32         roles = Roles(self.api, [role_id_or_name])
33         if not roles:
34             raise PLCInvalidArgument, "Invalid role '%s'" % unicode(role_id_or_name)
35         role = roles[0]
36
37         # Get subject tag type
38         tag_types = TagTypes(self.api, [tag_type_id_or_tagname])
39         if not tag_types:
40             raise PLCInvalidArgument, "No such tag type"
41         tag_type = tag_types[0]
42
43         # Authenticated function
44         assert self.caller is not None
45
46         # Only admins 
47         if 'admin' not in self.caller['roles']: 
48             raise PLCInvalidArgument, "Not allowed to revoke that role"
49
50         if role['role_id'] in tag_type['role_ids']:
51             tag_type.remove_role(role)
52
53         # Logging variables
54         self.event_objects = {'TagType': [tag_type['tag_type_id']],
55                               'Role': [role['role_id']]}
56         self.message = "Role %d revoked from tag_type %d" % \
57                        (role['role_id'], tag_type['tag_type_id'])
58
59         return 1