Skip to content

Commit

Permalink
feat(import) : add method to Destination to filter by a user's permis…
Browse files Browse the repository at this point in the history
…sion + change GET route /destinations/
  • Loading branch information
jacquesfize committed Oct 1, 2024
1 parent 523f48d commit e70453d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
18 changes: 18 additions & 0 deletions backend/geonature/core/imports/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from collections.abc import Mapping
import re
from typing import Iterable, List
from geonature.core.gn_permissions.models import PermAction, Permission
from packaging import version

from flask import g
Expand All @@ -22,6 +23,7 @@
else: # retro-compatibility Flask-SQLAlchemy 2
from flask_sqlalchemy import BaseQuery as Query

from utils_flask_sqla.models import qfilter
from utils_flask_sqla.serializers import serializable

from geonature.utils.env import db
Expand Down Expand Up @@ -149,6 +151,22 @@ def actions(self):
"""
raise AttributeError(f"Is your module of type '{self.module.type}' installed?") from exc

@staticmethod
def filter_by_permissions(user: User):

action_create = db.session.scalar(

Check warning on line 157 in backend/geonature/core/imports/models.py

View check run for this annotation

Codecov / codecov/patch

backend/geonature/core/imports/models.py#L157

Added line #L157 was not covered by tests
sa.select(PermAction.id_action).where(PermAction.code_action == "C"),
)
query = sa.select(Destination).where(

Check warning on line 160 in backend/geonature/core/imports/models.py

View check run for this annotation

Codecov / codecov/patch

backend/geonature/core/imports/models.py#L160

Added line #L160 was not covered by tests
sa.or_(
Permission.id_role == user.id_role,
Permission.role.has(User.members.any(User.id_role == user.id_role)),
),
Permission.id_module == Destination.id_module,
Permission.id_action == action_create,
)
return db.session.scalars(query).all()

Check warning on line 168 in backend/geonature/core/imports/models.py

View check run for this annotation

Codecov / codecov/patch

backend/geonature/core/imports/models.py#L168

Added line #L168 was not covered by tests


@serializable
class BibThemes(db.Model):
Expand Down
4 changes: 2 additions & 2 deletions backend/geonature/core/imports/routes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
from geonature.core.imports.schemas import DestinationSchema
from geonature.core.imports.blueprint import blueprint
from geonature.utils.env import db
from flask_login import current_user


@blueprint.route("/destinations/", methods=["GET"])
@login_required
def list_destinations():
schema = DestinationSchema()
destinations = Destination.query.all()
# FIXME: filter with C permissions?
destinations = Destination.filter_by_permissions(current_user)

Check warning on line 15 in backend/geonature/core/imports/routes/__init__.py

View check run for this annotation

Codecov / codecov/patch

backend/geonature/core/imports/routes/__init__.py#L15

Added line #L15 was not covered by tests
return schema.dump(destinations, many=True)


Expand Down

0 comments on commit e70453d

Please sign in to comment.