Skip to content

Commit

Permalink
Add permissions to custom filters
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidMStraub committed Apr 27, 2024
1 parent 9a51c41 commit 2176ea9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
23 changes: 20 additions & 3 deletions gramps_webapi/api/resources/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@
from typing import Any, Dict, List, Optional, Set

import gramps.gen.filters as filters
from flask import Response, abort
from flask import Response, abort, current_app
from gramps.gen.db.base import DbReadBase
from gramps.gen.filters import GenericFilter
from marshmallow import Schema
from webargs import ValidationError, fields, validate

from ..util import abort_with_message, use_args
from ...const import GRAMPS_NAMESPACES
from ...auth.const import PERM_EDIT_CUSTOM_FILTER
from ...const import GRAMPS_NAMESPACES, TREE_MULTI
from ...types import Handle
from ..auth import require_permissions
from ..util import abort_with_message, use_args
from . import ProtectedResource
from .emit import GrampsJSONEncoder

Expand Down Expand Up @@ -235,6 +237,11 @@ def get(self, args: Dict[str, str], namespace: str) -> Response:
@use_args(CustomFilterSchema(), location="json")
def post(self, args: Dict, namespace: str) -> Response:
"""Create a custom filter."""
if current_app.config["TREE"] == TREE_MULTI:
abort_with_message(
405, "Custom filters cannot be edited in a multi-tree setup"
)
require_permissions([PERM_EDIT_CUSTOM_FILTER])
try:
namespace = GRAMPS_NAMESPACES[namespace]
except KeyError:
Expand All @@ -252,6 +259,11 @@ def post(self, args: Dict, namespace: str) -> Response:
@use_args(CustomFilterSchema(), location="json")
def put(self, args: Dict, namespace: str) -> Response:
"""Update a custom filter."""
if current_app.config["TREE"] == TREE_MULTI:
abort_with_message(
405, "Custom filters cannot be edited in a multi-tree setup"
)
require_permissions([PERM_EDIT_CUSTOM_FILTER])
try:
namespace = GRAMPS_NAMESPACES[namespace]
except KeyError:
Expand Down Expand Up @@ -294,6 +306,11 @@ def get(self, namespace: str, name: str) -> Response:
)
def delete(self, args: Dict, namespace: str, name: str) -> Response:
"""Delete a custom filter."""
if current_app.config["TREE"] == TREE_MULTI:
abort_with_message(
405, "Custom filters cannot be edited in a multi-tree setup"
)
require_permissions([PERM_EDIT_CUSTOM_FILTER])
try:
namespace = GRAMPS_NAMESPACES[namespace]
except KeyError:
Expand Down
2 changes: 2 additions & 0 deletions gramps_webapi/auth/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
PERM_EDIT_SETTINGS = "EditSettings"
PERM_TRIGGER_REINDEX = "TriggerReindex"
PERM_EDIT_NAME_GROUP = "EditNameGroup"
PERM_EDIT_CUSTOM_FILTER = "EditCustomFilter"
PERM_EDIT_TREE = "EditTree"
PERM_REPAIR_TREE = "RepairTree"
PERM_UPGRADE_TREE_SCHEMA = "UpgradeSchema"
Expand All @@ -88,6 +89,7 @@
PERM_EDIT_OBJ,
PERM_DEL_OBJ,
PERM_EDIT_NAME_GROUP,
PERM_EDIT_CUSTOM_FILTER,
}


Expand Down

0 comments on commit 2176ea9

Please sign in to comment.