@@ -259,7 +259,7 @@ |
||
259 | 259 | * Reset the given user's password. |
260 | 260 | * |
261 | 261 | * @param array $credentials |
262 | - * @return array |
|
262 | + * @return string|null |
|
263 | 263 | */ |
264 | 264 | public function resetPassword($credentials) |
265 | 265 | { |
@@ -7,368 +7,368 @@ |
||
7 | 7 | |
8 | 8 | class UserRepository extends AbstractRepository |
9 | 9 | { |
10 | - /** |
|
11 | - * Return the model full namespace. |
|
12 | - * |
|
13 | - * @return string |
|
14 | - */ |
|
15 | - protected function getModel() |
|
16 | - { |
|
17 | - return 'App\Modules\V1\Acl\AclUser'; |
|
18 | - } |
|
19 | - |
|
20 | - /** |
|
21 | - * The loginProxy implementation. |
|
22 | - * |
|
23 | - * @var array |
|
24 | - */ |
|
25 | - protected $loginProxy; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var The accessTokenRepository implementation. |
|
29 | - */ |
|
30 | - private $accessTokenRepository; |
|
31 | - |
|
32 | - public function __construct(LoginProxy $loginProxy, AccessTokenRepositoryInterface $accessTokenRepository) |
|
33 | - { |
|
34 | - $this->loginProxy = $loginProxy; |
|
35 | - $this->accessTokenRepository = $accessTokenRepository; |
|
36 | - parent::__construct(); |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * Return the logged in user account. |
|
41 | - * |
|
42 | - * @param array $relations |
|
43 | - * @return boolean |
|
44 | - */ |
|
45 | - public function account($relations = []) |
|
46 | - { |
|
47 | - $permissions = []; |
|
48 | - $user = \Core::users()->find(\Auth::id(), $relations); |
|
49 | - foreach ($user->groups()->get() as $group) |
|
50 | - { |
|
51 | - $group->permissions->each(function ($permission) use (&$permissions){ |
|
52 | - $permissions[$permission->model][$permission->id] = $permission->name; |
|
53 | - }); |
|
54 | - } |
|
55 | - $user->permissions = $permissions; |
|
56 | - |
|
57 | - return $user; |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Check if the logged in user or the given user |
|
62 | - * has the given permissions on the given model. |
|
63 | - * |
|
64 | - * @param string $nameOfPermission |
|
65 | - * @param string $model |
|
66 | - * @param boolean $user |
|
67 | - * @return boolean |
|
68 | - */ |
|
69 | - public function can($nameOfPermission, $model, $user = false ) |
|
70 | - { |
|
71 | - $user = $user = $this->find(\Auth::id(), ['groups.permissions']); |
|
72 | - $permissions = []; |
|
73 | - |
|
74 | - $user->groups->pluck('permissions')->each(function ($permission) use (&$permissions, $model){ |
|
75 | - $permissions = array_merge($permissions, $permission->where('model', $model)->pluck('name')->toArray()); |
|
76 | - }); |
|
10 | + /** |
|
11 | + * Return the model full namespace. |
|
12 | + * |
|
13 | + * @return string |
|
14 | + */ |
|
15 | + protected function getModel() |
|
16 | + { |
|
17 | + return 'App\Modules\V1\Acl\AclUser'; |
|
18 | + } |
|
19 | + |
|
20 | + /** |
|
21 | + * The loginProxy implementation. |
|
22 | + * |
|
23 | + * @var array |
|
24 | + */ |
|
25 | + protected $loginProxy; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var The accessTokenRepository implementation. |
|
29 | + */ |
|
30 | + private $accessTokenRepository; |
|
31 | + |
|
32 | + public function __construct(LoginProxy $loginProxy, AccessTokenRepositoryInterface $accessTokenRepository) |
|
33 | + { |
|
34 | + $this->loginProxy = $loginProxy; |
|
35 | + $this->accessTokenRepository = $accessTokenRepository; |
|
36 | + parent::__construct(); |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * Return the logged in user account. |
|
41 | + * |
|
42 | + * @param array $relations |
|
43 | + * @return boolean |
|
44 | + */ |
|
45 | + public function account($relations = []) |
|
46 | + { |
|
47 | + $permissions = []; |
|
48 | + $user = \Core::users()->find(\Auth::id(), $relations); |
|
49 | + foreach ($user->groups()->get() as $group) |
|
50 | + { |
|
51 | + $group->permissions->each(function ($permission) use (&$permissions){ |
|
52 | + $permissions[$permission->model][$permission->id] = $permission->name; |
|
53 | + }); |
|
54 | + } |
|
55 | + $user->permissions = $permissions; |
|
56 | + |
|
57 | + return $user; |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Check if the logged in user or the given user |
|
62 | + * has the given permissions on the given model. |
|
63 | + * |
|
64 | + * @param string $nameOfPermission |
|
65 | + * @param string $model |
|
66 | + * @param boolean $user |
|
67 | + * @return boolean |
|
68 | + */ |
|
69 | + public function can($nameOfPermission, $model, $user = false ) |
|
70 | + { |
|
71 | + $user = $user = $this->find(\Auth::id(), ['groups.permissions']); |
|
72 | + $permissions = []; |
|
73 | + |
|
74 | + $user->groups->pluck('permissions')->each(function ($permission) use (&$permissions, $model){ |
|
75 | + $permissions = array_merge($permissions, $permission->where('model', $model)->pluck('name')->toArray()); |
|
76 | + }); |
|
77 | 77 | |
78 | - return in_array($nameOfPermission, $permissions); |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Check if the logged in user has the given group. |
|
83 | - * |
|
84 | - * @param string $groupName |
|
85 | - * @param integer $userId |
|
86 | - * @return boolean |
|
87 | - */ |
|
88 | - public function hasGroup($groupName, $userId = false) |
|
89 | - { |
|
90 | - $userId = $userId ?: \Auth::id(); |
|
91 | - $groups = $this->find($userId)->groups; |
|
92 | - return $groups->pluck('name')->search($groupName, true) === false ? false : true; |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Assign the given group ids to the given user. |
|
97 | - * |
|
98 | - * @param integer $user_id |
|
99 | - * @param array $group_ids |
|
100 | - * @return object |
|
101 | - */ |
|
102 | - public function assignGroups($user_id, $group_ids) |
|
103 | - { |
|
104 | - \DB::transaction(function () use ($user_id, $group_ids) { |
|
105 | - $user = $this->find($user_id); |
|
106 | - $user->groups()->detach(); |
|
107 | - $user->groups()->attach($group_ids); |
|
108 | - }); |
|
109 | - |
|
110 | - return $this->find($user_id); |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Handle a login request to the application. |
|
115 | - * |
|
116 | - * @param array $credentials |
|
117 | - * @param boolean $adminLogin |
|
118 | - * @return object |
|
119 | - */ |
|
120 | - public function login($credentials, $adminLogin = false) |
|
121 | - { |
|
122 | - if ( ! $user = $this->first(['email' => $credentials['email']])) |
|
123 | - { |
|
124 | - \ErrorHandler::loginFailed(); |
|
125 | - } |
|
126 | - else if ($adminLogin && $user->groups->pluck('name')->search('Admin', true) === false) |
|
127 | - { |
|
128 | - \ErrorHandler::loginFailed(); |
|
129 | - } |
|
130 | - else if ( ! $adminLogin && $user->groups->pluck('name')->search('Admin', true) !== false) |
|
131 | - { |
|
132 | - \ErrorHandler::loginFailed(); |
|
133 | - } |
|
134 | - else if ($user->blocked) |
|
135 | - { |
|
136 | - \ErrorHandler::userIsBlocked(); |
|
137 | - } |
|
138 | - |
|
139 | - return $user; |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Handle a social login request of the none admin to the application. |
|
144 | - * |
|
145 | - * @param array $credentials |
|
146 | - * @return array |
|
147 | - */ |
|
148 | - public function loginSocial($credentials) |
|
149 | - { |
|
150 | - $access_token = $credentials['auth_code'] ? \Socialite::driver($credentials['type'])->getAccessToken($credentials['auth_code']) : $credentials['access_token']; |
|
151 | - $user = \Socialite::driver($credentials['type'])->userFromToken($access_token); |
|
152 | - |
|
153 | - if ( ! $user->email) |
|
154 | - { |
|
155 | - \ErrorHandler::noSocialEmail(); |
|
156 | - } |
|
157 | - |
|
158 | - if ( ! $registeredUser = $this->model->where('email', $user->email)->first()) |
|
159 | - { |
|
160 | - $data = ['email' => $user->email, 'password' => '']; |
|
161 | - return $this->register($data); |
|
162 | - } |
|
163 | - else |
|
164 | - { |
|
165 | - if ( ! \Auth::attempt(['email' => $registeredUser->email, 'password' => ''])) |
|
166 | - { |
|
167 | - \ErrorHandler::userAlreadyRegistered(); |
|
168 | - } |
|
169 | - |
|
170 | - return $this->loginProxy->login(['email' => $registeredUser->email, 'password' => ''], 0); |
|
171 | - } |
|
172 | - } |
|
78 | + return in_array($nameOfPermission, $permissions); |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Check if the logged in user has the given group. |
|
83 | + * |
|
84 | + * @param string $groupName |
|
85 | + * @param integer $userId |
|
86 | + * @return boolean |
|
87 | + */ |
|
88 | + public function hasGroup($groupName, $userId = false) |
|
89 | + { |
|
90 | + $userId = $userId ?: \Auth::id(); |
|
91 | + $groups = $this->find($userId)->groups; |
|
92 | + return $groups->pluck('name')->search($groupName, true) === false ? false : true; |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * Assign the given group ids to the given user. |
|
97 | + * |
|
98 | + * @param integer $user_id |
|
99 | + * @param array $group_ids |
|
100 | + * @return object |
|
101 | + */ |
|
102 | + public function assignGroups($user_id, $group_ids) |
|
103 | + { |
|
104 | + \DB::transaction(function () use ($user_id, $group_ids) { |
|
105 | + $user = $this->find($user_id); |
|
106 | + $user->groups()->detach(); |
|
107 | + $user->groups()->attach($group_ids); |
|
108 | + }); |
|
109 | + |
|
110 | + return $this->find($user_id); |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Handle a login request to the application. |
|
115 | + * |
|
116 | + * @param array $credentials |
|
117 | + * @param boolean $adminLogin |
|
118 | + * @return object |
|
119 | + */ |
|
120 | + public function login($credentials, $adminLogin = false) |
|
121 | + { |
|
122 | + if ( ! $user = $this->first(['email' => $credentials['email']])) |
|
123 | + { |
|
124 | + \ErrorHandler::loginFailed(); |
|
125 | + } |
|
126 | + else if ($adminLogin && $user->groups->pluck('name')->search('Admin', true) === false) |
|
127 | + { |
|
128 | + \ErrorHandler::loginFailed(); |
|
129 | + } |
|
130 | + else if ( ! $adminLogin && $user->groups->pluck('name')->search('Admin', true) !== false) |
|
131 | + { |
|
132 | + \ErrorHandler::loginFailed(); |
|
133 | + } |
|
134 | + else if ($user->blocked) |
|
135 | + { |
|
136 | + \ErrorHandler::userIsBlocked(); |
|
137 | + } |
|
138 | + |
|
139 | + return $user; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Handle a social login request of the none admin to the application. |
|
144 | + * |
|
145 | + * @param array $credentials |
|
146 | + * @return array |
|
147 | + */ |
|
148 | + public function loginSocial($credentials) |
|
149 | + { |
|
150 | + $access_token = $credentials['auth_code'] ? \Socialite::driver($credentials['type'])->getAccessToken($credentials['auth_code']) : $credentials['access_token']; |
|
151 | + $user = \Socialite::driver($credentials['type'])->userFromToken($access_token); |
|
152 | + |
|
153 | + if ( ! $user->email) |
|
154 | + { |
|
155 | + \ErrorHandler::noSocialEmail(); |
|
156 | + } |
|
157 | + |
|
158 | + if ( ! $registeredUser = $this->model->where('email', $user->email)->first()) |
|
159 | + { |
|
160 | + $data = ['email' => $user->email, 'password' => '']; |
|
161 | + return $this->register($data); |
|
162 | + } |
|
163 | + else |
|
164 | + { |
|
165 | + if ( ! \Auth::attempt(['email' => $registeredUser->email, 'password' => ''])) |
|
166 | + { |
|
167 | + \ErrorHandler::userAlreadyRegistered(); |
|
168 | + } |
|
169 | + |
|
170 | + return $this->loginProxy->login(['email' => $registeredUser->email, 'password' => ''], 0); |
|
171 | + } |
|
172 | + } |
|
173 | 173 | |
174 | - /** |
|
175 | - * Handle a registration request. |
|
176 | - * |
|
177 | - * @param array $credentials |
|
178 | - * @return array |
|
179 | - */ |
|
180 | - public function register($credentials) |
|
181 | - { |
|
182 | - $this->model->create($credentials); |
|
183 | - return $this->loginProxy->login($credentials, 0); |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * Block the user. |
|
188 | - * |
|
189 | - * @param integer $user_id |
|
190 | - * @return object |
|
191 | - */ |
|
192 | - public function block($user_id) |
|
193 | - { |
|
194 | - if ( ! $user = $this->find($user_id)) |
|
195 | - { |
|
196 | - \ErrorHandler::notFound('user'); |
|
197 | - } |
|
198 | - if ( ! $this->hasGroup('Admin')) |
|
199 | - { |
|
200 | - \ErrorHandler::noPermissions(); |
|
201 | - } |
|
202 | - else if (\Auth::id() == $user_id) |
|
203 | - { |
|
204 | - \ErrorHandler::noPermissions(); |
|
205 | - } |
|
206 | - else if ($user->groups->pluck('name')->search('Admin', true) !== false) |
|
207 | - { |
|
208 | - \ErrorHandler::noPermissions(); |
|
209 | - } |
|
210 | - |
|
211 | - $user->blocked = 1; |
|
212 | - $user->save(); |
|
174 | + /** |
|
175 | + * Handle a registration request. |
|
176 | + * |
|
177 | + * @param array $credentials |
|
178 | + * @return array |
|
179 | + */ |
|
180 | + public function register($credentials) |
|
181 | + { |
|
182 | + $this->model->create($credentials); |
|
183 | + return $this->loginProxy->login($credentials, 0); |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * Block the user. |
|
188 | + * |
|
189 | + * @param integer $user_id |
|
190 | + * @return object |
|
191 | + */ |
|
192 | + public function block($user_id) |
|
193 | + { |
|
194 | + if ( ! $user = $this->find($user_id)) |
|
195 | + { |
|
196 | + \ErrorHandler::notFound('user'); |
|
197 | + } |
|
198 | + if ( ! $this->hasGroup('Admin')) |
|
199 | + { |
|
200 | + \ErrorHandler::noPermissions(); |
|
201 | + } |
|
202 | + else if (\Auth::id() == $user_id) |
|
203 | + { |
|
204 | + \ErrorHandler::noPermissions(); |
|
205 | + } |
|
206 | + else if ($user->groups->pluck('name')->search('Admin', true) !== false) |
|
207 | + { |
|
208 | + \ErrorHandler::noPermissions(); |
|
209 | + } |
|
210 | + |
|
211 | + $user->blocked = 1; |
|
212 | + $user->save(); |
|
213 | 213 | |
214 | - return $user; |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * Unblock the user. |
|
219 | - * |
|
220 | - * @param integer $user_id |
|
221 | - * @return object |
|
222 | - */ |
|
223 | - public function unblock($user_id) |
|
224 | - { |
|
225 | - if ( ! $this->hasGroup('Admin')) |
|
226 | - { |
|
227 | - \ErrorHandler::noPermissions(); |
|
228 | - } |
|
229 | - |
|
230 | - $user = $this->find($user_id); |
|
231 | - $user->blocked = 0; |
|
232 | - $user->save(); |
|
233 | - |
|
234 | - return $user; |
|
235 | - } |
|
236 | - |
|
237 | - /** |
|
238 | - * Send a reset link to the given user. |
|
239 | - * |
|
240 | - * @param string $email |
|
241 | - * @return void |
|
242 | - */ |
|
243 | - public function sendReset($email) |
|
244 | - { |
|
245 | - if ( ! $user = $this->model->where('email', $email)->first()) |
|
246 | - { |
|
247 | - \ErrorHandler::notFound('email'); |
|
248 | - } |
|
249 | - |
|
250 | - $url = $this->config['resetLink']; |
|
251 | - $token = \Password::getRepository()->create($user); |
|
214 | + return $user; |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * Unblock the user. |
|
219 | + * |
|
220 | + * @param integer $user_id |
|
221 | + * @return object |
|
222 | + */ |
|
223 | + public function unblock($user_id) |
|
224 | + { |
|
225 | + if ( ! $this->hasGroup('Admin')) |
|
226 | + { |
|
227 | + \ErrorHandler::noPermissions(); |
|
228 | + } |
|
229 | + |
|
230 | + $user = $this->find($user_id); |
|
231 | + $user->blocked = 0; |
|
232 | + $user->save(); |
|
233 | + |
|
234 | + return $user; |
|
235 | + } |
|
236 | + |
|
237 | + /** |
|
238 | + * Send a reset link to the given user. |
|
239 | + * |
|
240 | + * @param string $email |
|
241 | + * @return void |
|
242 | + */ |
|
243 | + public function sendReset($email) |
|
244 | + { |
|
245 | + if ( ! $user = $this->model->where('email', $email)->first()) |
|
246 | + { |
|
247 | + \ErrorHandler::notFound('email'); |
|
248 | + } |
|
249 | + |
|
250 | + $url = $this->config['resetLink']; |
|
251 | + $token = \Password::getRepository()->create($user); |
|
252 | 252 | |
253 | - \Mail::send('acl::resetpassword', ['user' => $user, 'url' => $url, 'token' => $token], function ($m) use ($user) { |
|
254 | - $m->to($user->email, $user->name)->subject('Your Password Reset Link'); |
|
255 | - }); |
|
256 | - } |
|
257 | - |
|
258 | - /** |
|
259 | - * Reset the given user's password. |
|
260 | - * |
|
261 | - * @param array $credentials |
|
262 | - * @return array |
|
263 | - */ |
|
264 | - public function resetPassword($credentials) |
|
265 | - { |
|
266 | - $response = \Password::reset($credentials, function ($user, $password) { |
|
267 | - $user->password = $password; |
|
268 | - $user->save(); |
|
269 | - }); |
|
270 | - |
|
271 | - switch ($response) { |
|
272 | - case \Password::PASSWORD_RESET: |
|
273 | - return 'success'; |
|
253 | + \Mail::send('acl::resetpassword', ['user' => $user, 'url' => $url, 'token' => $token], function ($m) use ($user) { |
|
254 | + $m->to($user->email, $user->name)->subject('Your Password Reset Link'); |
|
255 | + }); |
|
256 | + } |
|
257 | + |
|
258 | + /** |
|
259 | + * Reset the given user's password. |
|
260 | + * |
|
261 | + * @param array $credentials |
|
262 | + * @return array |
|
263 | + */ |
|
264 | + public function resetPassword($credentials) |
|
265 | + { |
|
266 | + $response = \Password::reset($credentials, function ($user, $password) { |
|
267 | + $user->password = $password; |
|
268 | + $user->save(); |
|
269 | + }); |
|
270 | + |
|
271 | + switch ($response) { |
|
272 | + case \Password::PASSWORD_RESET: |
|
273 | + return 'success'; |
|
274 | 274 | |
275 | - case \Password::INVALID_TOKEN: |
|
276 | - \ErrorHandler::invalidResetToken('token'); |
|
277 | - |
|
278 | - case \Password::INVALID_PASSWORD: |
|
279 | - \ErrorHandler::invalidResetPassword('email'); |
|
280 | - |
|
281 | - case \Password::INVALID_USER: |
|
282 | - \ErrorHandler::notFound('user'); |
|
283 | - |
|
284 | - default: |
|
285 | - \ErrorHandler::generalError(); |
|
286 | - } |
|
287 | - } |
|
288 | - |
|
289 | - /** |
|
290 | - * Change the logged in user password. |
|
291 | - * |
|
292 | - * @param array $credentials |
|
293 | - * @return void |
|
294 | - */ |
|
295 | - public function changePassword($credentials) |
|
296 | - { |
|
297 | - $user = \Auth::user(); |
|
298 | - if ( ! \Hash::check($credentials['old_password'], $user->password)) |
|
299 | - { |
|
300 | - \ErrorHandler::invalidOldPassword(); |
|
301 | - } |
|
302 | - |
|
303 | - $user->password = $credentials['password']; |
|
304 | - $user->save(); |
|
305 | - } |
|
306 | - |
|
307 | - /** |
|
308 | - * Paginate all users in the given group based on the given conditions. |
|
309 | - * |
|
310 | - * @param string $groupName |
|
311 | - * @param array $relations |
|
312 | - * @param integer $perPage |
|
313 | - * @param string $sortBy |
|
314 | - * @param boolean $desc |
|
315 | - * @return \Illuminate\Http\Response |
|
316 | - */ |
|
317 | - public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc) |
|
318 | - { |
|
319 | - unset($conditions['page']); |
|
320 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
321 | - $sort = $desc ? 'desc' : 'asc'; |
|
322 | - $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
323 | - |
|
324 | - $model->whereHas('groups', function($q) use ($groupName){ |
|
325 | - $q->where('name', $groupName); |
|
326 | - }); |
|
275 | + case \Password::INVALID_TOKEN: |
|
276 | + \ErrorHandler::invalidResetToken('token'); |
|
277 | + |
|
278 | + case \Password::INVALID_PASSWORD: |
|
279 | + \ErrorHandler::invalidResetPassword('email'); |
|
280 | + |
|
281 | + case \Password::INVALID_USER: |
|
282 | + \ErrorHandler::notFound('user'); |
|
283 | + |
|
284 | + default: |
|
285 | + \ErrorHandler::generalError(); |
|
286 | + } |
|
287 | + } |
|
288 | + |
|
289 | + /** |
|
290 | + * Change the logged in user password. |
|
291 | + * |
|
292 | + * @param array $credentials |
|
293 | + * @return void |
|
294 | + */ |
|
295 | + public function changePassword($credentials) |
|
296 | + { |
|
297 | + $user = \Auth::user(); |
|
298 | + if ( ! \Hash::check($credentials['old_password'], $user->password)) |
|
299 | + { |
|
300 | + \ErrorHandler::invalidOldPassword(); |
|
301 | + } |
|
302 | + |
|
303 | + $user->password = $credentials['password']; |
|
304 | + $user->save(); |
|
305 | + } |
|
306 | + |
|
307 | + /** |
|
308 | + * Paginate all users in the given group based on the given conditions. |
|
309 | + * |
|
310 | + * @param string $groupName |
|
311 | + * @param array $relations |
|
312 | + * @param integer $perPage |
|
313 | + * @param string $sortBy |
|
314 | + * @param boolean $desc |
|
315 | + * @return \Illuminate\Http\Response |
|
316 | + */ |
|
317 | + public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc) |
|
318 | + { |
|
319 | + unset($conditions['page']); |
|
320 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
321 | + $sort = $desc ? 'desc' : 'asc'; |
|
322 | + $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
323 | + |
|
324 | + $model->whereHas('groups', function($q) use ($groupName){ |
|
325 | + $q->where('name', $groupName); |
|
326 | + }); |
|
327 | 327 | |
328 | 328 | |
329 | - if (count($conditions['conditionValues'])) |
|
330 | - { |
|
331 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
332 | - } |
|
333 | - |
|
334 | - if ($perPage) |
|
335 | - { |
|
336 | - return $model->orderBy($sortBy, $sort)->paginate($perPage); |
|
337 | - } |
|
338 | - |
|
339 | - return $model->orderBy($sortBy, $sort)->get(); |
|
340 | - } |
|
341 | - |
|
342 | - /** |
|
343 | - * Save the given data to the logged in user. |
|
344 | - * |
|
345 | - * @param array $credentials |
|
346 | - * @return object |
|
347 | - */ |
|
348 | - public function saveProfile($credentials) |
|
349 | - { |
|
350 | - $user = \Auth::user(); |
|
351 | - $user->save($credentials); |
|
352 | - |
|
353 | - return $user; |
|
354 | - } |
|
355 | - |
|
356 | - /** |
|
357 | - * Ensure access token hasn't expired or revoked. |
|
358 | - * |
|
359 | - * @param string $accessToken |
|
360 | - * @return boolean |
|
361 | - */ |
|
362 | - public function accessTokenExpiredOrRevoked($accessToken) |
|
363 | - { |
|
364 | - $data = new ValidationData(); |
|
365 | - $data->setCurrentTime(time()); |
|
366 | - |
|
367 | - if ($accessToken->validate($data) === false || $this->accessTokenRepository->isAccessTokenRevoked($accessToken->getClaim('jti'))) |
|
368 | - { |
|
369 | - return true; |
|
370 | - } |
|
371 | - |
|
372 | - return false; |
|
373 | - } |
|
329 | + if (count($conditions['conditionValues'])) |
|
330 | + { |
|
331 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
332 | + } |
|
333 | + |
|
334 | + if ($perPage) |
|
335 | + { |
|
336 | + return $model->orderBy($sortBy, $sort)->paginate($perPage); |
|
337 | + } |
|
338 | + |
|
339 | + return $model->orderBy($sortBy, $sort)->get(); |
|
340 | + } |
|
341 | + |
|
342 | + /** |
|
343 | + * Save the given data to the logged in user. |
|
344 | + * |
|
345 | + * @param array $credentials |
|
346 | + * @return object |
|
347 | + */ |
|
348 | + public function saveProfile($credentials) |
|
349 | + { |
|
350 | + $user = \Auth::user(); |
|
351 | + $user->save($credentials); |
|
352 | + |
|
353 | + return $user; |
|
354 | + } |
|
355 | + |
|
356 | + /** |
|
357 | + * Ensure access token hasn't expired or revoked. |
|
358 | + * |
|
359 | + * @param string $accessToken |
|
360 | + * @return boolean |
|
361 | + */ |
|
362 | + public function accessTokenExpiredOrRevoked($accessToken) |
|
363 | + { |
|
364 | + $data = new ValidationData(); |
|
365 | + $data->setCurrentTime(time()); |
|
366 | + |
|
367 | + if ($accessToken->validate($data) === false || $this->accessTokenRepository->isAccessTokenRevoked($accessToken->getClaim('jti'))) |
|
368 | + { |
|
369 | + return true; |
|
370 | + } |
|
371 | + |
|
372 | + return false; |
|
373 | + } |
|
374 | 374 | } |
@@ -2,117 +2,117 @@ |
||
2 | 2 | |
3 | 3 | class CachingDecorator |
4 | 4 | { |
5 | - /** |
|
6 | - * The repo implementation. |
|
7 | - * |
|
8 | - * @var string |
|
9 | - */ |
|
10 | - public $repo; |
|
5 | + /** |
|
6 | + * The repo implementation. |
|
7 | + * |
|
8 | + * @var string |
|
9 | + */ |
|
10 | + public $repo; |
|
11 | 11 | |
12 | - /** |
|
13 | - * The cache implementation. |
|
14 | - * |
|
15 | - * @var object |
|
16 | - */ |
|
17 | - protected $cache; |
|
12 | + /** |
|
13 | + * The cache implementation. |
|
14 | + * |
|
15 | + * @var object |
|
16 | + */ |
|
17 | + protected $cache; |
|
18 | 18 | |
19 | - /** |
|
20 | - * The modelKey implementation. |
|
21 | - * |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - public $modelKey; |
|
19 | + /** |
|
20 | + * The modelKey implementation. |
|
21 | + * |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + public $modelKey; |
|
25 | 25 | |
26 | - /** |
|
27 | - * The model implementation. |
|
28 | - * |
|
29 | - * @var string |
|
30 | - */ |
|
31 | - public $model; |
|
26 | + /** |
|
27 | + * The model implementation. |
|
28 | + * |
|
29 | + * @var string |
|
30 | + */ |
|
31 | + public $model; |
|
32 | 32 | |
33 | - /** |
|
34 | - * The modelClass implementation. |
|
35 | - * |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - public $modelClass; |
|
33 | + /** |
|
34 | + * The modelClass implementation. |
|
35 | + * |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + public $modelClass; |
|
39 | 39 | |
40 | - /** |
|
41 | - * The cacheConfig implementation. |
|
42 | - * |
|
43 | - * @var array |
|
44 | - */ |
|
45 | - public $cacheConfig; |
|
40 | + /** |
|
41 | + * The cacheConfig implementation. |
|
42 | + * |
|
43 | + * @var array |
|
44 | + */ |
|
45 | + public $cacheConfig; |
|
46 | 46 | |
47 | - /** |
|
48 | - * The cacheTag implementation. |
|
49 | - * |
|
50 | - * @var string |
|
51 | - */ |
|
52 | - public $cacheTag; |
|
47 | + /** |
|
48 | + * The cacheTag implementation. |
|
49 | + * |
|
50 | + * @var string |
|
51 | + */ |
|
52 | + public $cacheTag; |
|
53 | 53 | |
54 | - /** |
|
55 | - * Create new CachingDecorator instance. |
|
56 | - */ |
|
57 | - public function __construct($repo, $cache) |
|
58 | - { |
|
59 | - $this->repo = $repo; |
|
60 | - $this->cache = $cache; |
|
61 | - $this->model = $this->repo->model; |
|
62 | - $this->modelClass = get_class($this->model); |
|
63 | - $repoClass = explode('\\', get_class($this->repo)); |
|
64 | - $repoName = end($repoClass); |
|
65 | - $this->cacheTag = str_plural(lcfirst(substr($repoName, 0, strpos($repoName, 'Repository')))); |
|
66 | - } |
|
54 | + /** |
|
55 | + * Create new CachingDecorator instance. |
|
56 | + */ |
|
57 | + public function __construct($repo, $cache) |
|
58 | + { |
|
59 | + $this->repo = $repo; |
|
60 | + $this->cache = $cache; |
|
61 | + $this->model = $this->repo->model; |
|
62 | + $this->modelClass = get_class($this->model); |
|
63 | + $repoClass = explode('\\', get_class($this->repo)); |
|
64 | + $repoName = end($repoClass); |
|
65 | + $this->cacheTag = str_plural(lcfirst(substr($repoName, 0, strpos($repoName, 'Repository')))); |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * Handle the cache mechanism for the called method |
|
70 | - * based the configurations. |
|
71 | - * |
|
72 | - * @param string $name the called method name |
|
73 | - * @param array $arguments the method arguments |
|
74 | - * @return object |
|
75 | - */ |
|
76 | - public function __call($name, $arguments) |
|
77 | - { |
|
78 | - $this->setCacheConfig($name); |
|
68 | + /** |
|
69 | + * Handle the cache mechanism for the called method |
|
70 | + * based the configurations. |
|
71 | + * |
|
72 | + * @param string $name the called method name |
|
73 | + * @param array $arguments the method arguments |
|
74 | + * @return object |
|
75 | + */ |
|
76 | + public function __call($name, $arguments) |
|
77 | + { |
|
78 | + $this->setCacheConfig($name); |
|
79 | 79 | |
80 | - if ($this->cacheConfig && $this->cacheConfig == 'cache') |
|
81 | - { |
|
82 | - $page = \Request::get('page') !== null ? \Request::get('page') : '1'; |
|
83 | - $cacheKey = $name . $page . \Session::get('locale') . serialize($arguments); |
|
84 | - return $this->cache->tags([$this->cacheTag])->rememberForever($cacheKey, function() use ($arguments, $name) { |
|
85 | - return call_user_func_array([$this->repo, $name], $arguments); |
|
86 | - }); |
|
87 | - } |
|
88 | - else if ($this->cacheConfig) |
|
89 | - { |
|
90 | - $this->cache->tags($this->cacheConfig)->flush(); |
|
91 | - return call_user_func_array([$this->repo, $name], $arguments); |
|
92 | - } |
|
80 | + if ($this->cacheConfig && $this->cacheConfig == 'cache') |
|
81 | + { |
|
82 | + $page = \Request::get('page') !== null ? \Request::get('page') : '1'; |
|
83 | + $cacheKey = $name . $page . \Session::get('locale') . serialize($arguments); |
|
84 | + return $this->cache->tags([$this->cacheTag])->rememberForever($cacheKey, function() use ($arguments, $name) { |
|
85 | + return call_user_func_array([$this->repo, $name], $arguments); |
|
86 | + }); |
|
87 | + } |
|
88 | + else if ($this->cacheConfig) |
|
89 | + { |
|
90 | + $this->cache->tags($this->cacheConfig)->flush(); |
|
91 | + return call_user_func_array([$this->repo, $name], $arguments); |
|
92 | + } |
|
93 | 93 | |
94 | - return call_user_func_array([$this->repo, $name], $arguments); |
|
95 | - } |
|
94 | + return call_user_func_array([$this->repo, $name], $arguments); |
|
95 | + } |
|
96 | 96 | |
97 | - /** |
|
98 | - * Set cache config based on the called method. |
|
99 | - * |
|
100 | - * @param string $name |
|
101 | - * @return void |
|
102 | - */ |
|
103 | - private function setCacheConfig($name) |
|
104 | - { |
|
105 | - $config = \CoreConfig::getConfig(); |
|
106 | - $cacheConfig = array_key_exists($this->cacheTag, $config['cacheConfig']) ? $config['cacheConfig'][$this->cacheTag] : false; |
|
107 | - $this->cacheConfig = false; |
|
97 | + /** |
|
98 | + * Set cache config based on the called method. |
|
99 | + * |
|
100 | + * @param string $name |
|
101 | + * @return void |
|
102 | + */ |
|
103 | + private function setCacheConfig($name) |
|
104 | + { |
|
105 | + $config = \CoreConfig::getConfig(); |
|
106 | + $cacheConfig = array_key_exists($this->cacheTag, $config['cacheConfig']) ? $config['cacheConfig'][$this->cacheTag] : false; |
|
107 | + $this->cacheConfig = false; |
|
108 | 108 | |
109 | - if ($cacheConfig && in_array($name, $cacheConfig['cache'])) |
|
110 | - { |
|
111 | - $this->cacheConfig = 'cache'; |
|
112 | - } |
|
113 | - else if ($cacheConfig && isset($cacheConfig['clear'][$name])) |
|
114 | - { |
|
115 | - $this->cacheConfig = $cacheConfig['clear'][$name]; |
|
116 | - } |
|
117 | - } |
|
109 | + if ($cacheConfig && in_array($name, $cacheConfig['cache'])) |
|
110 | + { |
|
111 | + $this->cacheConfig = 'cache'; |
|
112 | + } |
|
113 | + else if ($cacheConfig && isset($cacheConfig['clear'][$name])) |
|
114 | + { |
|
115 | + $this->cacheConfig = $cacheConfig['clear'][$name]; |
|
116 | + } |
|
117 | + } |
|
118 | 118 | } |
119 | 119 | \ No newline at end of file |
@@ -8,264 +8,264 @@ |
||
8 | 8 | |
9 | 9 | class UsersController extends BaseApiController |
10 | 10 | { |
11 | - /** |
|
12 | - * The name of the model that is used by the base api controller |
|
13 | - * to preform actions like (add, edit ... etc). |
|
14 | - * @var string |
|
15 | - */ |
|
16 | - protected $model = 'users'; |
|
11 | + /** |
|
12 | + * The name of the model that is used by the base api controller |
|
13 | + * to preform actions like (add, edit ... etc). |
|
14 | + * @var string |
|
15 | + */ |
|
16 | + protected $model = 'users'; |
|
17 | 17 | |
18 | - /** |
|
19 | - * List of all route actions that the base api controller |
|
20 | - * will skip permissions check for them. |
|
21 | - * @var array |
|
22 | - */ |
|
23 | - protected $skipPermissionCheck = ['account', 'logout', 'changePassword']; |
|
18 | + /** |
|
19 | + * List of all route actions that the base api controller |
|
20 | + * will skip permissions check for them. |
|
21 | + * @var array |
|
22 | + */ |
|
23 | + protected $skipPermissionCheck = ['account', 'logout', 'changePassword']; |
|
24 | 24 | |
25 | - /** |
|
26 | - * List of all route actions that the base api controller |
|
27 | - * will skip login check for them. |
|
28 | - * @var array |
|
29 | - */ |
|
30 | - protected $skipLoginCheck = ['login', 'loginSocial', 'register', 'sendreset', 'resetpassword', 'refreshtoken']; |
|
25 | + /** |
|
26 | + * List of all route actions that the base api controller |
|
27 | + * will skip login check for them. |
|
28 | + * @var array |
|
29 | + */ |
|
30 | + protected $skipLoginCheck = ['login', 'loginSocial', 'register', 'sendreset', 'resetpassword', 'refreshtoken']; |
|
31 | 31 | |
32 | - /** |
|
33 | - * The validations rules used by the base api controller |
|
34 | - * to check before add. |
|
35 | - * @var array |
|
36 | - */ |
|
37 | - protected $validationRules = [ |
|
38 | - 'name' => 'nullable|string', |
|
39 | - 'email' => 'required|email|unique:users,email,{id}', |
|
40 | - 'password' => 'nullable|min:6' |
|
41 | - ]; |
|
32 | + /** |
|
33 | + * The validations rules used by the base api controller |
|
34 | + * to check before add. |
|
35 | + * @var array |
|
36 | + */ |
|
37 | + protected $validationRules = [ |
|
38 | + 'name' => 'nullable|string', |
|
39 | + 'email' => 'required|email|unique:users,email,{id}', |
|
40 | + 'password' => 'nullable|min:6' |
|
41 | + ]; |
|
42 | 42 | |
43 | - /** |
|
44 | - * The loginProxy implementation. |
|
45 | - * |
|
46 | - * @var App\Modules\V1\Acl\Proxy\LoginProxy |
|
47 | - */ |
|
48 | - protected $loginProxy; |
|
43 | + /** |
|
44 | + * The loginProxy implementation. |
|
45 | + * |
|
46 | + * @var App\Modules\V1\Acl\Proxy\LoginProxy |
|
47 | + */ |
|
48 | + protected $loginProxy; |
|
49 | 49 | |
50 | - public function __construct(LoginProxy $loginProxy) |
|
51 | - { |
|
52 | - $this->loginProxy = $loginProxy; |
|
53 | - parent::__construct(); |
|
54 | - } |
|
50 | + public function __construct(LoginProxy $loginProxy) |
|
51 | + { |
|
52 | + $this->loginProxy = $loginProxy; |
|
53 | + parent::__construct(); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Return the logged in user account. |
|
58 | - * |
|
59 | - * @return \Illuminate\Http\Response |
|
60 | - */ |
|
61 | - public function account() |
|
62 | - { |
|
63 | - return \Response::json($this->repo->account($this->relations), 200); |
|
64 | - } |
|
56 | + /** |
|
57 | + * Return the logged in user account. |
|
58 | + * |
|
59 | + * @return \Illuminate\Http\Response |
|
60 | + */ |
|
61 | + public function account() |
|
62 | + { |
|
63 | + return \Response::json($this->repo->account($this->relations), 200); |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * Block the user. |
|
68 | - * |
|
69 | - * @param integer $id Id of the user. |
|
70 | - * @return \Illuminate\Http\Response |
|
71 | - */ |
|
72 | - public function block($id) |
|
73 | - { |
|
74 | - return \Response::json($this->repo->block($id), 200); |
|
75 | - } |
|
66 | + /** |
|
67 | + * Block the user. |
|
68 | + * |
|
69 | + * @param integer $id Id of the user. |
|
70 | + * @return \Illuminate\Http\Response |
|
71 | + */ |
|
72 | + public function block($id) |
|
73 | + { |
|
74 | + return \Response::json($this->repo->block($id), 200); |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * Unblock the user. |
|
79 | - * |
|
80 | - * @param integer $id Id of the user. |
|
81 | - * @return \Illuminate\Http\Response |
|
82 | - */ |
|
83 | - public function unblock($id) |
|
84 | - { |
|
85 | - return \Response::json($this->repo->unblock($id), 200); |
|
86 | - } |
|
77 | + /** |
|
78 | + * Unblock the user. |
|
79 | + * |
|
80 | + * @param integer $id Id of the user. |
|
81 | + * @return \Illuminate\Http\Response |
|
82 | + */ |
|
83 | + public function unblock($id) |
|
84 | + { |
|
85 | + return \Response::json($this->repo->unblock($id), 200); |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * Logout the user. |
|
90 | - * |
|
91 | - * @return \Illuminate\Http\Response |
|
92 | - */ |
|
93 | - public function logout() |
|
94 | - { |
|
95 | - return \Response::json($this->loginProxy->logout(), 200); |
|
96 | - } |
|
88 | + /** |
|
89 | + * Logout the user. |
|
90 | + * |
|
91 | + * @return \Illuminate\Http\Response |
|
92 | + */ |
|
93 | + public function logout() |
|
94 | + { |
|
95 | + return \Response::json($this->loginProxy->logout(), 200); |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * Handle a registration request. |
|
100 | - * |
|
101 | - * @param \Illuminate\Http\Request $request |
|
102 | - * @return \Illuminate\Http\Response |
|
103 | - */ |
|
104 | - public function register(Request $request) |
|
105 | - { |
|
106 | - $this->validate($request, [ |
|
107 | - 'name' => 'nullable|string', |
|
108 | - 'email' => 'required|email|unique:users,email,{id}', |
|
109 | - 'password' => 'required|min:6' |
|
110 | - ]); |
|
98 | + /** |
|
99 | + * Handle a registration request. |
|
100 | + * |
|
101 | + * @param \Illuminate\Http\Request $request |
|
102 | + * @return \Illuminate\Http\Response |
|
103 | + */ |
|
104 | + public function register(Request $request) |
|
105 | + { |
|
106 | + $this->validate($request, [ |
|
107 | + 'name' => 'nullable|string', |
|
108 | + 'email' => 'required|email|unique:users,email,{id}', |
|
109 | + 'password' => 'required|min:6' |
|
110 | + ]); |
|
111 | 111 | |
112 | - return \Response::json($this->repo->register($request->only('email', 'password')), 200); |
|
113 | - } |
|
112 | + return \Response::json($this->repo->register($request->only('email', 'password')), 200); |
|
113 | + } |
|
114 | 114 | |
115 | - /** |
|
116 | - * Handle a login request to the application. |
|
117 | - * |
|
118 | - * @param \Illuminate\Http\Request $request |
|
119 | - * @return \Illuminate\Http\Response |
|
120 | - */ |
|
121 | - public function login(Request $request) |
|
122 | - { |
|
123 | - $this->validate($request, [ |
|
124 | - 'email' => 'required|email', |
|
125 | - 'password' => 'required|min:6', |
|
126 | - 'admin' => 'boolean' |
|
127 | - ]); |
|
115 | + /** |
|
116 | + * Handle a login request to the application. |
|
117 | + * |
|
118 | + * @param \Illuminate\Http\Request $request |
|
119 | + * @return \Illuminate\Http\Response |
|
120 | + */ |
|
121 | + public function login(Request $request) |
|
122 | + { |
|
123 | + $this->validate($request, [ |
|
124 | + 'email' => 'required|email', |
|
125 | + 'password' => 'required|min:6', |
|
126 | + 'admin' => 'boolean' |
|
127 | + ]); |
|
128 | 128 | |
129 | - return \Response::json($this->loginProxy->login($request->only('email', 'password'), $request->get('admin')), 200); |
|
130 | - } |
|
129 | + return \Response::json($this->loginProxy->login($request->only('email', 'password'), $request->get('admin')), 200); |
|
130 | + } |
|
131 | 131 | |
132 | - /** |
|
133 | - * Handle a social login request of the none admin to the application. |
|
134 | - * |
|
135 | - * @param \Illuminate\Http\Request $request |
|
136 | - * @return \Illuminate\Http\Response |
|
137 | - */ |
|
138 | - public function loginSocial(Request $request) |
|
139 | - { |
|
140 | - $this->validate($request, [ |
|
141 | - 'auth_code' => 'required_without:access_token', |
|
142 | - 'access_token' => 'required_without:auth_code', |
|
143 | - 'type' => 'required|in:facebook,google' |
|
144 | - ]); |
|
132 | + /** |
|
133 | + * Handle a social login request of the none admin to the application. |
|
134 | + * |
|
135 | + * @param \Illuminate\Http\Request $request |
|
136 | + * @return \Illuminate\Http\Response |
|
137 | + */ |
|
138 | + public function loginSocial(Request $request) |
|
139 | + { |
|
140 | + $this->validate($request, [ |
|
141 | + 'auth_code' => 'required_without:access_token', |
|
142 | + 'access_token' => 'required_without:auth_code', |
|
143 | + 'type' => 'required|in:facebook,google' |
|
144 | + ]); |
|
145 | 145 | |
146 | - return \Response::json($this->repo->loginSocial($request->only('auth_code', 'access_token', 'type')), 200); |
|
147 | - } |
|
146 | + return \Response::json($this->repo->loginSocial($request->only('auth_code', 'access_token', 'type')), 200); |
|
147 | + } |
|
148 | 148 | |
149 | - /** |
|
150 | - * Assign the given groups to the given user. |
|
151 | - * |
|
152 | - * @param \Illuminate\Http\Request $request |
|
153 | - * @return \Illuminate\Http\Response |
|
154 | - */ |
|
155 | - public function assigngroups(Request $request) |
|
156 | - { |
|
157 | - $this->validate($request, [ |
|
158 | - 'group_ids' => 'required|exists:groups,id', |
|
159 | - 'user_id' => 'required|exists:users,id' |
|
160 | - ]); |
|
149 | + /** |
|
150 | + * Assign the given groups to the given user. |
|
151 | + * |
|
152 | + * @param \Illuminate\Http\Request $request |
|
153 | + * @return \Illuminate\Http\Response |
|
154 | + */ |
|
155 | + public function assigngroups(Request $request) |
|
156 | + { |
|
157 | + $this->validate($request, [ |
|
158 | + 'group_ids' => 'required|exists:groups,id', |
|
159 | + 'user_id' => 'required|exists:users,id' |
|
160 | + ]); |
|
161 | 161 | |
162 | - return \Response::json($this->repo->assignGroups($request->get('user_id'), $request->get('group_ids')), 200); |
|
163 | - } |
|
162 | + return \Response::json($this->repo->assignGroups($request->get('user_id'), $request->get('group_ids')), 200); |
|
163 | + } |
|
164 | 164 | |
165 | - /** |
|
166 | - * Send a reset link to the given user. |
|
167 | - * |
|
168 | - * @param \Illuminate\Http\Request $request |
|
169 | - * @return \Illuminate\Http\Response |
|
170 | - */ |
|
171 | - public function sendreset(Request $request) |
|
172 | - { |
|
173 | - $this->validate($request, ['email' => 'required|email']); |
|
165 | + /** |
|
166 | + * Send a reset link to the given user. |
|
167 | + * |
|
168 | + * @param \Illuminate\Http\Request $request |
|
169 | + * @return \Illuminate\Http\Response |
|
170 | + */ |
|
171 | + public function sendreset(Request $request) |
|
172 | + { |
|
173 | + $this->validate($request, ['email' => 'required|email']); |
|
174 | 174 | |
175 | - return \Response::json($this->repo->sendReset($request->only('email')), 200); |
|
176 | - } |
|
175 | + return \Response::json($this->repo->sendReset($request->only('email')), 200); |
|
176 | + } |
|
177 | 177 | |
178 | - /** |
|
179 | - * Reset the given user's password. |
|
180 | - * |
|
181 | - * @param \Illuminate\Http\Request $request |
|
182 | - * @return \Illuminate\Http\Response |
|
183 | - */ |
|
184 | - public function resetpassword(Request $request) |
|
185 | - { |
|
186 | - $this->validate($request, [ |
|
187 | - 'token' => 'required', |
|
188 | - 'email' => 'required|email', |
|
189 | - 'password' => 'required|confirmed|min:6', |
|
190 | - 'password_confirmation' => 'required', |
|
191 | - ]); |
|
178 | + /** |
|
179 | + * Reset the given user's password. |
|
180 | + * |
|
181 | + * @param \Illuminate\Http\Request $request |
|
182 | + * @return \Illuminate\Http\Response |
|
183 | + */ |
|
184 | + public function resetpassword(Request $request) |
|
185 | + { |
|
186 | + $this->validate($request, [ |
|
187 | + 'token' => 'required', |
|
188 | + 'email' => 'required|email', |
|
189 | + 'password' => 'required|confirmed|min:6', |
|
190 | + 'password_confirmation' => 'required', |
|
191 | + ]); |
|
192 | 192 | |
193 | - return \Response::json($this->repo->resetPassword($request->only('email', 'password', 'password_confirmation', 'token')), 200); |
|
194 | - } |
|
193 | + return \Response::json($this->repo->resetPassword($request->only('email', 'password', 'password_confirmation', 'token')), 200); |
|
194 | + } |
|
195 | 195 | |
196 | - /** |
|
197 | - * Change the logged in user password. |
|
198 | - * |
|
199 | - * @param \Illuminate\Http\Request $request |
|
200 | - * @return \Illuminate\Http\Response |
|
201 | - */ |
|
202 | - public function changePassword(Request $request) |
|
203 | - { |
|
204 | - $this->validate($request, [ |
|
205 | - 'old_password' => 'required', |
|
206 | - 'password' => 'required|confirmed|min:6', |
|
207 | - 'password_confirmation' => 'required', |
|
208 | - ]); |
|
196 | + /** |
|
197 | + * Change the logged in user password. |
|
198 | + * |
|
199 | + * @param \Illuminate\Http\Request $request |
|
200 | + * @return \Illuminate\Http\Response |
|
201 | + */ |
|
202 | + public function changePassword(Request $request) |
|
203 | + { |
|
204 | + $this->validate($request, [ |
|
205 | + 'old_password' => 'required', |
|
206 | + 'password' => 'required|confirmed|min:6', |
|
207 | + 'password_confirmation' => 'required', |
|
208 | + ]); |
|
209 | 209 | |
210 | - return \Response::json($this->repo->changePassword($request->only('old_password', 'password', 'password_confirmation')), 200); |
|
211 | - } |
|
210 | + return \Response::json($this->repo->changePassword($request->only('old_password', 'password', 'password_confirmation')), 200); |
|
211 | + } |
|
212 | 212 | |
213 | - /** |
|
214 | - * Refresh the expired login token. |
|
215 | - * |
|
216 | - * @param string $refreshToken |
|
217 | - * @return \Illuminate\Http\Response |
|
218 | - */ |
|
219 | - public function refreshtoken($refreshToken) |
|
220 | - { |
|
221 | - return \Response::json($this->loginProxy->refreshtoken($refreshToken), 200); |
|
222 | - } |
|
213 | + /** |
|
214 | + * Refresh the expired login token. |
|
215 | + * |
|
216 | + * @param string $refreshToken |
|
217 | + * @return \Illuminate\Http\Response |
|
218 | + */ |
|
219 | + public function refreshtoken($refreshToken) |
|
220 | + { |
|
221 | + return \Response::json($this->loginProxy->refreshtoken($refreshToken), 200); |
|
222 | + } |
|
223 | 223 | |
224 | - /** |
|
225 | - * Paginate all users with inthe given group. |
|
226 | - * |
|
227 | - * @param \Illuminate\Http\Request $request |
|
228 | - * @param string $groupName The name of the requested group. |
|
229 | - * @param integer $perPage Number of rows per page default 15. |
|
230 | - * @param string $sortBy The name of the column to sort by. |
|
231 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
232 | - * @return \Illuminate\Http\Response |
|
233 | - */ |
|
234 | - public function group(Request $request, $groupName, $perPage = false, $sortBy = 'created_at', $desc = 1) |
|
235 | - { |
|
236 | - return \Response::json($this->repo->group($request->all(), $groupName, $this->relations, $perPage, $sortBy, $desc), 200); |
|
237 | - } |
|
224 | + /** |
|
225 | + * Paginate all users with inthe given group. |
|
226 | + * |
|
227 | + * @param \Illuminate\Http\Request $request |
|
228 | + * @param string $groupName The name of the requested group. |
|
229 | + * @param integer $perPage Number of rows per page default 15. |
|
230 | + * @param string $sortBy The name of the column to sort by. |
|
231 | + * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
232 | + * @return \Illuminate\Http\Response |
|
233 | + */ |
|
234 | + public function group(Request $request, $groupName, $perPage = false, $sortBy = 'created_at', $desc = 1) |
|
235 | + { |
|
236 | + return \Response::json($this->repo->group($request->all(), $groupName, $this->relations, $perPage, $sortBy, $desc), 200); |
|
237 | + } |
|
238 | 238 | |
239 | - /** |
|
240 | - * Save the given data to the logged in user. |
|
241 | - * |
|
242 | - * @param \Illuminate\Http\Request $request |
|
243 | - * @return \Illuminate\Http\Response |
|
244 | - */ |
|
245 | - public function saveProfile(Request $request) |
|
246 | - { |
|
247 | - foreach ($this->validationRules as &$rule) |
|
248 | - { |
|
249 | - if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) |
|
250 | - { |
|
251 | - $rule .= ',deleted_at,NULL'; |
|
252 | - } |
|
239 | + /** |
|
240 | + * Save the given data to the logged in user. |
|
241 | + * |
|
242 | + * @param \Illuminate\Http\Request $request |
|
243 | + * @return \Illuminate\Http\Response |
|
244 | + */ |
|
245 | + public function saveProfile(Request $request) |
|
246 | + { |
|
247 | + foreach ($this->validationRules as &$rule) |
|
248 | + { |
|
249 | + if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) |
|
250 | + { |
|
251 | + $rule .= ',deleted_at,NULL'; |
|
252 | + } |
|
253 | 253 | |
254 | - if ($request->has('id')) |
|
255 | - { |
|
256 | - $rule = str_replace('{id}', $request->get('id'), $rule); |
|
257 | - } |
|
258 | - else |
|
259 | - { |
|
260 | - $rule = str_replace(',{id}', '', $rule); |
|
261 | - } |
|
262 | - } |
|
254 | + if ($request->has('id')) |
|
255 | + { |
|
256 | + $rule = str_replace('{id}', $request->get('id'), $rule); |
|
257 | + } |
|
258 | + else |
|
259 | + { |
|
260 | + $rule = str_replace(',{id}', '', $rule); |
|
261 | + } |
|
262 | + } |
|
263 | 263 | |
264 | - $this->validate($request, $this->validationRules); |
|
264 | + $this->validate($request, $this->validationRules); |
|
265 | 265 | |
266 | - if ($this->model) |
|
267 | - { |
|
268 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->saveProfile($request->all()), 200); |
|
269 | - } |
|
270 | - } |
|
266 | + if ($this->model) |
|
267 | + { |
|
268 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->saveProfile($request->all()), 200); |
|
269 | + } |
|
270 | + } |
|
271 | 271 | } |
@@ -8,46 +8,46 @@ |
||
8 | 8 | |
9 | 9 | class OauthClientsController extends BaseApiController |
10 | 10 | { |
11 | - /** |
|
12 | - * The name of the model that is used by the base api controller |
|
13 | - * to preform actions like (add, edit ... etc). |
|
14 | - * @var string |
|
15 | - */ |
|
16 | - protected $model = 'oauthClients'; |
|
11 | + /** |
|
12 | + * The name of the model that is used by the base api controller |
|
13 | + * to preform actions like (add, edit ... etc). |
|
14 | + * @var string |
|
15 | + */ |
|
16 | + protected $model = 'oauthClients'; |
|
17 | 17 | |
18 | - /** |
|
19 | - * The validations rules used by the base api controller |
|
20 | - * to check before add. |
|
21 | - * @var array |
|
22 | - */ |
|
23 | - protected $validationRules = [ |
|
24 | - 'name' => 'required|max:255', |
|
25 | - 'redirect' => 'required|url', |
|
26 | - 'user_id' => 'required|array|exists:users,id', |
|
27 | - 'personal_access_client' => 'boolean', |
|
28 | - 'password_client' => 'boolean', |
|
29 | - 'revoked' => 'boolean' |
|
30 | - ]; |
|
18 | + /** |
|
19 | + * The validations rules used by the base api controller |
|
20 | + * to check before add. |
|
21 | + * @var array |
|
22 | + */ |
|
23 | + protected $validationRules = [ |
|
24 | + 'name' => 'required|max:255', |
|
25 | + 'redirect' => 'required|url', |
|
26 | + 'user_id' => 'required|array|exists:users,id', |
|
27 | + 'personal_access_client' => 'boolean', |
|
28 | + 'password_client' => 'boolean', |
|
29 | + 'revoked' => 'boolean' |
|
30 | + ]; |
|
31 | 31 | |
32 | - /** |
|
33 | - * Revoke the given client. |
|
34 | - * |
|
35 | - * @param integer $clientId |
|
36 | - * @return \Illuminate\Http\Response |
|
37 | - */ |
|
38 | - public function revoke($clientId) |
|
39 | - { |
|
40 | - return \Response::json($this->repo->revoke($clientId), 200); |
|
41 | - } |
|
32 | + /** |
|
33 | + * Revoke the given client. |
|
34 | + * |
|
35 | + * @param integer $clientId |
|
36 | + * @return \Illuminate\Http\Response |
|
37 | + */ |
|
38 | + public function revoke($clientId) |
|
39 | + { |
|
40 | + return \Response::json($this->repo->revoke($clientId), 200); |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * Regenerate seceret for the given client. |
|
45 | - * |
|
46 | - * @param integer $clientId |
|
47 | - * @return \Illuminate\Http\Response |
|
48 | - */ |
|
49 | - public function regenerateSecret($clientId) |
|
50 | - { |
|
51 | - return \Response::json($this->repo->regenerateSecret($clientId), 200); |
|
52 | - } |
|
43 | + /** |
|
44 | + * Regenerate seceret for the given client. |
|
45 | + * |
|
46 | + * @param integer $clientId |
|
47 | + * @return \Illuminate\Http\Response |
|
48 | + */ |
|
49 | + public function regenerateSecret($clientId) |
|
50 | + { |
|
51 | + return \Response::json($this->repo->regenerateSecret($clientId), 200); |
|
52 | + } |
|
53 | 53 | } |
@@ -13,14 +13,14 @@ |
||
13 | 13 | * to preform actions like (add, edit ... etc). |
14 | 14 | * @var string |
15 | 15 | */ |
16 | - protected $model = 'oauthClients'; |
|
16 | + protected $model = 'oauthClients'; |
|
17 | 17 | |
18 | 18 | /** |
19 | 19 | * The validations rules used by the base api controller |
20 | 20 | * to check before add. |
21 | 21 | * @var array |
22 | 22 | */ |
23 | - protected $validationRules = [ |
|
23 | + protected $validationRules = [ |
|
24 | 24 | 'name' => 'required|max:255', |
25 | 25 | 'redirect' => 'required|url', |
26 | 26 | 'user_id' => 'required|array|exists:users,id', |
@@ -5,105 +5,105 @@ |
||
5 | 5 | |
6 | 6 | class LoginProxy |
7 | 7 | { |
8 | - private $apiConsumer; |
|
9 | - |
|
10 | - private $auth; |
|
11 | - |
|
12 | - private $db; |
|
13 | - |
|
14 | - private $request; |
|
15 | - |
|
16 | - private $userRepository; |
|
17 | - |
|
18 | - public function __construct(Application $app, UserRepository $userRepository) |
|
19 | - { |
|
20 | - |
|
21 | - $this->userRepository = $userRepository; |
|
22 | - $this->apiConsumer = $app->make('apiconsumer'); |
|
23 | - $this->auth = $app->make('auth'); |
|
24 | - $this->db = $app->make('db'); |
|
25 | - $this->request = $app->make('request'); |
|
26 | - } |
|
27 | - |
|
28 | - /** |
|
29 | - * Attempt to create an access token using user credentials. |
|
30 | - * |
|
31 | - * @param array $credentials |
|
32 | - * @param boolean $adminLogin |
|
33 | - * @return array |
|
34 | - */ |
|
35 | - public function login($credentials, $adminLogin = false) |
|
36 | - { |
|
37 | - $this->userRepository->login($credentials, $adminLogin); |
|
38 | - |
|
39 | - return $this->proxy('password', [ |
|
40 | - 'username' => $credentials['email'], |
|
41 | - 'password' => $credentials['password'] |
|
42 | - ]); |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * Attempt to refresh the access token useing the given refresh token. |
|
47 | - * |
|
48 | - * @param string $refreshToken |
|
49 | - * @return array |
|
50 | - */ |
|
51 | - public function refreshtoken($refreshToken) |
|
52 | - { |
|
53 | - return $this->proxy('refresh_token', [ |
|
54 | - 'refresh_token' => $refreshToken |
|
55 | - ]); |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * Proxy a request to the OAuth server. |
|
60 | - * |
|
61 | - * @param string $grantType what type of grant type should be proxied |
|
62 | - * @param array |
|
63 | - */ |
|
64 | - public function proxy($grantType, array $data = []) |
|
65 | - { |
|
66 | - $data = array_merge($data, [ |
|
67 | - 'client_id' => env('PASSWORD_CLIENT_ID'), |
|
68 | - 'client_secret' => env('PASSWORD_CLIENT_SECRET'), |
|
69 | - 'grant_type' => $grantType |
|
70 | - ]); |
|
71 | - |
|
72 | - $response = $this->apiConsumer->post('/oauth/token', $data); |
|
73 | - |
|
74 | - if ( ! $response->isSuccessful()) |
|
75 | - { |
|
76 | - if ($grantType == 'refresh_token') |
|
77 | - { |
|
78 | - \ErrorHandler::invalidRefreshToken(); |
|
79 | - } |
|
80 | - |
|
81 | - \ErrorHandler::loginFailed(); |
|
82 | - } |
|
83 | - |
|
84 | - $data = json_decode($response->getContent()); |
|
85 | - |
|
86 | - return [ |
|
87 | - 'access_token' => $data->access_token, |
|
88 | - 'refresh_token' => $data->refresh_token, |
|
89 | - 'expires_in' => $data->expires_in |
|
90 | - ]; |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Logs out the user. We revoke access token and refresh token. |
|
95 | - */ |
|
96 | - public function logout() |
|
97 | - { |
|
98 | - $accessToken = $this->auth->user()->token(); |
|
99 | - |
|
100 | - $this->db |
|
101 | - ->table('oauth_refresh_tokens') |
|
102 | - ->where('access_token_id', $accessToken->id) |
|
103 | - ->update([ |
|
104 | - 'revoked' => true |
|
105 | - ]); |
|
106 | - |
|
107 | - $accessToken->revoke(); |
|
108 | - } |
|
8 | + private $apiConsumer; |
|
9 | + |
|
10 | + private $auth; |
|
11 | + |
|
12 | + private $db; |
|
13 | + |
|
14 | + private $request; |
|
15 | + |
|
16 | + private $userRepository; |
|
17 | + |
|
18 | + public function __construct(Application $app, UserRepository $userRepository) |
|
19 | + { |
|
20 | + |
|
21 | + $this->userRepository = $userRepository; |
|
22 | + $this->apiConsumer = $app->make('apiconsumer'); |
|
23 | + $this->auth = $app->make('auth'); |
|
24 | + $this->db = $app->make('db'); |
|
25 | + $this->request = $app->make('request'); |
|
26 | + } |
|
27 | + |
|
28 | + /** |
|
29 | + * Attempt to create an access token using user credentials. |
|
30 | + * |
|
31 | + * @param array $credentials |
|
32 | + * @param boolean $adminLogin |
|
33 | + * @return array |
|
34 | + */ |
|
35 | + public function login($credentials, $adminLogin = false) |
|
36 | + { |
|
37 | + $this->userRepository->login($credentials, $adminLogin); |
|
38 | + |
|
39 | + return $this->proxy('password', [ |
|
40 | + 'username' => $credentials['email'], |
|
41 | + 'password' => $credentials['password'] |
|
42 | + ]); |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * Attempt to refresh the access token useing the given refresh token. |
|
47 | + * |
|
48 | + * @param string $refreshToken |
|
49 | + * @return array |
|
50 | + */ |
|
51 | + public function refreshtoken($refreshToken) |
|
52 | + { |
|
53 | + return $this->proxy('refresh_token', [ |
|
54 | + 'refresh_token' => $refreshToken |
|
55 | + ]); |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * Proxy a request to the OAuth server. |
|
60 | + * |
|
61 | + * @param string $grantType what type of grant type should be proxied |
|
62 | + * @param array |
|
63 | + */ |
|
64 | + public function proxy($grantType, array $data = []) |
|
65 | + { |
|
66 | + $data = array_merge($data, [ |
|
67 | + 'client_id' => env('PASSWORD_CLIENT_ID'), |
|
68 | + 'client_secret' => env('PASSWORD_CLIENT_SECRET'), |
|
69 | + 'grant_type' => $grantType |
|
70 | + ]); |
|
71 | + |
|
72 | + $response = $this->apiConsumer->post('/oauth/token', $data); |
|
73 | + |
|
74 | + if ( ! $response->isSuccessful()) |
|
75 | + { |
|
76 | + if ($grantType == 'refresh_token') |
|
77 | + { |
|
78 | + \ErrorHandler::invalidRefreshToken(); |
|
79 | + } |
|
80 | + |
|
81 | + \ErrorHandler::loginFailed(); |
|
82 | + } |
|
83 | + |
|
84 | + $data = json_decode($response->getContent()); |
|
85 | + |
|
86 | + return [ |
|
87 | + 'access_token' => $data->access_token, |
|
88 | + 'refresh_token' => $data->refresh_token, |
|
89 | + 'expires_in' => $data->expires_in |
|
90 | + ]; |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Logs out the user. We revoke access token and refresh token. |
|
95 | + */ |
|
96 | + public function logout() |
|
97 | + { |
|
98 | + $accessToken = $this->auth->user()->token(); |
|
99 | + |
|
100 | + $this->db |
|
101 | + ->table('oauth_refresh_tokens') |
|
102 | + ->where('access_token_id', $accessToken->id) |
|
103 | + ->update([ |
|
104 | + 'revoked' => true |
|
105 | + ]); |
|
106 | + |
|
107 | + $accessToken->revoke(); |
|
108 | + } |
|
109 | 109 | } |
110 | 110 | \ No newline at end of file |