Skip to content

Commit

Permalink
Merge pull request #2 from vincenzoraco/master
Browse files Browse the repository at this point in the history
Authorization For Each Link
  • Loading branch information
josemanuelsh authored Sep 14, 2019
2 parents 91a48d6 + 9dc2df5 commit fa1af76
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 48 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ To add links to the navigation section you need to add entries to the `links` se
return [
'links' => [
'{TEXT}' => [ // Top level section title text
'_can' => '{PERMISSION}' // The name of the permission
'_icon' => '{ICON}', // SVG icon code (optional)
'_url' => '{URL}', // URL (optional if _links is present)
'_target' => '{TARGET}', // Link target (optional)
'_links' => [ // Section extra links (optional if _url is present
'{TEXT}' => [ // Sub section text
'_can' => '{PERMISSION}' // The name of the permission
'_url' => '{URL}', // URL
'_target' => '{TARGET}', // Link target (optional)
]
Expand Down Expand Up @@ -78,3 +80,24 @@ public function tools()
];
}
```

In addition, you can add the key `_can` in the links array, including the child links as well. We will use the `can()` function to check if the user has the permission to see the links or not.

Please note that if you don't want any authorization checks, do not add the `_can` key.

```php
return [
'links' => [
'{TEXT}' => [ // Top level section title text
'_can' => '{PERMISSION}' // Checks if the link and sublinks can be shown
// ...
'_links' => [ // Section extra links (optional if _url is present
'{TEXT}' => [ // Sub section text
'_can' => '{PERMISSION}' // Checks if the link can be shown
// ...
]
]
]
]
];
```
106 changes: 58 additions & 48 deletions resources/views/navigation.blade.php
Original file line number Diff line number Diff line change
@@ -1,61 +1,71 @@
@foreach (config('custom-links.links') as $name => $link)

@if (empty($link['_url']))

<h3 class="flex items-center font-normal text-white mb-{{ isset($link['_links']) ? '6' : '8' }} text-base no-underline">
@if (empty($link['_icon']))
<svg class="sidebar-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path fill="#B3C1D1" d="M19.48 13.03A4 4 0 0 1 16 19h-4a4 4 0 1 1 0-8h1a1 1 0 0 0 0-2h-1a6 6 0 1 0 0 12h4a6 6 0 0 0 5.21-8.98L21.2 12a1 1 0 1 0-1.72 1.03zM4.52 10.97A4 4 0 0 1 8 5h4a4 4 0 1 1 0 8h-1a1 1 0 0 0 0 2h1a6 6 0 1 0 0-12H8a6 6 0 0 0-5.21 8.98l.01.02a1 1 0 1 0 1.72-1.03z"></path>
</svg>
@else
{!! $link['_icon'] !!}
@endif
<span class="sidebar-label">
@foreach (config('custom-links.links') as $name => $link)

@if(empty($link['_can']) || auth()->user()->can($link['_can']))

@if (empty($link['_url']))

<h3 class="flex items-center font-normal text-white mb-{{ isset($link['_links']) ? '6' : '8' }} text-base no-underline">
@if (empty($link['_icon']))
<svg class="sidebar-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path fill="#B3C1D1"
d="M19.48 13.03A4 4 0 0 1 16 19h-4a4 4 0 1 1 0-8h1a1 1 0 0 0 0-2h-1a6 6 0 1 0 0 12h4a6 6 0 0 0 5.21-8.98L21.2 12a1 1 0 1 0-1.72 1.03zM4.52 10.97A4 4 0 0 1 8 5h4a4 4 0 1 1 0 8h-1a1 1 0 0 0 0 2h1a6 6 0 1 0 0-12H8a6 6 0 0 0-5.21 8.98l.01.02a1 1 0 1 0 1.72-1.03z"></path>
</svg>
@else
{!! $link['_icon'] !!}
@endif
<span class="sidebar-label">
{{ $name }}
</span>
</h3>

@else

<a href="{{ $link['_url'] }}"
class="cursor-pointer flex items-center font-normal dim text-white mb-{{ isset($link['_links']) ? '6' : '8' }} text-base no-underline"
target="{{ isset($link['_target']) ? $link['_target'] : '_self' }}">
@if (empty($link['_icon']))
<svg class="sidebar-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path fill="#B3C1D1"
d="M19.48 13.03A4 4 0 0 1 16 19h-4a4 4 0 1 1 0-8h1a1 1 0 0 0 0-2h-1a6 6 0 1 0 0 12h4a6 6 0 0 0 5.21-8.98L21.2 12a1 1 0 1 0-1.72 1.03zM4.52 10.97A4 4 0 0 1 8 5h4a4 4 0 1 1 0 8h-1a1 1 0 0 0 0 2h1a6 6 0 1 0 0-12H8a6 6 0 0 0-5.21 8.98l.01.02a1 1 0 1 0 1.72-1.03z"></path>
</svg>
@else
{!! $link['_icon'] !!}
@endif
<span class="text-white sidebar-label">
{{ $name }}
</span>
</h3>

@else
</a>

<a href="{{ $link['_url'] }}" class="cursor-pointer flex items-center font-normal dim text-white mb-{{ isset($link['_links']) ? '6' : '8' }} text-base no-underline" target="{{ isset($link['_target']) ? $link['_target'] : '_self' }}">
@if (empty($link['_icon']))
<svg class="sidebar-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path fill="#B3C1D1" d="M19.48 13.03A4 4 0 0 1 16 19h-4a4 4 0 1 1 0-8h1a1 1 0 0 0 0-2h-1a6 6 0 1 0 0 12h4a6 6 0 0 0 5.21-8.98L21.2 12a1 1 0 1 0-1.72 1.03zM4.52 10.97A4 4 0 0 1 8 5h4a4 4 0 1 1 0 8h-1a1 1 0 0 0 0 2h1a6 6 0 1 0 0-12H8a6 6 0 0 0-5.21 8.98l.01.02a1 1 0 1 0 1.72-1.03z"></path>
</svg>
@else
{!! $link['_icon'] !!}
@endif
<span class="text-white sidebar-label">
{{ $name }}
</span>
</a>

@endif
@endif

@if (isset($link['_links']))
@if (isset($link['_links']))

@if (count($link['_links']))
@if (count($link['_links']))

<ul class="list-reset mb-8">
<ul class="list-reset mb-8">

@foreach ($link['_links'] as $name => $sublink)
@foreach ($link['_links'] as $name => $sublink)

<li class="leading-tight mb-4 ml-8 text-sm">
<a href="{{ $sublink['_url'] }}" class="text-white text-justify no-underline dim" target="{{ isset($sublink['_target']) ? $sublink['_target'] : '_self' }}">
{{ $name }}
</a>
</li>
@if(empty($sublink['_can']) || auth()->user()->can($sublink['_can']))

@endforeach
<li class="leading-tight mb-4 ml-8 text-sm">
<a href="{{ $sublink['_url'] }}" class="text-white text-justify no-underline dim"
target="{{ isset($sublink['_target']) ? $sublink['_target'] : '_self' }}">
{{ $name }}
</a>
</li>

</ul>
@endif

@endif
@endforeach

@endif
</ul>

@endif

@endif

@endif

{{-- @if (is_array($link))
Expand All @@ -73,10 +83,10 @@
@endforeach
</ul>
@endif
@else
@endif
@else
@endif --}}
@endforeach

@endforeach

0 comments on commit fa1af76

Please sign in to comment.