Skip to content

Commit

Permalink
fixed table action permission issue
Browse files Browse the repository at this point in the history
  • Loading branch information
cuneytsenturk committed May 22, 2023
1 parent 4f31721 commit 311b6c7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 60 deletions.
2 changes: 1 addition & 1 deletion app/Models/Common/Company.php
Expand Up @@ -548,7 +548,7 @@ public function getLineActionsAttribute()
'title' => trans('general.switch'),
'icon' => 'settings_ethernet',
'url' => route('companies.switch', $this->id),
'permission' => 'read-common-companies',
//'permission' => 'read-common-companies', remove this permission to allow switching to any company
'attributes' => [
'id' => 'index-line-actions-switch-company-' . $this->id,
],
Expand Down
115 changes: 56 additions & 59 deletions resources/views/components/table/actions.blade.php
Expand Up @@ -8,73 +8,70 @@
@else
<div class="absolute ltr:right-12 rtl:left-12 -top-4 hidden items-center group-hover:flex">
@foreach ($actions as $action)
@if (! empty($action['permission']))
@can($action['permission'])
@endif

@if ($count_buttons > 3 && $loop->count > 4)
@break
@endif

@php
$type = ! empty($action['type']) ? $action['type'] : 'link';
@endphp

@switch($type)
@case('button')
<button type="button" class="relative bg-white hover:bg-gray-100 border py-0.5 px-1 cursor-pointer index-actions group/tooltip" {!! $action['attributes'] ?? null !!}>
<span class="material-icons-outlined text-purple text-lg pointer-events-none">
{{ $action['icon'] }}
</span>

<div class="inline-block absolute invisible z-20 py-1 px-2 text-sm font-medium text-gray-900 bg-white rounded-lg border border-gray-200 shadow-sm whitespace-nowrap opacity-0 -top-10 -left-2 group-hover/tooltip:opacity-100 group-hover/tooltip:visible" data-tooltip-placement="top">
<span>{{ $action['title'] }}</span>
<div class="absolute w-2 h-2 -bottom-1 before:content-[' '] before:absolute before:w-2 before:h-2 before:bg-white before:border-gray-200 before:transform before:rotate-45 before:border before:border-t-0 before:border-l-0" data-popper-arrow></div>
</div>
</button>
@break

@case('delete')
@php
$text = $action['text'] ?? null;
$title = $action['title'] ?? null;
$modelId = ! empty($action['model-id']) ? $action['model-id'] : 'id';
$modelName = ! empty($action['model-name']) ? $action['model-name'] : 'name';
@endphp

<x-delete-button :model="$action['model']" :route="$action['route']" :title="$title" :text="$text" :model-id="$modelId" :model-name="$modelName" />
@break

@default
<a href="{{ $action['url'] }}" class="relative bg-white hover:bg-gray-100 border py-0.5 px-1 cursor-pointer index-actions group/tooltip" {!! $action['attributes'] ?? null !!}>
<span class="material-icons-outlined text-purple text-lg pointer-events-none">
{{ $action['icon'] }}
</span>

<div class="inline-block absolute invisible z-20 py-1 px-2 text-sm font-medium text-gray-900 bg-white rounded-lg border border-gray-200 shadow-sm whitespace-nowrap opacity-0 -top-10 -left-2 group-hover/tooltip:opacity-100 group-hover/tooltip:visible" data-tooltip-placement="top">
<span>{{ $action['title'] }}</span>
<div class="absolute w-2 h-2 -bottom-1 before:content-[' '] before:absolute before:w-2 before:h-2 before:bg-white before:border-gray-200 before:transform before:rotate-45 before:border before:border-t-0 before:border-l-0" data-popper-arrow></div>
</div>
</a>
@endswitch

@php
array_shift($actions);
$count_buttons++;
@endphp

@if (! empty($action['permission']))
@endcan
@if (
empty($action['permission'])
|| (! empty($action['permission']) && user()->can($action['permission']))
)

@php
$type = ! empty($action['type']) ? $action['type'] : 'link';
@endphp

@switch($type)
@case('button')
<button type="button" class="relative bg-white hover:bg-gray-100 border py-0.5 px-1 cursor-pointer index-actions group/tooltip" {!! $action['attributes'] ?? null !!}>
<span class="material-icons-outlined text-purple text-lg pointer-events-none">
{{ $action['icon'] }}
</span>

<div class="inline-block absolute invisible z-20 py-1 px-2 text-sm font-medium text-gray-900 bg-white rounded-lg border border-gray-200 shadow-sm whitespace-nowrap opacity-0 -top-10 -left-2 group-hover/tooltip:opacity-100 group-hover/tooltip:visible" data-tooltip-placement="top">
<span>{{ $action['title'] }}</span>
<div class="absolute w-2 h-2 -bottom-1 before:content-[' '] before:absolute before:w-2 before:h-2 before:bg-white before:border-gray-200 before:transform before:rotate-45 before:border before:border-t-0 before:border-l-0" data-popper-arrow></div>
</div>
</button>
@break

@case('delete')
@php
$text = $action['text'] ?? null;
$title = $action['title'] ?? null;
$modelId = ! empty($action['model-id']) ? $action['model-id'] : 'id';
$modelName = ! empty($action['model-name']) ? $action['model-name'] : 'name';
@endphp

<x-delete-button :model="$action['model']" :route="$action['route']" :title="$title" :text="$text" :model-id="$modelId" :model-name="$modelName" />
@break

@default
<a href="{{ $action['url'] }}" class="relative bg-white hover:bg-gray-100 border py-0.5 px-1 cursor-pointer index-actions group/tooltip" {!! $action['attributes'] ?? null !!}>
<span class="material-icons-outlined text-purple text-lg pointer-events-none">
{{ $action['icon'] }}
</span>

<div class="inline-block absolute invisible z-20 py-1 px-2 text-sm font-medium text-gray-900 bg-white rounded-lg border border-gray-200 shadow-sm whitespace-nowrap opacity-0 -top-10 -left-2 group-hover/tooltip:opacity-100 group-hover/tooltip:visible" data-tooltip-placement="top">
<span>{{ $action['title'] }}</span>
<div class="absolute w-2 h-2 -bottom-1 before:content-[' '] before:absolute before:w-2 before:h-2 before:bg-white before:border-gray-200 before:transform before:rotate-45 before:border before:border-t-0 before:border-l-0" data-popper-arrow></div>
</div>
</a>
@endswitch

@php
array_shift($actions);
$count_buttons++;
@endphp
@endif
@endforeach

@foreach ($actions as $action)
@if (! empty($action['permission']))
@can($action['permission'])
@php $more_actions[] = $action; @endphp
@endcan
@else
@if (
empty($action['permission'])
|| (! empty($action['permission']) && user()->can($action['permission']))
)
@php $more_actions[] = $action; @endphp
@endif
@endforeach
Expand Down

0 comments on commit 311b6c7

Please sign in to comment.