reviewing the tags permission system
[plcapi.git] / PLC / Methods / AddSiteTag.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 Site, Sites
10 from PLC.Nodes import Nodes
11 from PLC.TagTypes import TagType, TagTypes
12 from PLC.SiteTags import SiteTag, SiteTags
13
14 from PLC.AuthorizeHelpers import AuthorizeHelpers
15
16 class AddSiteTag(Method):
17     """
18     Sets the specified setting for the specified site
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 site_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 site
34         SiteTag.fields['site_id'],
35         Mixed(TagType.fields['tag_type_id'],
36               TagType.fields['tagname']),
37         SiteTag.fields['value'],
38         ]
39
40     returns = Parameter(int, 'New site_tag_id (> 0) if successful')
41
42     def call(self, auth, site_id, tag_type_id_or_name, value):
43         sites = Sites(self.api, [site_id])
44         if not sites:
45             raise PLCInvalidArgument, "No such site %r"%site_id
46         site = sites[0]
47
48         tag_types = TagTypes(self.api, [tag_type_id_or_name])
49         if not tag_types:
50             raise PLCInvalidArgument, "No such tag type %r"%tag_type_id_or_name
51         tag_type = tag_types[0]
52
53         # checks for existence - does not allow several different settings
54         conflicts = SiteTags(self.api,
55                              {'site_id':site['site_id'],
56                               'tag_type_id':tag_type['tag_type_id']})
57
58         if len(conflicts) :
59             raise PLCInvalidArgument, "Site %d already has setting %d"%(site['site_id'],
60                                                                         tag_type['tag_type_id'])
61
62         # check authorizations
63         if 'admin' in self.caller['roles']:
64             pass
65         elif not AuthorizeHelpers.person_access_tag_type (self.api, self.caller, tag_type):
66             raise PLCPermissionDenied, "%s, no permission to use this tag type"%self.name
67         elif AuthorizeHelpers.person_belongs_to_site (self.api, self.caller, site):
68             pass
69         else:
70             raise PLCPermissionDenied, "%s: you must be part of the subject site"%self.name
71             
72         site_tag = SiteTag(self.api)
73         site_tag['site_id'] = site['site_id']
74         site_tag['tag_type_id'] = tag_type['tag_type_id']
75         site_tag['value'] = value
76
77         site_tag.sync()
78         self.object_ids = [site_tag['site_tag_id']]
79
80         return site_tag['site_tag_id']