====
[plcapi.git] / PLC / Methods / UpdateIlink.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.Ilinks import Ilink, Ilinks
11 from PLC.Interfaces import Interface, Interfaces
12 from PLC.TagTypes import TagType, TagTypes
13 from PLC.Sites import Sites
14
15 from PLC.AuthorizeHelpers import AuthorizeHelpers
16
17 class UpdateIlink(Method):
18     """
19     Updates the value of an existing ilink
20
21     Access rights depend on the tag type.
22
23     Returns 1 if successful, faults otherwise.
24     """
25
26     roles = ['admin', 'pi', 'tech', 'user']
27
28     accepts = [
29         Auth(),
30         Ilink.fields['ilink_id'],
31         Ilink.fields['value']
32         ]
33
34     returns = Parameter(int, '1 if successful')
35
36     object_type = 'Interface'
37
38     def call(self, auth, ilink_id, value):
39         ilinks = Ilinks(self.api, [ilink_id])
40         if not ilinks:
41             raise PLCInvalidArgument, "No such ilink %r"%ilink_id
42         ilink = ilinks[0]
43
44         src_if=Interfaces(self.api,ilink['src_interface_id'])[0]
45         dst_if=Interfaces(self.api,ilink['dst_interface_id'])[0]
46         tag_type_id = ilink['tag_type_id']
47         tag_type = TagTypes (self.api,[tag_type_id])[0]
48
49         # check authorizations
50         if 'admin' in self.caller['roles']:
51             pass
52         elif not AuthorizeHelpers.caller_may_access_tag_type (self.api, self.caller, tag_type):
53             raise PLCPermissionDenied, "%s, forbidden tag %s"%(self.name,tag_type['tagname'])
54         elif AuthorizeHelpers.interface_belongs_to_person (self.api, src_if, self.caller):
55             pass
56         elif src_if_id != dst_if_id and AuthorizeHelpers.interface_belongs_to_person (self.api, dst_if, self.caller):
57             pass
58         else:
59             raise PLCPermissionDenied, "%s: you must own either the src or dst interface"%self.name
60             
61         ilink['value'] = value
62         ilink.sync()
63
64         self.object_ids = [ilink['src_interface_id'],ilink['dst_interface_id']]
65         return 1