"""Helper module for user context functions.""" from owsrequest import error_response from owsrequest.constants import errors from owsrequest.constants import headers from owsresponse import response from pythonfeatures.constants import context as context_constants class AccountContext: """Class to organize legacy account based context logic and validations.""" def __init__( self, orchard_user_id=None, subaccount_id=None, vendor_id=None, identity_id=None, orchard_identity_uuid=None): """Initialize class.""" self.orchard_user_id = str(orchard_user_id) if orchard_user_id else None # noqa self.subaccount_id = str(subaccount_id) if subaccount_id else None self.vendor_id = str(vendor_id) if vendor_id else None self.identity_id = str(identity_id) if identity_id else None self.orchard_identity_uuid = str( orchard_identity_uuid) if orchard_identity_uuid else None self.is_valid = False def validate(self): """Validate initialization arguments.""" # check that accounts have users if (self.subaccount_id and not self.orchard_user_id) or ( self.vendor_id and not self.orchard_user_id): return response.create_error_response( code=errors.ERROR_CODE_ACCOUNT_WITHOUT_USER, message=errors.ERROR_MESSAGE_ACCOUNT_WITHOUT_USER, status=400) self.is_valid = True return response.Response() def to_dict(self): """Return a dict containing the full context.""" context = {} if self.is_valid: if self.orchard_user_id: context[ context_constants.ORCHARD_USER_ID] = self.orchard_user_id # Add LabelProfile if self.orchard_user_id.startswith('alw:'): profile = self.orchard_user_id.replace( 'alw', headers.PROFILE_TYPE_LABEL) context[context_constants.PROFILE_TYPE_AND_ID] = profile # Add OrchAdminProfile if self.orchard_user_id.startswith('oa:'): profile = self.orchard_user_id.replace( 'oa', headers.PROFILE_TYPE_ORCH_ADMIN) context[context_constants.PROFILE_TYPE_AND_ID] = profile if self.vendor_id: context[context_constants.VENDOR_ID] = self.vendor_id if self.subaccount_id: context[context_constants.SUBACCOUNT_ID] = self.subaccount_id if self.identity_id: context[context_constants.IDENTITY_ID] = self.identity_id if self.orchard_identity_uuid: context[context_constants.ORCHARD_IDENTITY_UUID] = self.orchard_identity_uuid # noqa return context class ProfileContext: """Class to organize profile based context logic and validations.""" def __init__( self, profile_type=None, profile_id=None, identity_id=None, orchard_identity_uuid=None): """Initialize class.""" self.profile_type = str(profile_type) if profile_type else None self.profile_id = str(profile_id) if profile_id else None self.identity_id = str(identity_id) if identity_id else None self.orchard_identity_uuid = str( orchard_identity_uuid) if orchard_identity_uuid else None self.context = {} self.is_valid = False def validate(self): """Validate initialization arguments.""" # profile type and profile id, no identity id if (self.profile_id and self.profile_type and not self.identity_id): self.is_valid = True # just identity id if (self.identity_id and not self.profile_id and not self.profile_type): self.is_valid = True # just orchard identity uuid if (self.orchard_identity_uuid and not self.identity_id and not self.profile_id and not self.profile_type): self.is_valid = True # all 3 if (self.identity_id and self.profile_id and self.profile_type): self.is_valid = True # none if (not self.identity_id and not self.profile_id and not self.profile_type): self.is_valid = True if not self.is_valid: self.profile_type = None self.profile_id = None self.identity_id = None self.orchard_identity_uuid = None return error_response.create_error_incomplete_profile_headers() return response.Response() def to_dict(self): """Return a dict containing the full context.""" if self.is_valid: if self.profile_type and self.profile_id: self.context[ context_constants.PROFILE_TYPE_AND_ID] = '{}:{}'.format( self.profile_type, self.profile_id) if self.identity_id: self.context[context_constants.IDENTITY_ID] = self.identity_id # if OrchAdminProfile, hyrdate oa user id if self.profile_type == headers.PROFILE_TYPE_ORCH_ADMIN: self.context[ context_constants.ORCHARD_USER_ID] = 'oa:{}'.format( self.profile_id) if self.orchard_identity_uuid: self.context[ context_constants.ORCHARD_IDENTITY_UUID] = self.orchard_identity_uuid # noqa return self.context