- Change .py files to use 4-space indents and no hard tab characters.
[plcapi.git] / PLC / Methods / UpdateInterfaceTag.py
1 # $Id$
2 # $URL$
3 #
4 # Thierry Parmentelat - INRIA
5 #
6 # $Revision$
7 #
8
9 from PLC.Faults import *
10 from PLC.Method import Method
11 from PLC.Parameter import Parameter, Mixed
12 from PLC.Auth import Auth
13
14 from PLC.InterfaceTags import InterfaceTag, InterfaceTags
15 from PLC.Interfaces import Interface, Interfaces
16
17 from PLC.Nodes import Nodes
18 from PLC.Sites import Sites
19
20 class UpdateInterfaceTag(Method):
21     """
22     Updates the value of an existing interface setting
23
24     Access rights depend on the tag type.
25
26     Returns 1 if successful, faults otherwise.
27     """
28
29     roles = ['admin', 'pi', 'tech', 'user']
30
31     accepts = [
32         Auth(),
33         InterfaceTag.fields['interface_tag_id'],
34         InterfaceTag.fields['value']
35         ]
36
37     returns = Parameter(int, '1 if successful')
38
39     object_type = 'Interface'
40
41     def call(self, auth, interface_tag_id, value):
42         interface_tags = InterfaceTags(self.api, [interface_tag_id])
43         if not interface_tags:
44             raise PLCInvalidArgument, "No such interface setting %r"%interface_tag_id
45         interface_tag = interface_tags[0]
46
47         ### reproducing a check from UpdateSliceTag, looks dumb though
48         interfaces = Interfaces(self.api, [interface_tag['interface_id']])
49         if not interfaces:
50             raise PLCInvalidArgument, "No such interface %r"%interface_tag['interface_id']
51         interface = interfaces[0]
52
53         assert interface_tag['interface_tag_id'] in interface['interface_tag_ids']
54
55         # check permission : it not admin, is the user affiliated with the right site
56         if 'admin' not in self.caller['roles']:
57             # locate node
58             node = Nodes (self.api,[interface['node_id']])[0]
59             # locate site
60             site = Sites (self.api, [node['site_id']])[0]
61             # check caller is affiliated with this site
62             if self.caller['person_id'] not in site['person_ids']:
63                 raise PLCPermissionDenied, "Not a member of the hosting site %s"%site['abbreviated_site']
64
65             required_min_role = tag_type ['min_role_id']
66             if required_min_role is not None and \
67                     min(self.caller['role_ids']) > required_min_role:
68                 raise PLCPermissionDenied, "Not allowed to modify the specified interface setting, requires role %d",required_min_role
69
70         interface_tag['value'] = value
71         interface_tag.sync()
72
73         self.object_ids = [interface_tag['interface_tag_id']]
74         return 1