permission checking on node tags factorized in Node.caller_may_write_tag
[plcapi.git] / PLC / Methods / UpdateInterfaceTag.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 UpdateInterfaceTag(Method):
16     """
17     Updates the value of an existing interface setting
18
19     Admins have full access.  Non-admins need to 
20     (1) have at least one of the roles attached to the tagtype, 
21     and (2) belong in the same site as the tagged subject.
22
23     Returns 1 if successful, faults otherwise.
24     """
25
26     roles = ['admin', 'pi', 'tech', 'user']
27
28     accepts = [
29         Auth(),
30         InterfaceTag.fields['interface_tag_id'],
31         InterfaceTag.fields['value']
32         ]
33
34     returns = Parameter(int, '1 if successful')
35
36     def call(self, auth, interface_tag_id, value):
37         interface_tags = InterfaceTags(self.api, [interface_tag_id])
38         if not interface_tags:
39             raise PLCInvalidArgument, "No such interface setting %r"%interface_tag_id
40         interface_tag = interface_tags[0]
41
42         tag_type_id = interface_tag['tag_type_id']
43         tag_type = TagTypes (self.api,[tag_type_id])[0]
44
45         interfaces = Interfaces (self.api, interface_tag['interface_id'])
46         if not interfaces:
47             raise PLCInvalidArgument, "No such interface %d"%interface_tag['interface_id']
48         interface=interfaces[0]
49
50         # check authorizations
51         if 'admin' in self.caller['roles']:
52             pass
53         elif not AuthorizeHelpers.caller_may_access_tag_type (self.api, self.caller, tag_type):
54             raise PLCPermissionDenied, "%s, forbidden tag %s"%(self.name,tag_type['tagname'])
55         elif AuthorizeHelpers.interface_belongs_to_person (self.api, interface, self.caller):
56             pass
57         else:
58             raise PLCPermissionDenied, "%s: you must belong in the same site as subject interface"%self.name
59
60
61         interface_tag['value'] = value
62         interface_tag.sync()
63
64         self.object_ids = [interface_tag['interface_tag_id']]
65         return 1