show tagname when permission is denied
[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 from PLC.AuthorizeHelpers import AuthorizeHelpers
15
16 class AddNodeTag(Method):
17     """
18     Sets the specified tag for the specified node
19     to the specified value.
20
21     Admins have full access.  Non-admins need to 
22     (1) have at least one of the roles attached to the tagtype, 
23     and (2) belong in the same site as the tagged subject.
24
25     Returns the new node_tag_id (> 0) if successful, faults
26     otherwise.
27     """
28
29     roles = ['admin', 'pi', 'tech', 'user']
30
31     accepts = [
32         Auth(),
33         # no other way to refer to a node
34         Mixed(Node.fields['node_id'],
35               Node.fields['hostname']),
36         Mixed(TagType.fields['tag_type_id'],
37               TagType.fields['tagname']),
38         NodeTag.fields['value'],
39         ]
40
41     returns = Parameter(int, 'New node_tag_id (> 0) if successful')
42
43     def call(self, auth, node_id, tag_type_id_or_name, value):
44         nodes = Nodes(self.api, [node_id])
45         if not nodes:
46             raise PLCInvalidArgument, "No such node %r"%node_id
47         node = nodes[0]
48
49         tag_types = TagTypes(self.api, [tag_type_id_or_name])
50         if not tag_types:
51             raise PLCInvalidArgument, "No such node tag type %r"%tag_type_id_or_name
52         tag_type = tag_types[0]
53
54         # checks for existence - does not allow several different tags
55         conflicts = NodeTags(self.api,
56                                         {'node_id':node['node_id'],
57                                          'tag_type_id':tag_type['tag_type_id']})
58
59         if len(conflicts) :
60             raise PLCInvalidArgument, "Node %d already has tag %d"%(node['node_id'],
61                                                                     tag_type['tag_type_id'])
62
63
64         # check authorizations
65         if 'admin' in self.caller['roles']:
66             pass
67         elif not AuthorizeHelpers.caller_may_access_tag_type (self.api, self.caller, tag_type):
68             raise PLCPermissionDenied, "%s, forbidden tag %s"%(self.name,tag_type['tagname'])
69         elif AuthorizeHelpers.node_belongs_to_person (self.api, node, self.caller):
70             pass
71         else:
72             raise PLCPermissionDenied, "%s: you must belong in the same site as subject node"%self.name
73
74
75         node_tag = NodeTag(self.api)
76         node_tag['node_id'] = node['node_id']
77         node_tag['tag_type_id'] = tag_type['tag_type_id']
78         node_tag['value'] = value
79
80         node_tag.sync()
81         self.object_ids = [node_tag['node_tag_id']]
82
83         return node_tag['node_tag_id']