Exemplo n.º 1
0
def validate_user(a_tuple: UserBasedTuple, user_service: UserService,
                  principal_service: PrincipalService) -> None:
    if not principal_service.is_admin():
        raise_403()

    if is_blank(a_tuple.userId):
        if principal_service.is_super_admin():
            raise_400('User id is required.')
        elif principal_service.is_tenant_admin():
            a_tuple.userId = principal_service.get_user_id()
        else:
            raise_403()
    else:
        if a_tuple.userId == principal_service.get_user_id():
            if principal_service.is_super_admin():
                raise_400(f'Incorrect user id[{a_tuple.userId}].')
        else:
            user: Optional[User] = user_service.find_by_id(a_tuple.userId)
            if user is None:
                raise_400('User id is required.')
            if principal_service.is_super_admin():
                if user.tenantId == principal_service.get_tenant_id():
                    raise_400(f'Incorrect user id[{a_tuple.userId}].')
            elif principal_service.is_tenant_admin():
                if user.tenantId != principal_service.get_tenant_id():
                    raise_400(f'Incorrect user id[{a_tuple.userId}].')
async def init_tenant(
    tenant_id: Optional[TenantId],
    principal_service: PrincipalService = Depends(get_any_admin_principal)
) -> None:
    if is_blank(tenant_id):
        if principal_service.is_super_admin():
            raise_400('Tenant id is required.')
        elif principal_service.is_tenant_admin():
            tenant_id = principal_service.get_tenant_id()
    else:
        if principal_service.get_tenant_id(
        ) != tenant_id and principal_service.is_tenant_admin():
            raise_400(f'Tenant[{tenant_id}] does not match principal.')
        elif principal_service.is_super_admin():
            tenant: Optional[Tenant] = get_tenant_service(
                principal_service).find_by_id(tenant_id)
            if tenant is None:
                raise_404(f'Tenant[id={tenant_id}] not found.')

    meta_tenant_service = get_meta_tenant_service(principal_service)

    def action() -> None:
        topics = ask_pipeline_monitor_topics()
        create_topics_and_pipelines(
            topics, lambda source_topics: ask_pipeline_monitor_pipelines(
                source_topics), tenant_id, meta_tenant_service,
            principal_service)
        topics = ask_dqc_topics()
        create_topics_and_pipelines(
            topics, lambda source_topics: ask_dqc_pipelines(source_topics),
            tenant_id, meta_tenant_service, principal_service)

    trans(meta_tenant_service, action)
def prepare_and_validate_request(
		request: MixImportDataRequest,
		user_service: UserService, principal_service: PrincipalService) -> None:
	tenant_id = validate_tenant_id(request, user_service, principal_service)
	clear_data_source_id(request.topics)
	clear_user_group_ids(request.spaces)

	def set_user_id_to_report(report: Report, user_id: UserId) -> None:
		report.userId = user_id

	def set_user_id_to_subject(subject: SubjectWithReports, user_id: UserId) -> None:
		subject.userId = user_id
		ArrayHelper(subject.reports) \
			.flatten() \
			.filter(lambda x: x is not None) \
			.each(lambda x: set_user_id_to_report(x, user_id))

	def set_user_id_to_connected_space(connected_space: ConnectedSpaceWithSubjects, user_id: UserId) -> None:
		connected_space.userId = user_id
		ArrayHelper(connected_space.subjects) \
			.flatten() \
			.filter(lambda x: x is not None) \
			.each(lambda x: set_user_id_to_subject(x, user_id))

	if principal_service.is_super_admin():
		# to find a tenant admin
		tenant_admin: Optional[User] = user_service.find_admin(tenant_id)
		if tenant_admin is None:
			raise_400(f'Admin user on tenant[{tenant_id}] to receive imported data is not found.')
	elif principal_service.is_tenant_admin():
		ArrayHelper(request.connectedSpaces).each(
			lambda x: set_user_id_to_connected_space(x, principal_service.get_user_id()))
	else:
		raise_403()
async def fetch_topic_by_name(
    query_name: Optional[str] = None,
    tenant_id: Optional[TenantId] = None,
    principal_service: PrincipalService = Depends(get_any_admin_principal)
) -> List[Topic]:
    if is_blank(query_name):
        raise_400('Name criteria is required.')

    if principal_service.is_tenant_admin():
        if is_not_blank(
                tenant_id) and tenant_id != principal_service.get_tenant_id():
            raise_400('Tenant id is incorrect.')
        else:
            tenant_id = principal_service.get_tenant_id()

    if principal_service.is_super_admin() and is_blank(tenant_id):
        raise_400('Tenant id is required.')

    topic_service = get_topic_service(principal_service)

    def action() -> List[Topic]:
        topic_index_service = get_topic_index_service(topic_service)
        factor_index_list = topic_index_service.find(query_name, tenant_id)
        if len(factor_index_list) == 0:
            return []

        topic_ids: List[TopicId] = []
        for factor_index in factor_index_list:
            if factor_index.topicId not in topic_ids:
                topic_ids.append(factor_index.topicId)
        return topic_service.find_by_ids(topic_ids, tenant_id)

    return trans_readonly(topic_service, action)
