Completed
Push — master ( a7be99...859bad )
by Sherif
02:52
created
src/Modules/V1/Acl/Repositories/UserRepository.php 2 patches
Indentation   +358 added lines, -358 removed lines patch added patch discarded remove patch
@@ -7,369 +7,369 @@
 block discarded – undo
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 \App\Modules\V1\Acl\Proxy\LoginProxy
24
-     */
25
-    protected $loginProxy;
26
-
27
-    /**
28
-     * The accessTokenRepository implementation.
29
-     * @var \League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface
30
-     */
31
-    private $accessTokenRepository;
32
-
33
-    public function __construct(LoginProxy $loginProxy, AccessTokenRepositoryInterface $accessTokenRepository)
34
-    {        
35
-        $this->loginProxy            = $loginProxy;
36
-        $this->accessTokenRepository = $accessTokenRepository;
37
-        parent::__construct();
38
-    }
39
-
40
-    /**
41
-     * Return the logged in user account.
42
-     *
43
-     * @param  array   $relations
44
-     * @return boolean
45
-     */
46
-    public function account($relations = [])
47
-    {
48
-        $permissions = [];
49
-        $user        = \Core::users()->find(\Auth::id(), $relations);
50
-        foreach ($user->groups()->get() as $group)
51
-        {
52
-            $group->permissions->each(function ($permission) use (&$permissions){
53
-                $permissions[$permission->model][$permission->id] = $permission->name;
54
-            });
55
-        }
56
-        $user->permissions = $permissions;
57
-
58
-       return $user;
59
-    }
60
-
61
-    /**
62
-     * Check if the logged in user or the given user 
63
-     * has the given permissions on the given model.
64
-     * 
65
-     * @param  string  $nameOfPermission
66
-     * @param  string  $model            
67
-     * @param  boolean $user
68
-     * @return boolean
69
-     */
70
-    public function can($nameOfPermission, $model, $user = false)
71
-    {      
72
-        $user        = $user ?: $this->find(\Auth::id(), ['groups.permissions']);
73
-        $permissions = [];
74
-
75
-        $user->groups->pluck('permissions')->each(function ($permission) use (&$permissions, $model){
76
-            $permissions = array_merge($permissions, $permission->where('model', $model)->pluck('name')->toArray()); 
77
-        });
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 \App\Modules\V1\Acl\Proxy\LoginProxy
24
+	 */
25
+	protected $loginProxy;
26
+
27
+	/**
28
+	 * The accessTokenRepository implementation.
29
+	 * @var \League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface
30
+	 */
31
+	private $accessTokenRepository;
32
+
33
+	public function __construct(LoginProxy $loginProxy, AccessTokenRepositoryInterface $accessTokenRepository)
34
+	{        
35
+		$this->loginProxy            = $loginProxy;
36
+		$this->accessTokenRepository = $accessTokenRepository;
37
+		parent::__construct();
38
+	}
39
+
40
+	/**
41
+	 * Return the logged in user account.
42
+	 *
43
+	 * @param  array   $relations
44
+	 * @return boolean
45
+	 */
46
+	public function account($relations = [])
47
+	{
48
+		$permissions = [];
49
+		$user        = \Core::users()->find(\Auth::id(), $relations);
50
+		foreach ($user->groups()->get() as $group)
51
+		{
52
+			$group->permissions->each(function ($permission) use (&$permissions){
53
+				$permissions[$permission->model][$permission->id] = $permission->name;
54
+			});
55
+		}
56
+		$user->permissions = $permissions;
57
+
58
+	   return $user;
59
+	}
60
+
61
+	/**
62
+	 * Check if the logged in user or the given user 
63
+	 * has the given permissions on the given model.
64
+	 * 
65
+	 * @param  string  $nameOfPermission
66
+	 * @param  string  $model            
67
+	 * @param  boolean $user
68
+	 * @return boolean
69
+	 */
70
+	public function can($nameOfPermission, $model, $user = false)
71
+	{      
72
+		$user        = $user ?: $this->find(\Auth::id(), ['groups.permissions']);
73
+		$permissions = [];
74
+
75
+		$user->groups->pluck('permissions')->each(function ($permission) use (&$permissions, $model){
76
+			$permissions = array_merge($permissions, $permission->where('model', $model)->pluck('name')->toArray()); 
77
+		});
78 78
         
79
-        return in_array($nameOfPermission, $permissions);
80
-    }
81
-
82
-    /**
83
-     * Check if the logged in user has the given group.
84
-     * 
85
-     * @param  string  $groupName
86
-     * @param  integer $userId
87
-     * @return boolean
88
-     */
89
-    public function hasGroup($groupName, $userId = false)
90
-    {
91
-        $userId = $userId ?: \Auth::id();
92
-        $groups = $this->find($userId)->groups;
93
-        return $groups->pluck('name')->search($groupName, true) === false ? false : true;
94
-    }
95
-
96
-    /**
97
-     * Assign the given group ids to the given user.
98
-     * 
99
-     * @param  integer $user_id    
100
-     * @param  array   $group_ids
101
-     * @return object
102
-     */
103
-    public function assignGroups($user_id, $group_ids)
104
-    {
105
-        \DB::transaction(function () use ($user_id, $group_ids) {
106
-            $user = $this->find($user_id);
107
-            $user->groups()->detach();
108
-            $user->groups()->attach($group_ids);
109
-        });
110
-
111
-        return $this->find($user_id);
112
-    }
113
-
114
-    /**
115
-     * Handle a login request to the application.
116
-     * 
117
-     * @param  array   $credentials    
118
-     * @param  boolean $adminLogin
119
-     * @return object
120
-     */
121
-    public function login($credentials, $adminLogin = false)
122
-    {
123
-        if ( ! $user = $this->first(['email' => $credentials['email']])) 
124
-        {
125
-            \ErrorHandler::loginFailed();
126
-        }
127
-        else if ($adminLogin && $user->groups->pluck('name')->search('Admin', true) === false) 
128
-        {
129
-            \ErrorHandler::loginFailed();
130
-        }
131
-        else if ( ! $adminLogin && $user->groups->pluck('name')->search('Admin', true) !== false) 
132
-        {
133
-            \ErrorHandler::loginFailed();
134
-        }
135
-        else if ($user->blocked)
136
-        {
137
-            \ErrorHandler::userIsBlocked();
138
-        }
139
-
140
-        return $user;
141
-    }
142
-
143
-    /**
144
-     * Handle a social login request of the none admin to the application.
145
-     * 
146
-     * @param  array   $credentials
147
-     * @return array
148
-     */
149
-    public function loginSocial($credentials)
150
-    {
151
-        $access_token = $credentials['auth_code'] ? \Socialite::driver($credentials['type'])->getAccessToken($credentials['auth_code']) : $credentials['access_token'];
152
-        $user         = \Socialite::driver($credentials['type'])->userFromToken($access_token);
153
-
154
-        if ( ! $user->email)
155
-        {
156
-            \ErrorHandler::noSocialEmail();
157
-        }
158
-
159
-        if ( ! $registeredUser = $this->model->where('email', $user->email)->first()) 
160
-        {
161
-            $data = ['email' => $user->email, 'password' => ''];
162
-            return $this->register($data);
163
-        }
164
-        else
165
-        {
166
-            if ( ! \Auth::attempt(['email' => $registeredUser->email, 'password' => '']))
167
-            {
168
-                \ErrorHandler::userAlreadyRegistered();
169
-            }
170
-
171
-            return $this->loginProxy->login(['email' => $registeredUser->email, 'password' => ''], 0);
172
-        }
173
-    }
79
+		return in_array($nameOfPermission, $permissions);
80
+	}
81
+
82
+	/**
83
+	 * Check if the logged in user has the given group.
84
+	 * 
85
+	 * @param  string  $groupName
86
+	 * @param  integer $userId
87
+	 * @return boolean
88
+	 */
89
+	public function hasGroup($groupName, $userId = false)
90
+	{
91
+		$userId = $userId ?: \Auth::id();
92
+		$groups = $this->find($userId)->groups;
93
+		return $groups->pluck('name')->search($groupName, true) === false ? false : true;
94
+	}
95
+
96
+	/**
97
+	 * Assign the given group ids to the given user.
98
+	 * 
99
+	 * @param  integer $user_id    
100
+	 * @param  array   $group_ids
101
+	 * @return object
102
+	 */
103
+	public function assignGroups($user_id, $group_ids)
104
+	{
105
+		\DB::transaction(function () use ($user_id, $group_ids) {
106
+			$user = $this->find($user_id);
107
+			$user->groups()->detach();
108
+			$user->groups()->attach($group_ids);
109
+		});
110
+
111
+		return $this->find($user_id);
112
+	}
113
+
114
+	/**
115
+	 * Handle a login request to the application.
116
+	 * 
117
+	 * @param  array   $credentials    
118
+	 * @param  boolean $adminLogin
119
+	 * @return object
120
+	 */
121
+	public function login($credentials, $adminLogin = false)
122
+	{
123
+		if ( ! $user = $this->first(['email' => $credentials['email']])) 
124
+		{
125
+			\ErrorHandler::loginFailed();
126
+		}
127
+		else if ($adminLogin && $user->groups->pluck('name')->search('Admin', true) === false) 
128
+		{
129
+			\ErrorHandler::loginFailed();
130
+		}
131
+		else if ( ! $adminLogin && $user->groups->pluck('name')->search('Admin', true) !== false) 
132
+		{
133
+			\ErrorHandler::loginFailed();
134
+		}
135
+		else if ($user->blocked)
136
+		{
137
+			\ErrorHandler::userIsBlocked();
138
+		}
139
+
140
+		return $user;
141
+	}
142
+
143
+	/**
144
+	 * Handle a social login request of the none admin to the application.
145
+	 * 
146
+	 * @param  array   $credentials
147
+	 * @return array
148
+	 */
149
+	public function loginSocial($credentials)
150
+	{
151
+		$access_token = $credentials['auth_code'] ? \Socialite::driver($credentials['type'])->getAccessToken($credentials['auth_code']) : $credentials['access_token'];
152
+		$user         = \Socialite::driver($credentials['type'])->userFromToken($access_token);
153
+
154
+		if ( ! $user->email)
155
+		{
156
+			\ErrorHandler::noSocialEmail();
157
+		}
158
+
159
+		if ( ! $registeredUser = $this->model->where('email', $user->email)->first()) 
160
+		{
161
+			$data = ['email' => $user->email, 'password' => ''];
162
+			return $this->register($data);
163
+		}
164
+		else
165
+		{
166
+			if ( ! \Auth::attempt(['email' => $registeredUser->email, 'password' => '']))
167
+			{
168
+				\ErrorHandler::userAlreadyRegistered();
169
+			}
170
+
171
+			return $this->loginProxy->login(['email' => $registeredUser->email, 'password' => ''], 0);
172
+		}
173
+	}
174 174
     
