<s> """<STR_LIT>""" <EOL> from pyon . core import bootstrap <EOL> from pyon . core . bootstrap import IonObject <EOL> from pyon . core . exception import BadRequest , Inconsistent <EOL> from pyon . ion . resource import RT , PRED , LCS , OT <EOL> from pyon . util . containers import get_safe , get_ion_ts_millis <EOL> from pyon . util . log import log <EOL> DEFAULT_ACTOR_ID = '<STR_LIT>' <EOL> ORG_MANAGER_ROLE = '<STR_LIT>' <EOL> ORG_MEMBER_ROLE = '<STR_LIT>' <EOL> ION_MANAGER = '<STR_LIT>' <EOL> INSTRUMENT_OPERATOR = '<STR_LIT>' <EOL> OBSERVATORY_OPERATOR = '<STR_LIT>' <EOL> DATA_OPERATOR = '<STR_LIT>' <EOL> def get_role_message_headers ( org_roles ) : <EOL> '''<STR_LIT>''' <EOL> role_header = dict ( ) <EOL> try : <EOL> for org in org_roles : <EOL> role_header [ org ] = [ ] <EOL> for role in org_roles [ org ] : <EOL> role_header [ org ] . append ( role . governance_name ) <EOL> return role_header <EOL> except Exception , e : <EOL> log . error ( e ) <EOL> return role_header <EOL> def build_actor_header ( actor_id = DEFAULT_ACTOR_ID , actor_roles = None ) : <EOL> '''<STR_LIT>''' <EOL> actor_roles = actor_roles or { } <EOL> return { '<STR_LIT>' : actor_id , '<STR_LIT>' : actor_roles } <EOL> def get_actor_header ( actor_id ) : <EOL> '''<STR_LIT>''' <EOL> actor_header = build_actor_header ( DEFAULT_ACTOR_ID , { } ) <EOL> if actor_id : <EOL> try : <EOL> header_roles = find_roles_by_actor ( actor_id ) <EOL> actor_header = build_actor_header ( actor_id , header_roles ) <EOL> except Exception , e : <EOL> log . error ( e ) <EOL> return actor_header <EOL> def has_org_role ( role_header = None , org_governance_name = None , role_name = None ) : <EOL> '''<STR_LIT>''' <EOL> if role_header is None or org_governance_name is None or role_name is None : <EOL> raise BadRequest ( "<STR_LIT>" ) <EOL> if isinstance ( role_name , list ) : <EOL> for role in role_name : <EOL> if has_org_role ( role_header , org_governance_name , role ) : <EOL> return True <EOL> else : <EOL> if role_header . has_key ( org_governance_name ) : <EOL> if role_name in role_header [ org_governance_name ] : <EOL> return True <EOL> return False <EOL> def find_roles_by_actor ( actor_id = None ) : <EOL> '''<STR_LIT>''' <EOL> if actor_id is None or not len ( actor_id ) : <EOL> raise BadRequest ( "<STR_LIT>" ) <EOL> role_dict = dict ( ) <EOL> gov_controller = bootstrap . container_instance . governance_controller <EOL> role_list , _ = gov_controller . rr . find_objects ( actor_id , PRED . hasRole , RT . UserRole ) <EOL> for role in role_list : <EOL> if not role_dict . has_key ( role . org_governance_name ) : <EOL> role_dict [ role . org_governance_name ] = list ( ) <EOL> role_dict [ role . org_governance_name ] . append ( role . governance_name ) <EOL> if not role_dict . has_key ( gov_controller . system_root_org_name ) : <EOL> role_dict [ gov_controller . system_root_org_name ] = list ( ) <EOL> role_dict [ gov_controller . system_root_org_name ] . append ( ORG_MEMBER_ROLE ) <EOL> return role_dict <EOL> def get_web_authentication_actor ( ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> gov_controller = bootstrap . container_instance . governance_controller <EOL> web_actor , _ = gov_controller . rr . find_resources ( RT . ActorIdentity , name = get_safe ( gov_controller . CFG , "<STR_LIT>" , "<STR_LIT>" ) , id_only = False ) <EOL> if not web_actor : <EOL> return None <EOL> return web_actor [ <NUM_LIT:0> ] <EOL> except Exception , e : <EOL> log . error ( e ) <EOL> return None <EOL> def get_system_actor ( ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> gov_controller = bootstrap . container_instance . governance_controller <EOL> system_actor , _ = gov_controller . rr . find_resources ( RT . ActorIdentity , name = get_safe ( gov_controller . CFG , "<STR_LIT>" , "<STR_LIT>" ) , id_only = False ) <EOL> if not system_actor : <EOL> return None <EOL> return system_actor [ <NUM_LIT:0> ] <EOL> except Exception , e : <EOL> log . error ( e ) <EOL> return None <EOL> def is_system_actor ( actor_id ) : <EOL> '''<STR_LIT>''' <EOL> system_actor = get_system_actor ( ) <EOL> if system_actor is not None and system_actor . _id == actor_id : <EOL> return True <EOL> return False <EOL> def get_system_actor_header ( system_actor = None ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> if system_actor is None : <EOL> system_actor = get_system_actor ( ) <EOL> if not system_actor or system_actor is None : <EOL> log . warn ( '<STR_LIT>' ) <EOL> actor_header = get_actor_header ( None ) <EOL> else : <EOL> actor_header = get_actor_header ( system_actor . _id ) <EOL> return actor_header <EOL> except Exception , e : <EOL> log . error ( e ) <EOL> return get_actor_header ( None ) <EOL> def get_valid_resource_commitments ( resource_id = None , actor_id = None ) : <EOL> '''<STR_LIT>''' <EOL> log . debug ( "<STR_LIT>" % ( resource_id , actor_id ) ) <EOL> if resource_id is None : <EOL> return None <EOL> try : <EOL> gov_controller = bootstrap . container_instance . governance_controller <EOL> commitments , _ = gov_controller . rr . find_objects ( resource_id , PRED . hasCommitment , RT . Commitment ) <EOL> if not commitments : <EOL> return None <EOL> cur_time = get_ion_ts_millis ( ) <EOL> commitment_list = [ ] <EOL> for com in commitments : <EOL> if ( actor_id == None or com . consumer == actor_id ) and ( int ( com . expiration ) == <NUM_LIT:0> or ( int ( com . expiration ) > <NUM_LIT:0> and cur_time < int ( com . expiration ) ) ) : <EOL> commitment_list . append ( com ) <EOL> if commitment_list : <EOL> return commitment_list <EOL> except Exception , e : <EOL> log . error ( e ) <EOL> return None <EOL> def has_valid_resource_commitments ( actor_id , resource_id ) : <EOL> '''<STR_LIT>''' <EOL> ret_status = IonObject ( OT . ResourceCommitmentStatus ) <EOL> commitments = get_valid_resource_commitments ( resource_id , actor_id ) <EOL> if commitments is None : <EOL> return ret_status <EOL> ret_status . shared = True <EOL> for com in commitments : <EOL> if com . commitment . exclusive == True : <EOL> ret_status . exclusive = True <EOL> return ret_status <EOL> return ret_status <EOL> def has_valid_shared_resource_commitment ( actor_id = None , resource_id = None ) : <EOL> '''<STR_LIT>''' <EOL> if actor_id is None or resource_id is None : <EOL> raise BadRequest ( '<STR_LIT>' ) <EOL> commitment_status = has_valid_resource_commitments ( actor_id , resource_id ) <EOL> return commitment_status . shared <EOL> def has_valid_exclusive_resource_commitment ( actor_id = None , resource_id = None ) : <EOL> '''<STR_LIT>''' <EOL> if actor_id is None or resource_id is None : <EOL> raise BadRequest ( '<STR_LIT>' ) <EOL> commitment_status = has_valid_resource_commitments ( actor_id , resource_id ) <EOL> if not commitment_status . shared : <EOL> return False <EOL> return commitment_status . exclusive <EOL> def is_resource_owner ( actor_id = None , resource_id = None ) : <EOL> '''<STR_LIT>''' <EOL> if actor_id is None or resource_id is None : <EOL> raise BadRequest ( '<STR_LIT>' ) <EOL> gov_controller = bootstrap . container_instance . governance_controller <EOL> owners = gov_controller . rr . find_objects ( subject = resource_id , predicate = PRED . hasOwner , object_type = RT . ActorIdentity , id_only = True ) <EOL> if actor_id not in owners [ <NUM_LIT:0> ] : <EOL> return False <EOL> return True <EOL> class GovernanceHeaderValues ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , headers , process = None , resource_id_required = True ) : <EOL> '''<STR_LIT>''' <EOL> if not headers or not isinstance ( headers , dict ) or not len ( headers ) : <EOL> raise BadRequest ( '<STR_LIT>' ) <EOL> if headers . has_key ( '<STR_LIT>' ) : <EOL> self . _op = headers [ '<STR_LIT>' ] <EOL> else : <EOL> self . _op = "<STR_LIT>" <EOL> if process is not None and hasattr ( process , '<STR_LIT:name>' ) : <EOL> self . _process_name = process . name <EOL> else : <EOL> if headers . has_key ( '<STR_LIT>' ) : <EOL> if getattr ( headers [ '<STR_LIT>' ] , '<STR_LIT:name>' ) : <EOL> self . _process_name = headers [ '<STR_LIT>' ] . name <EOL> else : <EOL> self . _process_name = "<STR_LIT>" <EOL> else : <EOL> self . _process_name = "<STR_LIT>" <EOL> if headers . has_key ( '<STR_LIT>' ) : <EOL> self . _actor_id = headers [ '<STR_LIT>' ] <EOL> else : <EOL> raise Inconsistent ( '<STR_LIT>' % ( self . _process_name , self . _op ) ) <EOL> if headers . has_key ( '<STR_LIT>' ) : <EOL> self . _actor_roles = headers [ '<STR_LIT>' ] <EOL> else : <EOL> raise Inconsistent ( '<STR_LIT>' % ( self . _process_name , self . _op ) ) <EOL> if headers . has_key ( '<STR_LIT>' ) : <EOL> self . _resource_id = headers [ '<STR_LIT>' ] <EOL> else : <EOL> if resource_id_required : <EOL> raise Inconsistent ( '<STR_LIT>' % ( self . _process_name , self . _op ) ) <EOL> self . _resource_id = '<STR_LIT>' <EOL> @ property <EOL> def op ( self ) : <EOL> return self . _op <EOL> @ property <EOL> def actor_id ( self ) : <EOL> return self . _actor_id <EOL> @ property <EOL> def actor_roles ( self ) : <EOL> return self . _actor_roles <EOL> @ property <EOL> def resource_id ( self ) : <EOL> return self . _resource_id <EOL> @ property <EOL> def process_name ( self ) : <EOL> return self . _process_name </s>
