review (and fix) the way we retrieve the subject object
[plcapi.git] / PLC / Methods / DeleteNodeTag.py
1 #
2 # Thierry Parmentelat - INRIA
3 #
4
5 from PLC.Faults import *
6 from PLC.Method import Method
7 from PLC.Parameter import Parameter, Mixed
8 from PLC.Auth import Auth
9
10 from PLC.Sites import Site, Sites
11 from PLC.Nodes import Node, Nodes
12 from PLC.TagTypes import TagType, TagTypes
13 from PLC.NodeTags import NodeTag, NodeTags
14
15 from PLC.AuthorizeHelpers import AuthorizeHelpers
16
17 class DeleteNodeTag(Method):
18     """
19     Deletes the specified node tag
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 1 if successful, faults otherwise.
26     """
27
28     roles = ['admin', 'pi', 'user', 'tech']
29
30     accepts = [
31         Auth(),
32         NodeTag.fields['node_tag_id']
33         ]
34
35     returns = Parameter(int, '1 if successful')
36
37     def call(self, auth, node_tag_id):
38         node_tags = NodeTags(self.api, [node_tag_id])
39         if not node_tags:
40             raise PLCInvalidArgument, "No such node tag %r"%node_tag_id
41         node_tag = node_tags[0]
42
43         tag_type_id = node_tag['tag_type_id']
44         tag_type = TagTypes (self.api,[tag_type_id])[0]
45
46         nodes = Nodes (self.api, node_tag['node_id'])
47         if not nodes:
48             raise PLCInvalidArgument, "No such node %d"%node_tag['node_id']
49         node=nodes[0]
50
51         # check authorizations
52         if 'admin' in self.caller['roles']:
53             pass
54         elif not AuthorizeHelpers.caller_may_access_tag_type (self.api, self.caller, tag_type):
55             raise PLCPermissionDenied, "%s, forbidden tag %s"%(self.name,tag_type['tagname'])
56         elif AuthorizeHelpers.node_belongs_to_person (self.api, node, self.caller):
57             pass
58         else:
59             raise PLCPermissionDenied, "%s: you must belong in the same site as subject node"%self.name
60
61         node_tag.delete()
62         self.object_ids = [node_tag['node_tag_id']]
63
64         return 1