Ejemplo n.º 1
0
def base_import_templates(request: Request, template_model_cls: object,
                          import_kwargs: dict):
    f = request.FILES["data_file"]
    override = string_to_boolean(request.POST["override"])

    r = read_template_data_file(f)
    templates_data = r["data"]["template_data"]

    try:
        result = template_model_cls.objects.import_templates(
            template_data=templates_data,
            override=override,
            operator=request.user.username,
            **import_kwargs)
    except Exception:
        logger.error(traceback.format_exc())
        return JsonResponse({
            "result": False,
            "message":
            "invalid flow data or error occur, please contact administrator",
            "code": err_code.UNKNOWN_ERROR.code,
            "data": None,
        })

    return JsonResponse(result)
Ejemplo n.º 2
0
def base_check_before_import(request: Request, template_model_cls: object,
                             import_args: list):
    r = read_template_data_file(request.FILES["data_file"])

    check_info = template_model_cls.objects.import_operation_check(
        r["data"]["template_data"], *import_args)

    return JsonResponse({
        "result": True,
        "data": check_info,
        "code": err_code.SUCCESS.code,
        "message": ""
    })
Ejemplo n.º 3
0
    def validate(self, request, *args, **kwargs):

        f = request.FILES.get("data_file", None)

        if not f:
            return False, "data_file can not be empty"

        r = read_template_data_file(f)
        if not r["result"]:
            return False, r["message"]

        if "template_source" in r["data"]["template_data"]:
            if r["data"]["template_data"][
                    "template_source"] != TEMPLATE_EXPORTER_SOURCE_PROJECT:
                return False, "can not import common template"

        f.seek(0)

        return True, ""
Ejemplo n.º 4
0
    def validate(self, request, *args, **kwargs):
        f = request.FILES.get("data_file", None)

        if not f:
            return False, "data_file can not be empty"

        override = request.POST.get("override")
        if override is None:
            return False, "override can not be empty"

        r = read_template_data_file(f)
        if not r["result"]:
            return False, r["message"]

        if "template_source" in r["data"]["template_data"]:
            if r["data"]["template_data"][
                    "template_source"] != TEMPLATE_EXPORTER_SOURCE_COMMON:
                return False, "can not import project template"

        f.seek(0)

        return True, ""
Ejemplo n.º 5
0
    def process(self, request, *args, **kwargs):
        project_id = kwargs["project_id"]
        templates_data = read_template_data_file(
            request.FILES["data_file"])["data"]["template_data"]
        request.FILES["data_file"].seek(0)
        override = string_to_boolean(request.POST["override"])

        check_info = TaskTemplate.objects.import_operation_check(
            templates_data, project_id)

        subject = Subject("user", request.user.username)

        create_action = Action(IAMMeta.FLOW_CREATE_ACTION)
        project_resources = res_factory.resources_for_project(project_id)
        create_request = Request(IAMMeta.SYSTEM_ID, subject, create_action,
                                 project_resources, {})

        # check flow create permission
        if not override:
            allowed = iam.is_allowed(create_request)

            if not allowed:
                raise AuthFailedException(IAMMeta.SYSTEM_ID, subject,
                                          create_action, project_resources)

        else:

            # check flow create permission
            if len(check_info["new_template"]) != len(
                    check_info["override_template"]):
                allowed = iam.is_allowed(create_request)

                if not allowed:
                    raise AuthFailedException(IAMMeta.SYSTEM_ID, subject,
                                              create_action, project_resources)

            # check flow edit permission
            if check_info["override_template"]:
                tids = [
                    template_info["id"]
                    for template_info in check_info["override_template"]
                ]

                resources_list = res_factory.resources_list_for_flows(tids)

                if not resources_list:
                    return

                resources_map = {}
                for resources in resources_list:
                    resources_map[resources[0].id] = resources

                edit_action = Action(IAMMeta.FLOW_EDIT_ACTION)
                edit_request = Request(IAMMeta.SYSTEM_ID, subject, edit_action,
                                       [], {})
                result = iam.batch_is_allowed(edit_request, resources_list)
                if not result:
                    raise MultiAuthFailedException(IAMMeta.SYSTEM_ID, subject,
                                                   edit_action, resources_list)

                not_allowed_list = []
                for tid, allow in result.items():
                    if not allow:
                        not_allowed_list.append(resources_map[tid])

                if not_allowed_list:
                    raise MultiAuthFailedException(IAMMeta.SYSTEM_ID, subject,
                                                   edit_action,
                                                   not_allowed_list)