Completed
Push — master ( dc5510...4d2223 )
by CodexShaper
06:27
created

PermissionController::getUsers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 23
nc 2
nop 1
dl 0
loc 32
ccs 0
cts 27
cp 0
crap 6
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
namespace CodexShaper\DBM\Http\Controllers;
4
5
use CodexShaper\DBM\Facades\Manager as DBM;
6
use Illuminate\Http\Request;
7
8
class PermissionController extends Controller
9
{
10
    /**
11
     * Get all permissions
12
     *
13
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
14
     */
15
    public function index()
16
    {
17
        return view('dbm::app');
18
    }
19
    /**
20
     * Get all users with permissions
21
     *
22
     * @return \Illuminate\Http\JsonResponse
23
     */
24
    public function all(Request $request)
25
    {
26
        if ($request->ajax()) {
27
28
            if (($response = DBM::authorize('permission.browse')) !== true) {
29
                return $response;
30
            }
31
32
            try
33
            {
34
                $users = $this->getUsers($request);
35
36
                $privileges = DBM::Permission()->all();
37
38
                $permissions               = new \StdClass;
39
                $permissions->database     = DBM::Permission()->where('prefix', 'database')->get();
40
                $permissions->crud         = DBM::Permission()->where('prefix', 'crud')->get();
41
                $permissions->relationship = DBM::Permission()->where('prefix', 'relationship')->get();
42
                $permissions->record       = DBM::Permission()->where('prefix', 'record')->get();
43
                $permissions->backup       = DBM::Permission()->where('prefix', 'backup')->get();
44
                $permissions->permission   = DBM::Permission()->where('prefix', 'permission')->get();
45
46
                return response()->json([
47
                    'success'     => true,
48
                    'privileges'  => $privileges,
49
                    'permissions' => $permissions,
50
                    'pagination'  => $users,
51
                ]);
52
53
            } catch (\Exception $e) {
54
                return response()->json([
55
                    'success' => false,
56
                    'errors'  => [$e->getMessage()],
57
                ], 200);
58
            }
59
60
        }
61
62
        return response()->json(['success' => false]);
63
64
    }
65
    /**
66
     * get Permission Users
67
     *
68
     * @return \Illuminate\Support\Collection|array
69
     */
70
    public function getUsers(Request $request)
71
    {
72
        $user_model        = config('dbm.auth.user.model');
73
        $user_table        = config('dbm.auth.user.table');
74
        $user_local_key    = config('dbm.auth.user.local_key');
0 ignored issues
show
Unused Code introduced by
The assignment to $user_local_key is dead and can be removed.
Loading history...
75
        $user_display_name = config('dbm.auth.user.display_name');
76
77
        $perPage = (int) $request->perPage;
78
        $query   = $request->q;
79
        $users   = DBM::model($user_model, $user_table)->paginate($perPage);
80
81
        if (!empty($query)) {
82
            $users = DBM::model($user_model, $user_table)
83
                ->where('name', 'LIKE', '%' . $query . '%')
84
                ->paginate($perPage);
85
        }
86
87
        $users->getCollection()->transform(function ($user) use ($user_display_name) {
88
            $user->permissions = DBM::Object()
89
                ->setManyToManyRelation(
90
                    $user,
91
                    DBM::Permission(),
92
                    'dbm_user_permissions',
93
                    'user_id',
94
                    'dbm_permission_id'
95
                )
96
                ->belongs_to_many;
0 ignored issues
show
Bug introduced by
The property belongs_to_many does not seem to exist on CodexShaper\DBM\Models\DBM_Object. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
97
            $user->display_name = $user_display_name;
98
            return $user;
99
        });
100
101
        return $users;
102
    }
103
    /**
104
     * Assign Permissions to User
105
     *
106
     * @return \Illuminate\Http\JsonResponse
107
     */
108
    public function assignUserPermissions(Request $request)
109
    {
110
        if ($request->ajax()) {
111
112
            if (($response = DBM::authorize('permission.create')) !== true) {
113
                return $response;
114
            }
115
116
            $privileges = $request->privileges;
117
            $user       = (object) $request->user;
118
119
            $this->getRelation($user)->attach($privileges);
120
121
            return response()->json(['success' => true]);
122
        }
123
124
        return response()->json(['success' => false]);
125
    }
126
    /**
127
     * Update User Permissions
128
     *
129
     * @return \Illuminate\Http\JsonResponse
130
     */
131
    public function syncUserPermissions(Request $request)
132
    {
133
        if ($request->ajax()) {
134
135
            if (($response = DBM::authorize('permission.update')) !== true) {
136
                return $response;
137
            }
138
139
            $privileges = $request->privileges;
140
            $user       = (object) $request->user;
141
142
            $this->getRelation($user)->sync($privileges);
143
144
            return response()->json(['success' => true]);
145
        }
146
147
        return response()->json(['success' => false]);
148
    }
149
    /**
150
     * Delete User Permissions
151
     *
152
     * @return \Illuminate\Http\JsonResponse
153
     */
154
    public function deleteUserPermissions(Request $request)
155
    {
156
        if ($request->ajax()) {
157
158
            if (($response = DBM::authorize('permission.delete')) !== true) {
159
                return $response;
160
            }
161
162
            $user = json_decode($request->user);
163
164
            $this->getRelation($user)->detach();
165
166
            return response()->json(['success' => true]);
167
        }
168
169
        return response()->json(['success' => false]);
170
    }
171
    /**
172
     * Get User Relation
173
     *
174
     * @param object $user
175
     *
176
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
177
     */
178
    protected function getRelation($user)
179
    {
180
        $user_model        = config('dbm.auth.user.model');
181
        $user_table        = config('dbm.auth.user.table');
182
        $user_local_key    = config('dbm.auth.user.local_key');
183
        $user_display_name = config('dbm.auth.user.display_name');
0 ignored issues
show
Unused Code introduced by
The assignment to $user_display_name is dead and can be removed.
Loading history...
184
185
        $localModel = DBM::model($user_model, $user_table)
186
            ->where($user_local_key, $user->{$user_local_key})
187
            ->first();
188
        return DBM::Object()
189
            ->setManyToManyRelation(
190
                $localModel,
191
                DBM::Permission(),
192
                'dbm_user_permissions',
193
                'user_id',
194
                'dbm_permission_id'
195
            )
196
            ->belongs_to_many();
197
    }
198
199
}
200