UserCrudController::handlePasswordInput()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 18
rs 9.9666
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Events\UserCreated;
6
use App\Events\UserUpdated;
7
use App\Http\Requests\UserStoreCrudRequest as StoreRequest;
8
use App\Http\Requests\UserUpdateCrudRequest as UpdateRequest;
9
use Backpack\CRUD\app\Http\Controllers\CrudController;
10
use Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
11
use Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
12
use Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
13
use Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
14
use Backpack\CRUD\app\Http\Requests\CrudRequest;
15
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
16
use Backpack\PermissionManager\app\Models\Role;
17
18
class UserCrudController extends CrudController
19
{
20
    use ListOperation;
0 ignored issues
show
introduced by
The trait Backpack\CRUD\app\Http\C...perations\ListOperation requires some properties which are not provided by App\Http\Controllers\Admin\UserCrudController: $model, $query, $entity_name_plural
Loading history...
21
    use CreateOperation { store as traitStore; }
0 ignored issues
show
Bug introduced by
The trait Backpack\CRUD\app\Http\C...rations\CreateOperation requires the property $entity_name which is not provided by App\Http\Controllers\Admin\UserCrudController.
Loading history...
22
    use UpdateOperation { update as traitUpdate; }
0 ignored issues
show
introduced by
The trait Backpack\CRUD\app\Http\C...rations\UpdateOperation requires some properties which are not provided by App\Http\Controllers\Admin\UserCrudController: $entity_name, $model
Loading history...
23
    use DeleteOperation;
24
25
    public function setup()
26
    {
27
        CRUD::setModel(config('backpack.permissionmanager.models.user'));
28
        CRUD::setEntityNameStrings(trans('backpack::permissionmanager.user'), trans('backpack::permissionmanager.users'));
29
        CRUD::setRoute(backpack_url('user'));
30
    }
31
32
    public function setupListOperation()
33
    {
34
        // Columns.
35
        CRUD::setColumns([
36
            [
37
                'label' => __('First Name'),
38
                'type' => 'text',
39
                'name' => 'firstname',
40
            ],
41
            [
42
                'label' => __('Last Name'),
43
                'type' => 'text',
44
                'name' => 'lastname',
45
            ],
46
            [
47
                'name' => 'email',
48
                'label' => trans('backpack::permissionmanager.email'),
49
                'type' => 'email',
50
            ],
51
            [ // n-n relationship (with pivot table)
52
                'label' => trans('backpack::permissionmanager.roles'),
53
                'type' => 'select_multiple',
54
                'name' => 'roles',
55
                'entity' => 'roles',
56
                'attribute' => 'name',
57
                'model' => config('permission.models.role'),
58
            ],
59
60
        ]);
61
62
        // Role Filter
63
        $this->crud->addFilter(
64
            [
65
                'name' => 'role',
66
                'type' => 'dropdown',
67
                'label' => trans('backpack::permissionmanager.role'),
68
            ],
69
            config('permission.models.role')::all()->pluck(['name', 'id'])->toArray(),
70
            function ($value) {
71
                $this->crud->addClause('whereHas', 'roles', function ($query) use ($value) {
72
                    $query->where('role_id', '=', $value);
73
                });
74
            }
75
        );
76
    }
77
78
    protected function setupCreateOperation()
79
    {
80
        $this->crud->setValidation(StoreRequest::class);
81
        $this->addFields();
82
    }
83
84
    protected function setupUpdateOperation()
85
    {
86
        $this->crud->setValidation(UpdateRequest::class);
87
        $this->addFields();
88
    }
89
90
    /**
91
     * Store a newly created resource in the database.
92
     */
93
    public function store(StoreRequest $request)
94
    {
95
        $this->handlePasswordInput($request);
0 ignored issues
show
Bug introduced by
$request of type App\Http\Requests\UserStoreCrudRequest is incompatible with the type Backpack\CRUD\app\Http\Requests\CrudRequest expected by parameter $request of App\Http\Controllers\Adm...::handlePasswordInput(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
        $this->handlePasswordInput(/** @scrutinizer ignore-type */ $request);
Loading history...
96
        $response = $this->traitStore();
97
98
        $user = $this->crud->getCurrentEntry();
99
        UserCreated::dispatch($user, $request->input('password'));
100
101
        return $response;
102
    }
103
104
    /**
105
     * Update the specified resource in the database.
106
     */
107
    public function update(UpdateRequest $request)
108
    {
109
        $this->handlePasswordInput($request);
0 ignored issues
show
Bug introduced by
$request of type App\Http\Requests\UserUpdateCrudRequest is incompatible with the type Backpack\CRUD\app\Http\Requests\CrudRequest expected by parameter $request of App\Http\Controllers\Adm...::handlePasswordInput(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

109
        $this->handlePasswordInput(/** @scrutinizer ignore-type */ $request);
Loading history...
110
        $user = $this->crud->getCurrentEntry();
111
        UserUpdated::dispatch($user, $request->input('password'));
112
113
        return $this->traitUpdate();
114
    }
115
116
    /**
117
     * Add the fields needed in the Create and Update operations.
118
     */
119
    protected function addFields()
120
    {
121
        $this->crud->addFields([
122
            [  // Select2
123
                'label' => trans('firstname'),
124
                'type' => 'text',
125
                'name' => 'firstname',
126
            ],
127
            [  // Select2
128
                'label' => trans('lastname'),
129
                'type' => 'text',
130
                'name' => 'lastname',
131
            ],
132
            [
133
                'name' => 'email',
134
                'label' => trans('backpack::permissionmanager.email'),
135
                'type' => 'email',
136
            ],
137
            [
138
                'name' => 'password',
139
                'label' => trans('password'),
140
                'type' => 'password',
141
            ],
142
143
            [
144
                'label' => 'Roles',
145
                'type' => 'checklist',
146
                'name' => 'roles',
147
                'entity' => 'roles',
148
                'attribute' => 'name',
149
                'model' => Role::class,
150
                'pivot' => true,
151
            ],
152
        ]);
153
    }
154
155
    /**
156
     * Handle password input fields.
157
     *
158
     * @param CrudRequest $request
159
     */
160
    protected function handlePasswordInput($request)
161
    {
162
        $crud_request = $this->crud->getRequest();
163
164
        // If a password was specified
165
        if ($request->input('password')) {
166
            // encrypt it before storing it
167
            $hashed_password = bcrypt($request->input('password'));
168
169
            $crud_request->request->set('password', $hashed_password);
170
            $crud_request->request->set('password_confirmation', $hashed_password);
171
        } else {
172
            // ignore the password inputs entirely
173
            $crud_request->request->remove('password');
174
            $crud_request->request->remove('password_confirmation');
175
        }
176
177
        $this->crud->setRequest($crud_request);
178
    }
179
}
180