175
-    /**
176
-     * Handle a registration request.
177
-     * 
178
-     * @param  array $credentials
179
-     * @return array
180
-     */
181
-    public function register($credentials)
182
-    {
183
-        $this->model->create($credentials);
184
-        return $this->loginProxy->login($credentials, 0);
185
-    }
186
-
187
-    /**
188
-     * Block the user.
189
-     *
190
-     * @param  integer $user_id
191
-     * @return object
192
-     */
193
-    public function block($user_id)
194
-    {
195
-        if ( ! $user = $this->find($user_id)) 
196
-        {
197
-            \ErrorHandler::notFound('user');
198
-        }
199
-        if ( ! $this->hasGroup('Admin'))
200
-        {
201
-            \ErrorHandler::noPermissions();
202
-        }
203
-        else if (\Auth::id() == $user_id)
204
-        {
205
-            \ErrorHandler::noPermissions();
206
-        }
207
-        else if ($user->groups->pluck('name')->search('Admin', true) !== false) 
208
-        {
209
-            \ErrorHandler::noPermissions();
210
-        }
211
-
212
-        $user->blocked = 1;
213
-        $user->save();
175
+	/**
176
+	 * Handle a registration request.
177
+	 * 
178
+	 * @param  array $credentials
179
+	 * @return array
180
+	 */
181
+	public function register($credentials)
182
+	{
183
+		$this->model->create($credentials);
184
+		return $this->loginProxy->login($credentials, 0);
185
+	}
186
+
187
+	/**
188
+	 * Block the user.
189
+	 *
190
+	 * @param  integer $user_id
191
+	 * @return object
192
+	 */
193
+	public function block($user_id)
194
+	{
195
+		if ( ! $user = $this->find($user_id)) 
196
+		{
197
+			\ErrorHandler::notFound('user');
198
+		}
199
+		if ( ! $this->hasGroup('Admin'))
200
+		{
201
+			\ErrorHandler::noPermissions();
202
+		}
203
+		else if (\Auth::id() == $user_id)
204
+		{
205
+			\ErrorHandler::noPermissions();
206
+		}
207
+		else if ($user->groups->pluck('name')->search('Admin', true) !== false) 
208
+		{
209
+			\ErrorHandler::noPermissions();
210
+		}
211
+
212
+		$user->blocked = 1;
213
+		$user->save();
214 214
         
215
-        return $user;
216
-    }
217
-
218
-    /**
219
-     * Unblock the user.
220
-     *
221
-     * @param  integer $user_id
222
-     * @return object
223
-     */
224
-    public function unblock($user_id)
225
-    {
226
-        if ( ! $this->hasGroup('Admin'))
227
-        {
228
-            \ErrorHandler::noPermissions();
229
-        }
230
-
231
-        $user          = $this->find($user_id);
232
-        $user->blocked = 0;
233
-        $user->save();
234
-
235
-        return $user;
236
-    }
237
-
238
-    /**
239
-     * Send a reset link to the given user.
240
-     *
241
-     * @param  string  $email
242
-     * @return void
243
-     */
244
-    public function sendReset($email)
245
-    {
246
-        if ( ! $user = $this->model->where('email', $email)->first())
247
-        {
248
-            \ErrorHandler::notFound('email');
249
-        }
250
-
251
-        $url   = $this->config['resetLink'];
252
-        $token = \Password::getRepository()->create($user);
215
+		return $user;
216
+	}
217
+
218
+	/**
219
+	 * Unblock the user.
220
+	 *
221
+	 * @param  integer $user_id
222
+	 * @return object
223
+	 */
224
+	public function unblock($user_id)
225
+	{
226
+		if ( ! $this->hasGroup('Admin'))
227
+		{
228
+			\ErrorHandler::noPermissions();
229
+		}
230
+
231
+		$user          = $this->find($user_id);
232
+		$user->blocked = 0;
233
+		$user->save();
234
+
235
+		return $user;
236
+	}
237
+
238
+	/**
239
+	 * Send a reset link to the given user.
240
+	 *
241
+	 * @param  string  $email
242
+	 * @return void
243
+	 */
244
+	public function sendReset($email)
245
+	{
246
+		if ( ! $user = $this->model->where('email', $email)->first())
247
+		{
248
+			\ErrorHandler::notFound('email');
249
+		}
250
+
251
+		$url   = $this->config['resetLink'];
252
+		$token = \Password::getRepository()->create($user);
253 253
         
254
-        \Mail::send('acl::resetpassword', ['user' => $user, 'url' => $url, 'token' => $token], function ($m) use ($user) {
255
-            $m->to($user->email, $user->name)->subject('Your Password Reset Link');
256
-        });
257
-    }
258
-
259
-    /**
260
-     * Reset the given user's password.
261
-     *
262
-     * @param  array  $credentials
263
-     * @return array
264
-     */
265
-    public function resetPassword($credentials)
266
-    {
267
-        $response = \Password::reset($credentials, function ($user, $password) {
268
-            $user->password = $password;
269
-            $user->save();
270
-        });
271
-
272
-        switch ($response) {
273
-            case \Password::PASSWORD_RESET:
274
-                return 'success';
254
+		\Mail::send('acl::resetpassword', ['user' => $user, 'url' => $url, 'token' => $token], function ($m) use ($user) {
255
+			$m->to($user->email, $user->name)->subject('Your Password Reset Link');
256
+		});
257
+	}
258
+
259
+	/**
260
+	 * Reset the given user's password.
261
+	 *
262
+	 * @param  array  $credentials
263
+	 * @return array
264
+	 */
265
+	public function resetPassword($credentials)
266
+	{
267
+		$response = \Password::reset($credentials, function ($user, $password) {
268
+			$user->password = $password;
269
+			$user->save();
270
+		});
271
+
272
+		switch ($response) {
273
+			case \Password::PASSWORD_RESET:
274
+				return 'success';
275 275
                 
276
-            case \Password::INVALID_TOKEN:
277
-                \ErrorHandler::invalidResetToken('token');
278
-
279
-            case \Password::INVALID_PASSWORD:
280
-                \ErrorHandler::invalidResetPassword('email');
281
-
282
-            case \Password::INVALID_USER:
283
-                \ErrorHandler::notFound('user');
284
-
285
-            default:
286
-                \ErrorHandler::generalError();
287
-        }
288
-    }
289
-
290
-    /**
291
-     * Change the logged in user password.
292
-     *
293
-     * @param  array  $credentials
294
-     * @return void
295
-     */
296
-    public function changePassword($credentials)
297
-    {
298
-        $user = \Auth::user();
299
-        if ( ! \Hash::check($credentials['old_password'], $user->password)) 
300
-        {
301
-            \ErrorHandler::invalidOldPassword();
302
-        }
303
-
304
-        $user->password = $credentials['password'];
305
-        $user->save();
306
-    }
307
-
308
-    /**
309
-     * Paginate all users in the given group based on the given conditions.
310
-     * 
311
-     * @param  string  $groupName
312
-     * @param  array   $relations
313
-     * @param  integer $perPage
314
-     * @param  string  $sortBy
315
-     * @param  boolean $desc
316
-     * @return \Illuminate\Http\Response
317
-     */
318
-    public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc)
319
-    {   
320
-        unset($conditions['page']);
321
-        $conditions = $this->constructConditions($conditions, $this->model);
322
-        $sort       = $desc ? 'desc' : 'asc';
323
-        $model      = call_user_func_array("{$this->getModel()}::with", array($relations));
324
-
325
-        $model->whereHas('groups', function($q) use ($groupName){
326
-            $q->where('name', $groupName);
327
-        });
276
+			case \Password::INVALID_TOKEN:
277
+				\ErrorHandler::invalidResetToken('token');
278
+
279
+			case \Password::INVALID_PASSWORD:
280
+				\ErrorHandler::invalidResetPassword('email');
281
+
282
+			case \Password::INVALID_USER:
283
+				\ErrorHandler::notFound('user');
284
+
285
+			default:
286
+				\ErrorHandler::generalError();
287
+		}
288
+	}
289
+
290
+	/**
291
+	 * Change the logged in user password.
292
+	 *
293
+	 * @param  array  $credentials
294
+	 * @return void
295
+	 */
296
+	public function changePassword($credentials)
297
+	{
298
+		$user = \Auth::user();
299
+		if ( ! \Hash::check($credentials['old_password'], $user->password)) 
300
+		{
301
+			\ErrorHandler::invalidOldPassword();
302
+		}
303
+
304
+		$user->password = $credentials['password'];
305
+		$user->save();
306
+	}
307
+
308
+	/**
309
+	 * Paginate all users in the given group based on the given conditions.
310
+	 * 
311
+	 * @param  string  $groupName
312
+	 * @param  array   $relations
313
+	 * @param  integer $perPage
314
+	 * @param  string  $sortBy
315
+	 * @param  boolean $desc
316
+	 * @return \Illuminate\Http\Response
317
+	 */
318
+	public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc)
319
+	{   
320
+		unset($conditions['page']);
321
+		$conditions = $this->constructConditions($conditions, $this->model);
322
+		$sort       = $desc ? 'desc' : 'asc';
323
+		$model      = call_user_func_array("{$this->getModel()}::with", array($relations));
324
+
325
+		$model->whereHas('groups', function($q) use ($groupName){
326
+			$q->where('name', $groupName);
327
+		});
328 328
 
329 329
         
330
-        if (count($conditions['conditionValues']))
331
-        {
332
-            $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']);
333
-        }
334
-
335
-        if ($perPage) 
336
-        {
337
-            return $model->orderBy($sortBy, $sort)->paginate($perPage);
338
-        }
339
-
340
-        return $model->orderBy($sortBy, $sort)->get();
341
-    }
342
-
343
-    /**
344
-     * Save the given data to the logged in user.
345
-     *
346
-     * @param  array $credentials
347
-     * @return object
348
-     */
349
-    public function saveProfile($credentials) 
350
-    {
351
-        $user = \Auth::user();
352
-        $user->save($credentials);
353
-
354
-        return $user;
355
-    }
356
-
357
-    /**
358
-     * Ensure access token hasn't expired or revoked.
359
-     * 
360
-     * @param  string $accessToken
361
-     * @return boolean
362
-     */
363
-    public function accessTokenExpiredOrRevoked($accessToken)
364
-    {
365
-        $data = new ValidationData();
366
-        $data->setCurrentTime(time());
367
-
368
-        if ($accessToken->validate($data) === false || $this->accessTokenRepository->isAccessTokenRevoked($accessToken->getClaim('jti'))) 
369
-        {
370
-            return true;
371
-        }
372
-
373
-        return false;
374
-    }
330
+		if (count($conditions['conditionValues']))
331
+		{
332
+			$model->whereRaw($conditions['conditionString'], $conditions['conditionValues']);
333
+		}
334
+
335
+		if ($perPage) 
336
+		{
337
+			return $model->orderBy($sortBy, $sort)->paginate($perPage);
338
+		}
339
+
340
+		return $model->orderBy($sortBy, $sort)->get();
341
+	}
342
+
343
+	/**
344
+	 * Save the given data to the logged in user.
345
+	 *
346
+	 * @param  array $credentials
347
+	 * @return object
348
+	 */
349
+	public function saveProfile($credentials) 
350
+	{
351
+		$user = \Auth::user();
352
+		$user->save($credentials);
353
+
354
+		return $user;
355
+	}
356
+
357
+	/**
358
+	 * Ensure access token hasn't expired or revoked.
359
+	 * 
360
+	 * @param  string $accessToken
361
+	 * @return boolean
362
+	 */
363
+	public function accessTokenExpiredOrRevoked($accessToken)
364
+	{
365
+		$data = new ValidationData();
366
+		$data->setCurrentTime(time());
367
+
368
+		if ($accessToken->validate($data) === false || $this->accessTokenRepository->isAccessTokenRevoked($accessToken->getClaim('jti'))) 
369
+		{
370
+			return true;
371
+		}
372
+
373
+		return false;
374
+	}
375 375
 }
