Passed
Push — master ( 62919d...1f65fa )
by Iman
04:23
created

AdminPrivilegesController   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 163
rs 10
c 0
b 0
f 0
wmc 17

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getDelete() 0 10 1
A setTheme() 0 3 1
A postEditSave() 0 23 3
A postAddSave() 0 22 2
A __construct() 0 5 1
A getAdd() 0 22 1
A setButtons() 0 7 1
A cbInit() 0 5 1
A makeForm() 0 6 1
A getEdit() 0 9 2
A makeCols() 0 10 2
A savePermissions() 0 7 1
1
<?php
2
3
namespace Crocodicstudio\Crudbooster\Modules\PrivilegeModule;
4
5
use Crocodicstudio\Crudbooster\controllers\CBController;
6
use Crocodicstudio\Crudbooster\controllers\FormValidator;
7
use Crocodicstudio\Crudbooster\helpers\CRUDBooster;
8
use Illuminate\Support\Facades\DB;
9
use Illuminate\Support\Facades\Request;
10
use Illuminate\Support\Facades\Route;
11
12
class AdminPrivilegesController extends CBController
13
{
14
    /**
15
     * AdminPrivilegesController constructor.
16
     */
17
    public function __construct()
18
    {
19
        $this->table = 'cms_roles';
20
        $this->primaryKey = 'id';
21
        $this->titleField = "name";
22
    }
23
24
    public function cbInit()
25
    {
26
        $this->setButtons();
27
        $this->makeCols();
28
        $this->makeForm();
29
    }
30
31
    public function getAdd()
32
    {
33
        $this->cbLoader();
34
35
        $id = 0;
36
        $data = [
37
            'page_title' => 'Add Data',
38
            'page_menu' => Route::getCurrentRoute()->getActionName(),
39
        ];
40
        $data['moduls'] = $this->table('cms_modules')
41
            ->where('is_protected', 0)
42
            ->select('cms_modules.*',
43
                DB::raw("(select can_see_module from cms_roles_privileges where cms_modules_id = cms_modules.id and cms_roles_id = '$id') as can_see_module"),
44
                DB::raw("(select can_create  from cms_roles_privileges where cms_modules_id = cms_modules.id and cms_roles_id = '$id') as can_create"),
45
                DB::raw("(select can_read    from cms_roles_privileges where cms_modules_id = cms_modules.id and cms_roles_id = '$id') as can_read"),
46
                DB::raw("(select can_edit    from cms_roles_privileges where cms_modules_id = cms_modules.id and cms_roles_id = '$id') as can_edit"),
47
                DB::raw("(select can_delete  from cms_roles_privileges where cms_modules_id  = cms_modules.id and cms_roles_id = '$id') as can_delete"))
48
            ->orderby('name', 'asc')
49
            ->get();
50
51
52
        return view('CbPrivilege::privileges', $data);
53
    }
54
55
    public function postAddSave()
56
    {
57
        $this->cbInit();
58
        app(FormValidator::class)->validate(null, $this->form, $this);
59
        $this->inputAssignment();
60
61
        $this->table()->insert($this->arr);
62
        $id = $this->arr[$this->primaryKey];
63
64
        $this->setTheme();
65
66
        foreach (Request::input('privileges', []) as $moduleId => $data) {
67
            $arrs = array_get_keys($data, ['can_see_module', 'can_create', 'can_read', 'can_edit', 'can_delete'], 0);
68
            $arrs['cms_roles_id'] = $id;
69
            $arrs['cms_modules_id'] = $moduleId;
70
            $this->table('cms_roles_privileges')->insert($arrs);
71
            //$module = DB::table('cms_modules')->where('id', $moduleId)->first();
72
        }
73
74
        CRUDBooster::refreshSessionRoles();
75
76
        CRUDBooster::redirect(CRUDBooster::mainpath(), cbTrans('alert_add_data_success'), 'success');
77
    }
78
79
    public function getEdit($id)
80
    {
81
        $this->cbLoader();
82
83
        $role = $this->findRow($id)->first() ?: new \stdClass();
84
85
        $page_title = cbTrans('edit_data_page_title', ['module' => 'Privilege', 'name' => $role->name]);
86
87
        return view('CbPrivilege::privileges', compact('role', 'page_title', 'id'));
88
    }
89
90
    public function postEditSave($id)
91
    {
92
        $this->cbInit();
93
        $row = CRUDBooster::first($this->table, $id);
94
95
        app(FormValidator::class)->validate($id, $this->form, $this->table);
96
        $this->inputAssignment($id);
97
98
        $this->findRow($id)->update($this->arr);
99
        foreach (request('privileges', []) as $moduleId => $data) {
100
            $arrs = array_get_keys($data, ['can_see_module', 'can_create', 'can_read', 'can_edit', 'can_delete',], 0);
101
            $this->savePermissions($id, $moduleId, $arrs);
102
        }
103
104
        if ($id == cbUser()->cms_roles_id) {
0 ignored issues
show
Bug introduced by
Accessing cms_roles_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
105
            CRUDBooster::refreshSessionRoles();
106
            $this->setTheme();
107
        }
108
109
        CRUDBooster::redirect(CRUDBooster::mainpath(), cbTrans('alert_update_data_success', [
110
            'module' => 'Privilege',
111
            'title' => $row->name,
112
        ]), 'success');
113
    }
114
115
    public function getDelete($id)
116
    {
117
        $this->cbLoader();
118
119
        $row = $this->findRow($id)->first();
120
121
        $this->findRow($id)->delete();
122
        PrivilegeRepo::deleteByRoleId($row->id);
123
124
        CRUDBooster::redirect(CRUDBooster::mainpath(), cbTrans('alert_delete_data_success'), 'success');
125
    }
126
127
    /**
128
     * @param $id
129
     * @param $moduleId
130
     * @param $arrs
131
     * @return mixed
132
     */
133
    private function savePermissions($id, $moduleId, $arrs)
134
    {
135
        $conditions = [
136
            'cms_modules_id' => $moduleId,
137
            'cms_roles_id' => $id,
138
        ];
139
        return \DB::table('cms_roles_privileges')->updateOrInsert($conditions, $arrs);
140
    }
141
142
    private function setTheme()
143
    {
144
        session()->put('theme_color', $this->arr['theme_color']);
145
    }
146
147
    private function setButtons()
148
    {
149
        $this->buttonImport = false;
150
        $this->buttonExport = false;
151
        $this->buttonActionStyle = 'button_icon';
152
        $this->buttonDetail = false;
153
        $this->buttonBulkAction = false;
154
    }
155
156
    private function makeForm()
157
    {
158
        $this->form = [
159
            ['label' => 'Name', 'name' => 'name', 'required' => true],
160
            ['label' => 'Is Superadmin', 'name' => 'is_superadmin', 'required' => true],
161
            ['label' => 'Theme Color', 'name' => 'theme_color', 'required' => true],
162
        ];
163
    }
164
165
    private function makeCols()
166
    {
167
        $this->col = [
168
            ['label' => 'ID', 'name' => 'id'],
169
            ['label' => 'Name', 'name' => 'name'],
170
            [
171
                'label' => 'Superadmin',
172
                'name' => 'is_superadmin',
173
                'callback' => function ($row) {
174
                    return ($row->is_superadmin) ? "<span class='label label-success'>Superadmin</span>" : "<span class='label label-default'>Standard</span>";
175
                },
176
            ],
177
        ];
178
    }
179
}
180