Completed
Push — master ( 0b4fd8...dc5510 )
by CodexShaper
06:02
created

PermissionController::all()   B

Complexity

Conditions 5
Paths 30

Size

Total Lines 66
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 45
c 1
b 0
f 0
nc 30
nop 1
dl 0
loc 66
ccs 0
cts 54
cp 0
crap 30
rs 8.8888

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
                $user_model        = config('dbm.auth.user.model');
35
                $user_table        = config('dbm.auth.user.table');
36
                $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...
37
                $user_display_name = config('dbm.auth.user.display_name');
38
39
                $perPage = (int) $request->perPage;
40
                $query   = $request->q;
41
                $users   = DBM::model($user_model, $user_table)->paginate($perPage);
42
43
                if (!empty($query)) {
44
                    $users = DBM::model($user_model, $user_table)
45
                        ->where('name', 'LIKE', '%' . $query . '%')
46
                        ->paginate($perPage);
47
                }
48
49
                $users->getCollection()->transform(function ($user) use ($user_display_name) {
50
                    $user->permissions = DBM::Object()
51
                        ->setManyToManyRelation(
52
                            $user,
53
                            DBM::Permission(),
54
                            'dbm_user_permissions',
55
                            'user_id',
56
                            'dbm_permission_id'
57
                        )
58
                        ->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...
59
                    $user->display_name = $user_display_name;
60
                    return $user;
61
                });
62
63
                $privileges = DBM::Permission()->all();
64
65
                $permissions               = new \StdClass;
66
                $permissions->database     = DBM::Permission()->where('prefix', 'database')->get();
67
                $permissions->crud         = DBM::Permission()->where('prefix', 'crud')->get();
68
                $permissions->relationship = DBM::Permission()->where('prefix', 'relationship')->get();
69
                $permissions->record       = DBM::Permission()->where('prefix', 'record')->get();
70
                $permissions->backup       = DBM::Permission()->where('prefix', 'backup')->get();
71
                $permissions->permission   = DBM::Permission()->where('prefix', 'permission')->get();
72
73
                return response()->json([
74
                    'success'     => true,
75
                    'privileges'  => $privileges,
76
                    'permissions' => $permissions,
77
                    'pagination'  => $users,
78
                ]);
79
80
            } catch (\Exception $e) {
81
                return response()->json([
82
                    'success' => false,
83
                    'errors'  => [$e->getMessage()],
84
                ], 200);
85
            }
86
87
        }
88
89
        return response()->json(['success' => false]);
90
91
    }
92
    /**
93
     * Assign Permissions to User
94
     *
95
     * @return \Illuminate\Http\JsonResponse
96
     */
97
    public function assignUserPermissions(Request $request)
98
    {
99
        if ($request->ajax()) {
100
101
            if (($response = DBM::authorize('permission.create')) !== true) {
102
                return $response;
103
            }
104
105
            $privileges = $request->privileges;
106
            $user       = (object) $request->user;
107
108
            $this->getRelation($user)->attach($privileges);
109
110
            return response()->json(['success' => true]);
111
        }
112
113
        return response()->json(['success' => false]);
114
    }
115
    /**
116
     * Update User Permissions
117
     *
118
     * @return \Illuminate\Http\JsonResponse
119
     */
120
    public function syncUserPermissions(Request $request)
121
    {
122
        if ($request->ajax()) {
123
124
            if (($response = DBM::authorize('permission.update')) !== true) {
125
                return $response;
126
            }
127
128
            $privileges = $request->privileges;
129
            $user       = (object) $request->user;
130
131
            $this->getRelation($user)->sync($privileges);
132
133
            return response()->json(['success' => true]);
134
        }
135
136
        return response()->json(['success' => false]);
137
    }
138
    /**
139
     * Delete User Permissions
140
     *
141
     * @return \Illuminate\Http\JsonResponse
142
     */
143
    public function deleteUserPermissions(Request $request)
144
    {
145
        if ($request->ajax()) {
146
147
            if (($response = DBM::authorize('permission.delete')) !== true) {
148
                return $response;
149
            }
150
151
            $user = json_decode($request->user);
152
153
            $this->getRelation($user)->detach();
154
155
            return response()->json(['success' => true]);
156
        }
157
158
        return response()->json(['success' => false]);
159
    }
160
    /**
161
     * Get User Relation
162
     *
163
     * @param object $user
164
     *
165
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
166
     */
167
    protected function getRelation($user)
168
    {
169
        $user_model        = config('dbm.auth.user.model');
170
        $user_table        = config('dbm.auth.user.table');
171
        $user_local_key    = config('dbm.auth.user.local_key');
172
        $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...
173
174
        $localModel = DBM::model($user_model, $user_table)
175
            ->where($user_local_key, $user->{$user_local_key})
176
            ->first();
177
        return DBM::Object()
178
            ->setManyToManyRelation(
179
                $localModel,
180
                DBM::Permission(),
181
                'dbm_user_permissions',
182
                'user_id',
183
                'dbm_permission_id'
184
            )
185
            ->belongs_to_many();
186
    }
187
188
}
189