Exemple #1
0
    def get(self):
        """
        ---
        summary: Get list of attribute keys
        description: |
            Returns list of attribute key definitions.
        security:
            - bearerAuth: []
        tags:
            - attribute
        parameters:
            - in: query
              name: access
              schema:
                type: string
                enum: [read, set, manage]
                default: read
              description: Type of desired access
        responses:
            200:
                description: List of attribute key definitions
                content:
                  application/json:
                    schema: AttributeDefinitionListResponseSchema
            400:
                description: When used unknown access type
            403:
                description: |
                    When requested `manage` access
                    but user doesn't have 'manage_users' capability
            503:
                description: |
                    Request canceled due to database statement timeout.
        """
        schema = AttributeDefinitionListRequestSchema()
        obj = load_schema(request.args, schema)
        access = obj["access"]

        if access == "read":
            attribute_definitions = AttributeDefinition.query_for_read()
        elif access == "set":
            attribute_definitions = AttributeDefinition.query_for_set()
        elif access == "manage":
            if not g.auth_user.has_rights(Capabilities.manage_users):
                raise Forbidden("You are not permitted to manage attributes")
            attribute_definitions = db.session.query(AttributeDefinition)
        else:
            raise BadRequest(f"Unknown desired access type '{access}'")

        attribute_definitions = attribute_definitions.order_by(
            AttributeDefinition.key
        ).all()
        schema = AttributeDefinitionListResponseSchema()
        return schema.dump({"attribute_definitions": attribute_definitions})
Exemple #2
0
    def get(self, access):
        """
        ---
        summary: Get list of attribute keys
        description: |
            Returns list of attribute keys which currently authenticated user
            can read or set.
        security:
            - bearerAuth: []
        tags:
            - deprecated
        parameters:
            - in: path
              name: access
              schema:
                type: string
                enum: [read, set]
              description: Type of desired access
        responses:
            200:
                description: List of attribute keys and definitions
                content:
                  application/json:
                    schema: MetakeyDefinitionListResponseSchema
            400:
                description: When used unknown access type (other than read or set)
            503:
                description: |
                    Request canceled due to database statement timeout.
        """
        if access == "read":
            metakeys = AttributeDefinition.query_for_read()
        elif access == "set":
            metakeys = AttributeDefinition.query_for_set()
        else:
            raise BadRequest(f"Unknown desired access type '{access}'")

        metakeys = metakeys.order_by(AttributeDefinition.key).all()
        schema = MetakeyDefinitionListResponseSchema()
        return schema.dump({"metakeys": metakeys})
Exemple #3
0
    def create_object(self, params):
        params = dict(params)

        # Validate parent object
        if params["parent"] is not None:
            if not g.auth_user.has_rights(Capabilities.adding_parents):
                raise Forbidden("You are not permitted to link with parent")

            parent_object = Object.access(params["parent"])

            if parent_object is None:
                raise NotFound("Parent object not found")
        else:
            parent_object = None

        # Validate metakeys and Karton assignment
        analysis_id = params.get("karton_id")

        if params["metakeys"]:
            # If 'metakeys' are defined: keep legacy behavior
            if "attributes" in params and params["attributes"]:
                raise BadRequest(
                    "'attributes' and 'metakeys' options can't be mixed")

            attributes = params["metakeys"]
            for attribute in params["metakeys"]:
                key = attribute["key"]
                if key == "karton":
                    if analysis_id is not None:
                        raise BadRequest(
                            "You can't provide more than one Karton analysis identifier"
                        )
                    try:
                        analysis_id = UUID(attribute["value"])
                    except (ValueError, AttributeError):
                        raise BadRequest(
                            "'karton' attribute accepts only UUID values")
                elif not AttributeDefinition.query_for_set(key).first():
                    raise NotFound(
                        f"Attribute '{key}' not defined or insufficient "
                        "permissions to set that one")
        else:
            # If not, rely on 'attributes'
            attributes = params["attributes"]
            for attribute in params["attributes"]:
                key = attribute["key"]
                if not AttributeDefinition.query_for_set(key).first():
                    raise NotFound(
                        f"Attribute '{key}' not defined or insufficient "
                        "permissions to set that one")

        if analysis_id is not None:
            if not g.auth_user.has_rights(Capabilities.karton_assign):
                raise Forbidden(
                    "You are not permitted to assign Karton analysis to object"
                )

        # Validate upload_as argument
        share_with = get_shares_for_upload(params["upload_as"])

        # Tags argument
        tags = params.get("tags")

        item, is_new = self._create_object(params, parent_object, share_with,
                                           attributes, analysis_id, tags)

        try:
            db.session.commit()

            if is_new:
                self.on_created(item, params)
            else:
                self.on_reuploaded(item, params)
        finally:
            item.release_after_upload()

        logger.info(
            f"{self.ObjectType.__name__} added",
            extra={
                "dhash": item.dhash,
                "is_new": is_new
            },
        )
        schema = self.ItemResponseSchema()
        return schema.dump(item)