Example #1
0
    def patch(self, uuid=None, user_id=None):
        """Update a policy

        :param uuid: uuid of the policy to update
        :param user_id: user ID who do the request
        :return: {
            "policy_id1": {
                "name": "name of the policy (mandatory)",
                "model_id": "ID of the model linked to this policy",
                "genre": "authz of admin (optional, default to authz)",
                "description": "description of the policy (optional)",
            }
        }
        :internal_api: update_policy
        """

        data = PolicyManager.update_policy(user_id=user_id,
                                           policy_id=uuid,
                                           value=request.json)

        return {"policies": data}
Example #2
0
    def patch(self, uuid=None, user_id=None):
        """Update a policy

        :param uuid: uuid of the policy to update
        :param user_id: user ID who do the request
        :return: {
            "policy_id1": {
                "name": "...",
                "model_id": "...",
                "genre": "...",
                "description": "...",
            }
        }
        :internal_api: update_policy
        """
        try:
            data = PolicyManager.update_policy(user_id=user_id,
                                               policy_id=uuid,
                                               value=request.json)
        except Exception as e:
            logger.error(e, exc_info=True)
            return {"result": False, "error": str(e)}, 500
        return {"policies": data}
Example #3
0
def update_policy(policy_id, value):
    from python_moondb.core import PolicyManager
    return PolicyManager.update_policy("admin", policy_id, value)
Example #4
0
    def _import_policies(self, json_policies):
        policy_mandatory_ids = []

        if not isinstance(json_policies, list):
            raise InvalidJson("policies shall be a list!")

        for json_policy in json_policies:
            # TODO put this in moondb
            # policy_in_db = PolicyManager.get_policies_by_name(json_without_model_name["name"])
            policies = PolicyManager.get_policies(self._user_id)
            policy_in_db = None
            policy_id = None
            for policy_key in policies:
                if policies[policy_key]["name"] == json_policy["name"]:
                    policy_in_db = policies[policy_key]
                    policy_id = policy_key
            # end TODO
            if policy_in_db is None:
                policy_does_exist = False
            else:
                policy_does_exist = True

            policy_override = JsonUtils.get_override(json_policy)
            policy_mandatory = JsonUtils.get_mandatory(json_policy)

            if policy_override is False and policy_does_exist:
                if policy_id:
                    policy_mandatory_ids.append(policy_id)
                    logger.warning(
                        "Existing policy not updated because of the override option is not set !"
                    )
                    continue

            json_without_model_name = dict()
            JsonUtils.copy_field_if_exists(json_policy,
                                           json_without_model_name, "name",
                                           str)
            JsonUtils.copy_field_if_exists(json_policy,
                                           json_without_model_name,
                                           "description", str)
            JsonUtils.copy_field_if_exists(json_policy,
                                           json_without_model_name, "genre",
                                           str)
            JsonUtils.convert_name_to_id(json_policy,
                                         json_without_model_name,
                                         "model",
                                         "model_id",
                                         "model",
                                         ModelManager,
                                         self._user_id,
                                         field_mandatory=False)

            if not policy_does_exist:
                logger.debug(
                    "Creating policy {} ".format(json_without_model_name))
                added_policy = PolicyManager.add_policy(
                    self._user_id, None, json_without_model_name)
                if policy_mandatory is True:
                    keys = list(added_policy.keys())
                    policy_mandatory_ids.append(keys[0])
            elif policy_override is True:
                logger.debug(
                    "Updating policy {} ".format(json_without_model_name))
                updated_policy = PolicyManager.update_policy(
                    self._user_id, policy_id, json_without_model_name)
                if policy_mandatory is True:
                    policy_mandatory_ids.append(policy_id)
        return policy_mandatory_ids