From 22892d4d73be9176ce98e7e7c8812d65b7a7a373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89lie=20Bouttier?= Date: Fri, 14 Apr 2023 15:47:37 +0200 Subject: [PATCH] permissions: add flask-admin --- backend/geonature/core/admin/__init__.py | 1 + .../geonature/core/gn_permissions/admin.py | 57 +++++++++++++++++++ .../geonature/core/gn_permissions/models.py | 6 ++ 3 files changed, 64 insertions(+) create mode 100644 backend/geonature/core/gn_permissions/admin.py diff --git a/backend/geonature/core/admin/__init__.py b/backend/geonature/core/admin/__init__.py index e69de29bb2..c31328c68b 100644 --- a/backend/geonature/core/admin/__init__.py +++ b/backend/geonature/core/admin/__init__.py @@ -0,0 +1 @@ +import geonature.core.gn_permissions.admin diff --git a/backend/geonature/core/gn_permissions/admin.py b/backend/geonature/core/gn_permissions/admin.py new file mode 100644 index 0000000000..01f19b80a5 --- /dev/null +++ b/backend/geonature/core/gn_permissions/admin.py @@ -0,0 +1,57 @@ +from flask_admin.contrib.sqla import ModelView + +from geonature.utils.env import db +from geonature.core.admin.admin import admin +from geonature.core.admin.utils import CruvedProtectedMixin +from geonature.core.gn_permissions.models import PermObject, Permission + + +class ObjectAdmin(CruvedProtectedMixin, ModelView): + module_code = "ADMIN" + object_code = "PERMISSIONS" + + column_list = ("code_object", "description_object", "modules") + column_labels = { + "code_object": "Code", + "description_object": "Description", + } + + can_create = False + can_edit = False + can_delete = False + + +class PermissionAdmin(CruvedProtectedMixin, ModelView): + module_code = "ADMIN" + object_code = "PERMISSIONS" + + column_list = ("role", "module", "object", "action", "scope") + column_labels = { + "role": "Rôle", + "scope": "Porté", + "object": "Objet", + } + column_formatters = { + "module": lambda v, c, m, p: m.module.module_code, + "object": lambda v, c, m, p: m.object.code_object, + } + + +admin.add_view( + ObjectAdmin( + PermObject, + db.session, + name="Objets", + category="Permissions", + ) +) + + +admin.add_view( + PermissionAdmin( + Permission, + db.session, + name="Permissions", + category="Permissions", + ) +) diff --git a/backend/geonature/core/gn_permissions/models.py b/backend/geonature/core/gn_permissions/models.py index 0309a6fd1b..4f84ddf453 100644 --- a/backend/geonature/core/gn_permissions/models.py +++ b/backend/geonature/core/gn_permissions/models.py @@ -31,6 +31,9 @@ class PermScope(db.Model): label = db.Column(db.Unicode) description = db.Column(db.Unicode) + def __str__(self): + return self.description + @serializable class PermAction(db.Model): @@ -40,6 +43,9 @@ class PermAction(db.Model): code_action = db.Column(db.Unicode) description_action = db.Column(db.Unicode) + def __str__(self): + return self.description_action + cor_object_module = db.Table( "cor_object_module",