Passed
Push — master ( db1581...a5af1f )
by Ferry
04:30
created

DeveloperUsersController::getDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: User
5
 * Date: 4/25/2019
6
 * Time: 9:28 PM
7
 */
8
9
namespace crocodicstudio\crudbooster\controllers;
10
11
use crocodicstudio\crudbooster\exceptions\CBValidationException;
12
use crocodicstudio\crudbooster\helpers\ModuleGenerator;
13
use Illuminate\Support\Facades\DB;
14
use Illuminate\Support\Facades\Hash;
15
16
class DeveloperUsersController extends Controller
17
{
18
19
    private $view = "crudbooster::dev_layouts.modules.users";
20
21
    public function __construct()
22
    {
23
        view()->share(['page_title'=>'User Management']);
24
    }
25
26
27
    public function getIndex() {
28
        $data = [];
29
        $data['result'] = DB::table("users")
30
            ->join("cb_roles","cb_roles.id","=","cb_roles_id")
31
            ->select("users.*","cb_roles.name as cb_roles_name")
32
            ->get();
33
        return view($this->view.'.index',$data);
34
    }
35
36
    public function getAdd() {
37
        $data = [];
38
        $data['roles'] = DB::table("cb_roles")->get();
39
        return view($this->view.'.add', $data);
40
    }
41
42
    public function postAddSave() {
43
        try {
44
            cb()->validation(['name', 'email','password','cb_roles_id']);
45
46
            $user = [];
47
            $user['name'] = request('name');
48
            $user['email'] = request('email');
49
            $user['password'] = Hash::make(request('password'));
0 ignored issues
show
Bug introduced by
It seems like request('password') can also be of type array; however, parameter $value of Illuminate\Support\Facades\Hash::make() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

49
            $user['password'] = Hash::make(/** @scrutinizer ignore-type */ request('password'));
Loading history...
50
            $user['cb_roles_id'] = request('cb_roles_id');
51
            DB::table('users')->insert($user);
52
53
            return cb()->redirect(route("DeveloperUsersControllerGetIndex"),"New user has been created!","success");
54
55
        } catch (CBValidationException $e) {
56
            return cb()->redirectBack($e->getMessage());
57
        }
58
    }
59
60
    public function getEdit($id) {
61
        $data = [];
62
        $data['row'] = cb()->find("users", $id);
63
        $data['roles'] = DB::table("cb_roles")->get();
64
        return view($this->view.".edit", $data);
65
    }
66
67
    public function postEditSave($id) {
68
        try {
69
            cb()->validation(['name', 'email','cb_roles_id']);
70
71
            $user = [];
72
            $user['name'] = request('name');
73
            $user['email'] = request('email');
74
            if(request('password')) $user['password'] = Hash::make(request('password'));
0 ignored issues
show
Bug introduced by
It seems like request('password') can also be of type array; however, parameter $value of Illuminate\Support\Facades\Hash::make() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

74
            if(request('password')) $user['password'] = Hash::make(/** @scrutinizer ignore-type */ request('password'));
Loading history...
75
            $user['cb_roles_id'] = request('cb_roles_id');
76
            DB::table('users')->where('id',$id)->update($user);
77
78
            return cb()->redirect(route("DeveloperUsersControllerGetIndex"),"The user has been updated!","success");
79
80
        } catch (CBValidationException $e) {
81
            return cb()->redirectBack($e->getMessage());
82
        }
83
    }
84
85
    public function getDelete($id) {
86
        DB::table("users")->where("id",$id)->delete();
87
        return cb()->redirectBack("The user has been deleted!","success");
88
    }
89
90
}