Skip to main content
BackpackForLaravel

Backpack for Laravel roles and permissions for the Users module

By May 27, 2021July 19th, 2021No Comments

All details can be found here about the package.

A thing to note here is the package comes with the roles and permissions manageable out of the box. Check out this part for customizing on the user’s module.

We can use our own UsersCrudController as it is far more likely there will be some customization required. For whatever we require from their setup we can copy from their file.

For example: the following gives the manageable roles fields. Copied from their file as is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
$this->crud->addFields([
      [
          'tab' => 'Roles',
        // two interconnected entities
        'label'             => trans('backpack::permissionmanager.user_role_permission'),
        'field_unique_name' => 'user_role_permission',
        'type'              => 'checklist_dependency',
        'name'              => ['roles', 'permissions'],
        'subfields'         => [
            'primary' => [
                'label'            => trans('backpack::permissionmanager.roles'),
                'name'             => 'roles', // the method that defines the relationship in your Model
                'entity'           => 'roles', // the method that defines the relationship in your Model
                'entity_secondary' => 'permissions', // the method that defines the relationship in your Model
                'attribute'        => 'name', // foreign key attribute that is shown to user
                'model'            => config('permission.models.role'), // foreign key model
                'pivot'            => true, // on create&update, do you need to add/delete pivot table entries?]
                'number_columns'   => 3, //can be 1,2,3,4,6
            ],
            'secondary' => [
                'label'          => ucfirst(trans('backpack::permissionmanager.permission_singular')),
                'name'           => 'permissions', // the method that defines the relationship in your Model
                'entity'         => 'permissions', // the method that defines the relationship in your Model
                'entity_primary' => 'roles', // the method that defines the relationship in your Model
                'attribute'      => 'name', // foreign key attribute that is shown to user
                'model'          => config('permission.models.permission'), // foreign key model
                'pivot'          => true, // on create&update, do you need to add/delete pivot table entries?]
                'number_columns' => 3, //can be 1,2,3,4,6
            ],
        ],
    ],
]);

Gotcha #1

If you are using Mutator for the password field on the Users model. Be sure to update the code accordingly. Here’s what I mean:

1
2
3
4
5
6
7
8
        // Encrypt password if specified.
        if ($request->input('password')) {
//            $request->request->set('password', Hash::make($request->input('password')));
            // Removed hash because User model has a mutator on the password.
            $request->request->set('password', $request->input('password'));
        } else {
            $request->request->remove('password');
        }

Gotcha #2

When the password is updated user is logged out and if you are using prefix, you are logged in to /login which is not even your admin login page.

First, let’s understand why we are logged out? Read this post.

The backpack version of solution to it is the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
     * Update the specified resource in the database.
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update()
    {

        $this->crud->setRequest($this->crud->validateRequest());
        $this->crud->setRequest($this->handlePasswordInput($this->crud->getRequest()));
        $this->crud->unsetValidation(); // validation has already been run

        $return = $this->traitUpdate();

        /**
         * Otherwise the use will be logged out when the password is changed!
         * @source https://stackoverflow.com/questions/47147988/prevent-logout-after-updating-user-password-in-laravel-5-5
         */
        Auth::loginUsingId(Request::segment(3));

        return $return;
    }