reviewing the tags permission system
[plcapi.git] / PLC / Methods / AddInterfaceTag.py
1 #
2 # Thierry Parmentelat - INRIA
3 #
4 from PLC.Faults import *
5 from PLC.Method import Method
6 from PLC.Parameter import Parameter, Mixed
7 from PLC.Auth import Auth
8
9 from PLC.Sites import Sites
10 from PLC.Nodes import Nodes
11 from PLC.Interfaces import Interface, Interfaces
12 from PLC.TagTypes import TagType, TagTypes
13 from PLC.InterfaceTags import InterfaceTag, InterfaceTags
14
15 class AddInterfaceTag(Method):
16     """
17     Sets the specified setting for the specified interface
18     to the specified value.
19
20     Admins have full access.  Non-admins need to 
21     (1) have at least one of the roles attached to the tagtype, 
22     and (2) belong in the same site as the tagged subject.
23
24     Returns the new interface_tag_id (> 0) if successful, faults
25     otherwise.
26     """
27
28     roles = ['admin', 'pi', 'tech', 'user']
29
30     accepts = [
31         Auth(),
32         # no other way to refer to a interface
33         InterfaceTag.fields['interface_id'],
34         Mixed(TagType.fields['tag_type_id'],
35               TagType.fields['tagname']),
36         InterfaceTag.fields['value'],
37         ]
38
39     returns = Parameter(int, 'New interface_tag_id (> 0) if successful')
40
41     def call(self, auth, interface_id, tag_type_id_or_name, value):
42         interfaces = Interfaces(self.api, [interface_id])
43         if not interfaces:
44             raise PLCInvalidArgument, "No such interface %r"%interface_id
45         interface = interfaces[0]
46
47         tag_types = TagTypes(self.api, [tag_type_id_or_name])
48         if not tag_types:
49             raise PLCInvalidArgument, "No such tag type %r"%tag_type_id_or_name
50         tag_type = tag_types[0]
51
52         # checks for existence - does not allow several different settings
53         conflicts = InterfaceTags(self.api,
54                                         {'interface_id':interface['interface_id'],
55                                          'tag_type_id':tag_type['tag_type_id']})
56
57         if len(conflicts) :
58             raise PLCInvalidArgument, "Interface %d already has setting %d"%(interface['interface_id'],
59                                                                                tag_type['tag_type_id'])
60
61         # check authorizations
62         if 'admin' in self.caller['roles']:
63             pass
64         elif not AuthorizeHelpers.person_access_tag_type (self.api, self.caller, tag_type):
65             raise PLCPermissionDenied, "%s, no permission to use this tag type"%self.name
66         elif AuthorizeHelpers.interface_belongs_to_person (self.api, interface, self.caller):
67             pass
68         else:
69             raise PLCPermissionDenied, "%s: you must belong in the same site as subject interface"%self.name
70         
71
72         interface_tag = InterfaceTag(self.api)
73         interface_tag['interface_id'] = interface['interface_id']
74         interface_tag['tag_type_id'] = tag_type['tag_type_id']
75         interface_tag['value'] = value
76
77         interface_tag.sync()
78         self.object_ids = [interface_tag['interface_tag_id']]
79
80         return interface_tag['interface_tag_id']