0

I want to create a policy which would not be linked to any model because I use external APIs in this sense so therefore I store the information in the Session example I have Session::get('isAdmin') but since that does not not working

I have used the two following code already without success

public function isAdmin(Session $session)
{
    if ($session->get) {
        return true;
    }
}

and

public function isAdmin()
{
    if (Session::get('isAdmin')) {
        return true;
    }
}

Session::get('isAdmin') is an boolean

For the moment I just use it in my saw template blade and I do

@can('isAmin')
    <li class="nav-item @yield('user')"><a href="{{ url('/users/new') }}" class="nav-link"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-file-plus"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline><line x1="12" y1="18" x2="12" y2="12"></line><line x1="9" y1="15" x2="15" y2="15"></line></svg><span> Ajouter un Utilisateurs</span></a></li>
  @endcan
WillDev
  • 16
  • 2
  • Welcome. Have you written policy class? How it is used in controller? From what class are these code blocks? – Tpojka Feb 07 '20 at 20:51
  • I have updated the question – WillDev Feb 07 '20 at 22:57
  • You want to hide certain block on page for non-admin user? Have you tried `@if (\Session::get('isAdmin')) /** code */ @endif`? – Tpojka Feb 07 '20 at 23:11
  • it works but i was wondering how to do it with the policy – WillDev Feb 07 '20 at 23:37
  • Did you check samples in [docs](https://laravel.com/docs/master/authorization) of how blade directives look or being created and how to create policy? – Tpojka Feb 07 '20 at 23:40
  • For me it's pretty simple: just use `@if` directive. Even more, you should pass variable from controller `$isAdmin = \Session::has('isAdmin') && Session::get('isAdmin')` to not having class manipulation in blade file. I don't see the point having not made `@can('isAdmin')` compared to existing `@if('isAdmin')`. :/ – Tpojka Feb 08 '20 at 00:05
  • I understand but in fact the HTML element that I want to hide is in the menu so for your solution to work it would be necessary that I pass to each function to control the variable isAdmin which is more tedious than doing directly @if(\ Session::get ( 'isAdmin')) – WillDev Feb 11 '20 at 09:05

0 Answers0