Skip to content

Commit

Permalink
fix(model, Destination) : using get_permissions method instead of dup…
Browse files Browse the repository at this point in the history
…licating permission query retrieval + change name
  • Loading branch information
jacquesfize committed Oct 2, 2024
1 parent e70453d commit 67123b5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
46 changes: 31 additions & 15 deletions backend/geonature/core/imports/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime
from collections.abc import Mapping
import re
from typing import Iterable, List
from typing import Iterable, List, Optional
from geonature.core.gn_permissions.models import PermAction, Permission
from packaging import version

Expand All @@ -28,7 +28,7 @@

from geonature.utils.env import db
from geonature.utils.celery import celery_app
from geonature.core.gn_permissions.tools import get_scopes_by_action
from geonature.core.gn_permissions.tools import get_permissions, get_scopes_by_action
from geonature.core.gn_commons.models import TModules
from geonature.core.gn_meta.models import TDatasets
from pypnnomenclature.models import BibNomenclaturesTypes
Expand Down Expand Up @@ -152,20 +152,36 @@ def actions(self):
raise AttributeError(f"Is your module of type '{self.module.type}' installed?") from exc

@staticmethod
def filter_by_permissions(user: User):
def filter_by_role(user: Optional[User] = None) -> List["Destination"]:
"""
Return a list of allowed destinations for a given user.
Parameters
----------
user : User, optional
The user to filter destinations for. If not provided, the current_user is used.
Returns
-------
allowed_destination : List of Destination
List of allowed destinations for the given user.
"""
# Retrieve all destinations
all_destination = db.session.scalars(sa.select(Destination)).all()

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L170 was not covered by tests

# If no user is provided, use the current user
if not user:
user = g.current_user

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

View check run for this annotation

Codecov / codecov/patch

backend/geonature/core/imports/models.py#L173-L174

Added lines #L173 - L174 were not covered by tests

action_create = db.session.scalar(
sa.select(PermAction.id_action).where(PermAction.code_action == "C"),
)
query = sa.select(Destination).where(
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()
# Filter destinations based on permissions for each destination module for the selected user
allowed_destination = []
for dest in all_destination:
perm = get_permissions(

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

View check run for this annotation

Codecov / codecov/patch

backend/geonature/core/imports/models.py#L177-L179

Added lines #L177 - L179 were not covered by tests
action_code="C", id_role=user.id_role, module_code=dest.module.module_code
)
if len(perm) > 0:
allowed_destination.append(dest)
return allowed_destination

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

View check run for this annotation

Codecov / codecov/patch

backend/geonature/core/imports/models.py#L182-L184

Added lines #L182 - L184 were not covered by tests


@serializable
Expand Down
2 changes: 1 addition & 1 deletion backend/geonature/core/imports/routes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@login_required
def list_destinations():
schema = DestinationSchema()
destinations = Destination.filter_by_permissions(current_user)
destinations = Destination.filter_by_role(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 67123b5

Please sign in to comment.