2 # Thierry Parmentelat - INRIA
4 from PLC.Faults import *
5 from PLC.Persons import Persons,Person
6 from PLC.Sites import Sites,Site
7 from PLC.Nodes import Nodes,Node
9 class AuthorizeHelpers:
12 def person_tag_type_common_roles (api, person, tag_type):
13 return list (set(person['roles']).intersection(set(tag_type['roles'])))
16 def caller_may_access_tag_type (api, caller, tag_type):
17 if isinstance(caller,Person):
18 return len(AuthorizeHelpers.person_tag_type_common_roles(api,caller,tag_type))!=0
19 elif isinstance(caller,Node):
20 return 'node' in tag_type['roles']
22 raise PLCInvalidArgument, "caller_may_access_tag_type - unexpected arg"
25 def person_may_access_person (api, caller_person, subject_person):
26 # keep it simple for now - could be a bit more advanced for PIs maybe
27 try: return caller_person['person_id'] == subject_person['person_id']
31 def person_in_site (api, person, site):
32 return site['site_id'] in person['site_ids']
35 def person_in_slice (api, caller_person, slice):
36 return caller_person['person_id'] in slice['person_ids']
39 def slice_in_site (api, slice, site):
40 return slice['site_id']==site['site_id']
43 def node_id_in_slice (api, node_id_or_hostname, slice):
44 if isinstance (node_id_or_hostname,int):
45 return node_id_or_hostname in slice['node_ids']
47 try: return Nodes(api,node_id_or_hostname)[0]['node_id'] in slice['node_ids']
51 def node_id_in_site (api, node_id_or_hostname, site):
52 if isinstance (node_id_or_hostname,int):
53 return node_id_or_hostname in site['node_ids']
55 try: return Nodes(api,node_id_or_hostname)[0]['node_id'] in site['node_ids']
60 def node_match_id (api, node, node_id_or_hostname):
61 if isinstance (node_id_or_hostname,int):
62 return node['node_id']==node_id_or_hostname
64 return node['hostname']==node_id_or_hostname
67 def interface_belongs_to_person (api,interface, person):
69 node=Nodes(api,[interface['node_id']])[0]
70 return AuthorizeHelpers.node_belongs_to_person (api, node, person)
75 def node_belongs_to_person (api, node, person):
77 site=Sites(api,[node['site_id']])[0]
78 return AuthorizeHelpers.person_in_site (api, person, site)
82 # does the slice belong to the site that the (pi) user is in ?
84 def slice_belongs_to_pi (api, slice, pi):
85 return slice['site_id'] in pi['site_ids']