permission checking on node tags factorized in Node.caller_may_write_tag
[plcapi.git] / PLC / Methods / AddNodeTag.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 Node, Nodes
11 from PLC.TagTypes import TagType, TagTypes
12 from PLC.NodeTags import NodeTag, NodeTags
13
14 class AddNodeTag(Method):
15     """
16     Sets the specified tag for the specified node
17     to the specified value.
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 the new node_tag_id (> 0) if successful, faults
24     otherwise.
25     """
26
27     roles = ['admin', 'pi', 'tech', 'user']
28
29     accepts = [
30         Auth(),
31         # no other way to refer to a node
32         Mixed(Node.fields['node_id'],
33               Node.fields['hostname']),
34         Mixed(TagType.fields['tag_type_id'],
35               TagType.fields['tagname']),
36         NodeTag.fields['value'],
37         ]
38
39     returns = Parameter(int, 'New node_tag_id (> 0) if successful')
40
41     def call(self, auth, node_id, tag_type_id_or_name, value):
42         nodes = Nodes(self.api, [node_id])
43         if not nodes:
44             raise PLCInvalidArgument, "No such node %r"%node_id
45         node = nodes[0]
46
47         tag_types = TagTypes(self.api, [tag_type_id_or_name])
48         if not tag_types:
49             raise PLCInvalidArgument, "No such node tag type %r"%tag_type_id_or_name
50         tag_type = tag_types[0]
51
52         # checks for existence - does not allow several different tags
53         conflicts = NodeTags(self.api,
54                                         {'node_id':node['node_id'],
55                                          'tag_type_id':tag_type['tag_type_id']})
56
57         if len(conflicts) :
58             raise PLCInvalidArgument, "Node %d already has tag %d"%(node['node_id'],
59                                                                     tag_type['tag_type_id'])
60
61         # check authorizations
62         node.caller_may_write_tag(self.api,self.caller,tag_type)
63
64         node_tag = NodeTag(self.api)
65         node_tag['node_id'] = node['node_id']
66         node_tag['tag_type_id'] = tag_type['tag_type_id']
67         node_tag['value'] = value
68
69         node_tag.sync()
70         self.object_ids = [node_tag['node_tag_id']]
71
72         return node_tag['node_tag_id']