@@ -2,7 +2,6 @@ |
||
2 | 2 | |
3 | 3 | use App\User; |
4 | 4 | use Illuminate\Database\Eloquent\SoftDeletes; |
5 | -use Illuminate\Notifications\Notifiable; |
|
6 | 5 | use Laravel\Passport\HasApiTokens; |
7 | 6 | |
8 | 7 | class AclUser extends User { |
@@ -7,135 +7,135 @@ |
||
7 | 7 | |
8 | 8 | class AclUser extends User { |
9 | 9 | |
10 | - use SoftDeletes, HasApiTokens; |
|
11 | - protected $table = 'users'; |
|
12 | - protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
13 | - protected $hidden = ['password', 'remember_token','deleted_at']; |
|
14 | - protected $guarded = ['id']; |
|
15 | - protected $fillable = ['profile_picture', 'name', 'email', 'password']; |
|
16 | - public $searchable = ['name', 'email']; |
|
10 | + use SoftDeletes, HasApiTokens; |
|
11 | + protected $table = 'users'; |
|
12 | + protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
13 | + protected $hidden = ['password', 'remember_token','deleted_at']; |
|
14 | + protected $guarded = ['id']; |
|
15 | + protected $fillable = ['profile_picture', 'name', 'email', 'password']; |
|
16 | + public $searchable = ['name', 'email']; |
|
17 | 17 | |
18 | - public function getCreatedAtAttribute($value) |
|
19 | - { |
|
20 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
21 | - } |
|
22 | - |
|
23 | - public function getUpdatedAtAttribute($value) |
|
24 | - { |
|
25 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
26 | - } |
|
27 | - |
|
28 | - public function getDeletedAtAttribute($value) |
|
29 | - { |
|
30 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
31 | - } |
|
32 | - |
|
33 | - /** |
|
34 | - * Encrypt the password attribute before |
|
35 | - * saving it in the storage. |
|
36 | - * |
|
37 | - * @param string $value |
|
38 | - */ |
|
39 | - public function setPasswordAttribute($value) |
|
40 | - { |
|
41 | - $this->attributes['password'] = bcrypt($value); |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * Get the entity's notifications. |
|
46 | - */ |
|
47 | - public function notifications() |
|
48 | - { |
|
49 | - return $this->morphMany('\App\Modules\Notifications\Notification', 'notifiable')->orderBy('created_at', 'desc'); |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * Get the entity's read notifications. |
|
54 | - */ |
|
55 | - public function readNotifications() |
|
56 | - { |
|
57 | - return $this->notifications()->whereNotNull('read_at'); |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Get the entity's unread notifications. |
|
62 | - */ |
|
63 | - public function unreadNotifications() |
|
64 | - { |
|
65 | - return $this->notifications()->whereNull('read_at'); |
|
66 | - } |
|
67 | - |
|
68 | - public function groups() |
|
69 | - { |
|
70 | - return $this->belongsToMany('\App\Modules\Acl\AclGroup','users_groups','user_id','group_id')->whereNull('users_groups.deleted_at')->withTimestamps(); |
|
71 | - } |
|
72 | - |
|
73 | - public function oauthClients() |
|
74 | - { |
|
75 | - return $this->hasMany('App\Modules\Acl\OauthClient', 'user_id'); |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * Return fcm device tokens that will be used in sending fcm notifications. |
|
80 | - * |
|
81 | - * @return array |
|
82 | - */ |
|
83 | - public function routeNotificationForFCM() |
|
84 | - { |
|
85 | - $devices = \Core::pushNotificationsDevices()->findBy(['user_id' => $this->id]); |
|
86 | - $tokens = []; |
|
87 | - |
|
88 | - foreach ($devices as $device) |
|
89 | - { |
|
90 | - $accessToken = decrypt($device->access_token); |
|
91 | - |
|
92 | - try |
|
93 | - { |
|
94 | - if (\Core::users()->accessTokenExpiredOrRevoked($accessToken)) |
|
95 | - { |
|
96 | - continue; |
|
97 | - } |
|
98 | - |
|
99 | - $tokens[] = $device->device_token; |
|
100 | - } |
|
101 | - catch (\Exception $e) |
|
102 | - { |
|
103 | - $device->forceDelete(); |
|
104 | - } |
|
105 | - } |
|
106 | - |
|
107 | - return $tokens; |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * The channels the user receives notification broadcasts on. |
|
112 | - * |
|
113 | - * @return string |
|
114 | - */ |
|
115 | - public function receivesBroadcastNotificationsOn() |
|
116 | - { |
|
117 | - return 'users.' . $this->id; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Custom password validation. |
|
122 | - * |
|
123 | - * @param string $password |
|
124 | - * @return boolean |
|
125 | - */ |
|
126 | - public function validateForPassportPasswordGrant($password) |
|
127 | - { |
|
128 | - if ($password == config('skeleton.social_pass')) |
|
129 | - { |
|
130 | - return true; |
|
131 | - } |
|
132 | - |
|
133 | - return \Hash::check($password, $this->password); |
|
134 | - } |
|
18 | + public function getCreatedAtAttribute($value) |
|
19 | + { |
|
20 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
21 | + } |
|
22 | + |
|
23 | + public function getUpdatedAtAttribute($value) |
|
24 | + { |
|
25 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
26 | + } |
|
27 | + |
|
28 | + public function getDeletedAtAttribute($value) |
|
29 | + { |
|
30 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
31 | + } |
|
32 | + |
|
33 | + /** |
|
34 | + * Encrypt the password attribute before |
|
35 | + * saving it in the storage. |
|
36 | + * |
|
37 | + * @param string $value |
|
38 | + */ |
|
39 | + public function setPasswordAttribute($value) |
|
40 | + { |
|
41 | + $this->attributes['password'] = bcrypt($value); |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * Get the entity's notifications. |
|
46 | + */ |
|
47 | + public function notifications() |
|
48 | + { |
|
49 | + return $this->morphMany('\App\Modules\Notifications\Notification', 'notifiable')->orderBy('created_at', 'desc'); |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * Get the entity's read notifications. |
|
54 | + */ |
|
55 | + public function readNotifications() |
|
56 | + { |
|
57 | + return $this->notifications()->whereNotNull('read_at'); |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Get the entity's unread notifications. |
|
62 | + */ |
|
63 | + public function unreadNotifications() |
|
64 | + { |
|
65 | + return $this->notifications()->whereNull('read_at'); |
|
66 | + } |
|
67 | + |
|
68 | + public function groups() |
|
69 | + { |
|
70 | + return $this->belongsToMany('\App\Modules\Acl\AclGroup','users_groups','user_id','group_id')->whereNull('users_groups.deleted_at')->withTimestamps(); |
|
71 | + } |
|
72 | + |
|
73 | + public function oauthClients() |
|
74 | + { |
|
75 | + return $this->hasMany('App\Modules\Acl\OauthClient', 'user_id'); |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * Return fcm device tokens that will be used in sending fcm notifications. |
|
80 | + * |
|
81 | + * @return array |
|
82 | + */ |
|
83 | + public function routeNotificationForFCM() |
|
84 | + { |
|
85 | + $devices = \Core::pushNotificationsDevices()->findBy(['user_id' => $this->id]); |
|
86 | + $tokens = []; |
|
87 | + |
|
88 | + foreach ($devices as $device) |
|
89 | + { |
|
90 | + $accessToken = decrypt($device->access_token); |
|
91 | + |
|
92 | + try |
|
93 | + { |
|
94 | + if (\Core::users()->accessTokenExpiredOrRevoked($accessToken)) |
|
95 | + { |
|
96 | + continue; |
|
97 | + } |
|
98 | + |
|
99 | + $tokens[] = $device->device_token; |
|
100 | + } |
|
101 | + catch (\Exception $e) |
|
102 | + { |
|
103 | + $device->forceDelete(); |
|
104 | + } |
|
105 | + } |
|
106 | + |
|
107 | + return $tokens; |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * The channels the user receives notification broadcasts on. |
|
112 | + * |
|
113 | + * @return string |
|
114 | + */ |
|
115 | + public function receivesBroadcastNotificationsOn() |
|
116 | + { |
|
117 | + return 'users.' . $this->id; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Custom password validation. |
|
122 | + * |
|
123 | + * @param string $password |
|
124 | + * @return boolean |
|
125 | + */ |
|
126 | + public function validateForPassportPasswordGrant($password) |
|
127 | + { |
|
128 | + if ($password == config('skeleton.social_pass')) |
|
129 | + { |
|
130 | + return true; |
|
131 | + } |
|
132 | + |
|
133 | + return \Hash::check($password, $this->password); |
|
134 | + } |
|
135 | 135 | |
136 | - public static function boot() |
|
137 | - { |
|
138 | - parent::boot(); |
|
139 | - parent::observe(\App::make('App\Modules\Acl\ModelObservers\AclUserObserver')); |
|
140 | - } |
|
136 | + public static function boot() |
|
137 | + { |
|
138 | + parent::boot(); |
|
139 | + parent::observe(\App::make('App\Modules\Acl\ModelObservers\AclUserObserver')); |
|
140 | + } |
|
141 | 141 | } |
@@ -61,8 +61,8 @@ discard block |
||
61 | 61 | /** |
62 | 62 | * Check if the logged in user has the given group. |
63 | 63 | * |
64 | - * @param string $groupName |
|
65 | - * @param integer $userId |
|
64 | + * @param integer $user |
|
65 | + * @param string[] $groups |
|
66 | 66 | * @return boolean |
67 | 67 | */ |
68 | 68 | public function hasGroup($groups, $user = false) |
@@ -155,7 +155,7 @@ discard block |
||
155 | 155 | * |
156 | 156 | * @param array $credentials |
157 | 157 | * @param boolean $skipConfirmEmail |
158 | - * @return array |
|
158 | + * @return boolean |
|
159 | 159 | */ |
160 | 160 | public function register($credentials, $skipConfirmEmail = false) |
161 | 161 | { |
@@ -246,7 +246,7 @@ discard block |
||
246 | 246 | * Reset the given user's password. |
247 | 247 | * |
248 | 248 | * @param array $credentials |
249 | - * @return array |
|
249 | + * @return string|null |
|
250 | 250 | */ |
251 | 251 | public function resetPassword($credentials) |
252 | 252 | { |
@@ -363,7 +363,6 @@ discard block |
||
363 | 363 | /** |
364 | 364 | * Save the given data to the logged in user. |
365 | 365 | * |
366 | - * @param array $credentials |
|
367 | 366 | * @return void |
368 | 367 | */ |
369 | 368 | public function saveProfile($data) |
@@ -5,414 +5,414 @@ |
||
5 | 5 | |
6 | 6 | class UserRepository extends AbstractRepository |
7 | 7 | { |
8 | - /** |
|
9 | - * Return the model full namespace. |
|
10 | - * |
|
11 | - * @return string |
|
12 | - */ |
|
13 | - protected function getModel() |
|
14 | - { |
|
15 | - return 'App\Modules\Acl\AclUser'; |
|
16 | - } |
|
17 | - |
|
18 | - |
|
19 | - /** |
|
20 | - * Return the logged in user account. |
|
21 | - * |
|
22 | - * @param array $relations |
|
23 | - * @return boolean |
|
24 | - */ |
|
25 | - public function account($relations = []) |
|
26 | - { |
|
27 | - $permissions = []; |
|
28 | - $user = \Core::users()->find(\Auth::id(), $relations); |
|
29 | - foreach ($user->groups()->get() as $group) |
|
30 | - { |
|
31 | - $group->permissions->each(function ($permission) use (&$permissions){ |
|
32 | - $permissions[$permission->model][$permission->id] = $permission->name; |
|
33 | - }); |
|
34 | - } |
|
35 | - $user->permissions = $permissions; |
|
36 | - |
|
37 | - return $user; |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * Check if the logged in user or the given user |
|
42 | - * has the given permissions on the given model. |
|
43 | - * |
|
44 | - * @param string $nameOfPermission |
|
45 | - * @param string $model |
|
46 | - * @param boolean $user |
|
47 | - * @return boolean |
|
48 | - */ |
|
49 | - public function can($nameOfPermission, $model, $user = false) |
|
50 | - { |
|
51 | - $user = $user ?: $this->find(\Auth::id(), ['groups.permissions']); |
|
52 | - $permissions = []; |
|
53 | - |
|
54 | - $user->groups->pluck('permissions')->each(function ($permission) use (&$permissions, $model){ |
|
55 | - $permissions = array_merge($permissions, $permission->where('model', $model)->pluck('name')->toArray()); |
|
56 | - }); |
|
8 | + /** |
|
9 | + * Return the model full namespace. |
|
10 | + * |
|
11 | + * @return string |
|
12 | + */ |
|
13 | + protected function getModel() |
|
14 | + { |
|
15 | + return 'App\Modules\Acl\AclUser'; |
|
16 | + } |
|
17 | + |
|
18 | + |
|
19 | + /** |
|
20 | + * Return the logged in user account. |
|
21 | + * |
|
22 | + * @param array $relations |
|
23 | + * @return boolean |
|
24 | + */ |
|
25 | + public function account($relations = []) |
|
26 | + { |
|
27 | + $permissions = []; |
|
28 | + $user = \Core::users()->find(\Auth::id(), $relations); |
|
29 | + foreach ($user->groups()->get() as $group) |
|
30 | + { |
|
31 | + $group->permissions->each(function ($permission) use (&$permissions){ |
|
32 | + $permissions[$permission->model][$permission->id] = $permission->name; |
|
33 | + }); |
|
34 | + } |
|
35 | + $user->permissions = $permissions; |
|
36 | + |
|
37 | + return $user; |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * Check if the logged in user or the given user |
|
42 | + * has the given permissions on the given model. |
|
43 | + * |
|
44 | + * @param string $nameOfPermission |
|
45 | + * @param string $model |
|
46 | + * @param boolean $user |
|
47 | + * @return boolean |
|
48 | + */ |
|
49 | + public function can($nameOfPermission, $model, $user = false) |
|
50 | + { |
|
51 | + $user = $user ?: $this->find(\Auth::id(), ['groups.permissions']); |
|
52 | + $permissions = []; |
|
53 | + |
|
54 | + $user->groups->pluck('permissions')->each(function ($permission) use (&$permissions, $model){ |
|
55 | + $permissions = array_merge($permissions, $permission->where('model', $model)->pluck('name')->toArray()); |
|
56 | + }); |
|
57 | 57 | |
58 | - return in_array($nameOfPermission, $permissions); |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Check if the logged in user has the given group. |
|
63 | - * |
|
64 | - * @param string $groupName |
|
65 | - * @param integer $userId |
|
66 | - * @return boolean |
|
67 | - */ |
|
68 | - public function hasGroup($groups, $user = false) |
|
69 | - { |
|
70 | - $user = $user ?: $this->find(\Auth::id()); |
|
71 | - return $user->groups->whereIn('name', $groups)->count() ? true : false; |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Assign the given group ids to the given user. |
|
76 | - * |
|
77 | - * @param integer $userId |
|
78 | - * @param array $group_ids |
|
79 | - * @return object |
|
80 | - */ |
|
81 | - public function assignGroups($userId, $group_ids) |
|
82 | - { |
|
83 | - \DB::transaction(function () use ($userId, $group_ids) { |
|
84 | - $user = $this->find($userId); |
|
85 | - $user->groups()->detach(); |
|
86 | - $user->groups()->attach($group_ids); |
|
87 | - }); |
|
88 | - |
|
89 | - return $this->find($userId); |
|
90 | - } |
|
91 | - |
|
92 | - |
|
93 | - /** |
|
94 | - * Handle a login request to the application. |
|
95 | - * |
|
96 | - * @param array $credentials |
|
97 | - * @param boolean $adminLogin |
|
98 | - * @return object |
|
99 | - */ |
|
100 | - public function login($credentials, $adminLogin = false) |
|
101 | - { |
|
102 | - if ( ! $user = $this->first(['email' => $credentials['email']])) |
|
103 | - { |
|
104 | - \ErrorHandler::loginFailed(); |
|
105 | - } |
|
106 | - else if ($adminLogin && ! $user->groups->whereIn('name', ['Admin'])->count()) |
|
107 | - { |
|
108 | - \ErrorHandler::loginFailed(); |
|
109 | - } |
|
110 | - else if ( ! $adminLogin && $user->groups->whereIn('name', ['Admin'])->count()) |
|
111 | - { |
|
112 | - \ErrorHandler::loginFailed(); |
|
113 | - } |
|
114 | - else if ($user->blocked) |
|
115 | - { |
|
116 | - \ErrorHandler::userIsBlocked(); |
|
117 | - } |
|
118 | - else if ( ! config('skeleton.disable_confirm_email') && ! $user->confirmed) |
|
119 | - { |
|
120 | - \ErrorHandler::emailNotConfirmed(); |
|
121 | - } |
|
122 | - |
|
123 | - return $user; |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Handle a social login request of the none admin to the application. |
|
128 | - * |
|
129 | - * @param string $authCode |
|
130 | - * @param string $accessToken |
|
131 | - * @param string $type |
|
132 | - * @return array |
|
133 | - */ |
|
134 | - public function loginSocial($authCode, $accessToken, $type) |
|
135 | - { |
|
136 | - $access_token = $authCode ? array_get(\Socialite::driver($type)->getAccessTokenResponse($authCode), 'access_token') : $accessToken; |
|
137 | - $user = \Socialite::driver($type)->userFromToken($access_token); |
|
138 | - |
|
139 | - if ( ! $user->email) |
|
140 | - { |
|
141 | - \ErrorHandler::noSocialEmail(); |
|
142 | - } |
|
143 | - |
|
144 | - if ( ! $registeredUser = $this->model->where('email', $user->email)->first()) |
|
145 | - { |
|
146 | - $this->register(['email' => $user->email, 'password' => ''], 1); |
|
147 | - } |
|
148 | - |
|
149 | - $loginProxy = \App::make('App\Modules\Acl\Proxy\LoginProxy'); |
|
150 | - return $loginProxy->login(['email' => $credentials['email'], 'password' => config('skeleton.social_pass')], 0); |
|
151 | - } |
|
58 | + return in_array($nameOfPermission, $permissions); |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Check if the logged in user has the given group. |
|
63 | + * |
|
64 | + * @param string $groupName |
|
65 | + * @param integer $userId |
|
66 | + * @return boolean |
|
67 | + */ |
|
68 | + public function hasGroup($groups, $user = false) |
|
69 | + { |
|
70 | + $user = $user ?: $this->find(\Auth::id()); |
|
71 | + return $user->groups->whereIn('name', $groups)->count() ? true : false; |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Assign the given group ids to the given user. |
|
76 | + * |
|
77 | + * @param integer $userId |
|
78 | + * @param array $group_ids |
|
79 | + * @return object |
|
80 | + */ |
|
81 | + public function assignGroups($userId, $group_ids) |
|
82 | + { |
|
83 | + \DB::transaction(function () use ($userId, $group_ids) { |
|
84 | + $user = $this->find($userId); |
|
85 | + $user->groups()->detach(); |
|
86 | + $user->groups()->attach($group_ids); |
|
87 | + }); |
|
88 | + |
|
89 | + return $this->find($userId); |
|
90 | + } |
|
91 | + |
|
92 | + |
|
93 | + /** |
|
94 | + * Handle a login request to the application. |
|
95 | + * |
|
96 | + * @param array $credentials |
|
97 | + * @param boolean $adminLogin |
|
98 | + * @return object |
|
99 | + */ |
|
100 | + public function login($credentials, $adminLogin = false) |
|
101 | + { |
|
102 | + if ( ! $user = $this->first(['email' => $credentials['email']])) |
|
103 | + { |
|
104 | + \ErrorHandler::loginFailed(); |
|
105 | + } |
|
106 | + else if ($adminLogin && ! $user->groups->whereIn('name', ['Admin'])->count()) |
|
107 | + { |
|
108 | + \ErrorHandler::loginFailed(); |
|
109 | + } |
|
110 | + else if ( ! $adminLogin && $user->groups->whereIn('name', ['Admin'])->count()) |
|
111 | + { |
|
112 | + \ErrorHandler::loginFailed(); |
|
113 | + } |
|
114 | + else if ($user->blocked) |
|
115 | + { |
|
116 | + \ErrorHandler::userIsBlocked(); |
|
117 | + } |
|
118 | + else if ( ! config('skeleton.disable_confirm_email') && ! $user->confirmed) |
|
119 | + { |
|
120 | + \ErrorHandler::emailNotConfirmed(); |
|
121 | + } |
|
122 | + |
|
123 | + return $user; |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Handle a social login request of the none admin to the application. |
|
128 | + * |
|
129 | + * @param string $authCode |
|
130 | + * @param string $accessToken |
|
131 | + * @param string $type |
|
132 | + * @return array |
|
133 | + */ |
|
134 | + public function loginSocial($authCode, $accessToken, $type) |
|
135 | + { |
|
136 | + $access_token = $authCode ? array_get(\Socialite::driver($type)->getAccessTokenResponse($authCode), 'access_token') : $accessToken; |
|
137 | + $user = \Socialite::driver($type)->userFromToken($access_token); |
|
138 | + |
|
139 | + if ( ! $user->email) |
|
140 | + { |
|
141 | + \ErrorHandler::noSocialEmail(); |
|
142 | + } |
|
143 | + |
|
144 | + if ( ! $registeredUser = $this->model->where('email', $user->email)->first()) |
|
145 | + { |
|
146 | + $this->register(['email' => $user->email, 'password' => ''], 1); |
|
147 | + } |
|
148 | + |
|
149 | + $loginProxy = \App::make('App\Modules\Acl\Proxy\LoginProxy'); |
|
150 | + return $loginProxy->login(['email' => $credentials['email'], 'password' => config('skeleton.social_pass')], 0); |
|
151 | + } |
|
152 | 152 | |
153 | - /** |
|
154 | - * Handle a registration request. |
|
155 | - * |
|
156 | - * @param array $credentials |
|
157 | - * @param boolean $skipConfirmEmail |
|
158 | - * @return array |
|
159 | - */ |
|
160 | - public function register($credentials, $skipConfirmEmail = false) |
|
161 | - { |
|
162 | - $user = $this->save($credentials); |
|
163 | - |
|
164 | - if ($skipConfirmEmail) |
|
165 | - { |
|
166 | - $user->confirmed = 1; |
|
167 | - $user->save(); |
|
168 | - } |
|
169 | - else if ( ! config('skeleton.disable_confirm_email')) |
|
170 | - { |
|
171 | - $this->sendConfirmationEmail($user->email); |
|
172 | - } |
|
173 | - |
|
174 | - return $user; |
|
175 | - } |
|
153 | + /** |
|
154 | + * Handle a registration request. |
|
155 | + * |
|
156 | + * @param array $credentials |
|
157 | + * @param boolean $skipConfirmEmail |
|
158 | + * @return array |
|
159 | + */ |
|
160 | + public function register($credentials, $skipConfirmEmail = false) |
|
161 | + { |
|
162 | + $user = $this->save($credentials); |
|
163 | + |
|
164 | + if ($skipConfirmEmail) |
|
165 | + { |
|
166 | + $user->confirmed = 1; |
|
167 | + $user->save(); |
|
168 | + } |
|
169 | + else if ( ! config('skeleton.disable_confirm_email')) |
|
170 | + { |
|
171 | + $this->sendConfirmationEmail($user->email); |
|
172 | + } |
|
173 | + |
|
174 | + return $user; |
|
175 | + } |
|
176 | 176 | |
177 | - /** |
|
178 | - * Block the user. |
|
179 | - * |
|
180 | - * @param integer $userId |
|
181 | - * @return object |
|
182 | - */ |
|
183 | - public function block($userId) |
|
184 | - { |
|
185 | - if ( ! $user = $this->find($userId)) |
|
186 | - { |
|
187 | - \ErrorHandler::notFound('user'); |
|
188 | - } |
|
189 | - if ( ! $this->hasGroup(['Admin'])) |
|
190 | - { |
|
191 | - \ErrorHandler::noPermissions(); |
|
192 | - } |
|
193 | - else if (\Auth::id() == $userId) |
|
194 | - { |
|
195 | - \ErrorHandler::noPermissions(); |
|
196 | - } |
|
197 | - else if ($user->groups->pluck('name')->search('Admin', true) !== false) |
|
198 | - { |
|
199 | - \ErrorHandler::noPermissions(); |
|
200 | - } |
|
201 | - |
|
202 | - $user->blocked = 1; |
|
203 | - $user->save(); |
|
177 | + /** |
|
178 | + * Block the user. |
|
179 | + * |
|
180 | + * @param integer $userId |
|
181 | + * @return object |
|
182 | + */ |
|
183 | + public function block($userId) |
|
184 | + { |
|
185 | + if ( ! $user = $this->find($userId)) |
|
186 | + { |
|
187 | + \ErrorHandler::notFound('user'); |
|
188 | + } |
|
189 | + if ( ! $this->hasGroup(['Admin'])) |
|
190 | + { |
|
191 | + \ErrorHandler::noPermissions(); |
|
192 | + } |
|
193 | + else if (\Auth::id() == $userId) |
|
194 | + { |
|
195 | + \ErrorHandler::noPermissions(); |
|
196 | + } |
|
197 | + else if ($user->groups->pluck('name')->search('Admin', true) !== false) |
|
198 | + { |
|
199 | + \ErrorHandler::noPermissions(); |
|
200 | + } |
|
201 | + |
|
202 | + $user->blocked = 1; |
|
203 | + $user->save(); |
|
204 | 204 | |
205 | - return $user; |
|
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * Unblock the user. |
|
210 | - * |
|
211 | - * @param integer $userId |
|
212 | - * @return object |
|
213 | - */ |
|
214 | - public function unblock($userId) |
|
215 | - { |
|
216 | - if ( ! $this->hasGroup(['Admin'])) |
|
217 | - { |
|
218 | - \ErrorHandler::noPermissions(); |
|
219 | - } |
|
220 | - |
|
221 | - $user = $this->find($userId); |
|
222 | - $user->blocked = 0; |
|
223 | - $user->save(); |
|
224 | - |
|
225 | - return $user; |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * Send a reset link to the given user. |
|
230 | - * |
|
231 | - * @param string $email |
|
232 | - * @return void |
|
233 | - */ |
|
234 | - public function sendReset($email) |
|
235 | - { |
|
236 | - if ( ! $user = $this->model->where('email', $email)->first()) |
|
237 | - { |
|
238 | - \ErrorHandler::notFound('email'); |
|
239 | - } |
|
240 | - |
|
241 | - $token = \Password::getRepository()->create($user); |
|
242 | - \Core::notifications()->notify($user, 'ResetPassword', $token); |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * Reset the given user's password. |
|
247 | - * |
|
248 | - * @param array $credentials |
|
249 | - * @return array |
|
250 | - */ |
|
251 | - public function resetPassword($credentials) |
|
252 | - { |
|
253 | - $response = \Password::reset($credentials, function ($user, $password) { |
|
254 | - $user->password = $password; |
|
255 | - $user->save(); |
|
256 | - }); |
|
257 | - |
|
258 | - switch ($response) { |
|
259 | - case \Password::PASSWORD_RESET: |
|
260 | - return 'success'; |
|
205 | + return $user; |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * Unblock the user. |
|
210 | + * |
|
211 | + * @param integer $userId |
|
212 | + * @return object |
|
213 | + */ |
|
214 | + public function unblock($userId) |
|
215 | + { |
|
216 | + if ( ! $this->hasGroup(['Admin'])) |
|
217 | + { |
|
218 | + \ErrorHandler::noPermissions(); |
|
219 | + } |
|
220 | + |
|
221 | + $user = $this->find($userId); |
|
222 | + $user->blocked = 0; |
|
223 | + $user->save(); |
|
224 | + |
|
225 | + return $user; |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * Send a reset link to the given user. |
|
230 | + * |
|
231 | + * @param string $email |
|
232 | + * @return void |
|
233 | + */ |
|
234 | + public function sendReset($email) |
|
235 | + { |
|
236 | + if ( ! $user = $this->model->where('email', $email)->first()) |
|
237 | + { |
|
238 | + \ErrorHandler::notFound('email'); |
|
239 | + } |
|
240 | + |
|
241 | + $token = \Password::getRepository()->create($user); |
|
242 | + \Core::notifications()->notify($user, 'ResetPassword', $token); |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * Reset the given user's password. |
|
247 | + * |
|
248 | + * @param array $credentials |
|
249 | + * @return array |
|
250 | + */ |
|
251 | + public function resetPassword($credentials) |
|
252 | + { |
|
253 | + $response = \Password::reset($credentials, function ($user, $password) { |
|
254 | + $user->password = $password; |
|
255 | + $user->save(); |
|
256 | + }); |
|
257 | + |
|
258 | + switch ($response) { |
|
259 | + case \Password::PASSWORD_RESET: |
|
260 | + return 'success'; |
|
261 | 261 | |
262 | - case \Password::INVALID_TOKEN: |
|
263 | - \ErrorHandler::invalidResetToken('token'); |
|
264 | - |
|
265 | - case \Password::INVALID_PASSWORD: |
|
266 | - \ErrorHandler::invalidResetPassword('email'); |
|
267 | - |
|
268 | - case \Password::INVALID_USER: |
|
269 | - \ErrorHandler::notFound('user'); |
|
270 | - |
|
271 | - default: |
|
272 | - \ErrorHandler::generalError(); |
|
273 | - } |
|
274 | - } |
|
275 | - |
|
276 | - /** |
|
277 | - * Change the logged in user password. |
|
278 | - * |
|
279 | - * @param array $credentials |
|
280 | - * @return void |
|
281 | - */ |
|
282 | - public function changePassword($credentials) |
|
283 | - { |
|
284 | - $user = \Auth::user(); |
|
285 | - if ( ! \Hash::check($credentials['old_password'], $user->password)) |
|
286 | - { |
|
287 | - \ErrorHandler::invalidOldPassword(); |
|
288 | - } |
|
289 | - |
|
290 | - $user->password = $credentials['password']; |
|
291 | - $user->save(); |
|
292 | - } |
|
293 | - |
|
294 | - /** |
|
295 | - * Confirm email using the confirmation code. |
|
296 | - * |
|
297 | - * @param string $confirmationCode |
|
298 | - * @return void |
|
299 | - */ |
|
300 | - public function confirmEmail($confirmationCode) |
|
301 | - { |
|
302 | - $user = $this->first(['confirmation_code' => $confirmationCode]); |
|
303 | - $user->confirmed = 1; |
|
304 | - $user->confirmation_code = null; |
|
305 | - $user->save(); |
|
306 | - } |
|
307 | - |
|
308 | - /** |
|
309 | - * Send the confirmation mail. |
|
310 | - * |
|
311 | - * @param string $email |
|
312 | - * @return void |
|
313 | - */ |
|
314 | - public function sendConfirmationEmail($email) |
|
315 | - { |
|
316 | - $user = $this->first(['email' => $email]); |
|
317 | - if ($user->confirmed) |
|
318 | - { |
|
319 | - \ErrorHandler::emailAlreadyConfirmed(); |
|
320 | - } |
|
321 | - |
|
322 | - $user->confirmed = 0; |
|
323 | - $user->confirmation_code = sha1(microtime()); |
|
324 | - $user->save(); |
|
325 | - \Core::notifications()->notify($user, 'ConfirmEmail'); |
|
326 | - } |
|
327 | - |
|
328 | - /** |
|
329 | - * Paginate all users in the given group based on the given conditions. |
|
330 | - * |
|
331 | - * @param string $groupName |
|
332 | - * @param array $relations |
|
333 | - * @param integer $perPage |
|
334 | - * @param string $sortBy |
|
335 | - * @param boolean $desc |
|
336 | - * @return \Illuminate\Http\Response |
|
337 | - */ |
|
338 | - public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc) |
|
339 | - { |
|
340 | - unset($conditions['page']); |
|
341 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
342 | - $sort = $desc ? 'desc' : 'asc'; |
|
343 | - $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
344 | - |
|
345 | - $model->whereHas('groups', function($q) use ($groupName){ |
|
346 | - $q->where('name', $groupName); |
|
347 | - }); |
|
262 | + case \Password::INVALID_TOKEN: |
|
263 | + \ErrorHandler::invalidResetToken('token'); |
|
264 | + |
|
265 | + case \Password::INVALID_PASSWORD: |
|
266 | + \ErrorHandler::invalidResetPassword('email'); |
|
267 | + |
|
268 | + case \Password::INVALID_USER: |
|
269 | + \ErrorHandler::notFound('user'); |
|
270 | + |
|
271 | + default: |
|
272 | + \ErrorHandler::generalError(); |
|
273 | + } |
|
274 | + } |
|
275 | + |
|
276 | + /** |
|
277 | + * Change the logged in user password. |
|
278 | + * |
|
279 | + * @param array $credentials |
|
280 | + * @return void |
|
281 | + */ |
|
282 | + public function changePassword($credentials) |
|
283 | + { |
|
284 | + $user = \Auth::user(); |
|
285 | + if ( ! \Hash::check($credentials['old_password'], $user->password)) |
|
286 | + { |
|
287 | + \ErrorHandler::invalidOldPassword(); |
|
288 | + } |
|
289 | + |
|
290 | + $user->password = $credentials['password']; |
|
291 | + $user->save(); |
|
292 | + } |
|
293 | + |
|
294 | + /** |
|
295 | + * Confirm email using the confirmation code. |
|
296 | + * |
|
297 | + * @param string $confirmationCode |
|
298 | + * @return void |
|
299 | + */ |
|
300 | + public function confirmEmail($confirmationCode) |
|
301 | + { |
|
302 | + $user = $this->first(['confirmation_code' => $confirmationCode]); |
|
303 | + $user->confirmed = 1; |
|
304 | + $user->confirmation_code = null; |
|
305 | + $user->save(); |
|
306 | + } |
|
307 | + |
|
308 | + /** |
|
309 | + * Send the confirmation mail. |
|
310 | + * |
|
311 | + * @param string $email |
|
312 | + * @return void |
|
313 | + */ |
|
314 | + public function sendConfirmationEmail($email) |
|
315 | + { |
|
316 | + $user = $this->first(['email' => $email]); |
|
317 | + if ($user->confirmed) |
|
318 | + { |
|
319 | + \ErrorHandler::emailAlreadyConfirmed(); |
|
320 | + } |
|
321 | + |
|
322 | + $user->confirmed = 0; |
|
323 | + $user->confirmation_code = sha1(microtime()); |
|
324 | + $user->save(); |
|
325 | + \Core::notifications()->notify($user, 'ConfirmEmail'); |
|
326 | + } |
|
327 | + |
|
328 | + /** |
|
329 | + * Paginate all users in the given group based on the given conditions. |
|
330 | + * |
|
331 | + * @param string $groupName |
|
332 | + * @param array $relations |
|
333 | + * @param integer $perPage |
|
334 | + * @param string $sortBy |
|
335 | + * @param boolean $desc |
|
336 | + * @return \Illuminate\Http\Response |
|
337 | + */ |
|
338 | + public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc) |
|
339 | + { |
|
340 | + unset($conditions['page']); |
|
341 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
342 | + $sort = $desc ? 'desc' : 'asc'; |
|
343 | + $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
344 | + |
|
345 | + $model->whereHas('groups', function($q) use ($groupName){ |
|
346 | + $q->where('name', $groupName); |
|
347 | + }); |
|
348 | 348 | |
349 | 349 | |
350 | - if (count($conditions['conditionValues'])) |
|
351 | - { |
|
352 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
353 | - } |
|
354 | - |
|
355 | - if ($perPage) |
|
356 | - { |
|
357 | - return $model->orderBy($sortBy, $sort)->paginate($perPage); |
|
358 | - } |
|
359 | - |
|
360 | - return $model->orderBy($sortBy, $sort)->get(); |
|
361 | - } |
|
362 | - |
|
363 | - /** |
|
364 | - * Save the given data to the logged in user. |
|
365 | - * |
|
366 | - * @param array $credentials |
|
367 | - * @return void |
|
368 | - */ |
|
369 | - public function saveProfile($data) |
|
370 | - { |
|
371 | - if (array_key_exists('profile_picture', $data)) |
|
372 | - { |
|
373 | - $data['profile_picture'] = \Media::uploadImageBas64($data['profile_picture'], 'admins/profile_pictures'); |
|
374 | - } |
|
350 | + if (count($conditions['conditionValues'])) |
|
351 | + { |
|
352 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
353 | + } |
|
354 | + |
|
355 | + if ($perPage) |
|
356 | + { |
|
357 | + return $model->orderBy($sortBy, $sort)->paginate($perPage); |
|
358 | + } |
|
359 | + |
|
360 | + return $model->orderBy($sortBy, $sort)->get(); |
|
361 | + } |
|
362 | + |
|
363 | + /** |
|
364 | + * Save the given data to the logged in user. |
|
365 | + * |
|
366 | + * @param array $credentials |
|
367 | + * @return void |
|
368 | + */ |
|
369 | + public function saveProfile($data) |
|
370 | + { |
|
371 | + if (array_key_exists('profile_picture', $data)) |
|
372 | + { |
|
373 | + $data['profile_picture'] = \Media::uploadImageBas64($data['profile_picture'], 'admins/profile_pictures'); |
|
374 | + } |
|
375 | 375 | |
376 | - $data['id'] = \Auth::id(); |
|
377 | - $this->save($data); |
|
378 | - } |
|
379 | - |
|
380 | - /** |
|
381 | - * Ensure access token hasn't expired or revoked. |
|
382 | - * |
|
383 | - * @param string $accessToken |
|
384 | - * @return boolean |
|
385 | - */ |
|
386 | - public function accessTokenExpiredOrRevoked($accessToken) |
|
387 | - { |
|
388 | - |
|
389 | - $accessTokenRepository = \App::make('League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface'); |
|
390 | - $data = new ValidationData(); |
|
391 | - $data->setCurrentTime(time()); |
|
392 | - |
|
393 | - if ($accessToken->validate($data) === false || $accessTokenRepository->isAccessTokenRevoked($accessToken->getClaim('jti'))) |
|
394 | - { |
|
395 | - return true; |
|
396 | - } |
|
397 | - |
|
398 | - return false; |
|
399 | - } |
|
400 | - |
|
401 | - /** |
|
402 | - * Revoke the given access token and all |
|
403 | - * associated refresh tokens. |
|
404 | - * |
|
405 | - * @param string $accessToken |
|
406 | - * @return void |
|
407 | - */ |
|
408 | - public function revokeAccessToken($accessToken) |
|
409 | - { |
|
410 | - \DB::table('oauth_refresh_tokens') |
|
411 | - ->where('access_token_id', $accessToken->id) |
|
412 | - ->update([ |
|
413 | - 'revoked' => true |
|
414 | - ]); |
|
415 | - |
|
416 | - $accessToken->revoke(); |
|
417 | - } |
|
376 | + $data['id'] = \Auth::id(); |
|
377 | + $this->save($data); |
|
378 | + } |
|
379 | + |
|
380 | + /** |
|
381 | + * Ensure access token hasn't expired or revoked. |
|
382 | + * |
|
383 | + * @param string $accessToken |
|
384 | + * @return boolean |
|
385 | + */ |
|
386 | + public function accessTokenExpiredOrRevoked($accessToken) |
|
387 | + { |
|
388 | + |
|
389 | + $accessTokenRepository = \App::make('League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface'); |
|
390 | + $data = new ValidationData(); |
|
391 | + $data->setCurrentTime(time()); |
|
392 | + |
|
393 | + if ($accessToken->validate($data) === false || $accessTokenRepository->isAccessTokenRevoked($accessToken->getClaim('jti'))) |
|
394 | + { |
|
395 | + return true; |
|
396 | + } |
|
397 | + |
|
398 | + return false; |
|
399 | + } |
|
400 | + |
|
401 | + /** |
|
402 | + * Revoke the given access token and all |
|
403 | + * associated refresh tokens. |
|
404 | + * |
|
405 | + * @param string $accessToken |
|
406 | + * @return void |
|
407 | + */ |
|
408 | + public function revokeAccessToken($accessToken) |
|
409 | + { |
|
410 | + \DB::table('oauth_refresh_tokens') |
|
411 | + ->where('access_token_id', $accessToken->id) |
|
412 | + ->update([ |
|
413 | + 'revoked' => true |
|
414 | + ]); |
|
415 | + |
|
416 | + $accessToken->revoke(); |
|
417 | + } |
|
418 | 418 | } |
@@ -8,282 +8,282 @@ |
||
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'; |
|
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', 'saveProfile', 'account']; |
|
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', 'confirmEmail', 'resendEmailConfirmation']; |
|
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 | - ]; |
|
42 | - |
|
43 | - /** |
|
44 | - * The loginProxy implementation. |
|
45 | - * |
|
46 | - * @var \App\Modules\Acl\Proxy\LoginProxy |
|
47 | - */ |
|
48 | - protected $loginProxy; |
|
49 | - |
|
50 | - public function __construct(LoginProxy $loginProxy) |
|
51 | - { |
|
52 | - $this->loginProxy = $loginProxy; |
|
53 | - parent::__construct(); |
|
54 | - } |
|
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 | - } |
|
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 | - } |
|
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 | - } |
|
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 | - } |
|
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 | - ]); |
|
111 | - |
|
112 | - return \Response::json($this->repo->register($request->only('name', 'email', 'password')), 200); |
|
113 | - } |
|
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' => 'nullable|boolean' |
|
127 | - ]); |
|
128 | - |
|
129 | - return \Response::json($this->loginProxy->login($request->only('email', 'password'), $request->get('admin')), 200); |
|
130 | - } |
|
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 | - ]); |
|
145 | - |
|
146 | - return \Response::json($this->repo->loginSocial($request->get('auth_code'), $request->get('access_token'), $request->get('type')), 200); |
|
147 | - } |
|
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 | - ]); |
|
161 | - |
|
162 | - return \Response::json($this->repo->assignGroups($request->get('user_id'), $request->get('group_ids')), 200); |
|
163 | - } |
|
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']); |
|
174 | - |
|
175 | - return \Response::json($this->repo->sendReset($request->get('email')), 200); |
|
176 | - } |
|
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 | - ]); |
|
192 | - |
|
193 | - return \Response::json($this->repo->resetPassword($request->only('email', 'password', 'password_confirmation', 'token')), 200); |
|
194 | - } |
|
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 | - ]); |
|
209 | - |
|
210 | - return \Response::json($this->repo->changePassword($request->only('old_password', 'password', 'password_confirmation')), 200); |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * Confirm email using the confirmation code. |
|
215 | - * |
|
216 | - * @param \Illuminate\Http\Request $request |
|
217 | - * @return \Illuminate\Http\Response |
|
218 | - */ |
|
219 | - public function confirmEmail(Request $request) |
|
220 | - { |
|
221 | - $this->validate($request, [ |
|
222 | - 'confirmation_code' => 'required|string|exists:users,confirmation_code' |
|
223 | - ]); |
|
224 | - |
|
225 | - return \Response::json($this->repo->confirmEmail($request->only('confirmation_code')), 200); |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * Resend the email confirmation mail. |
|
230 | - * |
|
231 | - * @param \Illuminate\Http\Request $request |
|
232 | - * @return \Illuminate\Http\Response |
|
233 | - */ |
|
234 | - public function resendEmailConfirmation(Request $request) |
|
235 | - { |
|
236 | - $this->validate($request, [ |
|
237 | - 'email' => 'required|exists:users,email' |
|
238 | - ]); |
|
239 | - |
|
240 | - return \Response::json($this->repo->sendConfirmationEmail($request->get('email')), 200); |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * Refresh the expired login token. |
|
245 | - * |
|
246 | - * @param \Illuminate\Http\Request $request |
|
247 | - * @return \Illuminate\Http\Response |
|
248 | - */ |
|
249 | - public function refreshtoken(Request $request) |
|
250 | - { |
|
251 | - $this->validate($request, [ |
|
252 | - 'refreshtoken' => 'required', |
|
253 | - ]); |
|
254 | - |
|
255 | - return \Response::json($this->loginProxy->refreshtoken($request->get('refreshtoken')), 200); |
|
256 | - } |
|
257 | - |
|
258 | - /** |
|
259 | - * Paginate all users with in the given group. |
|
260 | - * |
|
261 | - * @param \Illuminate\Http\Request $request |
|
262 | - * @param string $groupName The name of the requested group. |
|
263 | - * @param integer $perPage Number of rows per page default 15. |
|
264 | - * @param string $sortBy The name of the column to sort by. |
|
265 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
266 | - * @return \Illuminate\Http\Response |
|
267 | - */ |
|
268 | - public function group(Request $request, $groupName, $perPage = false, $sortBy = 'created_at', $desc = 1) |
|
269 | - { |
|
270 | - return \Response::json($this->repo->group($request->all(), $groupName, $this->relations, $perPage, $sortBy, $desc), 200); |
|
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * Save the given data to the logged in user. |
|
275 | - * |
|
276 | - * @param \Illuminate\Http\Request $request |
|
277 | - * @return \Illuminate\Http\Response |
|
278 | - */ |
|
279 | - public function saveProfile(Request $request) |
|
280 | - { |
|
281 | - $this->validate($request, [ |
|
282 | - 'profile_picture' => 'nullable|base64image', |
|
283 | - 'name' => 'nullable|string', |
|
284 | - 'email' => 'required|email|unique:users,email,' . \Auth::id() |
|
285 | - ]); |
|
286 | - |
|
287 | - return \Response::json($this->repo->saveProfile($request->only('name', 'email', 'profile_picture')), 200); |
|
288 | - } |
|
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 | + |
|
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', 'saveProfile', 'account']; |
|
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', 'confirmEmail', 'resendEmailConfirmation']; |
|
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 | + ]; |
|
42 | + |
|
43 | + /** |
|
44 | + * The loginProxy implementation. |
|
45 | + * |
|
46 | + * @var \App\Modules\Acl\Proxy\LoginProxy |
|
47 | + */ |
|
48 | + protected $loginProxy; |
|
49 | + |
|
50 | + public function __construct(LoginProxy $loginProxy) |
|
51 | + { |
|
52 | + $this->loginProxy = $loginProxy; |
|
53 | + parent::__construct(); |
|
54 | + } |
|
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 | + } |
|
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 | + } |
|
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 | + } |
|
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 | + } |
|
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 | + ]); |
|
111 | + |
|
112 | + return \Response::json($this->repo->register($request->only('name', 'email', 'password')), 200); |
|
113 | + } |
|
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' => 'nullable|boolean' |
|
127 | + ]); |
|
128 | + |
|
129 | + return \Response::json($this->loginProxy->login($request->only('email', 'password'), $request->get('admin')), 200); |
|
130 | + } |
|
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 | + ]); |
|
145 | + |
|
146 | + return \Response::json($this->repo->loginSocial($request->get('auth_code'), $request->get('access_token'), $request->get('type')), 200); |
|
147 | + } |
|
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 | + ]); |
|
161 | + |
|
162 | + return \Response::json($this->repo->assignGroups($request->get('user_id'), $request->get('group_ids')), 200); |
|
163 | + } |
|
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']); |
|
174 | + |
|
175 | + return \Response::json($this->repo->sendReset($request->get('email')), 200); |
|
176 | + } |
|
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 | + ]); |
|
192 | + |
|
193 | + return \Response::json($this->repo->resetPassword($request->only('email', 'password', 'password_confirmation', 'token')), 200); |
|
194 | + } |
|
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 | + ]); |
|
209 | + |
|
210 | + return \Response::json($this->repo->changePassword($request->only('old_password', 'password', 'password_confirmation')), 200); |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * Confirm email using the confirmation code. |
|
215 | + * |
|
216 | + * @param \Illuminate\Http\Request $request |
|
217 | + * @return \Illuminate\Http\Response |
|
218 | + */ |
|
219 | + public function confirmEmail(Request $request) |
|
220 | + { |
|
221 | + $this->validate($request, [ |
|
222 | + 'confirmation_code' => 'required|string|exists:users,confirmation_code' |
|
223 | + ]); |
|
224 | + |
|
225 | + return \Response::json($this->repo->confirmEmail($request->only('confirmation_code')), 200); |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * Resend the email confirmation mail. |
|
230 | + * |
|
231 | + * @param \Illuminate\Http\Request $request |
|
232 | + * @return \Illuminate\Http\Response |
|
233 | + */ |
|
234 | + public function resendEmailConfirmation(Request $request) |
|
235 | + { |
|
236 | + $this->validate($request, [ |
|
237 | + 'email' => 'required|exists:users,email' |
|
238 | + ]); |
|
239 | + |
|
240 | + return \Response::json($this->repo->sendConfirmationEmail($request->get('email')), 200); |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * Refresh the expired login token. |
|
245 | + * |
|
246 | + * @param \Illuminate\Http\Request $request |
|
247 | + * @return \Illuminate\Http\Response |
|
248 | + */ |
|
249 | + public function refreshtoken(Request $request) |
|
250 | + { |
|
251 | + $this->validate($request, [ |
|
252 | + 'refreshtoken' => 'required', |
|
253 | + ]); |
|
254 | + |
|
255 | + return \Response::json($this->loginProxy->refreshtoken($request->get('refreshtoken')), 200); |
|
256 | + } |
|
257 | + |
|
258 | + /** |
|
259 | + * Paginate all users with in the given group. |
|
260 | + * |
|
261 | + * @param \Illuminate\Http\Request $request |
|
262 | + * @param string $groupName The name of the requested group. |
|
263 | + * @param integer $perPage Number of rows per page default 15. |
|
264 | + * @param string $sortBy The name of the column to sort by. |
|
265 | + * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
266 | + * @return \Illuminate\Http\Response |
|
267 | + */ |
|
268 | + public function group(Request $request, $groupName, $perPage = false, $sortBy = 'created_at', $desc = 1) |
|
269 | + { |
|
270 | + return \Response::json($this->repo->group($request->all(), $groupName, $this->relations, $perPage, $sortBy, $desc), 200); |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * Save the given data to the logged in user. |
|
275 | + * |
|
276 | + * @param \Illuminate\Http\Request $request |
|
277 | + * @return \Illuminate\Http\Response |
|
278 | + */ |
|
279 | + public function saveProfile(Request $request) |
|
280 | + { |
|
281 | + $this->validate($request, [ |
|
282 | + 'profile_picture' => 'nullable|base64image', |
|
283 | + 'name' => 'nullable|string', |
|
284 | + 'email' => 'required|email|unique:users,email,' . \Auth::id() |
|
285 | + ]); |
|
286 | + |
|
287 | + return \Response::json($this->repo->saveProfile($request->only('name', 'email', 'profile_picture')), 200); |
|
288 | + } |
|
289 | 289 | } |