1
|
|
|
<?php namespace App\Modules\V1\Acl\Repositories; |
2
|
|
|
|
3
|
|
|
use App\Modules\V1\Core\AbstractRepositories\AbstractRepository; |
4
|
|
|
|
5
|
|
|
class UserRepository extends AbstractRepository |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Return the model full namespace. |
9
|
|
|
* |
10
|
|
|
* @return string |
11
|
|
|
*/ |
12
|
|
|
protected function getModel() |
13
|
|
|
{ |
14
|
|
|
return 'App\Modules\V1\Acl\AclUser'; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Check if the logged in user or the given user |
19
|
|
|
* has the given permissions on the given model. |
20
|
|
|
* |
21
|
|
|
* @param string $nameOfPermission |
22
|
|
|
* @param string $model |
23
|
|
|
* @param boolean $user |
24
|
|
|
* @return boolean |
25
|
|
|
*/ |
26
|
|
|
public function can($nameOfPermission, $model, $user = false ) |
27
|
|
|
{ |
28
|
|
|
$user = $user ?: \JWTAuth::parseToken()->authenticate(); |
29
|
|
|
$permissions = []; |
30
|
|
|
\Core::users()->find($user->id, ['groups.permissions'])->groups->lists('permissions')->each(function ($permission) use (&$permissions, $model){ |
31
|
|
|
$permissions = array_merge($permissions, $permission->where('model', $model)->lists('name')->toArray()); |
32
|
|
|
}); |
33
|
|
|
|
34
|
|
|
return in_array($nameOfPermission, $permissions); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Check if the logged in user has the given group. |
39
|
|
|
* |
40
|
|
|
* @param string $groupName |
41
|
|
|
* @return boolean |
42
|
|
|
*/ |
43
|
|
|
public function hasGroup($groupName) |
44
|
|
|
{ |
45
|
|
|
$groups = \Core::users()->find(\JWTAuth::parseToken()->authenticate()->id)->groups; |
46
|
|
|
return $groups->lists('name')->search($groupName, true) === false ? false : true; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Assign the given group ids to the given user. |
51
|
|
|
* |
52
|
|
|
* @param integer $user_id |
53
|
|
|
* @param array $group_ids |
54
|
|
|
* @return object |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function assignGroups($user_id, $group_ids) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
\DB::transaction(function () use ($user_id, $group_ids) { |
59
|
|
|
$user = \Core::users()->find($user_id); |
60
|
|
|
$user->groups()->detach(); |
61
|
|
|
$user->groups()->attach($group_ids); |
62
|
|
|
}); |
63
|
|
|
|
64
|
|
|
return \Core::users()->find($user_id); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Handle a login request to the application. |
69
|
|
|
* |
70
|
|
|
* @param array $credentials |
71
|
|
|
* @param boolean $adminLogin |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function login($credentials, $adminLogin = false) |
75
|
|
|
{ |
76
|
|
|
if ( ! $user = \Core::users()->first(['email' => $credentials['email']])) |
77
|
|
|
{ |
78
|
|
|
\ErrorHandler::loginFailed(); |
79
|
|
|
} |
80
|
|
|
else if ($adminLogin && $user->groups->lists('name')->search('Admin', true) === false) |
81
|
|
|
{ |
82
|
|
|
\ErrorHandler::loginFailed(); |
83
|
|
|
} |
84
|
|
|
else if ( ! $adminLogin && $user->groups->lists('name')->search('Admin', true) !== false) |
85
|
|
|
{ |
86
|
|
|
\ErrorHandler::loginFailed(); |
87
|
|
|
} |
88
|
|
|
else if ($user->blocked) |
89
|
|
|
{ |
90
|
|
|
\ErrorHandler::userIsBlocked(); |
91
|
|
|
} |
92
|
|
|
else if ($token = \JWTAuth::attempt($credentials)) |
93
|
|
|
{ |
94
|
|
|
return $token; |
95
|
|
|
} |
96
|
|
|
else |
97
|
|
|
{ |
98
|
|
|
\ErrorHandler::loginFailed(); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Handle a registration request. |
104
|
|
|
* |
105
|
|
|
* @param array $credentials |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public function register($credentials) |
109
|
|
|
{ |
110
|
|
|
return \JWTAuth::fromUser(\Core::users()->model->create($credentials)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Logout the user. |
115
|
|
|
* |
116
|
|
|
* @return boolean |
117
|
|
|
*/ |
118
|
|
|
public function logout() |
119
|
|
|
{ |
120
|
|
|
return \JWTAuth::invalidate(\JWTAuth::getToken()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Block the user. |
125
|
|
|
* |
126
|
|
|
* @param integer $user_id |
127
|
|
|
* @return object |
128
|
|
|
*/ |
129
|
|
|
public function block($user_id) |
130
|
|
|
{ |
131
|
|
|
if ( ! $user = \Core::users()->find($user_id)) |
132
|
|
|
{ |
133
|
|
|
\ErrorHandler::notFound('user'); |
134
|
|
|
} |
135
|
|
|
if ( ! $this->hasGroup('Admin')) |
136
|
|
|
{ |
137
|
|
|
\ErrorHandler::noPermissions(); |
138
|
|
|
} |
139
|
|
|
else if (\JWTAuth::parseToken()->authenticate()->id == $user_id) |
140
|
|
|
{ |
141
|
|
|
\ErrorHandler::noPermissions(); |
142
|
|
|
} |
143
|
|
|
else if ($user->groups->lists('name')->search('Admin', true) !== false) |
144
|
|
|
{ |
145
|
|
|
\ErrorHandler::noPermissions(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$user->blocked = 1; |
149
|
|
|
$user->save(); |
150
|
|
|
|
151
|
|
|
return $user; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Unblock the user. |
156
|
|
|
* |
157
|
|
|
* @param integer $user_id |
158
|
|
|
* @return object |
159
|
|
|
*/ |
160
|
|
|
public function unblock($user_id) |
161
|
|
|
{ |
162
|
|
|
if ( ! $this->hasGroup('Admin')) |
163
|
|
|
{ |
164
|
|
|
\ErrorHandler::noPermissions(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$user = \Core::users()->find($user_id); |
168
|
|
|
$user->blocked = 0; |
169
|
|
|
$user->save(); |
170
|
|
|
|
171
|
|
|
return $user; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Handle the editing of the user profile. |
176
|
|
|
* |
177
|
|
|
* @param array $profile |
178
|
|
|
* @return object |
179
|
|
|
*/ |
180
|
|
|
public function editProfile($profile) |
181
|
|
|
{ |
182
|
|
|
unset($profile['email']); |
183
|
|
|
unset($profile['password']); |
184
|
|
|
$profile['id'] = \JWTAuth::parseToken()->authenticate()->id; |
185
|
|
|
|
186
|
|
|
return $this->save($profile); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Send a reset link to the given user. |
191
|
|
|
* |
192
|
|
|
* @param string $url |
193
|
|
|
* @param string $email |
194
|
|
|
* @return void |
195
|
|
|
*/ |
196
|
|
|
public function sendReset($email, $url) |
197
|
|
|
{ |
198
|
|
|
view()->composer('auth.emails.password', function($view) use ($url) { |
199
|
|
|
$view->with(['url' => $url]); |
200
|
|
|
}); |
201
|
|
|
|
202
|
|
|
$response = \Password::sendResetLink($email, function (\Illuminate\Mail\Message $message) { |
203
|
|
|
$message->subject('Your Password Reset Link'); |
204
|
|
|
}); |
205
|
|
|
|
206
|
|
|
switch ($response) |
207
|
|
|
{ |
208
|
|
|
case \Password::INVALID_USER: |
209
|
|
|
\ErrorHandler::notFound('email'); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Reset the given user's password. |
215
|
|
|
* |
216
|
|
|
* @param array $credentials |
217
|
|
|
* @return integer |
218
|
|
|
*/ |
219
|
|
|
public function resetPassword($credentials) |
220
|
|
|
{ |
221
|
|
|
$token = false; |
222
|
|
|
$response = \Password::reset($credentials, function ($user, $password) use (&$token) { |
223
|
|
|
$user->password = bcrypt($password); |
224
|
|
|
$user->save(); |
225
|
|
|
|
226
|
|
|
$token = \JWTAuth::fromUser($user); |
227
|
|
|
}); |
228
|
|
|
|
229
|
|
|
|
230
|
|
|
switch ($response) { |
231
|
|
|
case \Password::PASSWORD_RESET: |
232
|
|
|
return $token; |
233
|
|
|
|
234
|
|
|
case \Password::INVALID_TOKEN: |
|
|
|
|
235
|
|
|
\ErrorHandler::invalidResetToken('token'); |
236
|
|
|
|
237
|
|
|
case \Password::INVALID_PASSWORD: |
|
|
|
|
238
|
|
|
\ErrorHandler::invalidResetPassword('email'); |
239
|
|
|
|
240
|
|
|
case \Password::INVALID_USER: |
|
|
|
|
241
|
|
|
\ErrorHandler::notFound('user'); |
242
|
|
|
|
243
|
|
|
default: |
244
|
|
|
\ErrorHandler::generalError(); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.