romanbican / roles

Powerful package for handling roles and permissions in Laravel 5
MIT License
1.16k stars 296 forks source link

managing permissions by role #195

Closed RosiersRobin closed 8 years ago

RosiersRobin commented 8 years ago

I'm currently trying to make this work:

http://image.prntscr.com/image/ac213fa5acc9477f912623f07f2f0576.png

Using this package to manage the roles.

However, this doesn't seem to work out for me. So I want to be able to manage the permissions by role. Like Role 'member' needs to have specific perms, but if necessary, I want to be able to add or remove those permissions. I really don't know how I should fix this and I'm stuck at the moment I need to show if the user has the permission enabled or not.

This is my current code:

HTML:

<table class="table">
   <thead>
      <tr>
        <th>Naam</th>
        <th>Beschrijving</th>
        <th>Status</th>
        <th></th>
      </tr>
    </thead>
    <tbody>
      @foreach($getperms as $perm)
         <tr>
           <td>{{ $perm->name }}</td>
           <td>{{ $perm->description }}</td>
           <td><span class="label label-danger">Not enabled</span></td>
           <td><input type="checkbox" name=""></td>
          </tr>
       @endforeach
      </tbody>
</table>

And this is my controller:

/**
     * editUserPerms
     *
     * @return Response
     */
    public function editRolePerms($id)
    {
      $getrole = Role::find($id);

      $getperms = Permission::all();

      return view('admin.editroleperms')->with('role', $getrole)->with('getperms', $getperms);
    }

If someone could help me out with this, so I'm able to grant or revoke permissions and see if the permission is nabled or not would be awesome.

CallumCarmicheal commented 8 years ago

@Advil-Robin Have you found a solution for your problem yet? If not i may be able to provide assistance. I am currently doing the same thing well actually i got the idea by your post (thanks for that), if i get it working ill share my code when and if i do get it working.

RosiersRobin commented 8 years ago

Hi @CallumCarmicheal !

Still didn't find the solution :(

Hope you can help me out!

CallumCarmicheal commented 8 years ago

@Advil-Robin I am currently doing a livestream on LiveCoding.TV, i am trying my method of doing this. Just to confirm you want a interactive (Disable/Enabled toggle) for each permission on a user / role right?

Link to stream: https://livecoding.tv/callumc

RosiersRobin commented 8 years ago

Yes so what I want is the ability to toggle permissions on or off on a role.

CallumCarmicheal commented 8 years ago

@Advil-Robin I've written a way to get the permissions in a role, so the only idea i can think of is to make another model and in that have the following methods, id, name, slug, description and inRole - bool. With that information you can get the perm with a id and the inRole property will tell you if the permission is in the role. Ill write the code for that now. Is that what you needed?

CallumCarmicheal commented 8 years ago

@Advil-Robin You want something like this right? db637c768f8c72b7beb8a7ac91b438ea

image

RosiersRobin commented 8 years ago

Euh I think so... if you look at the screen, that is what i like to make work.

CallumCarmicheal commented 8 years ago

@Advil-Robin Im uploading my code to github with a example. First im finding out what changes i have made to the source. Are you using Laravel 5.3?

RosiersRobin commented 8 years ago

@CallumCarmicheal I'm using Laravel 5.1, but I can modify the code more. I just need a sort of base.

CallumCarmicheal commented 8 years ago

@Advil-Robin Okay i have posted my solution on the repo, with a little example of flipping the roles. Every permission in the role is removed and every permission that is not in the role is added. So it flips the Enabled status. Take a look here. Hope this helps :)

Sidenote: i also added in a little sorting feature that can sort the slug or name. (it can be disabled if you read the params).

CallumCarmicheal commented 8 years ago

A example you could use for this is using the RolePermissions to get and list of all the permissions and format them into the table, to get the permission $rp->permission->name, and to enable/disable the permission you can use the object or when printing to the page store a hidden attribute on the button to toggle the row set it to the id of the permission and with that id you can attach the permission to the user. Thats how i am going to do it, maybe that makes sense or maybe not... But hope this helps XD

This is what i mean: Gif of my role permission manager

RosiersRobin commented 8 years ago

I've made something my own wich works really well.

Image of the system

With this I didn't need to change any code. I just checked if the permission exists in the database and if so, it's active, if not, then display as not active.

jonjie0317 commented 7 years ago

@Advil-Robin Hm, Im having the same issue. What did you do to check if the role has a specific permission? so confusing.

RosiersRobin commented 7 years ago

@jonjie0317 the tables are linked to each other, so you can just execute a query to check if the permission has a fole id.

jonjie0317 commented 7 years ago

@Advil-Robin what query you used?

jonjie0317 commented 7 years ago

@Advil-Robin I used <input type="checkbox" name="status" id="{{ $permission->slug }}"{{ ($permission->exists()) ? ' checked' : '' }}> {{ $permission->name }} , and it returns all the permissions with all checked

RosiersRobin commented 7 years ago

@jonjie0317 What do you want to know, the permissions by the roles? Then I have this code:

@foreach($getperms as $perm)
                                    <tr>
                                        <td>{{ $perm->name }}</td>
                                        <td>{{ $perm->description }}</td>
                                        @if(!in_array($perm->id, $permbyrole))
                                            <td><span id="{{ $perm->id }}" class="label label-danger">Not enabled</span></td>
                                            <td><input onclick="toggleRole({{ $perm->id }}, {{ $role->id }})" type="checkbox" id="checkbox_{{ $perm->id }}"></td>
                                        @else
                                            <td><span id="{{ $perm->id }}" class="label label-success">Enabled</span></td>
                                            <td><input onclick="toggleRole({{ $perm->id }}, {{ $role->id }})" type="checkbox" checked="true" id="checkbox_{{ $perm->id }}"></td>
                                        @endif
                                    </tr>
                                @endforeach
jonjie0317 commented 7 years ago

@Advil-Robin I got the idea on how to struct the table. hmm Just want to check all the permissions that the role has. For example, I want to list all the permissions, and if the id of the role passed in the uri have those permissions then, checked it using <input type="checkbox"> otherwise checkbox uncheck. Thanks a lot men

jonjie0317 commented 7 years ago

@Advil-Robin I need your help men :(

RosiersRobin commented 7 years ago

@jonjie0317 Make a question on [http://stackoverflow.com/](http://stackoverflow.com/, this is really not the place to help with such full questions. Post your link to it in a comment and I'll look at it.

jonjie0317 commented 7 years ago

@Advil-Robin here's the link sir http://stackoverflow.com/questions/43859917/how-to-get-list-of-permissions-in-a-role

arulmurugan198 commented 7 years ago

test open