Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Push — master ( 337e88...70cbb1 )
by Cristian
11:08
created

UserCrudController::store()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 4 Features 1
Metric Value
c 5
b 4
f 1
dl 0
loc 23
rs 9.0856
cc 2
eloc 11
nc 2
nop 1
1
<?php
2
3
namespace Backpack\PermissionManager\app\Http\Controllers;
4
5
use App\Http\Requests;
6
use Backpack\CRUD\app\Http\Controllers\CrudController;
7
use Backpack\PermissionManager\app\Http\Requests\UserStoreCrudRequest as StoreRequest;
8
// VALIDATION
9
use Backpack\PermissionManager\app\Http\Requests\UserUpdateCrudRequest as UpdateRequest;
10
use Illuminate\Http\Request;
11
12
class UserCrudController extends CrudController
13
{
14
    public function setup()
15
    {
16
        $this->crud->setModel(config('backpack.permissionmanager.user_model'));
17
        $this->crud->setEntityNameStrings(trans('backpack::permissionmanager.user'), trans('backpack::permissionmanager.users'));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
18
        $this->crud->setRoute(config('backpack.base.route_prefix').'/user');
19
        $this->crud->enableAjaxTable();
20
21
        $this->crud->setColumns([
22
            [
23
                'name'  => 'name',
24
                'label' => trans('backpack::permissionmanager.name'),
25
                'type'  => 'text',
26
            ],
27
            [
28
                'name'  => 'email',
29
                'label' => trans('backpack::permissionmanager.email'),
30
                'type'  => 'email',
31
            ],
32
        ]);
33
34
        $this->crud->addColumn([ // n-n relationship (with pivot table)
35
           'label'     => trans('backpack::permissionmanager.roles'), // Table column heading
36
           'type'      => 'select_multiple',
37
           'name'      => 'roles', // the method that defines the relationship in your Model
38
           'entity'    => 'roles', // the method that defines the relationship in your Model
39
           'attribute' => 'name', // foreign key attribute that is shown to user
40
           'model'     => "Backpack\PermissionManager\app\Models\Roles", // foreign key model
41
        ]);
42
43
        $this->crud->addColumn([ // n-n relationship (with pivot table)
44
           'label'     => trans('backpack::permissionmanager.extra_permissions'), // Table column heading
45
           'type'      => 'select_multiple',
46
           'name'      => 'permissions', // the method that defines the relationship in your Model
47
           'entity'    => 'permissions', // the method that defines the relationship in your Model
48
           'attribute' => 'name', // foreign key attribute that is shown to user
49
           'model'     => "Backpack\PermissionManager\app\Models\Permission", // foreign key model
50
        ]);
51
52
        $this->crud->addFields([
53
            [
54
                'name'  => 'name',
55
                'label' => trans('backpack::permissionmanager.name'),
56
                'type'  => 'text',
57
            ],
58
            [
59
                'name'  => 'email',
60
                'label' => trans('backpack::permissionmanager.email'),
61
                'type'  => 'email',
62
            ],
63
            [
64
                'name'  => 'password',
65
                'label' => trans('backpack::permissionmanager.password'),
66
                'type'  => 'password',
67
            ],
68
            [
69
                'name'  => 'password_confirmation',
70
                'label' => trans('backpack::permissionmanager.password_confirmation'),
71
                'type'  => 'password',
72
            ],
73
            [
74
            // two interconnected entities
75
            'label'             => trans('backpack::permissionmanager.user_role_permission'),
76
            'field_unique_name' => 'user_role_permission',
77
            'type'              => 'checklist_dependency',
78
            'name'              => 'roles_and_permissions', // the methods that defines the relationship in your Model
79
            'subfields'         => [
80
                    'primary' => [
81
                        'label'            => trans('backpack::permissionmanager.roles'),
82
                        'name'             => 'roles', // the method that defines the relationship in your Model
83
                        'entity'           => 'roles', // the method that defines the relationship in your Model
84
                        'entity_secondary' => 'permissions', // the method that defines the relationship in your Model
85
                        'attribute'        => 'name', // foreign key attribute that is shown to user
86
                        'model'            => "Backpack\PermissionManager\app\Models\Role", // foreign key model
87
                        'pivot'            => true, // on create&update, do you need to add/delete pivot table entries?]
88
                        'number_columns'   => 3, //can be 1,2,3,4,6
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
89
                    ],
90
                    'secondary' => [
91
                        'label'          => ucfirst(trans('backpack::permissionmanager.permission_singular')),
92
                        'name'           => 'permissions', // the method that defines the relationship in your Model
93
                        'entity'         => 'permissions', // the method that defines the relationship in your Model
94
                        'entity_primary' => 'roles', // the method that defines the relationship in your Model
95
                        'attribute'      => 'name', // foreign key attribute that is shown to user
96
                        'model'          => "Backpack\PermissionManager\app\Models\Permission", // foreign key model
97
                        'pivot'          => true, // on create&update, do you need to add/delete pivot table entries?]
98
                        'number_columns' => 3, //can be 1,2,3,4,6
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
99
                    ],
100
                ],
101
            ],
102
        ]);
103
    }
104
105
    /**
106
     * Store a newly created resource in the database.
107
     *
108
     * @param StoreRequest $request - type injection used for validation using Requests
109
     *
110
     * @return \Illuminate\Http\RedirectResponse
111
     */
112
    public function store(StoreRequest $request)
113
    {
114
        $this->crud->hasAccessOrFail('create');
115
116
        // insert item in the db
117
        if ($request->input('password')) {
118
            $item = $this->crud->create(\Request::except(['redirect_after_save']));
119
120
            // now bcrypt the password
121
            $item->password = bcrypt($request->input('password'));
0 ignored issues
show
Bug introduced by
It seems like $request->input('password') targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type array; however, bcrypt() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
122
            $item->save();
123
        } else {
124
            $item = $this->crud->create(\Request::except(['redirect_after_save', 'password']));
125
        }
126
127
        // show a success message
128
        \Alert::success(trans('backpack::crud.insert_success'))->flash();
129
130
        // save the redirect choice for next time
131
        $this->setSaveAction();
132
133
        return $this->performSaveAction($item->getKey());
134
    }
135
136
    public function update(UpdateRequest $request)
137
    {
138
        //encrypt password and set it to request
139
        $this->crud->hasAccessOrFail('update');
140
141
        $dataToUpdate = \Request::except(['redirect_after_save', 'password']);
142
143
        //encrypt password
144
        if ($request->input('password')) {
145
            $dataToUpdate['password'] = bcrypt($request->input('password'));
0 ignored issues
show
Bug introduced by
It seems like $request->input('password') targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type array; however, bcrypt() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
146
        }
147
148
        // update the row in the db
149
        $this->crud->update(\Request::get($this->crud->model->getKeyName()), $dataToUpdate);
150
151
        // show a success message
152
        \Alert::success(trans('backpack::crud.update_success'))->flash();
153
154
        // save the redirect choice for next time
155
        $this->setSaveAction();
156
157
        return $this->performSaveAction();
158
    }
159
}
160