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