Skip to content

Commit

Permalink
feat(permissions): route to get available perm
Browse files Browse the repository at this point in the history
  • Loading branch information
bouttier committed Nov 20, 2024
1 parent ef48080 commit 779906d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
22 changes: 21 additions & 1 deletion backend/geonature/core/gn_permissions/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
from copy import copy

from flask import Blueprint, Response, session
from geonature.core.gn_commons.models.base import TModules
import sqlalchemy as sa

from geonature.utils.env import db
from sqlalchemy.exc import NoResultFound
from sqlalchemy.orm import joinedload
from geonature.core.gn_permissions.models import PermissionAvailable
from geonature.core.gn_permissions.models import PermAction, PermissionAvailable, TObjects
from geonature.core.gn_permissions.schemas import PermissionAvailableSchema
from geonature.core.gn_permissions.decorators import login_required
from geonature.core.gn_permissions.commands import supergrant
from werkzeug.exceptions import NotFound


routes = Blueprint(
Expand Down Expand Up @@ -45,3 +48,20 @@ def list_permissions_availables():
pa = db.session.execute(sa.select(PermissionAvailable)).scalars()
schema = PermissionAvailableSchema(only=["action", "module", "object"])
return schema.dump(pa, many=True)


@routes.route("/availables/<module_code>/<code_object>/<code_action>", methods=["GET"])
@login_required
def get_permission_available(module_code, code_object, code_action):
try:
pa = db.session.execute(
sa.select(PermissionAvailable).where(
PermissionAvailable.module.has(TModules.module_code == module_code),
PermissionAvailable.object.has(TObjects.code_object == code_object),
PermissionAvailable.action.has(PermAction.code_action == code_action),
)
).scalar_one()
except NoResultFound:
raise NotFound
schema = PermissionAvailableSchema(only=["action", "module", "object"])
return schema.dump(pa)
15 changes: 15 additions & 0 deletions backend/geonature/tests/test_gn_permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,18 @@ def test_list_permissions_availables(self, users):
set_logged_user(self.client, users["user"])
response = self.client.get(url)
assert response.status_code == 200

def test_get_permission_available(self, users):
url = url_for(
"gn_permissions.get_permission_available",
module_code="METADATA",
code_object="ALL",
code_action="R",
)

response = self.client.get(url)
assert response.status_code == Unauthorized.code

set_logged_user(self.client, users["user"])
response = self.client.get(url)
assert response.status_code == 200

0 comments on commit 779906d

Please sign in to comment.