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