def validate_tenant_id(
		request: MixImportDataRequest,
		user_service: UserService, principal_service: PrincipalService) -> TenantId:
	if principal_service.is_super_admin():
		return validate_tenant_id_when_super_admin(request, user_service, principal_service)
	elif principal_service.is_tenant_admin():
		return validate_tenant_id_when_tenant_admin(request, principal_service)
	else:
		raise_403()
def validate_tenant_id(tenant_id: Optional[TenantId],
                       principal_service: PrincipalService) -> TenantId:
    if principal_service.is_tenant_admin():
        if is_not_blank(
                tenant_id) and tenant_id != principal_service.get_tenant_id():
            raise_400('Tenant id is incorrect.')
        return principal_service.get_tenant_id()
    elif principal_service.is_super_admin():
        if is_blank(tenant_id):
            raise_400('Tenant id is required.')
        return tenant_id
Exemplo n.º 7
0
async def find_all_external_writers(
        principal_service: PrincipalService = Depends(get_any_admin_principal)
) -> List[ExternalWriter]:
    tenant_id = None
    if principal_service.is_tenant_admin():
        tenant_id = principal_service.get_tenant_id()

    external_writer_service = get_external_writer_service(principal_service)

    def action() -> List[ExternalWriter]:
        return external_writer_service.find_all(tenant_id)

    return trans_readonly(external_writer_service, action)
Exemplo n.º 8
0
def validate_tenant(a_tuple: Union[UserBasedTuple, TenantBasedTuple],
                    user_service: UserService,
                    principal_service: PrincipalService) -> None:
    if not principal_service.is_admin():
        raise_403()

    if is_blank(a_tuple.tenantId):
        if principal_service.is_super_admin():
            raise_400('Tenant id is required.')
        elif principal_service.is_tenant_admin():
            a_tuple.tenantId = principal_service.get_tenant_id()
    else:
        if principal_service.is_tenant_admin():
            if a_tuple.tenantId != principal_service.get_tenant_id():
                raise_403()
        elif principal_service.is_super_admin():
            if a_tuple.tenantId == principal_service.get_tenant_id():
                raise_400(f'Incorrect tenant id[{a_tuple.tenantId}].')
            tenant_service = get_tenant_service(user_service)
            tenant: Optional[Tenant] = tenant_service.find_by_id(
                a_tuple.tenantId)
            if tenant is None:
                raise_400(f'Incorrect tenant id[{a_tuple.tenantId}].')
Exemplo n.º 9
0
async def rerun_by_topic_data(
    scheduler_id: Optional[TopicSnapshotSchedulerId],
    process_date: Optional[str],
    principal_service: PrincipalService = Depends(get_any_admin_principal)
) -> None:
    if is_blank(scheduler_id):
        raise_400('Scheduler id is required.')

    if is_blank(process_date):
        raise_400('Process date is required.')

    parsed, parsed_process_date = is_date(process_date, ask_date_formats())
    if not parsed:
        raise_400('Process date must be date.')

    scheduler_service = get_topic_snapshot_scheduler_service(principal_service)
    scheduler_service.begin_transaction()
    try:
        scheduler: Optional[
            TopicSnapshotScheduler] = scheduler_service.find_by_id(
                scheduler_id)
        if scheduler is None:
            raise_404(f'Scheduler[id={scheduler_id}] not found.')

        if principal_service.is_tenant_admin(
        ) and scheduler.tenantId != principal_service.get_tenant_id():
            raise_404(f'Scheduler[id={scheduler_id}] not found.')

        lock_service = get_lock_service(scheduler_service)
        lock = lock_service.find_by_scheduler_and_process_date(
            scheduler_id, scheduler.frequency, parsed_process_date)
        if lock is not None:
            raise_406(
                f'Scheduler[id={scheduler_id}, processDate={process_date}] run already.'
            )
    except HTTPException as e:
        raise e
    except Exception as e:
        raise_500(e)
    finally:
        scheduler_service.close_transaction()

    run_job(scheduler_id, parsed_process_date)
Exemplo n.º 10
0
def ask_principal_service(principal_service: PrincipalService,
                          tenant_id: Optional[TenantId]) -> PrincipalService:
    if principal_service.is_tenant_admin():
        if is_blank(tenant_id):
            return principal_service
        elif tenant_id != principal_service.get_tenant_id():
            raise_400(
                f'Tenant id[{tenant_id}] does not match current principal.')
        else:
            return principal_service
    elif principal_service.is_super_admin():
        if is_blank(tenant_id):
            raise_400('Tenant id is required.')
        tenant_service = get_tenant_service(principal_service)
        tenant: Optional[Tenant] = tenant_service.find_by_id(tenant_id)
        if tenant is None:
            raise_404(f'Tenant[id={tenant_id}] not found.')
        return PrincipalService(
            User(tenantId=tenant_id,
                 userId=principal_service.get_user_id(),
                 name=principal_service.get_user_name(),
                 role=UserRole.ADMIN))