renaming SliceAttribute into SliceTag and InterfaceSetting into InterfaceTag
[plcapi.git] / PLC / Methods / UpdateNodeTag.py
1 # $Id#
2 #
3 # Thierry Parmentelat - INRIA
4 #
5 # $Revision: 9423 $
6 #
7
8 from PLC.Faults import *
9 from PLC.Method import Method
10 from PLC.Parameter import Parameter, Mixed
11 from PLC.Auth import Auth
12
13 from PLC.NodeTags import NodeTag, NodeTags
14 from PLC.Nodes import Node, Nodes
15
16 from PLC.Nodes import Nodes
17 from PLC.Sites import Sites
18
19 class UpdateNodeTag(Method):
20     """
21     Updates the value of an existing node tag
22
23     Access rights depend on the node tag type.
24
25     Returns 1 if successful, faults otherwise.
26     """
27
28     roles = ['admin', 'pi', 'tech', 'user']
29
30     accepts = [
31         Auth(),
32         NodeTag.fields['node_tag_id'],
33         NodeTag.fields['tagvalue']
34         ]
35
36     returns = Parameter(int, '1 if successful')
37
38     object_type = 'Node'
39
40     def call(self, auth, node_tag_id, value):
41         node_tags = NodeTags(self.api, [node_tag_id])
42         if not node_tags:
43             raise PLCInvalidArgument, "No such node tag %r"%node_tag_id
44         node_tag = node_tags[0]
45
46         ### reproducing a check from UpdateSliceTag, looks dumb though
47         nodes = Nodes(self.api, [node_tag['node_id']])
48         if not nodes:
49             raise PLCInvalidArgument, "No such node %r"%node_tag['node_id']
50         node = nodes[0]
51
52         assert node_tag['node_tag_id'] in node['tag_ids']
53
54         # check permission : it not admin, is the user affiliated with the right site
55         if 'admin' not in self.caller['roles']:
56             # locate node
57             node = Nodes (self.api,[node['node_id']])[0]
58             # locate site
59             site = Sites (self.api, [node['site_id']])[0]
60             # check caller is affiliated with this site
61             if self.caller['person_id'] not in site['person_ids']:
62                 raise PLCPermissionDenied, "Not a member of the hosting site %s"%site['abbreviated_site']
63             
64             required_min_role = tag_type ['min_role_id']
65             if required_min_role is not None and \
66                     min(self.caller['role_ids']) > required_min_role:
67                 raise PLCPermissionDenied, "Not allowed to modify the specified node tag, requires role %d",required_min_role
68
69         node_tag['tagvalue'] = value
70         node_tag.sync()
71
72         self.object_ids = [node_tag['node_tag_id']]
73         return 1