c7f63a304f2bc3b6027223e94bd4973a767068d9
[plcapi.git] / PLC / Methods / UpdateSiteTag.py
1 # $Id: UpdateSiteTag.py 14587 2009-07-19 13:18:50Z thierry $
2 # $URL: http://svn.planet-lab.org/svn/PLCAPI/tags/PLCAPI-4.3-27/PLC/Methods/UpdateSiteTag.py $
3 #
4 # $Revision: 14587 $
5 #
6
7 from PLC.Faults import *
8 from PLC.Method import Method
9 from PLC.Parameter import Parameter, Mixed
10 from PLC.Auth import Auth
11
12 from PLC.SiteTags import SiteTag, SiteTags
13 from PLC.Sites import Site, Sites
14
15 from PLC.Nodes import Nodes
16 from PLC.Sites import Sites
17
18 class UpdateSiteTag(Method):
19     """
20     Updates the value of an existing site setting
21
22     Access rights depend on the tag type.
23
24     Returns 1 if successful, faults otherwise.
25     """
26
27     roles = ['admin', 'pi', 'tech', 'user']
28
29     accepts = [
30         Auth(),
31         SiteTag.fields['site_tag_id'],
32         SiteTag.fields['value']
33         ]
34
35     returns = Parameter(int, '1 if successful')
36
37     object_type = 'Site'
38
39     def call(self, auth, site_tag_id, value):
40         site_tags = SiteTags(self.api, [site_tag_id])
41         if not site_tags:
42             raise PLCInvalidArgument, "No such site setting %r"%site_tag_id
43         site_tag = site_tags[0]
44
45         ### reproducing a check from UpdateSliceTag, looks dumb though
46         sites = Sites(self.api, [site_tag['site_id']])
47         if not sites:
48             raise PLCInvalidArgument, "No such site %r"%site_tag['site_id']
49         site = sites[0]
50
51         assert site_tag['site_tag_id'] in site['site_tag_ids']
52
53         # check permission : it not admin, is the user affiliated with the right site
54         if 'admin' not in self.caller['roles']:
55             # check caller is affiliated with this site
56             if self.caller['person_id'] not in site['person_ids']:
57                 raise PLCPermissionDenied, "Not a member of the hosting site %s"%site['abbreviated_site']
58             
59             required_min_role = tag_type ['min_role_id']
60             if required_min_role is not None and \
61                     min(self.caller['role_ids']) > required_min_role:
62                 raise PLCPermissionDenied, "Not allowed to modify the specified site setting, requires role %d",required_min_role
63
64         site_tag['value'] = value
65         site_tag.sync()
66
67         self.object_ids = [site_tag['site_tag_id']]
68         return 1