Add 'php/phpxmlrpc/' from commit 'cd5dbb4a511e7a616a61187a5de1a611a9748cbd'
[plcapi.git] / PLC / Methods / AddRoleToTagType.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Auth import Auth
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.TagTypes import TagType, TagTypes
6 from PLC.Roles import Role, Roles
7
8 class AddRoleToTagType(Method):
9     """
10     Add the specified role to the tagtype so that 
11     users with that role can 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 grant that role"
49
50         if role['role_id'] not in tag_type['role_ids']:
51             tag_type.add_role(role)
52
53         self.event_objects = {'TagType': [tag_type['tag_type_id']],
54                               'Role': [role['role_id']]}
55         self.message = "Role %d added to tag_type %d" % \
56             (role['role_id'], tag_type['tag_type_id'])
57
58         return 1