Browse Source

fix: corrige carga de permissões públicas do crud

permissões declaradas como públicas no atributo "public" do Crud estavam
sendo descartadas pois estavam sendo guardadas no atributo crud as
subclasses originais do crud, e não a construída dentro do get_urls.
pull/3715/head
LeandroJatai 8 months ago
parent
commit
d5a2831c6a
  1. 10
      sapl/base/templatetags/menus.py
  2. 57
      sapl/crud/base.py

10
sapl/base/templatetags/menus.py

@ -183,17 +183,21 @@ def resolve_urls_inplace(menu, pk, rm, context):
as funcionalidades diretas do MasterDetailCrud, como: as funcionalidades diretas do MasterDetailCrud, como:
- visualização de detalhes, adição, edição, remoção. - visualização de detalhes, adição, edição, remoção.
""" """
try:
if 'view' in context: if 'view' in context:
view = context['view'] view = context['view']
if hasattr(view, '__class__') and\ if hasattr(view, 'crud'):
hasattr(view.__class__, 'crud'): urls = view.crud.get_urls()
urls = view.__class__.crud.get_urls()
for u in urls: for u in urls:
if (u.name == url_name or if (u.name == url_name or
'urls_extras' in menu and 'urls_extras' in menu and
u.name in menu['urls_extras']): u.name in menu['urls_extras']):
menu['active'] = 'active' menu['active'] = 'active'
break break
except:
url_active = menu.get('url', '')
logger.warning(
f'Não foi possível definir se url {url_active} é a url ativa.')
elif 'check_permission' in menu and not context[ elif 'check_permission' in menu and not context[
'request'].user.has_perm(menu['check_permission']): 'request'].user.has_perm(menu['check_permission']):
menu['active'] = '' menu['active'] = ''

57
sapl/crud/base.py

@ -263,18 +263,18 @@ class CrudBaseMixin(CrispyLayoutFormMixin):
self.model_name_set = getattr( self.model_name_set = getattr(
obj.model, obj.model_set).field.model._meta.model_name obj.model, obj.model_set).field.model._meta.model_name
if hasattr(self, 'permission_required') and self.permission_required: if not hasattr(obj, 'public'):
if hasattr(obj, 'public'):
self.permission_required = list(
set(self.permission_required) - set(obj.public))
else:
obj.public = [] obj.public = []
self.permission_required = tuple(( if hasattr(self, 'permission_required') and self.permission_required:
self.permission(pr) for pr in self.permission_required))
else: self.permission_required = tuple(
obj.public = [] (
self.permission(pr) for pr in (
set(self.permission_required) - set(obj.public)
)
)
)
@classmethod @classmethod
def url_name(cls, suffix): def url_name(cls, suffix):
@ -460,7 +460,8 @@ class CrudListView(PermissionRequiredContainerCrudMixin, ListView):
# URL padrão para primeira coluna da listagem # URL padrão para primeira coluna da listagem
url = self.resolve_url( url = self.resolve_url(
ACTION_DETAIL, args=(obj.id,)) if i == 0 else None ACTION_DETAIL, args=(obj.id,)) if i == 0 else None
# gera URL para matéria a partir de fk_urlify_for_list em layouts.yaml # gera URL para matéria a partir de fk_urlify_for_list em
# layouts.yaml
if i > 0 and func is not None: if i > 0 and func is not None:
url = getattr(self, func)(obj, name)[0] url = getattr(self, func)(obj, name)[0]
@ -947,6 +948,8 @@ class CrudDeleteView(PermissionRequiredContainerCrudMixin,
class Crud: class Crud:
__abstract__ = True
BaseMixin = CrudBaseMixin BaseMixin = CrudBaseMixin
ListView = CrudListView ListView = CrudListView
CreateView = CrudCreateView CreateView = CrudCreateView
@ -962,9 +965,26 @@ class Crud:
def get_urls(cls): def get_urls(cls):
def _add_base(view): def _add_base(view):
if view:
if not view:
return
if not cls.__abstract__:
return view
pr = set()
if hasattr(view, 'permission_required') and \
view.permission_required and \
hasattr(cls, 'public') and \
cls.public:
#print(view.permission_required, view)
#print(cls.public, cls)
pr = set(view.permission_required) - set(cls.public)
class CrudViewWithBase(cls.BaseMixin, view): class CrudViewWithBase(cls.BaseMixin, view):
permission_required = tuple(pr)
model = cls.model model = cls.model
help_topic = cls.help_topic help_topic = cls.help_topic
crud = cls crud = cls
@ -978,6 +998,21 @@ class Crud:
CrudUpdateView = _add_base(cls.UpdateView) CrudUpdateView = _add_base(cls.UpdateView)
CrudDeleteView = _add_base(cls.DeleteView) CrudDeleteView = _add_base(cls.DeleteView)
cruds = CrudListView, CrudCreateView, CrudDetailView, CrudUpdateView, CrudDeleteView
if cls.__abstract__:
class CRUD(cls):
__abstract__ = False
ListView = CrudListView
CreateView = CrudCreateView
DetailView = CrudDetailView
UpdateView = CrudUpdateView
DeleteView = CrudDeleteView
for c in cruds:
if c:
c.crud = CRUD
cruds_base = [ cruds_base = [
(CrudListView.get_url_regex() (CrudListView.get_url_regex()
if CrudListView else None, CrudListView, ACTION_LIST), if CrudListView else None, CrudListView, ACTION_LIST),

Loading…
Cancel
Save