Please login to merge, or discard this patch.
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -49,7 +49,7 @@  discard block
 block discarded – undo
49 49
         $user        = \Core::users()->find(\Auth::id(), $relations);
50 50
         foreach ($user->groups()->get() as $group)
51 51
         {
52
-            $group->permissions->each(function ($permission) use (&$permissions){
52
+            $group->permissions->each(function($permission) use (&$permissions){
53 53
                 $permissions[$permission->model][$permission->id] = $permission->name;
54 54
             });
55 55
         }
@@ -72,7 +72,7 @@  discard block
 block discarded – undo
72 72
         $user        = $user ?: $this->find(\Auth::id(), ['groups.permissions']);
73 73
         $permissions = [];
74 74
 
75
-        $user->groups->pluck('permissions')->each(function ($permission) use (&$permissions, $model){
75
+        $user->groups->pluck('permissions')->each(function($permission) use (&$permissions, $model){
76 76
             $permissions = array_merge($permissions, $permission->where('model', $model)->pluck('name')->toArray()); 
77 77
         });
78 78
         
@@ -102,7 +102,7 @@  discard block
 block discarded – undo
102 102
      */
103 103
     public function assignGroups($user_id, $group_ids)
104 104
     {
105
-        \DB::transaction(function () use ($user_id, $group_ids) {
105
+        \DB::transaction(function() use ($user_id, $group_ids) {
106 106
             $user = $this->find($user_id);
107 107
             $user->groups()->detach();
108 108
             $user->groups()->attach($group_ids);
@@ -251,7 +251,7 @@  discard block
 block discarded – undo
251 251
         $url   = $this->config['resetLink'];
252 252
         $token = \Password::getRepository()->create($user);
253 253
         
254
-        \Mail::send('acl::resetpassword', ['user' => $user, 'url' => $url, 'token' => $token], function ($m) use ($user) {
254
+        \Mail::send('acl::resetpassword', ['user' => $user, 'url' => $url, 'token' => $token], function($m) use ($user) {
255 255
             $m->to($user->email, $user->name)->subject('Your Password Reset Link');
256 256
         });
257 257
     }
@@ -264,7 +264,7 @@  discard block
 block discarded – undo
264 264
      */
265 265
     public function resetPassword($credentials)
266 266
     {
267
-        $response = \Password::reset($credentials, function ($user, $password) {
267
+        $response = \Password::reset($credentials, function($user, $password) {
268 268
             $user->password = $password;
269 269
             $user->save();
270 270
         });
Please login to merge, or discard this patch.
src/Modules/V1/Acl/Http/Controllers/UsersController.php 1 patch
Indentation   +231 added lines, -231 removed lines patch added patch discarded remove patch
@@ -8,264 +8,264 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.