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. 26
      sapl/base/templatetags/menus.py
  2. 73
      sapl/crud/base.py

26
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.
""" """
if 'view' in context: try:
view = context['view'] if 'view' in context:
if hasattr(view, '__class__') and\ view = context['view']
hasattr(view.__class__, 'crud'): if hasattr(view, 'crud'):
urls = view.__class__.crud.get_urls() urls = view.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'] = ''

73
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'): obj.public = []
self.permission_required = list(
set(self.permission_required) - set(obj.public))
else:
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]
@ -495,7 +496,7 @@ class CrudListView(PermissionRequiredContainerCrudMixin, ListView):
if m: if m:
ss = get_field_display(m, n[-1])[1] ss = get_field_display(m, n[-1])[1]
ss = ( ss = (
('<br>' if '<ul>' in ss else ' - ') + ss) \ ('<br>' if '<ul>' in ss else ' - ') + ss) \
if ss and j != 0 and s else ss if ss and j != 0 and s else ss
except: except:
pass pass
@ -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,15 +965,32 @@ class Crud:
def get_urls(cls): def get_urls(cls):
def _add_base(view): def _add_base(view):
if view:
class CrudViewWithBase(cls.BaseMixin, view): if not view:
model = cls.model return
help_topic = cls.help_topic
crud = cls
CrudViewWithBase.__name__ = view.__name__ if not cls.__abstract__:
return CrudViewWithBase 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):
permission_required = tuple(pr)
model = cls.model
help_topic = cls.help_topic
crud = cls
CrudViewWithBase.__name__ = view.__name__
return CrudViewWithBase
CrudListView = _add_base(cls.ListView) CrudListView = _add_base(cls.ListView)
CrudCreateView = _add_base(cls.CreateView) CrudCreateView = _add_base(cls.CreateView)
@ -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