2021-06-14 19:45:36 +00:00
|
|
|
# Copyright (C) 2018-2021 by BDE ENS Paris-Saclay
|
2020-03-18 13:42:35 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-07-30 13:53:23 +00:00
|
|
|
|
2020-03-18 13:42:35 +00:00
|
|
|
from django.core.exceptions import PermissionDenied
|
2020-04-25 17:29:18 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2021-06-15 12:40:32 +00:00
|
|
|
from note_kfet.middlewares import get_current_request
|
2020-03-22 13:57:51 +00:00
|
|
|
from permission.backends import PermissionBackend
|
2020-03-18 13:42:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
EXCLUDED = [
|
|
|
|
'cas_server.proxygrantingticket',
|
|
|
|
'cas_server.proxyticket',
|
|
|
|
'cas_server.serviceticket',
|
|
|
|
'cas_server.user',
|
|
|
|
'cas_server.userattributes',
|
|
|
|
'contenttypes.contenttype',
|
|
|
|
'logs.changelog',
|
|
|
|
'migrations.migration',
|
2021-06-15 13:50:36 +00:00
|
|
|
'oauth2_provider.accesstoken',
|
|
|
|
'oauth2_provider.grant',
|
|
|
|
'oauth2_provider.refreshtoken',
|
2020-03-18 13:42:35 +00:00
|
|
|
'sessions.session',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def pre_save_object(sender, instance, **kwargs):
|
|
|
|
"""
|
|
|
|
Before a model get saved, we check the permissions
|
|
|
|
"""
|
|
|
|
# noinspection PyProtectedMember
|
|
|
|
if instance._meta.label_lower in EXCLUDED:
|
|
|
|
return
|
|
|
|
|
2020-09-07 12:52:37 +00:00
|
|
|
if hasattr(instance, "_force_save") or hasattr(instance, "_no_signal"):
|
2020-04-01 01:42:19 +00:00
|
|
|
return
|
|
|
|
|
2021-06-15 12:40:32 +00:00
|
|
|
request = get_current_request()
|
|
|
|
if request is None:
|
2020-03-18 13:42:35 +00:00
|
|
|
# Action performed on shell is always granted
|
|
|
|
return
|
|
|
|
|
|
|
|
qs = sender.objects.filter(pk=instance.pk).all()
|
|
|
|
model_name_full = instance._meta.label_lower.split(".")
|
|
|
|
app_label = model_name_full[0]
|
|
|
|
model_name = model_name_full[1]
|
|
|
|
|
|
|
|
if qs.exists():
|
2020-03-19 23:06:28 +00:00
|
|
|
# We check if the user can change the model
|
|
|
|
|
|
|
|
# If the user has all right on a model, then OK
|
2021-06-15 12:40:32 +00:00
|
|
|
if PermissionBackend.check_perm(request, app_label + ".change_" + model_name, instance):
|
2020-03-18 13:42:35 +00:00
|
|
|
return
|
|
|
|
|
2020-03-19 23:06:28 +00:00
|
|
|
# In the other case, we check if he/she has the right to change one field
|
2020-03-18 13:42:35 +00:00
|
|
|
previous = qs.get()
|
2020-07-30 10:50:48 +00:00
|
|
|
|
2020-03-18 13:42:35 +00:00
|
|
|
for field in instance._meta.fields:
|
|
|
|
field_name = field.name
|
|
|
|
old_value = getattr(previous, field.name)
|
|
|
|
new_value = getattr(instance, field.name)
|
2020-03-19 23:06:28 +00:00
|
|
|
# If the field wasn't modified, no need to check the permissions
|
2020-03-18 13:42:35 +00:00
|
|
|
if old_value == new_value:
|
|
|
|
continue
|
2021-06-15 12:40:32 +00:00
|
|
|
if not PermissionBackend.check_perm(request, app_label + ".change_" + model_name + "_" + field_name,
|
|
|
|
instance):
|
2020-04-25 17:29:18 +00:00
|
|
|
raise PermissionDenied(
|
|
|
|
_("You don't have the permission to change the field {field} on this instance of model"
|
|
|
|
" {app_label}.{model_name}.")
|
|
|
|
.format(field=field_name, app_label=app_label, model_name=model_name, )
|
|
|
|
)
|
2020-03-18 13:42:35 +00:00
|
|
|
else:
|
2020-03-19 23:06:28 +00:00
|
|
|
# We check if the user has right to add the object
|
2021-06-15 12:40:32 +00:00
|
|
|
has_perm = PermissionBackend.check_perm(request, app_label + ".add_" + model_name, instance)
|
2020-03-19 23:06:28 +00:00
|
|
|
|
|
|
|
if not has_perm:
|
2020-04-25 17:29:18 +00:00
|
|
|
raise PermissionDenied(
|
2020-08-13 13:20:15 +00:00
|
|
|
_("You don't have the permission to add an instance of model {app_label}.{model_name}.")
|
2020-04-25 17:29:18 +00:00
|
|
|
.format(app_label=app_label, model_name=model_name, ))
|
2020-03-18 13:42:35 +00:00
|
|
|
|
|
|
|
|
2020-04-01 01:42:19 +00:00
|
|
|
def pre_delete_object(instance, **kwargs):
|
2020-03-18 13:42:35 +00:00
|
|
|
"""
|
|
|
|
Before a model get deleted, we check the permissions
|
|
|
|
"""
|
|
|
|
# noinspection PyProtectedMember
|
|
|
|
if instance._meta.label_lower in EXCLUDED:
|
|
|
|
return
|
|
|
|
|
2020-09-07 12:52:37 +00:00
|
|
|
if hasattr(instance, "_force_delete") or hasattr(instance, "pk") and instance.pk == 0 \
|
|
|
|
or hasattr(instance, "_no_signal"):
|
2020-07-30 12:58:18 +00:00
|
|
|
# Don't check permissions on force-deleted objects
|
2020-04-01 01:42:19 +00:00
|
|
|
return
|
|
|
|
|
2021-06-15 12:40:32 +00:00
|
|
|
request = get_current_request()
|
|
|
|
if request is None:
|
2020-03-18 13:42:35 +00:00
|
|
|
# Action performed on shell is always granted
|
|
|
|
return
|
|
|
|
|
|
|
|
model_name_full = instance._meta.label_lower.split(".")
|
|
|
|
app_label = model_name_full[0]
|
|
|
|
model_name = model_name_full[1]
|
|
|
|
|
2020-03-19 23:06:28 +00:00
|
|
|
# We check if the user has rights to delete the object
|
2021-06-15 12:40:32 +00:00
|
|
|
if not PermissionBackend.check_perm(request, app_label + ".delete_" + model_name, instance):
|
2020-04-25 17:29:18 +00:00
|
|
|
raise PermissionDenied(
|
|
|
|
_("You don't have the permission to delete this instance of model {app_label}.{model_name}.")
|
|
|
|
.format(app_label=app_label, model_name=model_name))
|