@@ -6,27 +6,27 @@ |
||
| 6 | 6 | |
| 7 | 7 | class StoreUser extends FormRequest |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Determine if the user is authorized to make this request. |
|
| 11 | - * |
|
| 12 | - * @return bool |
|
| 13 | - */ |
|
| 14 | - public function authorize() |
|
| 15 | - { |
|
| 16 | - return true; |
|
| 17 | - } |
|
| 9 | + /** |
|
| 10 | + * Determine if the user is authorized to make this request. |
|
| 11 | + * |
|
| 12 | + * @return bool |
|
| 13 | + */ |
|
| 14 | + public function authorize() |
|
| 15 | + { |
|
| 16 | + return true; |
|
| 17 | + } |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Get the validation rules that apply to the request. |
|
| 21 | - * |
|
| 22 | - * @return array |
|
| 23 | - */ |
|
| 24 | - public function rules() |
|
| 25 | - { |
|
| 26 | - return [ |
|
| 27 | - 'name' => 'nullable|string', |
|
| 28 | - 'email' => 'required|email|unique:users,email,'.$this->get('id'), |
|
| 29 | - 'password' => 'nullable|min:6' |
|
| 30 | - ]; |
|
| 31 | - } |
|
| 19 | + /** |
|
| 20 | + * Get the validation rules that apply to the request. |
|
| 21 | + * |
|
| 22 | + * @return array |
|
| 23 | + */ |
|
| 24 | + public function rules() |
|
| 25 | + { |
|
| 26 | + return [ |
|
| 27 | + 'name' => 'nullable|string', |
|
| 28 | + 'email' => 'required|email|unique:users,email,'.$this->get('id'), |
|
| 29 | + 'password' => 'nullable|min:6' |
|
| 30 | + ]; |
|
| 31 | + } |
|
| 32 | 32 | } |
@@ -15,29 +15,29 @@ |
||
| 15 | 15 | |
| 16 | 16 | Route::group(['prefix' => 'users'], function () { |
| 17 | 17 | |
| 18 | - Route::get('/', 'UserController@index'); |
|
| 19 | - Route::get('{id}', 'UserController@show'); |
|
| 20 | - Route::post('/', 'UserController@store'); |
|
| 21 | - Route::patch('{id}', 'UserController@update'); |
|
| 22 | - Route::delete('{id}', 'UserController@destroy'); |
|
| 23 | - Route::patch('{id}/restore', 'UserController@restore'); |
|
| 24 | - Route::patch('{id}/block', 'UserController@block'); |
|
| 25 | - Route::patch('{id}/unblock', 'UserController@unblock'); |
|
| 26 | - Route::patch('{id}/assign/roles', 'UserController@assignRoles'); |
|
| 18 | + Route::get('/', 'UserController@index'); |
|
| 19 | + Route::get('{id}', 'UserController@show'); |
|
| 20 | + Route::post('/', 'UserController@store'); |
|
| 21 | + Route::patch('{id}', 'UserController@update'); |
|
| 22 | + Route::delete('{id}', 'UserController@destroy'); |
|
| 23 | + Route::patch('{id}/restore', 'UserController@restore'); |
|
| 24 | + Route::patch('{id}/block', 'UserController@block'); |
|
| 25 | + Route::patch('{id}/unblock', 'UserController@unblock'); |
|
| 26 | + Route::patch('{id}/assign/roles', 'UserController@assignRoles'); |
|
| 27 | 27 | |
| 28 | - Route::group(['prefix' => 'account'], function () { |
|
| 28 | + Route::group(['prefix' => 'account'], function () { |
|
| 29 | 29 | |
| 30 | - Route::get('my', 'UserController@account'); |
|
| 31 | - Route::get('logout', 'UserController@logout'); |
|
| 32 | - Route::post('refresh/token', 'UserController@refreshToken'); |
|
| 33 | - Route::post('save', 'UserController@saveProfile'); |
|
| 34 | - Route::post('register', 'UserController@register'); |
|
| 35 | - Route::post('login', 'UserController@login'); |
|
| 36 | - Route::post('login/social', 'UserController@loginSocial'); |
|
| 37 | - Route::post('send/reset', 'UserController@sendReset'); |
|
| 38 | - Route::post('reset/password', 'UserController@resetPassword'); |
|
| 39 | - Route::post('change/password', 'UserController@changePassword'); |
|
| 40 | - Route::post('confirm/email', 'UserController@confirmEmail'); |
|
| 41 | - Route::post('resend/email/confirmation', 'UserController@resendEmailConfirmation'); |
|
| 42 | - }); |
|
| 30 | + Route::get('my', 'UserController@account'); |
|
| 31 | + Route::get('logout', 'UserController@logout'); |
|
| 32 | + Route::post('refresh/token', 'UserController@refreshToken'); |
|
| 33 | + Route::post('save', 'UserController@saveProfile'); |
|
| 34 | + Route::post('register', 'UserController@register'); |
|
| 35 | + Route::post('login', 'UserController@login'); |
|
| 36 | + Route::post('login/social', 'UserController@loginSocial'); |
|
| 37 | + Route::post('send/reset', 'UserController@sendReset'); |
|
| 38 | + Route::post('reset/password', 'UserController@resetPassword'); |
|
| 39 | + Route::post('change/password', 'UserController@changePassword'); |
|
| 40 | + Route::post('confirm/email', 'UserController@confirmEmail'); |
|
| 41 | + Route::post('resend/email/confirmation', 'UserController@resendEmailConfirmation'); |
|
| 42 | + }); |
|
| 43 | 43 | }); |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | | |
| 14 | 14 | */ |
| 15 | 15 | |
| 16 | -Route::group(['prefix' => 'users'], function () { |
|
| 16 | +Route::group(['prefix' => 'users'], function() { |
|
| 17 | 17 | |
| 18 | 18 | Route::get('/', 'UserController@index'); |
| 19 | 19 | Route::get('{id}', 'UserController@show'); |
@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | Route::patch('{id}/unblock', 'UserController@unblock'); |
| 26 | 26 | Route::patch('{id}/assign/roles', 'UserController@assignRoles'); |
| 27 | 27 | |
| 28 | - Route::group(['prefix' => 'account'], function () { |
|
| 28 | + Route::group(['prefix' => 'account'], function() { |
|
| 29 | 29 | |
| 30 | 30 | Route::get('my', 'UserController@account'); |
| 31 | 31 | Route::get('logout', 'UserController@logout'); |
@@ -12,372 +12,372 @@ |
||
| 12 | 12 | |
| 13 | 13 | class UserService extends BaseService |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * @var PermissionService |
|
| 17 | - */ |
|
| 18 | - protected $permissionService; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * @var LoginProxy |
|
| 22 | - */ |
|
| 23 | - protected $loginProxy; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * @var NotificationService |
|
| 27 | - */ |
|
| 28 | - protected $notificationService; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @var OauthClientService |
|
| 32 | - */ |
|
| 33 | - protected $oauthClientService; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Init new object. |
|
| 37 | - * |
|
| 38 | - * @param UserRepository $repo |
|
| 39 | - * @param PermissionService $permissionService |
|
| 40 | - * @param LoginProxy $loginProxy |
|
| 41 | - * @param NotificationService $notificationService |
|
| 42 | - * @param OauthClientService $oauthClientService |
|
| 43 | - * @return void |
|
| 44 | - */ |
|
| 45 | - public function __construct( |
|
| 46 | - UserRepository $repo, |
|
| 47 | - PermissionService $permissionService, |
|
| 48 | - LoginProxy $loginProxy, |
|
| 49 | - NotificationService $notificationService, |
|
| 50 | - OauthClientService $oauthClientService |
|
| 51 | - ) { |
|
| 52 | - $this->permissionService = $permissionService; |
|
| 53 | - $this->loginProxy = $loginProxy; |
|
| 54 | - $this->notificationService = $notificationService; |
|
| 55 | - $this->oauthClientService = $oauthClientService; |
|
| 56 | - parent::__construct($repo); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Return the logged in user account. |
|
| 61 | - * |
|
| 62 | - * @param array $relations |
|
| 63 | - * @return boolean |
|
| 64 | - */ |
|
| 65 | - public function account($relations = ['roles.permissions']) |
|
| 66 | - { |
|
| 67 | - $permissions = []; |
|
| 68 | - $user = $this->repo->find(\Auth::id(), $relations); |
|
| 69 | - foreach ($user->roles as $role) { |
|
| 70 | - $role->permissions->each(function ($permission) use (&$permissions) { |
|
| 71 | - $permissions[$permission->repo][$permission->id] = $permission->name; |
|
| 72 | - }); |
|
| 73 | - } |
|
| 74 | - $user->permissions = $permissions; |
|
| 75 | - |
|
| 76 | - return $user; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Check if the logged in user or the given user |
|
| 81 | - * has the given permissions on the given model. |
|
| 82 | - * |
|
| 83 | - * @param string $permissionName |
|
| 84 | - * @param string $model |
|
| 85 | - * @param mixed $userId |
|
| 86 | - * @return boolean |
|
| 87 | - */ |
|
| 88 | - public function can($permissionName, $model, $userId = false) |
|
| 89 | - { |
|
| 90 | - $permission = $this->permissionService->first([ |
|
| 91 | - 'and' => [ |
|
| 92 | - 'model' => $model, |
|
| 93 | - 'name' => $permissionName, |
|
| 94 | - 'roles' => [ |
|
| 95 | - 'op' => 'has', |
|
| 96 | - 'val' => [ |
|
| 97 | - 'users' => [ |
|
| 98 | - 'op' => 'has', |
|
| 99 | - 'val' => [ |
|
| 100 | - 'users.id' => $userId ?: \Auth::id() |
|
| 101 | - ] |
|
| 102 | - ] |
|
| 103 | - ] |
|
| 104 | - ] |
|
| 105 | - ] |
|
| 106 | - ]); |
|
| 107 | - |
|
| 108 | - return $permission ? true : false; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Check if the logged in or the given user has the given role. |
|
| 113 | - * |
|
| 114 | - * @param string[] $roles |
|
| 115 | - * @param mixed $user |
|
| 116 | - * @return boolean |
|
| 117 | - */ |
|
| 118 | - public function hasRoles($roles, $user = false) |
|
| 119 | - { |
|
| 120 | - return $this->repo->countRoles($user ?: \Auth::id(), $roles) ? true : false; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Assign the given role ids to the given user. |
|
| 125 | - * |
|
| 126 | - * @param integer $userId |
|
| 127 | - * @param array $roleIds |
|
| 128 | - * @return object |
|
| 129 | - */ |
|
| 130 | - public function assignRoles($userId, $roleIds) |
|
| 131 | - { |
|
| 132 | - $user = false; |
|
| 133 | - \DB::transaction(function () use ($userId, $roleIds, &$user) { |
|
| 134 | - $user = $this->repo->find($userId); |
|
| 135 | - $this->repo->detachPermissions($userId); |
|
| 136 | - $this->repo->attachPermissions($userId, $roleIds); |
|
| 137 | - }); |
|
| 138 | - |
|
| 139 | - return $user; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Handle the login request to the application. |
|
| 144 | - * |
|
| 145 | - * @param string $email |
|
| 146 | - * @param string $password |
|
| 147 | - * @return object |
|
| 148 | - */ |
|
| 149 | - public function login($email, $password) |
|
| 150 | - { |
|
| 151 | - if (! $user = $this->repo->first(['email' => $email])) { |
|
| 152 | - \Errors::loginFailed(); |
|
| 153 | - } elseif ($user->blocked) { |
|
| 154 | - \Errors::userIsBlocked(); |
|
| 155 | - } elseif (! config('skeleton.disable_confirm_email') && ! $user->confirmed) { |
|
| 156 | - \Errors::emailNotConfirmed(); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - return ['user' => $user, 'tokens' => $this->loginProxy->login($user->email, $password)]; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Handle the social login request to the application. |
|
| 164 | - * |
|
| 165 | - * @param string $authCode |
|
| 166 | - * @param string $accessToken |
|
| 167 | - * @return array |
|
| 168 | - */ |
|
| 169 | - public function loginSocial($authCode, $accessToken, $type) |
|
| 170 | - { |
|
| 171 | - $access_token = $authCode ? Arr::get(\Socialite::driver($type)->getAccessTokenResponse($authCode), 'access_token') : $accessToken; |
|
| 172 | - $user = \Socialite::driver($type)->userFromToken($access_token); |
|
| 173 | - |
|
| 174 | - if (! $user->email) { |
|
| 175 | - \Errors::noSocialEmail(); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - if (! $this->repo->first(['email' => $user->email])) { |
|
| 179 | - $this->register($user->email, '', true); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - return $this->loginProxy->login($user->email, config('skeleton.social_pass')); |
|
| 183 | - } |
|
| 15 | + /** |
|
| 16 | + * @var PermissionService |
|
| 17 | + */ |
|
| 18 | + protected $permissionService; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * @var LoginProxy |
|
| 22 | + */ |
|
| 23 | + protected $loginProxy; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * @var NotificationService |
|
| 27 | + */ |
|
| 28 | + protected $notificationService; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @var OauthClientService |
|
| 32 | + */ |
|
| 33 | + protected $oauthClientService; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Init new object. |
|
| 37 | + * |
|
| 38 | + * @param UserRepository $repo |
|
| 39 | + * @param PermissionService $permissionService |
|
| 40 | + * @param LoginProxy $loginProxy |
|
| 41 | + * @param NotificationService $notificationService |
|
| 42 | + * @param OauthClientService $oauthClientService |
|
| 43 | + * @return void |
|
| 44 | + */ |
|
| 45 | + public function __construct( |
|
| 46 | + UserRepository $repo, |
|
| 47 | + PermissionService $permissionService, |
|
| 48 | + LoginProxy $loginProxy, |
|
| 49 | + NotificationService $notificationService, |
|
| 50 | + OauthClientService $oauthClientService |
|
| 51 | + ) { |
|
| 52 | + $this->permissionService = $permissionService; |
|
| 53 | + $this->loginProxy = $loginProxy; |
|
| 54 | + $this->notificationService = $notificationService; |
|
| 55 | + $this->oauthClientService = $oauthClientService; |
|
| 56 | + parent::__construct($repo); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Return the logged in user account. |
|
| 61 | + * |
|
| 62 | + * @param array $relations |
|
| 63 | + * @return boolean |
|
| 64 | + */ |
|
| 65 | + public function account($relations = ['roles.permissions']) |
|
| 66 | + { |
|
| 67 | + $permissions = []; |
|
| 68 | + $user = $this->repo->find(\Auth::id(), $relations); |
|
| 69 | + foreach ($user->roles as $role) { |
|
| 70 | + $role->permissions->each(function ($permission) use (&$permissions) { |
|
| 71 | + $permissions[$permission->repo][$permission->id] = $permission->name; |
|
| 72 | + }); |
|
| 73 | + } |
|
| 74 | + $user->permissions = $permissions; |
|
| 75 | + |
|
| 76 | + return $user; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Check if the logged in user or the given user |
|
| 81 | + * has the given permissions on the given model. |
|
| 82 | + * |
|
| 83 | + * @param string $permissionName |
|
| 84 | + * @param string $model |
|
| 85 | + * @param mixed $userId |
|
| 86 | + * @return boolean |
|
| 87 | + */ |
|
| 88 | + public function can($permissionName, $model, $userId = false) |
|
| 89 | + { |
|
| 90 | + $permission = $this->permissionService->first([ |
|
| 91 | + 'and' => [ |
|
| 92 | + 'model' => $model, |
|
| 93 | + 'name' => $permissionName, |
|
| 94 | + 'roles' => [ |
|
| 95 | + 'op' => 'has', |
|
| 96 | + 'val' => [ |
|
| 97 | + 'users' => [ |
|
| 98 | + 'op' => 'has', |
|
| 99 | + 'val' => [ |
|
| 100 | + 'users.id' => $userId ?: \Auth::id() |
|
| 101 | + ] |
|
| 102 | + ] |
|
| 103 | + ] |
|
| 104 | + ] |
|
| 105 | + ] |
|
| 106 | + ]); |
|
| 107 | + |
|
| 108 | + return $permission ? true : false; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Check if the logged in or the given user has the given role. |
|
| 113 | + * |
|
| 114 | + * @param string[] $roles |
|
| 115 | + * @param mixed $user |
|
| 116 | + * @return boolean |
|
| 117 | + */ |
|
| 118 | + public function hasRoles($roles, $user = false) |
|
| 119 | + { |
|
| 120 | + return $this->repo->countRoles($user ?: \Auth::id(), $roles) ? true : false; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Assign the given role ids to the given user. |
|
| 125 | + * |
|
| 126 | + * @param integer $userId |
|
| 127 | + * @param array $roleIds |
|
| 128 | + * @return object |
|
| 129 | + */ |
|
| 130 | + public function assignRoles($userId, $roleIds) |
|
| 131 | + { |
|
| 132 | + $user = false; |
|
| 133 | + \DB::transaction(function () use ($userId, $roleIds, &$user) { |
|
| 134 | + $user = $this->repo->find($userId); |
|
| 135 | + $this->repo->detachPermissions($userId); |
|
| 136 | + $this->repo->attachPermissions($userId, $roleIds); |
|
| 137 | + }); |
|
| 138 | + |
|
| 139 | + return $user; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Handle the login request to the application. |
|
| 144 | + * |
|
| 145 | + * @param string $email |
|
| 146 | + * @param string $password |
|
| 147 | + * @return object |
|
| 148 | + */ |
|
| 149 | + public function login($email, $password) |
|
| 150 | + { |
|
| 151 | + if (! $user = $this->repo->first(['email' => $email])) { |
|
| 152 | + \Errors::loginFailed(); |
|
| 153 | + } elseif ($user->blocked) { |
|
| 154 | + \Errors::userIsBlocked(); |
|
| 155 | + } elseif (! config('skeleton.disable_confirm_email') && ! $user->confirmed) { |
|
| 156 | + \Errors::emailNotConfirmed(); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + return ['user' => $user, 'tokens' => $this->loginProxy->login($user->email, $password)]; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Handle the social login request to the application. |
|
| 164 | + * |
|
| 165 | + * @param string $authCode |
|
| 166 | + * @param string $accessToken |
|
| 167 | + * @return array |
|
| 168 | + */ |
|
| 169 | + public function loginSocial($authCode, $accessToken, $type) |
|
| 170 | + { |
|
| 171 | + $access_token = $authCode ? Arr::get(\Socialite::driver($type)->getAccessTokenResponse($authCode), 'access_token') : $accessToken; |
|
| 172 | + $user = \Socialite::driver($type)->userFromToken($access_token); |
|
| 173 | + |
|
| 174 | + if (! $user->email) { |
|
| 175 | + \Errors::noSocialEmail(); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + if (! $this->repo->first(['email' => $user->email])) { |
|
| 179 | + $this->register($user->email, '', true); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + return $this->loginProxy->login($user->email, config('skeleton.social_pass')); |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | - /** |
|
| 186 | - * Handle the registration request. |
|
| 187 | - * |
|
| 188 | - * @param string $name |
|
| 189 | - * @param string $email |
|
| 190 | - * @param string $password |
|
| 191 | - * @param boolean $skipConfirmEmail |
|
| 192 | - * @return array |
|
| 193 | - */ |
|
| 194 | - public function register($name, $email, $password, $skipConfirmEmail = false) |
|
| 195 | - { |
|
| 196 | - $user = $this->repo->save([ |
|
| 197 | - 'name' => $name, |
|
| 198 | - 'email' => $email, |
|
| 199 | - 'password' => $password, |
|
| 200 | - 'confirmed' => $skipConfirmEmail |
|
| 201 | - ]); |
|
| 202 | - |
|
| 203 | - if (! $skipConfirmEmail && ! config('skeleton.disable_confirm_email')) { |
|
| 204 | - $this->sendConfirmationEmail($user->email); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - return $user; |
|
| 208 | - } |
|
| 185 | + /** |
|
| 186 | + * Handle the registration request. |
|
| 187 | + * |
|
| 188 | + * @param string $name |
|
| 189 | + * @param string $email |
|
| 190 | + * @param string $password |
|
| 191 | + * @param boolean $skipConfirmEmail |
|
| 192 | + * @return array |
|
| 193 | + */ |
|
| 194 | + public function register($name, $email, $password, $skipConfirmEmail = false) |
|
| 195 | + { |
|
| 196 | + $user = $this->repo->save([ |
|
| 197 | + 'name' => $name, |
|
| 198 | + 'email' => $email, |
|
| 199 | + 'password' => $password, |
|
| 200 | + 'confirmed' => $skipConfirmEmail |
|
| 201 | + ]); |
|
| 202 | + |
|
| 203 | + if (! $skipConfirmEmail && ! config('skeleton.disable_confirm_email')) { |
|
| 204 | + $this->sendConfirmationEmail($user->email); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + return $user; |
|
| 208 | + } |
|
| 209 | 209 | |
| 210 | - /** |
|
| 211 | - * Block the user. |
|
| 212 | - * |
|
| 213 | - * @param integer $userId |
|
| 214 | - * @return object |
|
| 215 | - */ |
|
| 216 | - public function block($userId) |
|
| 217 | - { |
|
| 218 | - if (\Auth::id() == $userId) { |
|
| 219 | - \Errors::noPermissions(); |
|
| 220 | - } |
|
| 210 | + /** |
|
| 211 | + * Block the user. |
|
| 212 | + * |
|
| 213 | + * @param integer $userId |
|
| 214 | + * @return object |
|
| 215 | + */ |
|
| 216 | + public function block($userId) |
|
| 217 | + { |
|
| 218 | + if (\Auth::id() == $userId) { |
|
| 219 | + \Errors::noPermissions(); |
|
| 220 | + } |
|
| 221 | 221 | |
| 222 | - return $this->repo->save(['id' => $userId, 'blocked' => 1]); |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Unblock the user. |
|
| 227 | - * |
|
| 228 | - * @param integer $userId |
|
| 229 | - * @return object |
|
| 230 | - */ |
|
| 231 | - public function unblock($userId) |
|
| 232 | - { |
|
| 233 | - return $this->repo->save(['id' => $userId, 'blocked' => 0]); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * Send a reset link to the given user. |
|
| 238 | - * |
|
| 239 | - * @param string $email |
|
| 240 | - * @return void |
|
| 241 | - */ |
|
| 242 | - public function sendReset($email) |
|
| 243 | - { |
|
| 244 | - if (! $user = $this->repo->first(['email' => $email])) { |
|
| 245 | - \Errors::notFound('email'); |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - $token = \Password::getService()->create($user); |
|
| 249 | - $this->notificationService->notify($user, 'ResetPassword', $token); |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * Reset the given user's password. |
|
| 254 | - * |
|
| 255 | - * @param string $email |
|
| 256 | - * @param string $password |
|
| 257 | - * @param string $passwordConfirmation |
|
| 258 | - * @param string $token |
|
| 259 | - * @return string|void |
|
| 260 | - */ |
|
| 261 | - public function resetPassword($email, $password, $passwordConfirmation, $token) |
|
| 262 | - { |
|
| 263 | - $response = \Password::reset([ |
|
| 264 | - 'email' => $email, |
|
| 265 | - 'password' => $password, |
|
| 266 | - 'password_confirmation' => $passwordConfirmation, |
|
| 267 | - 'token' => $token |
|
| 268 | - ], function ($user, $password) { |
|
| 269 | - $this->repo->save(['id' => $user->id, 'password' => $password]); |
|
| 270 | - }); |
|
| 271 | - |
|
| 272 | - switch ($response) { |
|
| 273 | - case \Password::PASSWORD_RESET: |
|
| 274 | - return 'success'; |
|
| 275 | - break; |
|
| 276 | - |
|
| 277 | - case \Password::INVALID_TOKEN: |
|
| 278 | - \Errors::invalidResetToken(); |
|
| 279 | - break; |
|
| 280 | - |
|
| 281 | - case \Password::INVALID_PASSWORD: |
|
| 282 | - \Errors::invalidResetPassword(); |
|
| 283 | - break; |
|
| 284 | - |
|
| 285 | - case \Password::INVALID_USER: |
|
| 286 | - \Errors::notFound('user'); |
|
| 287 | - break; |
|
| 288 | - } |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * Change the logged in user password. |
|
| 293 | - * |
|
| 294 | - * @param string $password |
|
| 295 | - * @param string $oldPassword |
|
| 296 | - * @return void |
|
| 297 | - */ |
|
| 298 | - public function changePassword($password, $oldPassword) |
|
| 299 | - { |
|
| 300 | - $user = \Auth::user(); |
|
| 301 | - if (! \Hash::check($oldPassword, $user->password)) { |
|
| 302 | - \Errors::invalidOldPassword(); |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - $this->repo->save(['id' => $user->id, 'password' => $password]); |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * Confirm email using the confirmation code. |
|
| 310 | - * |
|
| 311 | - * @param string $confirmationCode |
|
| 312 | - * @return void |
|
| 313 | - */ |
|
| 314 | - public function confirmEmail($confirmationCode) |
|
| 315 | - { |
|
| 316 | - if (! $user = $this->repo->first(['confirmation_code' => $confirmationCode])) { |
|
| 317 | - \Errors::invalidConfirmationCode(); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - $this->repo->save(['id' => $user->id, 'confirmed' => 1, 'confirmation_code' => null]); |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * Send the confirmation mail. |
|
| 325 | - * |
|
| 326 | - * @param string $email |
|
| 327 | - * @return void |
|
| 328 | - */ |
|
| 329 | - public function sendConfirmationEmail($email) |
|
| 330 | - { |
|
| 331 | - $user = $this->repo->first(['email' => $email]); |
|
| 332 | - if ($user->confirmed) { |
|
| 333 | - \Errors::emailAlreadyConfirmed(); |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - $this->repo->save(['id' => $user->id, 'confirmation_code' => sha1(microtime())]); |
|
| 337 | - $this->notificationService->notify($user, 'ConfirmEmail'); |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * Save the given data to the logged in user. |
|
| 342 | - * |
|
| 343 | - * @param string $name |
|
| 344 | - * @param string $email |
|
| 345 | - * @param string $profilePicture |
|
| 346 | - * @return void |
|
| 347 | - */ |
|
| 348 | - public function saveProfile($name, $email, $profilePicture = false) |
|
| 349 | - { |
|
| 350 | - if ($profilePicture) { |
|
| 351 | - $data['profile_picture'] = \Media::uploadImageBas64($profilePicture, 'users/profile_pictures'); |
|
| 352 | - } |
|
| 222 | + return $this->repo->save(['id' => $userId, 'blocked' => 1]); |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Unblock the user. |
|
| 227 | + * |
|
| 228 | + * @param integer $userId |
|
| 229 | + * @return object |
|
| 230 | + */ |
|
| 231 | + public function unblock($userId) |
|
| 232 | + { |
|
| 233 | + return $this->repo->save(['id' => $userId, 'blocked' => 0]); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * Send a reset link to the given user. |
|
| 238 | + * |
|
| 239 | + * @param string $email |
|
| 240 | + * @return void |
|
| 241 | + */ |
|
| 242 | + public function sendReset($email) |
|
| 243 | + { |
|
| 244 | + if (! $user = $this->repo->first(['email' => $email])) { |
|
| 245 | + \Errors::notFound('email'); |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + $token = \Password::getService()->create($user); |
|
| 249 | + $this->notificationService->notify($user, 'ResetPassword', $token); |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * Reset the given user's password. |
|
| 254 | + * |
|
| 255 | + * @param string $email |
|
| 256 | + * @param string $password |
|
| 257 | + * @param string $passwordConfirmation |
|
| 258 | + * @param string $token |
|
| 259 | + * @return string|void |
|
| 260 | + */ |
|
| 261 | + public function resetPassword($email, $password, $passwordConfirmation, $token) |
|
| 262 | + { |
|
| 263 | + $response = \Password::reset([ |
|
| 264 | + 'email' => $email, |
|
| 265 | + 'password' => $password, |
|
| 266 | + 'password_confirmation' => $passwordConfirmation, |
|
| 267 | + 'token' => $token |
|
| 268 | + ], function ($user, $password) { |
|
| 269 | + $this->repo->save(['id' => $user->id, 'password' => $password]); |
|
| 270 | + }); |
|
| 271 | + |
|
| 272 | + switch ($response) { |
|
| 273 | + case \Password::PASSWORD_RESET: |
|
| 274 | + return 'success'; |
|
| 275 | + break; |
|
| 276 | + |
|
| 277 | + case \Password::INVALID_TOKEN: |
|
| 278 | + \Errors::invalidResetToken(); |
|
| 279 | + break; |
|
| 280 | + |
|
| 281 | + case \Password::INVALID_PASSWORD: |
|
| 282 | + \Errors::invalidResetPassword(); |
|
| 283 | + break; |
|
| 284 | + |
|
| 285 | + case \Password::INVALID_USER: |
|
| 286 | + \Errors::notFound('user'); |
|
| 287 | + break; |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * Change the logged in user password. |
|
| 293 | + * |
|
| 294 | + * @param string $password |
|
| 295 | + * @param string $oldPassword |
|
| 296 | + * @return void |
|
| 297 | + */ |
|
| 298 | + public function changePassword($password, $oldPassword) |
|
| 299 | + { |
|
| 300 | + $user = \Auth::user(); |
|
| 301 | + if (! \Hash::check($oldPassword, $user->password)) { |
|
| 302 | + \Errors::invalidOldPassword(); |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + $this->repo->save(['id' => $user->id, 'password' => $password]); |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * Confirm email using the confirmation code. |
|
| 310 | + * |
|
| 311 | + * @param string $confirmationCode |
|
| 312 | + * @return void |
|
| 313 | + */ |
|
| 314 | + public function confirmEmail($confirmationCode) |
|
| 315 | + { |
|
| 316 | + if (! $user = $this->repo->first(['confirmation_code' => $confirmationCode])) { |
|
| 317 | + \Errors::invalidConfirmationCode(); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + $this->repo->save(['id' => $user->id, 'confirmed' => 1, 'confirmation_code' => null]); |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * Send the confirmation mail. |
|
| 325 | + * |
|
| 326 | + * @param string $email |
|
| 327 | + * @return void |
|
| 328 | + */ |
|
| 329 | + public function sendConfirmationEmail($email) |
|
| 330 | + { |
|
| 331 | + $user = $this->repo->first(['email' => $email]); |
|
| 332 | + if ($user->confirmed) { |
|
| 333 | + \Errors::emailAlreadyConfirmed(); |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + $this->repo->save(['id' => $user->id, 'confirmation_code' => sha1(microtime())]); |
|
| 337 | + $this->notificationService->notify($user, 'ConfirmEmail'); |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * Save the given data to the logged in user. |
|
| 342 | + * |
|
| 343 | + * @param string $name |
|
| 344 | + * @param string $email |
|
| 345 | + * @param string $profilePicture |
|
| 346 | + * @return void |
|
| 347 | + */ |
|
| 348 | + public function saveProfile($name, $email, $profilePicture = false) |
|
| 349 | + { |
|
| 350 | + if ($profilePicture) { |
|
| 351 | + $data['profile_picture'] = \Media::uploadImageBas64($profilePicture, 'users/profile_pictures'); |
|
| 352 | + } |
|
| 353 | 353 | |
| 354 | - $data['id'] = \Auth::id(); |
|
| 355 | - return $this->repo->save([ |
|
| 356 | - 'id' => \Auth::id(), |
|
| 357 | - 'name' => $name, |
|
| 358 | - 'email' => $email, |
|
| 359 | - 'profilePicture' => $profilePicture, |
|
| 360 | - ]); |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - /** |
|
| 364 | - * Logs out the user, revoke access token and refresh token. |
|
| 365 | - * |
|
| 366 | - * @return void |
|
| 367 | - */ |
|
| 368 | - public function logout() |
|
| 369 | - { |
|
| 370 | - $this->oauthClientService->revokeAccessToken(\Auth::user()->token()); |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - /** |
|
| 374 | - * Attempt to refresh the access token using the given refresh token. |
|
| 375 | - * |
|
| 376 | - * @param string $refreshToken |
|
| 377 | - * @return array |
|
| 378 | - */ |
|
| 379 | - public function refreshToken($refreshToken) |
|
| 380 | - { |
|
| 381 | - return $this->loginProxy->refreshToken($refreshToken); |
|
| 382 | - } |
|
| 354 | + $data['id'] = \Auth::id(); |
|
| 355 | + return $this->repo->save([ |
|
| 356 | + 'id' => \Auth::id(), |
|
| 357 | + 'name' => $name, |
|
| 358 | + 'email' => $email, |
|
| 359 | + 'profilePicture' => $profilePicture, |
|
| 360 | + ]); |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + /** |
|
| 364 | + * Logs out the user, revoke access token and refresh token. |
|
| 365 | + * |
|
| 366 | + * @return void |
|
| 367 | + */ |
|
| 368 | + public function logout() |
|
| 369 | + { |
|
| 370 | + $this->oauthClientService->revokeAccessToken(\Auth::user()->token()); |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | + * Attempt to refresh the access token using the given refresh token. |
|
| 375 | + * |
|
| 376 | + * @param string $refreshToken |
|
| 377 | + * @return array |
|
| 378 | + */ |
|
| 379 | + public function refreshToken($refreshToken) |
|
| 380 | + { |
|
| 381 | + return $this->loginProxy->refreshToken($refreshToken); |
|
| 382 | + } |
|
| 383 | 383 | } |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | $permissions = []; |
| 68 | 68 | $user = $this->repo->find(\Auth::id(), $relations); |
| 69 | 69 | foreach ($user->roles as $role) { |
| 70 | - $role->permissions->each(function ($permission) use (&$permissions) { |
|
| 70 | + $role->permissions->each(function($permission) use (&$permissions) { |
|
| 71 | 71 | $permissions[$permission->repo][$permission->id] = $permission->name; |
| 72 | 72 | }); |
| 73 | 73 | } |
@@ -130,7 +130,7 @@ discard block |
||
| 130 | 130 | public function assignRoles($userId, $roleIds) |
| 131 | 131 | { |
| 132 | 132 | $user = false; |
| 133 | - \DB::transaction(function () use ($userId, $roleIds, &$user) { |
|
| 133 | + \DB::transaction(function() use ($userId, $roleIds, &$user) { |
|
| 134 | 134 | $user = $this->repo->find($userId); |
| 135 | 135 | $this->repo->detachPermissions($userId); |
| 136 | 136 | $this->repo->attachPermissions($userId, $roleIds); |
@@ -148,11 +148,11 @@ discard block |
||
| 148 | 148 | */ |
| 149 | 149 | public function login($email, $password) |
| 150 | 150 | { |
| 151 | - if (! $user = $this->repo->first(['email' => $email])) { |
|
| 151 | + if ( ! $user = $this->repo->first(['email' => $email])) { |
|
| 152 | 152 | \Errors::loginFailed(); |
| 153 | 153 | } elseif ($user->blocked) { |
| 154 | 154 | \Errors::userIsBlocked(); |
| 155 | - } elseif (! config('skeleton.disable_confirm_email') && ! $user->confirmed) { |
|
| 155 | + } elseif ( ! config('skeleton.disable_confirm_email') && ! $user->confirmed) { |
|
| 156 | 156 | \Errors::emailNotConfirmed(); |
| 157 | 157 | } |
| 158 | 158 | |
@@ -171,11 +171,11 @@ discard block |
||
| 171 | 171 | $access_token = $authCode ? Arr::get(\Socialite::driver($type)->getAccessTokenResponse($authCode), 'access_token') : $accessToken; |
| 172 | 172 | $user = \Socialite::driver($type)->userFromToken($access_token); |
| 173 | 173 | |
| 174 | - if (! $user->email) { |
|
| 174 | + if ( ! $user->email) { |
|
| 175 | 175 | \Errors::noSocialEmail(); |
| 176 | 176 | } |
| 177 | 177 | |
| 178 | - if (! $this->repo->first(['email' => $user->email])) { |
|
| 178 | + if ( ! $this->repo->first(['email' => $user->email])) { |
|
| 179 | 179 | $this->register($user->email, '', true); |
| 180 | 180 | } |
| 181 | 181 | |
@@ -200,7 +200,7 @@ discard block |
||
| 200 | 200 | 'confirmed' => $skipConfirmEmail |
| 201 | 201 | ]); |
| 202 | 202 | |
| 203 | - if (! $skipConfirmEmail && ! config('skeleton.disable_confirm_email')) { |
|
| 203 | + if ( ! $skipConfirmEmail && ! config('skeleton.disable_confirm_email')) { |
|
| 204 | 204 | $this->sendConfirmationEmail($user->email); |
| 205 | 205 | } |
| 206 | 206 | |
@@ -241,7 +241,7 @@ discard block |
||
| 241 | 241 | */ |
| 242 | 242 | public function sendReset($email) |
| 243 | 243 | { |
| 244 | - if (! $user = $this->repo->first(['email' => $email])) { |
|
| 244 | + if ( ! $user = $this->repo->first(['email' => $email])) { |
|
| 245 | 245 | \Errors::notFound('email'); |
| 246 | 246 | } |
| 247 | 247 | |
@@ -265,7 +265,7 @@ discard block |
||
| 265 | 265 | 'password' => $password, |
| 266 | 266 | 'password_confirmation' => $passwordConfirmation, |
| 267 | 267 | 'token' => $token |
| 268 | - ], function ($user, $password) { |
|
| 268 | + ], function($user, $password) { |
|
| 269 | 269 | $this->repo->save(['id' => $user->id, 'password' => $password]); |
| 270 | 270 | }); |
| 271 | 271 | |
@@ -298,7 +298,7 @@ discard block |
||
| 298 | 298 | public function changePassword($password, $oldPassword) |
| 299 | 299 | { |
| 300 | 300 | $user = \Auth::user(); |
| 301 | - if (! \Hash::check($oldPassword, $user->password)) { |
|
| 301 | + if ( ! \Hash::check($oldPassword, $user->password)) { |
|
| 302 | 302 | \Errors::invalidOldPassword(); |
| 303 | 303 | } |
| 304 | 304 | |
@@ -313,7 +313,7 @@ discard block |
||
| 313 | 313 | */ |
| 314 | 314 | public function confirmEmail($confirmationCode) |
| 315 | 315 | { |
| 316 | - if (! $user = $this->repo->first(['confirmation_code' => $confirmationCode])) { |
|
| 316 | + if ( ! $user = $this->repo->first(['confirmation_code' => $confirmationCode])) { |
|
| 317 | 317 | \Errors::invalidConfirmationCode(); |
| 318 | 318 | } |
| 319 | 319 | |