D
D
Dmitry Kuznetsov2018-05-23 11:07:27
Laravel
Dmitry Kuznetsov, 2018-05-23 11:07:27

How to display all permissions for one role and check if the role is activated?

Good afternoon. For my project I use this package: github for permissions and roles.
The essence of the task is as follows:
We go into the editor of a thread of the role. All rights that exist should be displayed there. There are no problems in this part. Next, the most difficult thing for me is to check whether this permission is activated for this role or not.
Controller code :

public function edit(int $id){
        if(auth()->user()->hasPermissionTo('admin.roles.edit')){
            $role = Role::with('permissions')->find($id);
            $permissions = Permission::all();

            dd($role);

            Breadcrumbs::addBreadcrumb('Роль: "' . $role->title . '"', route('admin.roles'));

            return view('admin.roles.edit', compact('role', 'permissions'));
        }else{
            flash('У вас не достаточно прав!')->warning();
            return redirect()->intended(route('admin.roles'));
        }
    }

View code :
@foreach($permissions as $permission)
    {{ Form::checkbox('permissions[]',  $permission->id ) }}
    {{ Form::label($permission->name, $permission->name) }}<br>
@endforeach

There is also another option how to do this - just manually add a list and check through the conditions whether this permission is activated or not - but I don’t really want to use this option yet, only if my code doesn’t help in any way.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ukolov, 2018-05-23
@dima9595

From documentation:
It's not clear from the question what exactly is causing the problem, so I can't give a more precise answer.

B
bastian19, 2018-07-13
@bastian19

Status: 
@if(Auth::user()->odmen == '1')  <h>ADMIN</h> @endif 

@if(Auth::user()->yb == '1' && Auth::user()->odmen == '0') <h>YOUTUBER</h>
@endif    

@if(Auth::user()->yb == '1' && Auth::user()->premium == '1' ) 
@endif 

@if(Auth::user()->yb == '1' && Auth::user()->odmen == '1' ) 
@endif 

@if(Auth::user()->premium == '1' && Auth::user()->odmen == '0' && Auth::user()->yb == '0' )  <h>PREMIUM</h>  
@endif 

@if(Auth::user()->premium == '0' && Auth::user()->odmen == '0' && Auth::user()->yb == '0' ) <h>USER</h>  
@endif

Everything works, regardless of the quality of the code)
I have four user statuses. And in such a simple way, I display it in the template.
At the same time, setting the logic of the status hierarchy: Admin -> YouTuber -> Premium -> User

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question