@@ -11,7 +11,7 @@ |
||
| 11 | 11 | | |
| 12 | 12 | */ |
| 13 | 13 | |
| 14 | -Route::group(['prefix' => 'settings'], function () { |
|
| 14 | +Route::group(['prefix' => 'settings'], function() { |
|
| 15 | 15 | |
| 16 | 16 | Route::get('/', 'SettingController@index'); |
| 17 | 17 | Route::get('/{id}', 'SettingController@find'); |
@@ -11,7 +11,7 @@ |
||
| 11 | 11 | | |
| 12 | 12 | */ |
| 13 | 13 | |
| 14 | -Route::group(['prefix' => 'oauth/clients'], function () { |
|
| 14 | +Route::group(['prefix' => 'oauth/clients'], function() { |
|
| 15 | 15 | |
| 16 | 16 | Route::get('/', 'OauthClientController@index'); |
| 17 | 17 | Route::get('/{id}', 'OauthClientController@find'); |
@@ -13,7 +13,7 @@ |
||
| 13 | 13 | | |
| 14 | 14 | */ |
| 15 | 15 | |
| 16 | -Route::group(['prefix' => 'push/notification/devices'], function () { |
|
| 16 | +Route::group(['prefix' => 'push/notification/devices'], function() { |
|
| 17 | 17 | |
| 18 | 18 | Route::get('/', 'PushNotificationDeviceController@index'); |
| 19 | 19 | Route::get('/{id}', 'PushNotificationDeviceController@find'); |
@@ -13,7 +13,7 @@ |
||
| 13 | 13 | | |
| 14 | 14 | */ |
| 15 | 15 | |
| 16 | -Route::group(['prefix' => 'groups'], function () { |
|
| 16 | +Route::group(['prefix' => 'groups'], function() { |
|
| 17 | 17 | |
| 18 | 18 | Route::get('/', 'GroupController@index'); |
| 19 | 19 | Route::get('/{id}', 'GroupController@find'); |
@@ -11,7 +11,7 @@ |
||
| 11 | 11 | | |
| 12 | 12 | */ |
| 13 | 13 | |
| 14 | -Route::group(['prefix' => 'reports'], function () { |
|
| 14 | +Route::group(['prefix' => 'reports'], function() { |
|
| 15 | 15 | |
| 16 | 16 | Route::get('/', 'ReportController@index'); |
| 17 | 17 | Route::get('/{id}', 'ReportController@find'); |
@@ -13,7 +13,7 @@ |
||
| 13 | 13 | | |
| 14 | 14 | */ |
| 15 | 15 | |
| 16 | -Route::group(['prefix' => 'permissions'], function () { |
|
| 16 | +Route::group(['prefix' => 'permissions'], function() { |
|
| 17 | 17 | |
| 18 | 18 | Route::get('/', 'PermissionController@index'); |
| 19 | 19 | Route::get('/{id}', 'PermissionController@find'); |
@@ -6,391 +6,391 @@ |
||
| 6 | 6 | |
| 7 | 7 | class UserRepository extends BaseRepository |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Init new object. |
|
| 11 | - * |
|
| 12 | - * @param AclUser $model |
|
| 13 | - * @return void |
|
| 14 | - */ |
|
| 15 | - public function __construct(AclUser $model) |
|
| 16 | - { |
|
| 17 | - parent::__construct($model); |
|
| 18 | - } |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Return the logged in user account. |
|
| 22 | - * |
|
| 23 | - * @param array $relations |
|
| 24 | - * @return boolean |
|
| 25 | - */ |
|
| 26 | - public function account($relations = []) |
|
| 27 | - { |
|
| 28 | - $permissions = []; |
|
| 29 | - $user = $this->find(\Auth::id(), $relations); |
|
| 30 | - foreach ($user->groups()->get() as $group) { |
|
| 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 mixed $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 | - |
|
| 58 | - return in_array($nameOfPermission, $permissions); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Check if the logged in user has the given group. |
|
| 63 | - * |
|
| 64 | - * @param string[] $groups |
|
| 65 | - * @param mixed $user |
|
| 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 $groupIds |
|
| 79 | - * @return object |
|
| 80 | - */ |
|
| 81 | - public function assignGroups($userId, $groupIds) |
|
| 82 | - { |
|
| 83 | - \DB::transaction(function () use ($userId, $groupIds) { |
|
| 84 | - $user = $this->find($userId); |
|
| 85 | - $user->groups()->detach(); |
|
| 86 | - $user->groups()->attach($groupIds); |
|
| 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 | - \ErrorHandler::loginFailed(); |
|
| 104 | - } elseif ($adminLogin && ! $user->groups->whereIn('name', ['Admin'])->count()) { |
|
| 105 | - \ErrorHandler::loginFailed(); |
|
| 106 | - } elseif (! $adminLogin && $user->groups->whereIn('name', ['Admin'])->count()) { |
|
| 107 | - \ErrorHandler::loginFailed(); |
|
| 108 | - } elseif ($user->blocked) { |
|
| 109 | - \ErrorHandler::userIsBlocked(); |
|
| 110 | - } elseif (! config('skeleton.disable_confirm_email') && ! $user->confirmed) { |
|
| 111 | - \ErrorHandler::emailNotConfirmed(); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - return $user; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Handle a social login request of the none admin to the application. |
|
| 119 | - * |
|
| 120 | - * @param string $authCode |
|
| 121 | - * @param string $accessToken |
|
| 122 | - * @param string $type |
|
| 123 | - * @return array |
|
| 124 | - */ |
|
| 125 | - public function loginSocial($authCode, $accessToken, $type) |
|
| 126 | - { |
|
| 127 | - $access_token = $authCode ? Arr::get(\Socialite::driver($type)->getAccessTokenResponse($authCode), 'access_token') : $accessToken; |
|
| 128 | - $user = \Socialite::driver($type)->userFromToken($access_token); |
|
| 129 | - |
|
| 130 | - if (! $user->email) { |
|
| 131 | - \ErrorHandler::noSocialEmail(); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - if (! $this->model->where('email', $user->email)->first()) { |
|
| 135 | - $this->register(['email' => $user->email, 'password' => ''], true); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - $loginProxy = \App::make('App\Modules\Users\Proxy\LoginProxy'); |
|
| 139 | - return $loginProxy->login(['email' => $user->email, 'password' => config('skeleton.social_pass')], 0); |
|
| 140 | - } |
|
| 9 | + /** |
|
| 10 | + * Init new object. |
|
| 11 | + * |
|
| 12 | + * @param AclUser $model |
|
| 13 | + * @return void |
|
| 14 | + */ |
|
| 15 | + public function __construct(AclUser $model) |
|
| 16 | + { |
|
| 17 | + parent::__construct($model); |
|
| 18 | + } |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Return the logged in user account. |
|
| 22 | + * |
|
| 23 | + * @param array $relations |
|
| 24 | + * @return boolean |
|
| 25 | + */ |
|
| 26 | + public function account($relations = []) |
|
| 27 | + { |
|
| 28 | + $permissions = []; |
|
| 29 | + $user = $this->find(\Auth::id(), $relations); |
|
| 30 | + foreach ($user->groups()->get() as $group) { |
|
| 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 mixed $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 | + |
|
| 58 | + return in_array($nameOfPermission, $permissions); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Check if the logged in user has the given group. |
|
| 63 | + * |
|
| 64 | + * @param string[] $groups |
|
| 65 | + * @param mixed $user |
|
| 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 $groupIds |
|
| 79 | + * @return object |
|
| 80 | + */ |
|
| 81 | + public function assignGroups($userId, $groupIds) |
|
| 82 | + { |
|
| 83 | + \DB::transaction(function () use ($userId, $groupIds) { |
|
| 84 | + $user = $this->find($userId); |
|
| 85 | + $user->groups()->detach(); |
|
| 86 | + $user->groups()->attach($groupIds); |
|
| 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 | + \ErrorHandler::loginFailed(); |
|
| 104 | + } elseif ($adminLogin && ! $user->groups->whereIn('name', ['Admin'])->count()) { |
|
| 105 | + \ErrorHandler::loginFailed(); |
|
| 106 | + } elseif (! $adminLogin && $user->groups->whereIn('name', ['Admin'])->count()) { |
|
| 107 | + \ErrorHandler::loginFailed(); |
|
| 108 | + } elseif ($user->blocked) { |
|
| 109 | + \ErrorHandler::userIsBlocked(); |
|
| 110 | + } elseif (! config('skeleton.disable_confirm_email') && ! $user->confirmed) { |
|
| 111 | + \ErrorHandler::emailNotConfirmed(); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + return $user; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Handle a social login request of the none admin to the application. |
|
| 119 | + * |
|
| 120 | + * @param string $authCode |
|
| 121 | + * @param string $accessToken |
|
| 122 | + * @param string $type |
|
| 123 | + * @return array |
|
| 124 | + */ |
|
| 125 | + public function loginSocial($authCode, $accessToken, $type) |
|
| 126 | + { |
|
| 127 | + $access_token = $authCode ? Arr::get(\Socialite::driver($type)->getAccessTokenResponse($authCode), 'access_token') : $accessToken; |
|
| 128 | + $user = \Socialite::driver($type)->userFromToken($access_token); |
|
| 129 | + |
|
| 130 | + if (! $user->email) { |
|
| 131 | + \ErrorHandler::noSocialEmail(); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + if (! $this->model->where('email', $user->email)->first()) { |
|
| 135 | + $this->register(['email' => $user->email, 'password' => ''], true); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + $loginProxy = \App::make('App\Modules\Users\Proxy\LoginProxy'); |
|
| 139 | + return $loginProxy->login(['email' => $user->email, 'password' => config('skeleton.social_pass')], 0); |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - /** |
|
| 143 | - * Handle a registration request. |
|
| 144 | - * |
|
| 145 | - * @param array $credentials |
|
| 146 | - * @param boolean $skipConfirmEmail |
|
| 147 | - * @return boolean |
|
| 148 | - */ |
|
| 149 | - public function register($credentials, $skipConfirmEmail = false) |
|
| 150 | - { |
|
| 151 | - $user = $this->save($credentials); |
|
| 152 | - |
|
| 153 | - if ($skipConfirmEmail) { |
|
| 154 | - $user->confirmed = 1; |
|
| 155 | - $user->save(); |
|
| 156 | - } elseif (! config('skeleton.disable_confirm_email')) { |
|
| 157 | - $this->sendConfirmationEmail($user->email); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - return $user; |
|
| 161 | - } |
|
| 142 | + /** |
|
| 143 | + * Handle a registration request. |
|
| 144 | + * |
|
| 145 | + * @param array $credentials |
|
| 146 | + * @param boolean $skipConfirmEmail |
|
| 147 | + * @return boolean |
|
| 148 | + */ |
|
| 149 | + public function register($credentials, $skipConfirmEmail = false) |
|
| 150 | + { |
|
| 151 | + $user = $this->save($credentials); |
|
| 152 | + |
|
| 153 | + if ($skipConfirmEmail) { |
|
| 154 | + $user->confirmed = 1; |
|
| 155 | + $user->save(); |
|
| 156 | + } elseif (! config('skeleton.disable_confirm_email')) { |
|
| 157 | + $this->sendConfirmationEmail($user->email); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + return $user; |
|
| 161 | + } |
|
| 162 | 162 | |
| 163 | - /** |
|
| 164 | - * Block the user. |
|
| 165 | - * |
|
| 166 | - * @param integer $userId |
|
| 167 | - * @return object |
|
| 168 | - */ |
|
| 169 | - public function block($userId) |
|
| 170 | - { |
|
| 171 | - if (! $user = $this->find($userId)) { |
|
| 172 | - \ErrorHandler::notFound('user'); |
|
| 173 | - } |
|
| 174 | - if (! $this->hasGroup(['Admin'])) { |
|
| 175 | - \ErrorHandler::noPermissions(); |
|
| 176 | - } elseif (\Auth::id() == $userId) { |
|
| 177 | - \ErrorHandler::noPermissions(); |
|
| 178 | - } elseif ($user->groups->pluck('name')->search('Admin', true) !== false) { |
|
| 179 | - \ErrorHandler::noPermissions(); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - $user->blocked = 1; |
|
| 183 | - $user->save(); |
|
| 163 | + /** |
|
| 164 | + * Block the user. |
|
| 165 | + * |
|
| 166 | + * @param integer $userId |
|
| 167 | + * @return object |
|
| 168 | + */ |
|
| 169 | + public function block($userId) |
|
| 170 | + { |
|
| 171 | + if (! $user = $this->find($userId)) { |
|
| 172 | + \ErrorHandler::notFound('user'); |
|
| 173 | + } |
|
| 174 | + if (! $this->hasGroup(['Admin'])) { |
|
| 175 | + \ErrorHandler::noPermissions(); |
|
| 176 | + } elseif (\Auth::id() == $userId) { |
|
| 177 | + \ErrorHandler::noPermissions(); |
|
| 178 | + } elseif ($user->groups->pluck('name')->search('Admin', true) !== false) { |
|
| 179 | + \ErrorHandler::noPermissions(); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + $user->blocked = 1; |
|
| 183 | + $user->save(); |
|
| 184 | 184 | |
| 185 | - return $user; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Unblock the user. |
|
| 190 | - * |
|
| 191 | - * @param integer $userId |
|
| 192 | - * @return object |
|
| 193 | - */ |
|
| 194 | - public function unblock($userId) |
|
| 195 | - { |
|
| 196 | - if (! $this->hasGroup(['Admin'])) { |
|
| 197 | - \ErrorHandler::noPermissions(); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - $user = $this->find($userId); |
|
| 201 | - $user->blocked = 0; |
|
| 202 | - $user->save(); |
|
| 203 | - |
|
| 204 | - return $user; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * Send a reset link to the given user. |
|
| 209 | - * |
|
| 210 | - * @param string $email |
|
| 211 | - * @return void |
|
| 212 | - */ |
|
| 213 | - public function sendReset($email) |
|
| 214 | - { |
|
| 215 | - if (! $user = $this->model->where('email', $email)->first()) { |
|
| 216 | - \ErrorHandler::notFound('email'); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - $token = \Password::getRepository()->create($user); |
|
| 220 | - \Core::notifications()->notify($user, 'ResetPassword', $token); |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Reset the given user's password. |
|
| 225 | - * |
|
| 226 | - * @param array $credentials |
|
| 227 | - * @return string|null |
|
| 228 | - */ |
|
| 229 | - public function resetPassword($credentials) |
|
| 230 | - { |
|
| 231 | - $response = \Password::reset($credentials, function ($user, $password) { |
|
| 232 | - $user->password = $password; |
|
| 233 | - $user->save(); |
|
| 234 | - }); |
|
| 235 | - |
|
| 236 | - switch ($response) { |
|
| 237 | - case \Password::PASSWORD_RESET: |
|
| 238 | - return 'success'; |
|
| 239 | - |
|
| 240 | - case \Password::INVALID_TOKEN: |
|
| 241 | - \ErrorHandler::invalidResetToken('token'); |
|
| 242 | - //no break |
|
| 243 | - |
|
| 244 | - case \Password::INVALID_PASSWORD: |
|
| 245 | - \ErrorHandler::invalidResetPassword('email'); |
|
| 246 | - //no break |
|
| 247 | - |
|
| 248 | - case \Password::INVALID_USER: |
|
| 249 | - \ErrorHandler::notFound('user'); |
|
| 250 | - //no break |
|
| 251 | - |
|
| 252 | - default: |
|
| 253 | - \ErrorHandler::generalError(); |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * Change the logged in user password. |
|
| 259 | - * |
|
| 260 | - * @param array $credentials |
|
| 261 | - * @return void |
|
| 262 | - */ |
|
| 263 | - public function changePassword($credentials) |
|
| 264 | - { |
|
| 265 | - $user = \Auth::user(); |
|
| 266 | - if (! \Hash::check($credentials['old_password'], $user->password)) { |
|
| 267 | - \ErrorHandler::invalidOldPassword(); |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - $user->password = $credentials['password']; |
|
| 271 | - $user->save(); |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * Confirm email using the confirmation code. |
|
| 276 | - * |
|
| 277 | - * @param string $confirmationCode |
|
| 278 | - * @return void |
|
| 279 | - */ |
|
| 280 | - public function confirmEmail($confirmationCode) |
|
| 281 | - { |
|
| 282 | - if (! $user = $this->first(['confirmation_code' => $confirmationCode])) { |
|
| 283 | - \ErrorHandler::invalidConfirmationCode(); |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - $user->confirmed = 1; |
|
| 287 | - $user->confirmation_code = null; |
|
| 288 | - $user->save(); |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * Send the confirmation mail. |
|
| 293 | - * |
|
| 294 | - * @param string $email |
|
| 295 | - * @return void |
|
| 296 | - */ |
|
| 297 | - public function sendConfirmationEmail($email) |
|
| 298 | - { |
|
| 299 | - $user = $this->first(['email' => $email]); |
|
| 300 | - if ($user->confirmed) { |
|
| 301 | - \ErrorHandler::emailAlreadyConfirmed(); |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - $user->confirmed = 0; |
|
| 305 | - $user->confirmation_code = sha1(microtime()); |
|
| 306 | - $user->save(); |
|
| 307 | - \Core::notifications()->notify($user, 'ConfirmEmail'); |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * Paginate all users in the given group based on the given conditions. |
|
| 312 | - * |
|
| 313 | - * @param string $groupName |
|
| 314 | - * @param array $relations |
|
| 315 | - * @param integer $perPage |
|
| 316 | - * @param string $sortBy |
|
| 317 | - * @param boolean $desc |
|
| 318 | - * @return \Illuminate\Http\Response |
|
| 319 | - */ |
|
| 320 | - public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc) |
|
| 321 | - { |
|
| 322 | - unset($conditions['page']); |
|
| 323 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 324 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 325 | - $model = $this->model->with($relations); |
|
| 326 | - |
|
| 327 | - $model->whereHas('groups', function ($q) use ($groupName) { |
|
| 328 | - $q->where('name', $groupName); |
|
| 329 | - }); |
|
| 185 | + return $user; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Unblock the user. |
|
| 190 | + * |
|
| 191 | + * @param integer $userId |
|
| 192 | + * @return object |
|
| 193 | + */ |
|
| 194 | + public function unblock($userId) |
|
| 195 | + { |
|
| 196 | + if (! $this->hasGroup(['Admin'])) { |
|
| 197 | + \ErrorHandler::noPermissions(); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + $user = $this->find($userId); |
|
| 201 | + $user->blocked = 0; |
|
| 202 | + $user->save(); |
|
| 203 | + |
|
| 204 | + return $user; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * Send a reset link to the given user. |
|
| 209 | + * |
|
| 210 | + * @param string $email |
|
| 211 | + * @return void |
|
| 212 | + */ |
|
| 213 | + public function sendReset($email) |
|
| 214 | + { |
|
| 215 | + if (! $user = $this->model->where('email', $email)->first()) { |
|
| 216 | + \ErrorHandler::notFound('email'); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + $token = \Password::getRepository()->create($user); |
|
| 220 | + \Core::notifications()->notify($user, 'ResetPassword', $token); |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Reset the given user's password. |
|
| 225 | + * |
|
| 226 | + * @param array $credentials |
|
| 227 | + * @return string|null |
|
| 228 | + */ |
|
| 229 | + public function resetPassword($credentials) |
|
| 230 | + { |
|
| 231 | + $response = \Password::reset($credentials, function ($user, $password) { |
|
| 232 | + $user->password = $password; |
|
| 233 | + $user->save(); |
|
| 234 | + }); |
|
| 235 | + |
|
| 236 | + switch ($response) { |
|
| 237 | + case \Password::PASSWORD_RESET: |
|
| 238 | + return 'success'; |
|
| 239 | + |
|
| 240 | + case \Password::INVALID_TOKEN: |
|
| 241 | + \ErrorHandler::invalidResetToken('token'); |
|
| 242 | + //no break |
|
| 243 | + |
|
| 244 | + case \Password::INVALID_PASSWORD: |
|
| 245 | + \ErrorHandler::invalidResetPassword('email'); |
|
| 246 | + //no break |
|
| 247 | + |
|
| 248 | + case \Password::INVALID_USER: |
|
| 249 | + \ErrorHandler::notFound('user'); |
|
| 250 | + //no break |
|
| 251 | + |
|
| 252 | + default: |
|
| 253 | + \ErrorHandler::generalError(); |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * Change the logged in user password. |
|
| 259 | + * |
|
| 260 | + * @param array $credentials |
|
| 261 | + * @return void |
|
| 262 | + */ |
|
| 263 | + public function changePassword($credentials) |
|
| 264 | + { |
|
| 265 | + $user = \Auth::user(); |
|
| 266 | + if (! \Hash::check($credentials['old_password'], $user->password)) { |
|
| 267 | + \ErrorHandler::invalidOldPassword(); |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + $user->password = $credentials['password']; |
|
| 271 | + $user->save(); |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * Confirm email using the confirmation code. |
|
| 276 | + * |
|
| 277 | + * @param string $confirmationCode |
|
| 278 | + * @return void |
|
| 279 | + */ |
|
| 280 | + public function confirmEmail($confirmationCode) |
|
| 281 | + { |
|
| 282 | + if (! $user = $this->first(['confirmation_code' => $confirmationCode])) { |
|
| 283 | + \ErrorHandler::invalidConfirmationCode(); |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + $user->confirmed = 1; |
|
| 287 | + $user->confirmation_code = null; |
|
| 288 | + $user->save(); |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * Send the confirmation mail. |
|
| 293 | + * |
|
| 294 | + * @param string $email |
|
| 295 | + * @return void |
|
| 296 | + */ |
|
| 297 | + public function sendConfirmationEmail($email) |
|
| 298 | + { |
|
| 299 | + $user = $this->first(['email' => $email]); |
|
| 300 | + if ($user->confirmed) { |
|
| 301 | + \ErrorHandler::emailAlreadyConfirmed(); |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + $user->confirmed = 0; |
|
| 305 | + $user->confirmation_code = sha1(microtime()); |
|
| 306 | + $user->save(); |
|
| 307 | + \Core::notifications()->notify($user, 'ConfirmEmail'); |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * Paginate all users in the given group based on the given conditions. |
|
| 312 | + * |
|
| 313 | + * @param string $groupName |
|
| 314 | + * @param array $relations |
|
| 315 | + * @param integer $perPage |
|
| 316 | + * @param string $sortBy |
|
| 317 | + * @param boolean $desc |
|
| 318 | + * @return \Illuminate\Http\Response |
|
| 319 | + */ |
|
| 320 | + public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc) |
|
| 321 | + { |
|
| 322 | + unset($conditions['page']); |
|
| 323 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 324 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 325 | + $model = $this->model->with($relations); |
|
| 326 | + |
|
| 327 | + $model->whereHas('groups', function ($q) use ($groupName) { |
|
| 328 | + $q->where('name', $groupName); |
|
| 329 | + }); |
|
| 330 | 330 | |
| 331 | 331 | |
| 332 | - if (count($conditions['conditionValues'])) { |
|
| 333 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - if ($perPage) { |
|
| 337 | - return $model->orderBy($sortBy, $sort)->paginate($perPage); |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - return $model->orderBy($sortBy, $sort)->get(); |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - /** |
|
| 344 | - * Save the given data to the logged in user. |
|
| 345 | - * |
|
| 346 | - * @param array $data |
|
| 347 | - * @return boolean |
|
| 348 | - */ |
|
| 349 | - public function saveProfile($data) |
|
| 350 | - { |
|
| 351 | - if (Arr::has($data, 'profile_picture')) { |
|
| 352 | - $data['profile_picture'] = \Media::uploadImageBas64($data['profile_picture'], 'admins/profile_pictures'); |
|
| 353 | - } |
|
| 332 | + if (count($conditions['conditionValues'])) { |
|
| 333 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + if ($perPage) { |
|
| 337 | + return $model->orderBy($sortBy, $sort)->paginate($perPage); |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + return $model->orderBy($sortBy, $sort)->get(); |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + /** |
|
| 344 | + * Save the given data to the logged in user. |
|
| 345 | + * |
|
| 346 | + * @param array $data |
|
| 347 | + * @return boolean |
|
| 348 | + */ |
|
| 349 | + public function saveProfile($data) |
|
| 350 | + { |
|
| 351 | + if (Arr::has($data, 'profile_picture')) { |
|
| 352 | + $data['profile_picture'] = \Media::uploadImageBas64($data['profile_picture'], 'admins/profile_pictures'); |
|
| 353 | + } |
|
| 354 | 354 | |
| 355 | - $data['id'] = \Auth::id(); |
|
| 356 | - return $this->save($data); |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * Ensure access token hasn't expired or revoked. |
|
| 361 | - * |
|
| 362 | - * @param string $accessToken |
|
| 363 | - * @return boolean |
|
| 364 | - */ |
|
| 365 | - public function accessTokenExpiredOrRevoked($accessToken) |
|
| 366 | - { |
|
| 367 | - $accessTokenId = json_decode($accessToken, true)['id']; |
|
| 368 | - $accessToken = \DB::table('oauth_access_tokens') |
|
| 369 | - ->where('id', $accessTokenId) |
|
| 370 | - ->first(); |
|
| 355 | + $data['id'] = \Auth::id(); |
|
| 356 | + return $this->save($data); |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * Ensure access token hasn't expired or revoked. |
|
| 361 | + * |
|
| 362 | + * @param string $accessToken |
|
| 363 | + * @return boolean |
|
| 364 | + */ |
|
| 365 | + public function accessTokenExpiredOrRevoked($accessToken) |
|
| 366 | + { |
|
| 367 | + $accessTokenId = json_decode($accessToken, true)['id']; |
|
| 368 | + $accessToken = \DB::table('oauth_access_tokens') |
|
| 369 | + ->where('id', $accessTokenId) |
|
| 370 | + ->first(); |
|
| 371 | 371 | |
| 372 | - if (\Carbon\Carbon::parse($accessToken->expires_at)->isPast() || $accessToken->revoked) { |
|
| 373 | - return true; |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - return false; |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - /** |
|
| 380 | - * Revoke the given access token and all |
|
| 381 | - * associated refresh tokens. |
|
| 382 | - * |
|
| 383 | - * @param oject $accessToken |
|
| 384 | - * @return void |
|
| 385 | - */ |
|
| 386 | - public function revokeAccessToken($accessToken) |
|
| 387 | - { |
|
| 388 | - \DB::table('oauth_refresh_tokens') |
|
| 389 | - ->where('access_token_id', $accessToken->id) |
|
| 390 | - ->update([ |
|
| 391 | - 'revoked' => true |
|
| 392 | - ]); |
|
| 393 | - |
|
| 394 | - $accessToken->revoke(); |
|
| 395 | - } |
|
| 372 | + if (\Carbon\Carbon::parse($accessToken->expires_at)->isPast() || $accessToken->revoked) { |
|
| 373 | + return true; |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + return false; |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + /** |
|
| 380 | + * Revoke the given access token and all |
|
| 381 | + * associated refresh tokens. |
|
| 382 | + * |
|
| 383 | + * @param oject $accessToken |
|
| 384 | + * @return void |
|
| 385 | + */ |
|
| 386 | + public function revokeAccessToken($accessToken) |
|
| 387 | + { |
|
| 388 | + \DB::table('oauth_refresh_tokens') |
|
| 389 | + ->where('access_token_id', $accessToken->id) |
|
| 390 | + ->update([ |
|
| 391 | + 'revoked' => true |
|
| 392 | + ]); |
|
| 393 | + |
|
| 394 | + $accessToken->revoke(); |
|
| 395 | + } |
|
| 396 | 396 | } |
@@ -8,60 +8,60 @@ |
||
| 8 | 8 | |
| 9 | 9 | class PushNotificationDeviceRepository extends BaseRepository |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * Init new object. |
|
| 13 | - * |
|
| 14 | - * @param PushNotificationDevice $model |
|
| 15 | - * @return void |
|
| 16 | - */ |
|
| 17 | - public function __construct(PushNotificationDevice $model) |
|
| 18 | - { |
|
| 19 | - parent::__construct($model); |
|
| 20 | - } |
|
| 11 | + /** |
|
| 12 | + * Init new object. |
|
| 13 | + * |
|
| 14 | + * @param PushNotificationDevice $model |
|
| 15 | + * @return void |
|
| 16 | + */ |
|
| 17 | + public function __construct(PushNotificationDevice $model) |
|
| 18 | + { |
|
| 19 | + parent::__construct($model); |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Register the given device to the logged in user. |
|
| 24 | - * |
|
| 25 | - * @param array $data |
|
| 26 | - * @return boolean |
|
| 27 | - */ |
|
| 28 | - public function registerDevice($data) |
|
| 29 | - { |
|
| 30 | - $data['access_token'] = \Auth::user()->token(); |
|
| 31 | - $data['user_id'] = \Auth::id(); |
|
| 32 | - $device = $this->model->where('device_token', $data['device_token'])-> |
|
| 33 | - where('user_id', $data['user_id'])-> |
|
| 34 | - first(); |
|
| 22 | + /** |
|
| 23 | + * Register the given device to the logged in user. |
|
| 24 | + * |
|
| 25 | + * @param array $data |
|
| 26 | + * @return boolean |
|
| 27 | + */ |
|
| 28 | + public function registerDevice($data) |
|
| 29 | + { |
|
| 30 | + $data['access_token'] = \Auth::user()->token(); |
|
| 31 | + $data['user_id'] = \Auth::id(); |
|
| 32 | + $device = $this->model->where('device_token', $data['device_token'])-> |
|
| 33 | + where('user_id', $data['user_id'])-> |
|
| 34 | + first(); |
|
| 35 | 35 | |
| 36 | - if ($device) { |
|
| 37 | - $data['id'] = $device->id; |
|
| 38 | - } |
|
| 36 | + if ($device) { |
|
| 37 | + $data['id'] = $device->id; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - return $this->save($data); |
|
| 41 | - } |
|
| 40 | + return $this->save($data); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Generate the given message data. |
|
| 45 | - * |
|
| 46 | - * @param string $title |
|
| 47 | - * @param string $message |
|
| 48 | - * @param array $data |
|
| 49 | - * @return void |
|
| 50 | - */ |
|
| 51 | - public function generateMessageData($title, $message, $data = []) |
|
| 52 | - { |
|
| 53 | - $optionBuilder = new OptionsBuilder(); |
|
| 54 | - $notificationBuilder = new PayloadNotificationBuilder($title); |
|
| 55 | - $dataBuilder = new PayloadDataBuilder(); |
|
| 43 | + /** |
|
| 44 | + * Generate the given message data. |
|
| 45 | + * |
|
| 46 | + * @param string $title |
|
| 47 | + * @param string $message |
|
| 48 | + * @param array $data |
|
| 49 | + * @return void |
|
| 50 | + */ |
|
| 51 | + public function generateMessageData($title, $message, $data = []) |
|
| 52 | + { |
|
| 53 | + $optionBuilder = new OptionsBuilder(); |
|
| 54 | + $notificationBuilder = new PayloadNotificationBuilder($title); |
|
| 55 | + $dataBuilder = new PayloadDataBuilder(); |
|
| 56 | 56 | |
| 57 | - $optionBuilder->setTimeToLive(60 * 20); |
|
| 58 | - $notificationBuilder->setBody($message); |
|
| 59 | - $dataBuilder->addData($data); |
|
| 57 | + $optionBuilder->setTimeToLive(60 * 20); |
|
| 58 | + $notificationBuilder->setBody($message); |
|
| 59 | + $dataBuilder->addData($data); |
|
| 60 | 60 | |
| 61 | - $options = $optionBuilder->build(); |
|
| 62 | - $notification = $notificationBuilder->build(); |
|
| 63 | - $data = $dataBuilder->build(); |
|
| 61 | + $options = $optionBuilder->build(); |
|
| 62 | + $notification = $notificationBuilder->build(); |
|
| 63 | + $data = $dataBuilder->build(); |
|
| 64 | 64 | |
| 65 | - return compact('options', 'notification', 'data'); |
|
| 66 | - } |
|
| 65 | + return compact('options', 'notification', 'data'); |
|
| 66 | + } |
|
| 67 | 67 | } |
@@ -7,202 +7,202 @@ |
||
| 7 | 7 | class ApiConsumer |
| 8 | 8 | { |
| 9 | 9 | |
| 10 | - private $app; |
|
| 11 | - |
|
| 12 | - private $router; |
|
| 13 | - |
|
| 14 | - private $request; |
|
| 15 | - |
|
| 16 | - private $disableMiddleware = false; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * @param \Illuminate\Foundation\Application $app |
|
| 20 | - * @param \Illuminate\Http\Request $request, |
|
| 21 | - * @param \Illuminate\Routing\Router $router |
|
| 22 | - */ |
|
| 23 | - public function __construct(Application $app, Request $request, LaravelRouter $router) |
|
| 24 | - { |
|
| 25 | - $this->app = $app; |
|
| 26 | - $this->request = $request; |
|
| 27 | - $this->router = $router; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @return \Illuminate\Http\Response |
|
| 32 | - */ |
|
| 33 | - public function get() |
|
| 34 | - { |
|
| 35 | - return $this->quickCall('GET', func_get_args()); |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * @return \Illuminate\Http\Response |
|
| 40 | - */ |
|
| 41 | - public function post() |
|
| 42 | - { |
|
| 43 | - return $this->quickCall('POST', func_get_args()); |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @return \Illuminate\Http\Response |
|
| 48 | - */ |
|
| 49 | - public function put() |
|
| 50 | - { |
|
| 51 | - return $this->quickCall('PUT', func_get_args()); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @return \Illuminate\Http\Response |
|
| 56 | - */ |
|
| 57 | - public function delete() |
|
| 58 | - { |
|
| 59 | - return $this->quickCall('DELETE', func_get_args()); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @param array $requests An array of requests |
|
| 64 | - * @return array |
|
| 65 | - */ |
|
| 66 | - public function batchRequest(array $requests) |
|
| 67 | - { |
|
| 68 | - foreach ($requests as $i => $request) { |
|
| 69 | - $requests[$i] = call_user_func_array([$this, 'singleRequest'], $request); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - return $requests; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @param string $method |
|
| 77 | - * @param array $args |
|
| 78 | - * @return \Illuminate\Http\Response |
|
| 79 | - */ |
|
| 80 | - public function quickCall($method, array $args) |
|
| 81 | - { |
|
| 82 | - array_unshift($args, $method); |
|
| 83 | - return call_user_func_array([$this, "singleRequest"], $args); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * @param string $method |
|
| 88 | - * @param string $uri |
|
| 89 | - * @param array $data |
|
| 90 | - * @param array $headers |
|
| 91 | - * @param string $content |
|
| 92 | - * @return \Illuminate\Http\Response |
|
| 93 | - */ |
|
| 94 | - public function singleRequest($method, $uri, array $data = [], array $headers = [], $content = null) |
|
| 95 | - { |
|
| 96 | - // Save the current request so we can reset the router back to it |
|
| 97 | - // after we've completed our internal request. |
|
| 98 | - $currentRequest = $this->request->instance()->duplicate(); |
|
| 99 | - |
|
| 100 | - $headers = $this->overrideHeaders($currentRequest->server->getHeaders(), $headers); |
|
| 101 | - |
|
| 102 | - if ($this->disableMiddleware) { |
|
| 103 | - $this->app->instance('middleware.disable', true); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - $response = $this->request($method, $uri, $data, $headers, $content); |
|
| 107 | - |
|
| 108 | - if ($this->disableMiddleware) { |
|
| 109 | - $this->app->instance('middleware.disable', false); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - // Once the request has completed we reset the currentRequest of the router |
|
| 113 | - // to match the original request. |
|
| 114 | - $this->request->instance()->initialize( |
|
| 115 | - $currentRequest->query->all(), |
|
| 116 | - $currentRequest->request->all(), |
|
| 117 | - $currentRequest->attributes->all(), |
|
| 118 | - $currentRequest->cookies->all(), |
|
| 119 | - $currentRequest->files->all(), |
|
| 120 | - $currentRequest->server->all(), |
|
| 121 | - $currentRequest->content |
|
| 122 | - ); |
|
| 123 | - |
|
| 124 | - return $response; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - private function overrideHeaders(array $default, array $headers) |
|
| 128 | - { |
|
| 129 | - $headers = $this->transformHeadersToUppercaseUnderscoreType($headers); |
|
| 130 | - return array_merge($default, $headers); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - public function enableMiddleware() |
|
| 134 | - { |
|
| 135 | - $this->disableMiddleware = false; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - public function disableMiddleware() |
|
| 139 | - { |
|
| 140 | - $this->disableMiddleware = true; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * @param string $method |
|
| 145 | - * @param string $uri |
|
| 146 | - * @param array $data |
|
| 147 | - * @param array $headers |
|
| 148 | - * @param string $content |
|
| 149 | - * @return \Illuminate\Http\Response |
|
| 150 | - */ |
|
| 151 | - private function request($method, $uri, array $data = [], array $headers = [], $content = null) |
|
| 152 | - { |
|
| 153 | - // Create a new request object for the internal request |
|
| 154 | - $request = $this->createRequest($method, $uri, $data, $headers, $content); |
|
| 155 | - |
|
| 156 | - // Handle the request in the kernel and prepare a response |
|
| 157 | - $response = $this->router->prepareResponse($request, $this->app->handle($request)); |
|
| 158 | - |
|
| 159 | - return $response; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * @param string $method |
|
| 164 | - * @param string $uri |
|
| 165 | - * @param array $data |
|
| 166 | - * @param array $headers |
|
| 167 | - * @param string $content |
|
| 168 | - * @return \Illuminate\Http\Request |
|
| 169 | - */ |
|
| 170 | - private function createRequest($method, $uri, array $data = [], array $headers = [], $content = null) |
|
| 171 | - { |
|
| 172 | - $server = $this->transformHeadersToServerVariables($headers); |
|
| 173 | - |
|
| 174 | - return $this->request->create($uri, $method, $data, [], [], $server, $content); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - private function transformHeadersToUppercaseUnderscoreType($headers) |
|
| 178 | - { |
|
| 179 | - $transformed = []; |
|
| 180 | - |
|
| 181 | - foreach ($headers as $headerType => $headerValue) { |
|
| 182 | - $headerType = strtoupper(str_replace('-', '_', $headerType)); |
|
| 183 | - |
|
| 184 | - $transformed[$headerType] = $headerValue; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - return $transformed; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * https://github.com/symfony/symfony/issues/5074 |
|
| 192 | - * |
|
| 193 | - * @param array $headers |
|
| 194 | - * @return array |
|
| 195 | - */ |
|
| 196 | - private function transformHeadersToServerVariables($headers) |
|
| 197 | - { |
|
| 198 | - $server = []; |
|
| 199 | - |
|
| 200 | - foreach ($headers as $headerType => $headerValue) { |
|
| 201 | - $headerType = 'HTTP_' . $headerType; |
|
| 202 | - |
|
| 203 | - $server[$headerType] = $headerValue; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - return $server; |
|
| 207 | - } |
|
| 10 | + private $app; |
|
| 11 | + |
|
| 12 | + private $router; |
|
| 13 | + |
|
| 14 | + private $request; |
|
| 15 | + |
|
| 16 | + private $disableMiddleware = false; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * @param \Illuminate\Foundation\Application $app |
|
| 20 | + * @param \Illuminate\Http\Request $request, |
|
| 21 | + * @param \Illuminate\Routing\Router $router |
|
| 22 | + */ |
|
| 23 | + public function __construct(Application $app, Request $request, LaravelRouter $router) |
|
| 24 | + { |
|
| 25 | + $this->app = $app; |
|
| 26 | + $this->request = $request; |
|
| 27 | + $this->router = $router; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @return \Illuminate\Http\Response |
|
| 32 | + */ |
|
| 33 | + public function get() |
|
| 34 | + { |
|
| 35 | + return $this->quickCall('GET', func_get_args()); |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * @return \Illuminate\Http\Response |
|
| 40 | + */ |
|
| 41 | + public function post() |
|
| 42 | + { |
|
| 43 | + return $this->quickCall('POST', func_get_args()); |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @return \Illuminate\Http\Response |
|
| 48 | + */ |
|
| 49 | + public function put() |
|
| 50 | + { |
|
| 51 | + return $this->quickCall('PUT', func_get_args()); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @return \Illuminate\Http\Response |
|
| 56 | + */ |
|
| 57 | + public function delete() |
|
| 58 | + { |
|
| 59 | + return $this->quickCall('DELETE', func_get_args()); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @param array $requests An array of requests |
|
| 64 | + * @return array |
|
| 65 | + */ |
|
| 66 | + public function batchRequest(array $requests) |
|
| 67 | + { |
|
| 68 | + foreach ($requests as $i => $request) { |
|
| 69 | + $requests[$i] = call_user_func_array([$this, 'singleRequest'], $request); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + return $requests; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @param string $method |
|
| 77 | + * @param array $args |
|
| 78 | + * @return \Illuminate\Http\Response |
|
| 79 | + */ |
|
| 80 | + public function quickCall($method, array $args) |
|
| 81 | + { |
|
| 82 | + array_unshift($args, $method); |
|
| 83 | + return call_user_func_array([$this, "singleRequest"], $args); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * @param string $method |
|
| 88 | + * @param string $uri |
|
| 89 | + * @param array $data |
|
| 90 | + * @param array $headers |
|
| 91 | + * @param string $content |
|
| 92 | + * @return \Illuminate\Http\Response |
|
| 93 | + */ |
|
| 94 | + public function singleRequest($method, $uri, array $data = [], array $headers = [], $content = null) |
|
| 95 | + { |
|
| 96 | + // Save the current request so we can reset the router back to it |
|
| 97 | + // after we've completed our internal request. |
|
| 98 | + $currentRequest = $this->request->instance()->duplicate(); |
|
| 99 | + |
|
| 100 | + $headers = $this->overrideHeaders($currentRequest->server->getHeaders(), $headers); |
|
| 101 | + |
|
| 102 | + if ($this->disableMiddleware) { |
|
| 103 | + $this->app->instance('middleware.disable', true); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + $response = $this->request($method, $uri, $data, $headers, $content); |
|
| 107 | + |
|
| 108 | + if ($this->disableMiddleware) { |
|
| 109 | + $this->app->instance('middleware.disable', false); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + // Once the request has completed we reset the currentRequest of the router |
|
| 113 | + // to match the original request. |
|
| 114 | + $this->request->instance()->initialize( |
|
| 115 | + $currentRequest->query->all(), |
|
| 116 | + $currentRequest->request->all(), |
|
| 117 | + $currentRequest->attributes->all(), |
|
| 118 | + $currentRequest->cookies->all(), |
|
| 119 | + $currentRequest->files->all(), |
|
| 120 | + $currentRequest->server->all(), |
|
| 121 | + $currentRequest->content |
|
| 122 | + ); |
|
| 123 | + |
|
| 124 | + return $response; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + private function overrideHeaders(array $default, array $headers) |
|
| 128 | + { |
|
| 129 | + $headers = $this->transformHeadersToUppercaseUnderscoreType($headers); |
|
| 130 | + return array_merge($default, $headers); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + public function enableMiddleware() |
|
| 134 | + { |
|
| 135 | + $this->disableMiddleware = false; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + public function disableMiddleware() |
|
| 139 | + { |
|
| 140 | + $this->disableMiddleware = true; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * @param string $method |
|
| 145 | + * @param string $uri |
|
| 146 | + * @param array $data |
|
| 147 | + * @param array $headers |
|
| 148 | + * @param string $content |
|
| 149 | + * @return \Illuminate\Http\Response |
|
| 150 | + */ |
|
| 151 | + private function request($method, $uri, array $data = [], array $headers = [], $content = null) |
|
| 152 | + { |
|
| 153 | + // Create a new request object for the internal request |
|
| 154 | + $request = $this->createRequest($method, $uri, $data, $headers, $content); |
|
| 155 | + |
|
| 156 | + // Handle the request in the kernel and prepare a response |
|
| 157 | + $response = $this->router->prepareResponse($request, $this->app->handle($request)); |
|
| 158 | + |
|
| 159 | + return $response; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * @param string $method |
|
| 164 | + * @param string $uri |
|
| 165 | + * @param array $data |
|
| 166 | + * @param array $headers |
|
| 167 | + * @param string $content |
|
| 168 | + * @return \Illuminate\Http\Request |
|
| 169 | + */ |
|
| 170 | + private function createRequest($method, $uri, array $data = [], array $headers = [], $content = null) |
|
| 171 | + { |
|
| 172 | + $server = $this->transformHeadersToServerVariables($headers); |
|
| 173 | + |
|
| 174 | + return $this->request->create($uri, $method, $data, [], [], $server, $content); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + private function transformHeadersToUppercaseUnderscoreType($headers) |
|
| 178 | + { |
|
| 179 | + $transformed = []; |
|
| 180 | + |
|
| 181 | + foreach ($headers as $headerType => $headerValue) { |
|
| 182 | + $headerType = strtoupper(str_replace('-', '_', $headerType)); |
|
| 183 | + |
|
| 184 | + $transformed[$headerType] = $headerValue; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + return $transformed; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * https://github.com/symfony/symfony/issues/5074 |
|
| 192 | + * |
|
| 193 | + * @param array $headers |
|
| 194 | + * @return array |
|
| 195 | + */ |
|
| 196 | + private function transformHeadersToServerVariables($headers) |
|
| 197 | + { |
|
| 198 | + $server = []; |
|
| 199 | + |
|
| 200 | + foreach ($headers as $headerType => $headerValue) { |
|
| 201 | + $headerType = 'HTTP_' . $headerType; |
|
| 202 | + |
|
| 203 | + $server[$headerType] = $headerValue; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + return $server; |
|
| 207 | + } |
|
| 208 | 208 | } |