@@ -239,7 +239,7 @@ discard block |
||
| 239 | 239 | * Reset the given user's password. |
| 240 | 240 | * |
| 241 | 241 | * @param array $credentials |
| 242 | - * @return array |
|
| 242 | + * @return string|null |
|
| 243 | 243 | */ |
| 244 | 244 | public function resetPassword($credentials) |
| 245 | 245 | { |
@@ -322,7 +322,6 @@ discard block |
||
| 322 | 322 | /** |
| 323 | 323 | * Save the given data to the logged in user. |
| 324 | 324 | * |
| 325 | - * @param array $credentials |
|
| 326 | 325 | * @return void |
| 327 | 326 | */ |
| 328 | 327 | public function saveProfile($data) |
@@ -5,350 +5,350 @@ |
||
| 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\V1\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\V1\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($groupName, $userId = false) |
|
| 69 | - { |
|
| 70 | - $userId = $userId ?: \Auth::id(); |
|
| 71 | - $groups = $this->find($userId)->groups; |
|
| 72 | - return $groups->pluck('name')->search($groupName, true) === false ? false : true; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Assign the given group ids to the given user. |
|
| 77 | - * |
|
| 78 | - * @param integer $user_id |
|
| 79 | - * @param array $group_ids |
|
| 80 | - * @return object |
|
| 81 | - */ |
|
| 82 | - public function assignGroups($user_id, $group_ids) |
|
| 83 | - { |
|
| 84 | - \DB::transaction(function () use ($user_id, $group_ids) { |
|
| 85 | - $user = $this->find($user_id); |
|
| 86 | - $user->groups()->detach(); |
|
| 87 | - $user->groups()->attach($group_ids); |
|
| 88 | - }); |
|
| 89 | - |
|
| 90 | - return $this->find($user_id); |
|
| 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->pluck('name')->search('Admin', true) === false) |
|
| 107 | - { |
|
| 108 | - \ErrorHandler::loginFailed(); |
|
| 109 | - } |
|
| 110 | - else if ( ! $adminLogin && $user->groups->pluck('name')->search('Admin', true) !== false) |
|
| 111 | - { |
|
| 112 | - \ErrorHandler::loginFailed(); |
|
| 113 | - } |
|
| 114 | - else if ($user->blocked) |
|
| 115 | - { |
|
| 116 | - \ErrorHandler::userIsBlocked(); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - return $user; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Handle a social login request of the none admin to the application. |
|
| 124 | - * |
|
| 125 | - * @param array $credentials |
|
| 126 | - * @return array |
|
| 127 | - */ |
|
| 128 | - public function loginSocial($credentials) |
|
| 129 | - { |
|
| 130 | - $access_token = $credentials['auth_code'] ? \Socialite::driver($credentials['type'])->getAccessToken($credentials['auth_code']) : $credentials['access_token']; |
|
| 131 | - $user = \Socialite::driver($credentials['type'])->userFromToken($access_token); |
|
| 132 | - |
|
| 133 | - if ( ! $user->email) |
|
| 134 | - { |
|
| 135 | - \ErrorHandler::noSocialEmail(); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - if ( ! $registeredUser = $this->model->where('email', $user->email)->first()) |
|
| 139 | - { |
|
| 140 | - $data = ['email' => $user->email, 'password' => '']; |
|
| 141 | - return $this->register($data); |
|
| 142 | - } |
|
| 143 | - else |
|
| 144 | - { |
|
| 145 | - if ( ! \Auth::attempt(['email' => $registeredUser->email, 'password' => ''])) |
|
| 146 | - { |
|
| 147 | - \ErrorHandler::userAlreadyRegistered(); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - $loginProxy = \App::make('App\Modules\V1\Acl\Proxy\LoginProxy'); |
|
| 151 | - return $loginProxy->login(['email' => $registeredUser->email, 'password' => ''], 0); |
|
| 152 | - } |
|
| 153 | - } |
|
| 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($groupName, $userId = false) |
|
| 69 | + { |
|
| 70 | + $userId = $userId ?: \Auth::id(); |
|
| 71 | + $groups = $this->find($userId)->groups; |
|
| 72 | + return $groups->pluck('name')->search($groupName, true) === false ? false : true; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Assign the given group ids to the given user. |
|
| 77 | + * |
|
| 78 | + * @param integer $user_id |
|
| 79 | + * @param array $group_ids |
|
| 80 | + * @return object |
|
| 81 | + */ |
|
| 82 | + public function assignGroups($user_id, $group_ids) |
|
| 83 | + { |
|
| 84 | + \DB::transaction(function () use ($user_id, $group_ids) { |
|
| 85 | + $user = $this->find($user_id); |
|
| 86 | + $user->groups()->detach(); |
|
| 87 | + $user->groups()->attach($group_ids); |
|
| 88 | + }); |
|
| 89 | + |
|
| 90 | + return $this->find($user_id); |
|
| 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->pluck('name')->search('Admin', true) === false) |
|
| 107 | + { |
|
| 108 | + \ErrorHandler::loginFailed(); |
|
| 109 | + } |
|
| 110 | + else if ( ! $adminLogin && $user->groups->pluck('name')->search('Admin', true) !== false) |
|
| 111 | + { |
|
| 112 | + \ErrorHandler::loginFailed(); |
|
| 113 | + } |
|
| 114 | + else if ($user->blocked) |
|
| 115 | + { |
|
| 116 | + \ErrorHandler::userIsBlocked(); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + return $user; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Handle a social login request of the none admin to the application. |
|
| 124 | + * |
|
| 125 | + * @param array $credentials |
|
| 126 | + * @return array |
|
| 127 | + */ |
|
| 128 | + public function loginSocial($credentials) |
|
| 129 | + { |
|
| 130 | + $access_token = $credentials['auth_code'] ? \Socialite::driver($credentials['type'])->getAccessToken($credentials['auth_code']) : $credentials['access_token']; |
|
| 131 | + $user = \Socialite::driver($credentials['type'])->userFromToken($access_token); |
|
| 132 | + |
|
| 133 | + if ( ! $user->email) |
|
| 134 | + { |
|
| 135 | + \ErrorHandler::noSocialEmail(); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + if ( ! $registeredUser = $this->model->where('email', $user->email)->first()) |
|
| 139 | + { |
|
| 140 | + $data = ['email' => $user->email, 'password' => '']; |
|
| 141 | + return $this->register($data); |
|
| 142 | + } |
|
| 143 | + else |
|
| 144 | + { |
|
| 145 | + if ( ! \Auth::attempt(['email' => $registeredUser->email, 'password' => ''])) |
|
| 146 | + { |
|
| 147 | + \ErrorHandler::userAlreadyRegistered(); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + $loginProxy = \App::make('App\Modules\V1\Acl\Proxy\LoginProxy'); |
|
| 151 | + return $loginProxy->login(['email' => $registeredUser->email, 'password' => ''], 0); |
|
| 152 | + } |
|
| 153 | + } |
|
| 154 | 154 | |
| 155 | - /** |
|
| 156 | - * Handle a registration request. |
|
| 157 | - * |
|
| 158 | - * @param array $credentials |
|
| 159 | - * @return array |
|
| 160 | - */ |
|
| 161 | - public function register($credentials) |
|
| 162 | - { |
|
| 163 | - $this->model->create($credentials); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Block the user. |
|
| 168 | - * |
|
| 169 | - * @param integer $user_id |
|
| 170 | - * @return object |
|
| 171 | - */ |
|
| 172 | - public function block($user_id) |
|
| 173 | - { |
|
| 174 | - if ( ! $user = $this->find($user_id)) |
|
| 175 | - { |
|
| 176 | - \ErrorHandler::notFound('user'); |
|
| 177 | - } |
|
| 178 | - if ( ! $this->hasGroup('Admin')) |
|
| 179 | - { |
|
| 180 | - \ErrorHandler::noPermissions(); |
|
| 181 | - } |
|
| 182 | - else if (\Auth::id() == $user_id) |
|
| 183 | - { |
|
| 184 | - \ErrorHandler::noPermissions(); |
|
| 185 | - } |
|
| 186 | - else if ($user->groups->pluck('name')->search('Admin', true) !== false) |
|
| 187 | - { |
|
| 188 | - \ErrorHandler::noPermissions(); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - $user->blocked = 1; |
|
| 192 | - $user->save(); |
|
| 155 | + /** |
|
| 156 | + * Handle a registration request. |
|
| 157 | + * |
|
| 158 | + * @param array $credentials |
|
| 159 | + * @return array |
|
| 160 | + */ |
|
| 161 | + public function register($credentials) |
|
| 162 | + { |
|
| 163 | + $this->model->create($credentials); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Block the user. |
|
| 168 | + * |
|
| 169 | + * @param integer $user_id |
|
| 170 | + * @return object |
|
| 171 | + */ |
|
| 172 | + public function block($user_id) |
|
| 173 | + { |
|
| 174 | + if ( ! $user = $this->find($user_id)) |
|
| 175 | + { |
|
| 176 | + \ErrorHandler::notFound('user'); |
|
| 177 | + } |
|
| 178 | + if ( ! $this->hasGroup('Admin')) |
|
| 179 | + { |
|
| 180 | + \ErrorHandler::noPermissions(); |
|
| 181 | + } |
|
| 182 | + else if (\Auth::id() == $user_id) |
|
| 183 | + { |
|
| 184 | + \ErrorHandler::noPermissions(); |
|
| 185 | + } |
|
| 186 | + else if ($user->groups->pluck('name')->search('Admin', true) !== false) |
|
| 187 | + { |
|
| 188 | + \ErrorHandler::noPermissions(); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + $user->blocked = 1; |
|
| 192 | + $user->save(); |
|
| 193 | 193 | |
| 194 | - return $user; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Unblock the user. |
|
| 199 | - * |
|
| 200 | - * @param integer $user_id |
|
| 201 | - * @return object |
|
| 202 | - */ |
|
| 203 | - public function unblock($user_id) |
|
| 204 | - { |
|
| 205 | - if ( ! $this->hasGroup('Admin')) |
|
| 206 | - { |
|
| 207 | - \ErrorHandler::noPermissions(); |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - $user = $this->find($user_id); |
|
| 211 | - $user->blocked = 0; |
|
| 212 | - $user->save(); |
|
| 213 | - |
|
| 214 | - return $user; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Send a reset link to the given user. |
|
| 219 | - * |
|
| 220 | - * @param string $email |
|
| 221 | - * @return void |
|
| 222 | - */ |
|
| 223 | - public function sendReset($email) |
|
| 224 | - { |
|
| 225 | - if ( ! $user = $this->model->where('email', $email)->first()) |
|
| 226 | - { |
|
| 227 | - \ErrorHandler::notFound('email'); |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - $url = $this->config['resetLink']; |
|
| 231 | - $token = \Password::getRepository()->create($user); |
|
| 194 | + return $user; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Unblock the user. |
|
| 199 | + * |
|
| 200 | + * @param integer $user_id |
|
| 201 | + * @return object |
|
| 202 | + */ |
|
| 203 | + public function unblock($user_id) |
|
| 204 | + { |
|
| 205 | + if ( ! $this->hasGroup('Admin')) |
|
| 206 | + { |
|
| 207 | + \ErrorHandler::noPermissions(); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + $user = $this->find($user_id); |
|
| 211 | + $user->blocked = 0; |
|
| 212 | + $user->save(); |
|
| 213 | + |
|
| 214 | + return $user; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Send a reset link to the given user. |
|
| 219 | + * |
|
| 220 | + * @param string $email |
|
| 221 | + * @return void |
|
| 222 | + */ |
|
| 223 | + public function sendReset($email) |
|
| 224 | + { |
|
| 225 | + if ( ! $user = $this->model->where('email', $email)->first()) |
|
| 226 | + { |
|
| 227 | + \ErrorHandler::notFound('email'); |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + $url = $this->config['resetLink']; |
|
| 231 | + $token = \Password::getRepository()->create($user); |
|
| 232 | 232 | |
| 233 | - \Mail::send('acl::resetpassword', ['user' => $user, 'url' => $url, 'token' => $token], function ($m) use ($user) { |
|
| 234 | - $m->to($user->email, $user->name)->subject('Your Password Reset Link'); |
|
| 235 | - }); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * Reset the given user's password. |
|
| 240 | - * |
|
| 241 | - * @param array $credentials |
|
| 242 | - * @return array |
|
| 243 | - */ |
|
| 244 | - public function resetPassword($credentials) |
|
| 245 | - { |
|
| 246 | - $response = \Password::reset($credentials, function ($user, $password) { |
|
| 247 | - $user->password = $password; |
|
| 248 | - $user->save(); |
|
| 249 | - }); |
|
| 250 | - |
|
| 251 | - switch ($response) { |
|
| 252 | - case \Password::PASSWORD_RESET: |
|
| 253 | - return 'success'; |
|
| 233 | + \Mail::send('acl::resetpassword', ['user' => $user, 'url' => $url, 'token' => $token], function ($m) use ($user) { |
|
| 234 | + $m->to($user->email, $user->name)->subject('Your Password Reset Link'); |
|
| 235 | + }); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * Reset the given user's password. |
|
| 240 | + * |
|
| 241 | + * @param array $credentials |
|
| 242 | + * @return array |
|
| 243 | + */ |
|
| 244 | + public function resetPassword($credentials) |
|
| 245 | + { |
|
| 246 | + $response = \Password::reset($credentials, function ($user, $password) { |
|
| 247 | + $user->password = $password; |
|
| 248 | + $user->save(); |
|
| 249 | + }); |
|
| 250 | + |
|
| 251 | + switch ($response) { |
|
| 252 | + case \Password::PASSWORD_RESET: |
|
| 253 | + return 'success'; |
|
| 254 | 254 | |
| 255 | - case \Password::INVALID_TOKEN: |
|
| 256 | - \ErrorHandler::invalidResetToken('token'); |
|
| 257 | - |
|
| 258 | - case \Password::INVALID_PASSWORD: |
|
| 259 | - \ErrorHandler::invalidResetPassword('email'); |
|
| 260 | - |
|
| 261 | - case \Password::INVALID_USER: |
|
| 262 | - \ErrorHandler::notFound('user'); |
|
| 263 | - |
|
| 264 | - default: |
|
| 265 | - \ErrorHandler::generalError(); |
|
| 266 | - } |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - /** |
|
| 270 | - * Change the logged in user password. |
|
| 271 | - * |
|
| 272 | - * @param array $credentials |
|
| 273 | - * @return void |
|
| 274 | - */ |
|
| 275 | - public function changePassword($credentials) |
|
| 276 | - { |
|
| 277 | - $user = \Auth::user(); |
|
| 278 | - if ( ! \Hash::check($credentials['old_password'], $user->password)) |
|
| 279 | - { |
|
| 280 | - \ErrorHandler::invalidOldPassword(); |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - $user->password = $credentials['password']; |
|
| 284 | - $user->save(); |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - /** |
|
| 288 | - * Paginate all users in the given group based on the given conditions. |
|
| 289 | - * |
|
| 290 | - * @param string $groupName |
|
| 291 | - * @param array $relations |
|
| 292 | - * @param integer $perPage |
|
| 293 | - * @param string $sortBy |
|
| 294 | - * @param boolean $desc |
|
| 295 | - * @return \Illuminate\Http\Response |
|
| 296 | - */ |
|
| 297 | - public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc) |
|
| 298 | - { |
|
| 299 | - unset($conditions['page']); |
|
| 300 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 301 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 302 | - $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
| 303 | - |
|
| 304 | - $model->whereHas('groups', function($q) use ($groupName){ |
|
| 305 | - $q->where('name', $groupName); |
|
| 306 | - }); |
|
| 255 | + case \Password::INVALID_TOKEN: |
|
| 256 | + \ErrorHandler::invalidResetToken('token'); |
|
| 257 | + |
|
| 258 | + case \Password::INVALID_PASSWORD: |
|
| 259 | + \ErrorHandler::invalidResetPassword('email'); |
|
| 260 | + |
|
| 261 | + case \Password::INVALID_USER: |
|
| 262 | + \ErrorHandler::notFound('user'); |
|
| 263 | + |
|
| 264 | + default: |
|
| 265 | + \ErrorHandler::generalError(); |
|
| 266 | + } |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + /** |
|
| 270 | + * Change the logged in user password. |
|
| 271 | + * |
|
| 272 | + * @param array $credentials |
|
| 273 | + * @return void |
|
| 274 | + */ |
|
| 275 | + public function changePassword($credentials) |
|
| 276 | + { |
|
| 277 | + $user = \Auth::user(); |
|
| 278 | + if ( ! \Hash::check($credentials['old_password'], $user->password)) |
|
| 279 | + { |
|
| 280 | + \ErrorHandler::invalidOldPassword(); |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + $user->password = $credentials['password']; |
|
| 284 | + $user->save(); |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + /** |
|
| 288 | + * Paginate all users in the given group based on the given conditions. |
|
| 289 | + * |
|
| 290 | + * @param string $groupName |
|
| 291 | + * @param array $relations |
|
| 292 | + * @param integer $perPage |
|
| 293 | + * @param string $sortBy |
|
| 294 | + * @param boolean $desc |
|
| 295 | + * @return \Illuminate\Http\Response |
|
| 296 | + */ |
|
| 297 | + public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc) |
|
| 298 | + { |
|
| 299 | + unset($conditions['page']); |
|
| 300 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 301 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 302 | + $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
| 303 | + |
|
| 304 | + $model->whereHas('groups', function($q) use ($groupName){ |
|
| 305 | + $q->where('name', $groupName); |
|
| 306 | + }); |
|
| 307 | 307 | |
| 308 | 308 | |
| 309 | - if (count($conditions['conditionValues'])) |
|
| 310 | - { |
|
| 311 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - if ($perPage) |
|
| 315 | - { |
|
| 316 | - return $model->orderBy($sortBy, $sort)->paginate($perPage); |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - return $model->orderBy($sortBy, $sort)->get(); |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * Save the given data to the logged in user. |
|
| 324 | - * |
|
| 325 | - * @param array $credentials |
|
| 326 | - * @return void |
|
| 327 | - */ |
|
| 328 | - public function saveProfile($data) |
|
| 329 | - { |
|
| 330 | - $data['id'] = \Auth::id(); |
|
| 331 | - $this->save($data); |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - /** |
|
| 335 | - * Ensure access token hasn't expired or revoked. |
|
| 336 | - * |
|
| 337 | - * @param string $accessToken |
|
| 338 | - * @return boolean |
|
| 339 | - */ |
|
| 340 | - public function accessTokenExpiredOrRevoked($accessToken) |
|
| 341 | - { |
|
| 342 | - |
|
| 343 | - $accessTokenRepository = \App::make('League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface'); |
|
| 344 | - $data = new ValidationData(); |
|
| 345 | - $data->setCurrentTime(time()); |
|
| 346 | - |
|
| 347 | - if ($accessToken->validate($data) === false || $accessTokenRepository->isAccessTokenRevoked($accessToken->getClaim('jti'))) |
|
| 348 | - { |
|
| 349 | - return true; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - return false; |
|
| 353 | - } |
|
| 309 | + if (count($conditions['conditionValues'])) |
|
| 310 | + { |
|
| 311 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + if ($perPage) |
|
| 315 | + { |
|
| 316 | + return $model->orderBy($sortBy, $sort)->paginate($perPage); |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + return $model->orderBy($sortBy, $sort)->get(); |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * Save the given data to the logged in user. |
|
| 324 | + * |
|
| 325 | + * @param array $credentials |
|
| 326 | + * @return void |
|
| 327 | + */ |
|
| 328 | + public function saveProfile($data) |
|
| 329 | + { |
|
| 330 | + $data['id'] = \Auth::id(); |
|
| 331 | + $this->save($data); |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + /** |
|
| 335 | + * Ensure access token hasn't expired or revoked. |
|
| 336 | + * |
|
| 337 | + * @param string $accessToken |
|
| 338 | + * @return boolean |
|
| 339 | + */ |
|
| 340 | + public function accessTokenExpiredOrRevoked($accessToken) |
|
| 341 | + { |
|
| 342 | + |
|
| 343 | + $accessTokenRepository = \App::make('League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface'); |
|
| 344 | + $data = new ValidationData(); |
|
| 345 | + $data->setCurrentTime(time()); |
|
| 346 | + |
|
| 347 | + if ($accessToken->validate($data) === false || $accessTokenRepository->isAccessTokenRevoked($accessToken->getClaim('jti'))) |
|
| 348 | + { |
|
| 349 | + return true; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + return false; |
|
| 353 | + } |
|
| 354 | 354 | } |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | * |
| 33 | 33 | * @param array $relations |
| 34 | 34 | * @param string $sortBy |
| 35 | - * @param boolean $desc |
|
| 35 | + * @param integer $desc |
|
| 36 | 36 | * @param array $columns |
| 37 | 37 | * @return collection |
| 38 | 38 | */ |
@@ -50,7 +50,7 @@ discard block |
||
| 50 | 50 | * @param integer $perPage |
| 51 | 51 | * @param array $relations |
| 52 | 52 | * @param string $sortBy |
| 53 | - * @param boolean $desc |
|
| 53 | + * @param integer $desc |
|
| 54 | 54 | * @param array $columns |
| 55 | 55 | * @return collection |
| 56 | 56 | */ |
@@ -164,7 +164,7 @@ discard block |
||
| 164 | 164 | * @param integer $perPage |
| 165 | 165 | * @param array $relations |
| 166 | 166 | * @param string $sortBy |
| 167 | - * @param boolean $desc |
|
| 167 | + * @param integer $desc |
|
| 168 | 168 | * @param array $columns |
| 169 | 169 | * @return collection |
| 170 | 170 | */ |
@@ -182,7 +182,7 @@ discard block |
||
| 182 | 182 | * @param integer $perPage |
| 183 | 183 | * @param array $relations |
| 184 | 184 | * @param string $sortBy |
| 185 | - * @param boolean $desc |
|
| 185 | + * @param integer $desc |
|
| 186 | 186 | * @param array $columns |
| 187 | 187 | * @return collection |
| 188 | 188 | */ |
@@ -198,7 +198,7 @@ discard block |
||
| 198 | 198 | * Save the given model to the storage. |
| 199 | 199 | * |
| 200 | 200 | * @param array $data |
| 201 | - * @return object |
|
| 201 | + * @return boolean |
|
| 202 | 202 | */ |
| 203 | 203 | public function save(array $data) |
| 204 | 204 | { |
@@ -516,7 +516,7 @@ discard block |
||
| 516 | 516 | * id. |
| 517 | 517 | * |
| 518 | 518 | * @param integer $id |
| 519 | - * @param array $relations |
|
| 519 | + * @param string[] $relations |
|
| 520 | 520 | * @param array $columns |
| 521 | 521 | * @return object |
| 522 | 522 | */ |
@@ -532,7 +532,7 @@ discard block |
||
| 532 | 532 | * @param array $conditions array of conditions |
| 533 | 533 | * @param array $relations |
| 534 | 534 | * @param string $sortBy |
| 535 | - * @param boolean $desc |
|
| 535 | + * @param integer $desc |
|
| 536 | 536 | * @param array $columns |
| 537 | 537 | * @return collection |
| 538 | 538 | */ |
@@ -564,7 +564,7 @@ discard block |
||
| 564 | 564 | * @param array $conditions array of conditions |
| 565 | 565 | * @param integer $perPage |
| 566 | 566 | * @param string $sortBy |
| 567 | - * @param boolean $desc |
|
| 567 | + * @param integer $desc |
|
| 568 | 568 | * @param array $columns |
| 569 | 569 | * @return collection |
| 570 | 570 | */ |
@@ -4,716 +4,716 @@ |
||
| 4 | 4 | |
| 5 | 5 | abstract class AbstractRepository implements RepositoryInterface |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * The model implementation. |
|
| 9 | - * |
|
| 10 | - * @var model |
|
| 11 | - */ |
|
| 12 | - public $model; |
|
| 7 | + /** |
|
| 8 | + * The model implementation. |
|
| 9 | + * |
|
| 10 | + * @var model |
|
| 11 | + */ |
|
| 12 | + public $model; |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * The config implementation. |
|
| 16 | - * |
|
| 17 | - * @var config |
|
| 18 | - */ |
|
| 19 | - protected $config; |
|
| 14 | + /** |
|
| 15 | + * The config implementation. |
|
| 16 | + * |
|
| 17 | + * @var config |
|
| 18 | + */ |
|
| 19 | + protected $config; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Create new AbstractRepository instance. |
|
| 23 | - */ |
|
| 24 | - public function __construct() |
|
| 25 | - { |
|
| 26 | - $this->config = \CoreConfig::getConfig(); |
|
| 27 | - $this->model = \App::make($this->getModel()); |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Fetch all records with relations from the storage. |
|
| 32 | - * |
|
| 33 | - * @param array $relations |
|
| 34 | - * @param string $sortBy |
|
| 35 | - * @param boolean $desc |
|
| 36 | - * @param array $columns |
|
| 37 | - * @return collection |
|
| 38 | - */ |
|
| 39 | - public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 40 | - { |
|
| 41 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 42 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Fetch all records with relations from storage in pages |
|
| 47 | - * that matche the given query. |
|
| 48 | - * |
|
| 49 | - * @param string $query |
|
| 50 | - * @param integer $perPage |
|
| 51 | - * @param array $relations |
|
| 52 | - * @param string $sortBy |
|
| 53 | - * @param boolean $desc |
|
| 54 | - * @param array $columns |
|
| 55 | - * @return collection |
|
| 56 | - */ |
|
| 57 | - public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 58 | - { |
|
| 59 | - $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
| 60 | - $conditionColumns = $this->model->searchable; |
|
| 61 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Construct the select conditions for the model. |
|
| 65 | - */ |
|
| 66 | - $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
| 67 | - |
|
| 68 | - if (count($conditionColumns)) |
|
| 69 | - { |
|
| 70 | - $column = 'LOWER(' . array_shift($conditionColumns) . ')'; |
|
| 71 | - if (str_contains($column, '->')) |
|
| 72 | - { |
|
| 73 | - $column = $this->wrapJsonSelector($column); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Use the first element in the model columns to construct the first condition. |
|
| 78 | - */ |
|
| 79 | - $q->where(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Loop through the rest of the columns to construct or where conditions. |
|
| 84 | - */ |
|
| 85 | - foreach ($conditionColumns as $column) |
|
| 86 | - { |
|
| 87 | - $column = 'LOWER(' . $column . ')'; |
|
| 88 | - if (str_contains($column, '->')) |
|
| 89 | - { |
|
| 90 | - $column = $this->wrapJsonSelector($column); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - $q->orWhere(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Loop through the model relations. |
|
| 98 | - */ |
|
| 99 | - foreach ($relations as $relation) |
|
| 100 | - { |
|
| 101 | - /** |
|
| 102 | - * Remove the sub relation if exists. |
|
| 103 | - */ |
|
| 104 | - $relation = explode('.', $relation)[0]; |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Try to fetch the relation repository from the core. |
|
| 108 | - */ |
|
| 109 | - if (\Core::$relation()) |
|
| 110 | - { |
|
| 111 | - /** |
|
| 112 | - * Construct the relation condition. |
|
| 113 | - */ |
|
| 114 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
| 115 | - |
|
| 116 | - $subModel->where(function ($q) use ($query, $relation){ |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Get columns of the relation. |
|
| 120 | - */ |
|
| 121 | - $subConditionColumns = \Core::$relation()->model->searchable; |
|
| 122 | - |
|
| 123 | - if (count($subConditionColumns)) |
|
| 124 | - { |
|
| 125 | - |
|
| 126 | - $column = 'LOWER(' . array_shift($subConditionColumns) . ')'; |
|
| 127 | - if (str_contains($column, '->')) |
|
| 128 | - { |
|
| 129 | - $column = $this->wrapJsonSelector($column); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Use the first element in the relation model columns to construct the first condition. |
|
| 134 | - */ |
|
| 135 | - $q->where(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Loop through the rest of the columns to construct or where conditions. |
|
| 140 | - */ |
|
| 141 | - foreach ($subConditionColumns as $subConditionColumn) |
|
| 142 | - { |
|
| 143 | - $column = 'LOWER(' . $subConditionColumn . ')'; |
|
| 144 | - if (str_contains($column, '->')) |
|
| 145 | - { |
|
| 146 | - $column = $this->wrapJsonSelector($column); |
|
| 147 | - } |
|
| 21 | + /** |
|
| 22 | + * Create new AbstractRepository instance. |
|
| 23 | + */ |
|
| 24 | + public function __construct() |
|
| 25 | + { |
|
| 26 | + $this->config = \CoreConfig::getConfig(); |
|
| 27 | + $this->model = \App::make($this->getModel()); |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Fetch all records with relations from the storage. |
|
| 32 | + * |
|
| 33 | + * @param array $relations |
|
| 34 | + * @param string $sortBy |
|
| 35 | + * @param boolean $desc |
|
| 36 | + * @param array $columns |
|
| 37 | + * @return collection |
|
| 38 | + */ |
|
| 39 | + public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 40 | + { |
|
| 41 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 42 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Fetch all records with relations from storage in pages |
|
| 47 | + * that matche the given query. |
|
| 48 | + * |
|
| 49 | + * @param string $query |
|
| 50 | + * @param integer $perPage |
|
| 51 | + * @param array $relations |
|
| 52 | + * @param string $sortBy |
|
| 53 | + * @param boolean $desc |
|
| 54 | + * @param array $columns |
|
| 55 | + * @return collection |
|
| 56 | + */ |
|
| 57 | + public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 58 | + { |
|
| 59 | + $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
| 60 | + $conditionColumns = $this->model->searchable; |
|
| 61 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Construct the select conditions for the model. |
|
| 65 | + */ |
|
| 66 | + $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
| 67 | + |
|
| 68 | + if (count($conditionColumns)) |
|
| 69 | + { |
|
| 70 | + $column = 'LOWER(' . array_shift($conditionColumns) . ')'; |
|
| 71 | + if (str_contains($column, '->')) |
|
| 72 | + { |
|
| 73 | + $column = $this->wrapJsonSelector($column); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Use the first element in the model columns to construct the first condition. |
|
| 78 | + */ |
|
| 79 | + $q->where(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Loop through the rest of the columns to construct or where conditions. |
|
| 84 | + */ |
|
| 85 | + foreach ($conditionColumns as $column) |
|
| 86 | + { |
|
| 87 | + $column = 'LOWER(' . $column . ')'; |
|
| 88 | + if (str_contains($column, '->')) |
|
| 89 | + { |
|
| 90 | + $column = $this->wrapJsonSelector($column); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + $q->orWhere(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Loop through the model relations. |
|
| 98 | + */ |
|
| 99 | + foreach ($relations as $relation) |
|
| 100 | + { |
|
| 101 | + /** |
|
| 102 | + * Remove the sub relation if exists. |
|
| 103 | + */ |
|
| 104 | + $relation = explode('.', $relation)[0]; |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Try to fetch the relation repository from the core. |
|
| 108 | + */ |
|
| 109 | + if (\Core::$relation()) |
|
| 110 | + { |
|
| 111 | + /** |
|
| 112 | + * Construct the relation condition. |
|
| 113 | + */ |
|
| 114 | + $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
| 115 | + |
|
| 116 | + $subModel->where(function ($q) use ($query, $relation){ |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Get columns of the relation. |
|
| 120 | + */ |
|
| 121 | + $subConditionColumns = \Core::$relation()->model->searchable; |
|
| 122 | + |
|
| 123 | + if (count($subConditionColumns)) |
|
| 124 | + { |
|
| 125 | + |
|
| 126 | + $column = 'LOWER(' . array_shift($subConditionColumns) . ')'; |
|
| 127 | + if (str_contains($column, '->')) |
|
| 128 | + { |
|
| 129 | + $column = $this->wrapJsonSelector($column); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Use the first element in the relation model columns to construct the first condition. |
|
| 134 | + */ |
|
| 135 | + $q->where(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Loop through the rest of the columns to construct or where conditions. |
|
| 140 | + */ |
|
| 141 | + foreach ($subConditionColumns as $subConditionColumn) |
|
| 142 | + { |
|
| 143 | + $column = 'LOWER(' . $subConditionColumn . ')'; |
|
| 144 | + if (str_contains($column, '->')) |
|
| 145 | + { |
|
| 146 | + $column = $this->wrapJsonSelector($column); |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - $q->orWhere(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 150 | - } |
|
| 151 | - }); |
|
| 152 | - |
|
| 153 | - }); |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - }); |
|
| 149 | + $q->orWhere(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 150 | + } |
|
| 151 | + }); |
|
| 152 | + |
|
| 153 | + }); |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + }); |
|
| 157 | 157 | |
| 158 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 159 | - } |
|
| 158 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 159 | + } |
|
| 160 | 160 | |
| 161 | - /** |
|
| 162 | - * Fetch all records with relations from storage in pages. |
|
| 163 | - * |
|
| 164 | - * @param integer $perPage |
|
| 165 | - * @param array $relations |
|
| 166 | - * @param string $sortBy |
|
| 167 | - * @param boolean $desc |
|
| 168 | - * @param array $columns |
|
| 169 | - * @return collection |
|
| 170 | - */ |
|
| 171 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 172 | - { |
|
| 173 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 174 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Fetch all records with relations based on |
|
| 179 | - * the given condition from storage in pages. |
|
| 180 | - * |
|
| 181 | - * @param array $conditions array of conditions |
|
| 182 | - * @param integer $perPage |
|
| 183 | - * @param array $relations |
|
| 184 | - * @param string $sortBy |
|
| 185 | - * @param boolean $desc |
|
| 186 | - * @param array $columns |
|
| 187 | - * @return collection |
|
| 188 | - */ |
|
| 189 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 190 | - { |
|
| 191 | - unset($conditions['page']); |
|
| 192 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 193 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 194 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 195 | - } |
|
| 161 | + /** |
|
| 162 | + * Fetch all records with relations from storage in pages. |
|
| 163 | + * |
|
| 164 | + * @param integer $perPage |
|
| 165 | + * @param array $relations |
|
| 166 | + * @param string $sortBy |
|
| 167 | + * @param boolean $desc |
|
| 168 | + * @param array $columns |
|
| 169 | + * @return collection |
|
| 170 | + */ |
|
| 171 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 172 | + { |
|
| 173 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 174 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Fetch all records with relations based on |
|
| 179 | + * the given condition from storage in pages. |
|
| 180 | + * |
|
| 181 | + * @param array $conditions array of conditions |
|
| 182 | + * @param integer $perPage |
|
| 183 | + * @param array $relations |
|
| 184 | + * @param string $sortBy |
|
| 185 | + * @param boolean $desc |
|
| 186 | + * @param array $columns |
|
| 187 | + * @return collection |
|
| 188 | + */ |
|
| 189 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 190 | + { |
|
| 191 | + unset($conditions['page']); |
|
| 192 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 193 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 194 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 195 | + } |
|
| 196 | 196 | |
| 197 | - /** |
|
| 198 | - * Save the given model to the storage. |
|
| 199 | - * |
|
| 200 | - * @param array $data |
|
| 201 | - * @return object |
|
| 202 | - */ |
|
| 203 | - public function save(array $data) |
|
| 204 | - { |
|
| 205 | - $model = false; |
|
| 206 | - $modelClass = $this->model; |
|
| 207 | - $relations = []; |
|
| 208 | - |
|
| 209 | - \DB::transaction(function () use (&$model, &$relations, $data, $modelClass) { |
|
| 210 | - /** |
|
| 211 | - * If the id is present in the data then select the model for updating, |
|
| 212 | - * else create new model. |
|
| 213 | - * @var array |
|
| 214 | - */ |
|
| 215 | - $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
| 216 | - if ( ! $model) |
|
| 217 | - { |
|
| 218 | - \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * Construct the model object with the given data, |
|
| 223 | - * and if there is a relation add it to relations array, |
|
| 224 | - * then save the model. |
|
| 225 | - */ |
|
| 226 | - foreach ($data as $key => $value) |
|
| 227 | - { |
|
| 228 | - /** |
|
| 229 | - * If the attribute is a relation. |
|
| 230 | - */ |
|
| 231 | - $relation = camel_case($key); |
|
| 232 | - if (method_exists($model, $relation) && \Core::$relation()) |
|
| 233 | - { |
|
| 234 | - /** |
|
| 235 | - * Check if the relation is a collection. |
|
| 236 | - */ |
|
| 237 | - if (class_basename($model->$relation) == 'Collection') |
|
| 238 | - { |
|
| 239 | - /** |
|
| 240 | - * If the relation has no value then marke the relation data |
|
| 241 | - * related to the model to be deleted. |
|
| 242 | - */ |
|
| 243 | - if ( ! $value || ! count($value)) |
|
| 244 | - { |
|
| 245 | - $relations[$relation] = 'delete'; |
|
| 246 | - } |
|
| 247 | - } |
|
| 248 | - if (is_array($value)) |
|
| 249 | - { |
|
| 250 | - /** |
|
| 251 | - * Loop through the relation data. |
|
| 252 | - */ |
|
| 253 | - foreach ($value as $attr => $val) |
|
| 254 | - { |
|
| 255 | - /** |
|
| 256 | - * Get the relation model. |
|
| 257 | - */ |
|
| 258 | - $relationBaseModel = \Core::$relation()->model; |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * Check if the relation is a collection. |
|
| 262 | - */ |
|
| 263 | - if (class_basename($model->$relation) == 'Collection') |
|
| 264 | - { |
|
| 265 | - /** |
|
| 266 | - * If the id is present in the data then select the relation model for updating, |
|
| 267 | - * else create new model. |
|
| 268 | - */ |
|
| 269 | - $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * If model doesn't exists. |
|
| 273 | - */ |
|
| 274 | - if ( ! $relationModel) |
|
| 275 | - { |
|
| 276 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * Loop through the relation attributes. |
|
| 281 | - */ |
|
| 282 | - foreach ($val as $attr => $val) |
|
| 283 | - { |
|
| 284 | - /** |
|
| 285 | - * Prevent the sub relations or attributes not in the fillable. |
|
| 286 | - */ |
|
| 287 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
| 288 | - { |
|
| 289 | - $relationModel->$attr = $val; |
|
| 290 | - } |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - $relations[$relation][] = $relationModel; |
|
| 294 | - } |
|
| 295 | - /** |
|
| 296 | - * If not collection. |
|
| 297 | - */ |
|
| 298 | - else |
|
| 299 | - { |
|
| 300 | - /** |
|
| 301 | - * Prevent the sub relations. |
|
| 302 | - */ |
|
| 303 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
| 304 | - { |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * If the id is present in the data then select the relation model for updating, |
|
| 308 | - * else create new model. |
|
| 309 | - */ |
|
| 310 | - $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * If model doesn't exists. |
|
| 314 | - */ |
|
| 315 | - if ( ! $relationModel) |
|
| 316 | - { |
|
| 317 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - foreach ($value as $relationAttribute => $relationValue) |
|
| 321 | - { |
|
| 322 | - /** |
|
| 323 | - * Prevent attributes not in the fillable. |
|
| 324 | - */ |
|
| 325 | - if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) |
|
| 326 | - { |
|
| 327 | - $relationModel->$relationAttribute = $relationValue; |
|
| 328 | - } |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - $relations[$relation] = $relationModel; |
|
| 332 | - } |
|
| 333 | - } |
|
| 334 | - } |
|
| 335 | - } |
|
| 336 | - } |
|
| 337 | - /** |
|
| 338 | - * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
| 339 | - */ |
|
| 340 | - else if (array_search($key, $model->getFillable(), true) !== false) |
|
| 341 | - { |
|
| 342 | - $model->$key = $value; |
|
| 343 | - } |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * Loop through the relations array. |
|
| 348 | - */ |
|
| 349 | - foreach ($relations as $key => $value) |
|
| 350 | - { |
|
| 351 | - /** |
|
| 352 | - * If the relation is marked for delete then delete it. |
|
| 353 | - */ |
|
| 354 | - if ($value == 'delete' && $model->$key()->count()) |
|
| 355 | - { |
|
| 356 | - $model->$key()->delete(); |
|
| 357 | - } |
|
| 358 | - /** |
|
| 359 | - * If the relation is an array. |
|
| 360 | - */ |
|
| 361 | - else if (gettype($value) == 'array') |
|
| 362 | - { |
|
| 363 | - /** |
|
| 364 | - * Save the model. |
|
| 365 | - */ |
|
| 366 | - $model->save(); |
|
| 367 | - $ids = []; |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * Loop through the relations. |
|
| 371 | - */ |
|
| 372 | - foreach ($value as $val) |
|
| 373 | - { |
|
| 374 | - switch (class_basename($model->$key())) |
|
| 375 | - { |
|
| 376 | - /** |
|
| 377 | - * If the relation is one to many then update it's foreign key with |
|
| 378 | - * the model id and save it then add its id to ids array to delete all |
|
| 379 | - * relations who's id isn't in the ids array. |
|
| 380 | - */ |
|
| 381 | - case 'HasMany': |
|
| 382 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 383 | - $val->$foreignKeyName = $model->id; |
|
| 384 | - $val->save(); |
|
| 385 | - $ids[] = $val->id; |
|
| 386 | - break; |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * If the relation is many to many then add it's id to the ids array to |
|
| 390 | - * attache these ids to the model. |
|
| 391 | - */ |
|
| 392 | - case 'BelongsToMany': |
|
| 393 | - $val->save(); |
|
| 394 | - $ids[] = $val->id; |
|
| 395 | - break; |
|
| 396 | - } |
|
| 397 | - } |
|
| 398 | - switch (class_basename($model->$key())) |
|
| 399 | - { |
|
| 400 | - /** |
|
| 401 | - * If the relation is one to many then delete all |
|
| 402 | - * relations who's id isn't in the ids array. |
|
| 403 | - */ |
|
| 404 | - case 'HasMany': |
|
| 405 | - $model->$key()->whereNotIn('id', $ids)->delete(); |
|
| 406 | - break; |
|
| 407 | - |
|
| 408 | - /** |
|
| 409 | - * If the relation is many to many then |
|
| 410 | - * detach the previous data and attach |
|
| 411 | - * the ids array to the model. |
|
| 412 | - */ |
|
| 413 | - case 'BelongsToMany': |
|
| 414 | - $model->$key()->detach(); |
|
| 415 | - $model->$key()->attach($ids); |
|
| 416 | - break; |
|
| 417 | - } |
|
| 418 | - } |
|
| 419 | - /** |
|
| 420 | - * If the relation isn't array. |
|
| 421 | - */ |
|
| 422 | - else |
|
| 423 | - { |
|
| 424 | - switch (class_basename($model->$key())) |
|
| 425 | - { |
|
| 426 | - /** |
|
| 427 | - * If the relation is one to one. |
|
| 428 | - */ |
|
| 429 | - case 'HasOne': |
|
| 430 | - /** |
|
| 431 | - * Save the model. |
|
| 432 | - */ |
|
| 433 | - $model->save(); |
|
| 434 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 435 | - $value->$foreignKeyName = $model->id; |
|
| 436 | - $value->save(); |
|
| 437 | - break; |
|
| 438 | - case 'BelongsTo': |
|
| 439 | - /** |
|
| 440 | - * Save the model. |
|
| 441 | - */ |
|
| 442 | - $value->save(); |
|
| 443 | - $model->$key()->associate($value); |
|
| 444 | - break; |
|
| 445 | - } |
|
| 446 | - } |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - /** |
|
| 450 | - * Save the model. |
|
| 451 | - */ |
|
| 452 | - $model->save(); |
|
| 453 | - }); |
|
| 197 | + /** |
|
| 198 | + * Save the given model to the storage. |
|
| 199 | + * |
|
| 200 | + * @param array $data |
|
| 201 | + * @return object |
|
| 202 | + */ |
|
| 203 | + public function save(array $data) |
|
| 204 | + { |
|
| 205 | + $model = false; |
|
| 206 | + $modelClass = $this->model; |
|
| 207 | + $relations = []; |
|
| 208 | + |
|
| 209 | + \DB::transaction(function () use (&$model, &$relations, $data, $modelClass) { |
|
| 210 | + /** |
|
| 211 | + * If the id is present in the data then select the model for updating, |
|
| 212 | + * else create new model. |
|
| 213 | + * @var array |
|
| 214 | + */ |
|
| 215 | + $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
| 216 | + if ( ! $model) |
|
| 217 | + { |
|
| 218 | + \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * Construct the model object with the given data, |
|
| 223 | + * and if there is a relation add it to relations array, |
|
| 224 | + * then save the model. |
|
| 225 | + */ |
|
| 226 | + foreach ($data as $key => $value) |
|
| 227 | + { |
|
| 228 | + /** |
|
| 229 | + * If the attribute is a relation. |
|
| 230 | + */ |
|
| 231 | + $relation = camel_case($key); |
|
| 232 | + if (method_exists($model, $relation) && \Core::$relation()) |
|
| 233 | + { |
|
| 234 | + /** |
|
| 235 | + * Check if the relation is a collection. |
|
| 236 | + */ |
|
| 237 | + if (class_basename($model->$relation) == 'Collection') |
|
| 238 | + { |
|
| 239 | + /** |
|
| 240 | + * If the relation has no value then marke the relation data |
|
| 241 | + * related to the model to be deleted. |
|
| 242 | + */ |
|
| 243 | + if ( ! $value || ! count($value)) |
|
| 244 | + { |
|
| 245 | + $relations[$relation] = 'delete'; |
|
| 246 | + } |
|
| 247 | + } |
|
| 248 | + if (is_array($value)) |
|
| 249 | + { |
|
| 250 | + /** |
|
| 251 | + * Loop through the relation data. |
|
| 252 | + */ |
|
| 253 | + foreach ($value as $attr => $val) |
|
| 254 | + { |
|
| 255 | + /** |
|
| 256 | + * Get the relation model. |
|
| 257 | + */ |
|
| 258 | + $relationBaseModel = \Core::$relation()->model; |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * Check if the relation is a collection. |
|
| 262 | + */ |
|
| 263 | + if (class_basename($model->$relation) == 'Collection') |
|
| 264 | + { |
|
| 265 | + /** |
|
| 266 | + * If the id is present in the data then select the relation model for updating, |
|
| 267 | + * else create new model. |
|
| 268 | + */ |
|
| 269 | + $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * If model doesn't exists. |
|
| 273 | + */ |
|
| 274 | + if ( ! $relationModel) |
|
| 275 | + { |
|
| 276 | + \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * Loop through the relation attributes. |
|
| 281 | + */ |
|
| 282 | + foreach ($val as $attr => $val) |
|
| 283 | + { |
|
| 284 | + /** |
|
| 285 | + * Prevent the sub relations or attributes not in the fillable. |
|
| 286 | + */ |
|
| 287 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
| 288 | + { |
|
| 289 | + $relationModel->$attr = $val; |
|
| 290 | + } |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + $relations[$relation][] = $relationModel; |
|
| 294 | + } |
|
| 295 | + /** |
|
| 296 | + * If not collection. |
|
| 297 | + */ |
|
| 298 | + else |
|
| 299 | + { |
|
| 300 | + /** |
|
| 301 | + * Prevent the sub relations. |
|
| 302 | + */ |
|
| 303 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
| 304 | + { |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * If the id is present in the data then select the relation model for updating, |
|
| 308 | + * else create new model. |
|
| 309 | + */ |
|
| 310 | + $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * If model doesn't exists. |
|
| 314 | + */ |
|
| 315 | + if ( ! $relationModel) |
|
| 316 | + { |
|
| 317 | + \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + foreach ($value as $relationAttribute => $relationValue) |
|
| 321 | + { |
|
| 322 | + /** |
|
| 323 | + * Prevent attributes not in the fillable. |
|
| 324 | + */ |
|
| 325 | + if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) |
|
| 326 | + { |
|
| 327 | + $relationModel->$relationAttribute = $relationValue; |
|
| 328 | + } |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + $relations[$relation] = $relationModel; |
|
| 332 | + } |
|
| 333 | + } |
|
| 334 | + } |
|
| 335 | + } |
|
| 336 | + } |
|
| 337 | + /** |
|
| 338 | + * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
| 339 | + */ |
|
| 340 | + else if (array_search($key, $model->getFillable(), true) !== false) |
|
| 341 | + { |
|
| 342 | + $model->$key = $value; |
|
| 343 | + } |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * Loop through the relations array. |
|
| 348 | + */ |
|
| 349 | + foreach ($relations as $key => $value) |
|
| 350 | + { |
|
| 351 | + /** |
|
| 352 | + * If the relation is marked for delete then delete it. |
|
| 353 | + */ |
|
| 354 | + if ($value == 'delete' && $model->$key()->count()) |
|
| 355 | + { |
|
| 356 | + $model->$key()->delete(); |
|
| 357 | + } |
|
| 358 | + /** |
|
| 359 | + * If the relation is an array. |
|
| 360 | + */ |
|
| 361 | + else if (gettype($value) == 'array') |
|
| 362 | + { |
|
| 363 | + /** |
|
| 364 | + * Save the model. |
|
| 365 | + */ |
|
| 366 | + $model->save(); |
|
| 367 | + $ids = []; |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * Loop through the relations. |
|
| 371 | + */ |
|
| 372 | + foreach ($value as $val) |
|
| 373 | + { |
|
| 374 | + switch (class_basename($model->$key())) |
|
| 375 | + { |
|
| 376 | + /** |
|
| 377 | + * If the relation is one to many then update it's foreign key with |
|
| 378 | + * the model id and save it then add its id to ids array to delete all |
|
| 379 | + * relations who's id isn't in the ids array. |
|
| 380 | + */ |
|
| 381 | + case 'HasMany': |
|
| 382 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 383 | + $val->$foreignKeyName = $model->id; |
|
| 384 | + $val->save(); |
|
| 385 | + $ids[] = $val->id; |
|
| 386 | + break; |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * If the relation is many to many then add it's id to the ids array to |
|
| 390 | + * attache these ids to the model. |
|
| 391 | + */ |
|
| 392 | + case 'BelongsToMany': |
|
| 393 | + $val->save(); |
|
| 394 | + $ids[] = $val->id; |
|
| 395 | + break; |
|
| 396 | + } |
|
| 397 | + } |
|
| 398 | + switch (class_basename($model->$key())) |
|
| 399 | + { |
|
| 400 | + /** |
|
| 401 | + * If the relation is one to many then delete all |
|
| 402 | + * relations who's id isn't in the ids array. |
|
| 403 | + */ |
|
| 404 | + case 'HasMany': |
|
| 405 | + $model->$key()->whereNotIn('id', $ids)->delete(); |
|
| 406 | + break; |
|
| 407 | + |
|
| 408 | + /** |
|
| 409 | + * If the relation is many to many then |
|
| 410 | + * detach the previous data and attach |
|
| 411 | + * the ids array to the model. |
|
| 412 | + */ |
|
| 413 | + case 'BelongsToMany': |
|
| 414 | + $model->$key()->detach(); |
|
| 415 | + $model->$key()->attach($ids); |
|
| 416 | + break; |
|
| 417 | + } |
|
| 418 | + } |
|
| 419 | + /** |
|
| 420 | + * If the relation isn't array. |
|
| 421 | + */ |
|
| 422 | + else |
|
| 423 | + { |
|
| 424 | + switch (class_basename($model->$key())) |
|
| 425 | + { |
|
| 426 | + /** |
|
| 427 | + * If the relation is one to one. |
|
| 428 | + */ |
|
| 429 | + case 'HasOne': |
|
| 430 | + /** |
|
| 431 | + * Save the model. |
|
| 432 | + */ |
|
| 433 | + $model->save(); |
|
| 434 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 435 | + $value->$foreignKeyName = $model->id; |
|
| 436 | + $value->save(); |
|
| 437 | + break; |
|
| 438 | + case 'BelongsTo': |
|
| 439 | + /** |
|
| 440 | + * Save the model. |
|
| 441 | + */ |
|
| 442 | + $value->save(); |
|
| 443 | + $model->$key()->associate($value); |
|
| 444 | + break; |
|
| 445 | + } |
|
| 446 | + } |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + /** |
|
| 450 | + * Save the model. |
|
| 451 | + */ |
|
| 452 | + $model->save(); |
|
| 453 | + }); |
|
| 454 | 454 | |
| 455 | - return $model; |
|
| 456 | - } |
|
| 455 | + return $model; |
|
| 456 | + } |
|
| 457 | 457 | |
| 458 | - /** |
|
| 459 | - * Update record in the storage based on the given |
|
| 460 | - * condition. |
|
| 461 | - * |
|
| 462 | - * @param var $value condition value |
|
| 463 | - * @param array $data |
|
| 464 | - * @param string $attribute condition column name |
|
| 465 | - * @return void |
|
| 466 | - */ |
|
| 467 | - public function update($value, array $data, $attribute = 'id') |
|
| 468 | - { |
|
| 469 | - if ($attribute == 'id') |
|
| 470 | - { |
|
| 471 | - $model = $this->model->lockForUpdate()->find($value); |
|
| 472 | - $model ? $model->update($data) : 0; |
|
| 473 | - } |
|
| 474 | - else |
|
| 475 | - { |
|
| 476 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data){ |
|
| 477 | - $model->update($data); |
|
| 478 | - }); |
|
| 479 | - } |
|
| 480 | - } |
|
| 481 | - |
|
| 482 | - /** |
|
| 483 | - * Delete record from the storage based on the given |
|
| 484 | - * condition. |
|
| 485 | - * |
|
| 486 | - * @param var $value condition value |
|
| 487 | - * @param string $attribute condition column name |
|
| 488 | - * @return void |
|
| 489 | - */ |
|
| 490 | - public function delete($value, $attribute = 'id') |
|
| 491 | - { |
|
| 492 | - if ($attribute == 'id') |
|
| 493 | - { |
|
| 494 | - \DB::transaction(function () use ($value, $attribute, &$result) { |
|
| 495 | - $model = $this->model->lockForUpdate()->find($value); |
|
| 496 | - if ( ! $model) |
|
| 497 | - { |
|
| 498 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
| 499 | - } |
|
| 458 | + /** |
|
| 459 | + * Update record in the storage based on the given |
|
| 460 | + * condition. |
|
| 461 | + * |
|
| 462 | + * @param var $value condition value |
|
| 463 | + * @param array $data |
|
| 464 | + * @param string $attribute condition column name |
|
| 465 | + * @return void |
|
| 466 | + */ |
|
| 467 | + public function update($value, array $data, $attribute = 'id') |
|
| 468 | + { |
|
| 469 | + if ($attribute == 'id') |
|
| 470 | + { |
|
| 471 | + $model = $this->model->lockForUpdate()->find($value); |
|
| 472 | + $model ? $model->update($data) : 0; |
|
| 473 | + } |
|
| 474 | + else |
|
| 475 | + { |
|
| 476 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data){ |
|
| 477 | + $model->update($data); |
|
| 478 | + }); |
|
| 479 | + } |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + /** |
|
| 483 | + * Delete record from the storage based on the given |
|
| 484 | + * condition. |
|
| 485 | + * |
|
| 486 | + * @param var $value condition value |
|
| 487 | + * @param string $attribute condition column name |
|
| 488 | + * @return void |
|
| 489 | + */ |
|
| 490 | + public function delete($value, $attribute = 'id') |
|
| 491 | + { |
|
| 492 | + if ($attribute == 'id') |
|
| 493 | + { |
|
| 494 | + \DB::transaction(function () use ($value, $attribute, &$result) { |
|
| 495 | + $model = $this->model->lockForUpdate()->find($value); |
|
| 496 | + if ( ! $model) |
|
| 497 | + { |
|
| 498 | + \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
| 499 | + } |
|
| 500 | 500 | |
| 501 | - $model->delete(); |
|
| 502 | - }); |
|
| 503 | - } |
|
| 504 | - else |
|
| 505 | - { |
|
| 506 | - \DB::transaction(function () use ($value, $attribute, &$result) { |
|
| 507 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
|
| 508 | - $model->delete(); |
|
| 509 | - }); |
|
| 510 | - }); |
|
| 511 | - } |
|
| 512 | - } |
|
| 501 | + $model->delete(); |
|
| 502 | + }); |
|
| 503 | + } |
|
| 504 | + else |
|
| 505 | + { |
|
| 506 | + \DB::transaction(function () use ($value, $attribute, &$result) { |
|
| 507 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
|
| 508 | + $model->delete(); |
|
| 509 | + }); |
|
| 510 | + }); |
|
| 511 | + } |
|
| 512 | + } |
|
| 513 | 513 | |
| 514 | - /** |
|
| 515 | - * Fetch records from the storage based on the given |
|
| 516 | - * id. |
|
| 517 | - * |
|
| 518 | - * @param integer $id |
|
| 519 | - * @param array $relations |
|
| 520 | - * @param array $columns |
|
| 521 | - * @return object |
|
| 522 | - */ |
|
| 523 | - public function find($id, $relations = [], $columns = array('*')) |
|
| 524 | - { |
|
| 525 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
| 526 | - } |
|
| 514 | + /** |
|
| 515 | + * Fetch records from the storage based on the given |
|
| 516 | + * id. |
|
| 517 | + * |
|
| 518 | + * @param integer $id |
|
| 519 | + * @param array $relations |
|
| 520 | + * @param array $columns |
|
| 521 | + * @return object |
|
| 522 | + */ |
|
| 523 | + public function find($id, $relations = [], $columns = array('*')) |
|
| 524 | + { |
|
| 525 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
| 526 | + } |
|
| 527 | 527 | |
| 528 | - /** |
|
| 529 | - * Fetch records from the storage based on the given |
|
| 530 | - * condition. |
|
| 531 | - * |
|
| 532 | - * @param array $conditions array of conditions |
|
| 533 | - * @param array $relations |
|
| 534 | - * @param string $sortBy |
|
| 535 | - * @param boolean $desc |
|
| 536 | - * @param array $columns |
|
| 537 | - * @return collection |
|
| 538 | - */ |
|
| 539 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 540 | - { |
|
| 541 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 542 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 543 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - /** |
|
| 547 | - * Fetch the first record from the storage based on the given |
|
| 548 | - * condition. |
|
| 549 | - * |
|
| 550 | - * @param array $conditions array of conditions |
|
| 551 | - * @param array $relations |
|
| 552 | - * @param array $columns |
|
| 553 | - * @return object |
|
| 554 | - */ |
|
| 555 | - public function first($conditions, $relations = [], $columns = array('*')) |
|
| 556 | - { |
|
| 557 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 558 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - /** |
|
| 562 | - * Return the deleted models in pages based on the given conditions. |
|
| 563 | - * |
|
| 564 | - * @param array $conditions array of conditions |
|
| 565 | - * @param integer $perPage |
|
| 566 | - * @param string $sortBy |
|
| 567 | - * @param boolean $desc |
|
| 568 | - * @param array $columns |
|
| 569 | - * @return collection |
|
| 570 | - */ |
|
| 571 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 572 | - { |
|
| 573 | - unset($conditions['page']); |
|
| 574 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 575 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 576 | - $model = $this->model->onlyTrashed(); |
|
| 577 | - |
|
| 578 | - if (count($conditions['conditionValues'])) |
|
| 579 | - { |
|
| 580 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 581 | - } |
|
| 582 | - |
|
| 583 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
| 584 | - } |
|
| 585 | - |
|
| 586 | - /** |
|
| 587 | - * Restore the deleted model. |
|
| 588 | - * |
|
| 589 | - * @param integer $id |
|
| 590 | - * @return void |
|
| 591 | - */ |
|
| 592 | - public function restore($id) |
|
| 593 | - { |
|
| 594 | - $model = $this->model->onlyTrashed()->find($id); |
|
| 595 | - |
|
| 596 | - if ( ! $model) |
|
| 597 | - { |
|
| 598 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
| 599 | - } |
|
| 600 | - |
|
| 601 | - $model->restore(); |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - /** |
|
| 605 | - * Build the conditions recursively for the retrieving methods. |
|
| 606 | - * @param array $conditions |
|
| 607 | - * @return array |
|
| 608 | - */ |
|
| 609 | - protected function constructConditions($conditions, $model) |
|
| 610 | - { |
|
| 611 | - $conditionString = ''; |
|
| 612 | - $conditionValues = []; |
|
| 613 | - foreach ($conditions as $key => $value) |
|
| 614 | - { |
|
| 615 | - if (str_contains($key, '->')) |
|
| 616 | - { |
|
| 617 | - $key = $this->wrapJsonSelector($key); |
|
| 618 | - } |
|
| 619 | - |
|
| 620 | - if ($key == 'and') |
|
| 621 | - { |
|
| 622 | - $conditions = $this->constructConditions($value, $model); |
|
| 623 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
|
| 624 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 625 | - } |
|
| 626 | - else if ($key == 'or') |
|
| 627 | - { |
|
| 628 | - $conditions = $this->constructConditions($value, $model); |
|
| 629 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
|
| 630 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 631 | - } |
|
| 632 | - else |
|
| 633 | - { |
|
| 634 | - if (is_array($value)) |
|
| 635 | - { |
|
| 636 | - $operator = $value['op']; |
|
| 637 | - if (strtolower($operator) == 'between') |
|
| 638 | - { |
|
| 639 | - $value1 = $value['val1']; |
|
| 640 | - $value2 = $value['val2']; |
|
| 641 | - } |
|
| 642 | - else |
|
| 643 | - { |
|
| 644 | - $value = array_key_exists('val', $value) ? $value['val'] : ''; |
|
| 645 | - } |
|
| 646 | - } |
|
| 647 | - else |
|
| 648 | - { |
|
| 649 | - $operator = '='; |
|
| 650 | - } |
|
| 528 | + /** |
|
| 529 | + * Fetch records from the storage based on the given |
|
| 530 | + * condition. |
|
| 531 | + * |
|
| 532 | + * @param array $conditions array of conditions |
|
| 533 | + * @param array $relations |
|
| 534 | + * @param string $sortBy |
|
| 535 | + * @param boolean $desc |
|
| 536 | + * @param array $columns |
|
| 537 | + * @return collection |
|
| 538 | + */ |
|
| 539 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 540 | + { |
|
| 541 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 542 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 543 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + /** |
|
| 547 | + * Fetch the first record from the storage based on the given |
|
| 548 | + * condition. |
|
| 549 | + * |
|
| 550 | + * @param array $conditions array of conditions |
|
| 551 | + * @param array $relations |
|
| 552 | + * @param array $columns |
|
| 553 | + * @return object |
|
| 554 | + */ |
|
| 555 | + public function first($conditions, $relations = [], $columns = array('*')) |
|
| 556 | + { |
|
| 557 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 558 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + /** |
|
| 562 | + * Return the deleted models in pages based on the given conditions. |
|
| 563 | + * |
|
| 564 | + * @param array $conditions array of conditions |
|
| 565 | + * @param integer $perPage |
|
| 566 | + * @param string $sortBy |
|
| 567 | + * @param boolean $desc |
|
| 568 | + * @param array $columns |
|
| 569 | + * @return collection |
|
| 570 | + */ |
|
| 571 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 572 | + { |
|
| 573 | + unset($conditions['page']); |
|
| 574 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 575 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 576 | + $model = $this->model->onlyTrashed(); |
|
| 577 | + |
|
| 578 | + if (count($conditions['conditionValues'])) |
|
| 579 | + { |
|
| 580 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 581 | + } |
|
| 582 | + |
|
| 583 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
| 584 | + } |
|
| 585 | + |
|
| 586 | + /** |
|
| 587 | + * Restore the deleted model. |
|
| 588 | + * |
|
| 589 | + * @param integer $id |
|
| 590 | + * @return void |
|
| 591 | + */ |
|
| 592 | + public function restore($id) |
|
| 593 | + { |
|
| 594 | + $model = $this->model->onlyTrashed()->find($id); |
|
| 595 | + |
|
| 596 | + if ( ! $model) |
|
| 597 | + { |
|
| 598 | + \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
| 599 | + } |
|
| 600 | + |
|
| 601 | + $model->restore(); |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + /** |
|
| 605 | + * Build the conditions recursively for the retrieving methods. |
|
| 606 | + * @param array $conditions |
|
| 607 | + * @return array |
|
| 608 | + */ |
|
| 609 | + protected function constructConditions($conditions, $model) |
|
| 610 | + { |
|
| 611 | + $conditionString = ''; |
|
| 612 | + $conditionValues = []; |
|
| 613 | + foreach ($conditions as $key => $value) |
|
| 614 | + { |
|
| 615 | + if (str_contains($key, '->')) |
|
| 616 | + { |
|
| 617 | + $key = $this->wrapJsonSelector($key); |
|
| 618 | + } |
|
| 619 | + |
|
| 620 | + if ($key == 'and') |
|
| 621 | + { |
|
| 622 | + $conditions = $this->constructConditions($value, $model); |
|
| 623 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
|
| 624 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 625 | + } |
|
| 626 | + else if ($key == 'or') |
|
| 627 | + { |
|
| 628 | + $conditions = $this->constructConditions($value, $model); |
|
| 629 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
|
| 630 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 631 | + } |
|
| 632 | + else |
|
| 633 | + { |
|
| 634 | + if (is_array($value)) |
|
| 635 | + { |
|
| 636 | + $operator = $value['op']; |
|
| 637 | + if (strtolower($operator) == 'between') |
|
| 638 | + { |
|
| 639 | + $value1 = $value['val1']; |
|
| 640 | + $value2 = $value['val2']; |
|
| 641 | + } |
|
| 642 | + else |
|
| 643 | + { |
|
| 644 | + $value = array_key_exists('val', $value) ? $value['val'] : ''; |
|
| 645 | + } |
|
| 646 | + } |
|
| 647 | + else |
|
| 648 | + { |
|
| 649 | + $operator = '='; |
|
| 650 | + } |
|
| 651 | 651 | |
| 652 | - if (strtolower($operator) == 'between') |
|
| 653 | - { |
|
| 654 | - $conditionString .= $key . ' >= ? and '; |
|
| 655 | - $conditionValues[] = $value1; |
|
| 656 | - |
|
| 657 | - $conditionString .= $key . ' <= ? {op} '; |
|
| 658 | - $conditionValues[] = $value2; |
|
| 659 | - } |
|
| 660 | - elseif (strtolower($operator) == 'in') |
|
| 661 | - { |
|
| 662 | - $conditionValues = array_merge($conditionValues, $value); |
|
| 663 | - $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
| 664 | - $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
|
| 665 | - } |
|
| 666 | - elseif (strtolower($operator) == 'null') |
|
| 667 | - { |
|
| 668 | - $conditionString .= $key . ' is null {op} '; |
|
| 669 | - } |
|
| 670 | - elseif (strtolower($operator) == 'not null') |
|
| 671 | - { |
|
| 672 | - $conditionString .= $key . ' is not null {op} '; |
|
| 673 | - } |
|
| 674 | - elseif (strtolower($operator) == 'has') |
|
| 675 | - { |
|
| 676 | - $sql = $model->withTrashed()->has($key)->toSql(); |
|
| 677 | - $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
| 678 | - $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
|
| 679 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 680 | - } |
|
| 681 | - else |
|
| 682 | - { |
|
| 683 | - $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
| 684 | - $conditionValues[] = $value; |
|
| 685 | - } |
|
| 686 | - } |
|
| 687 | - } |
|
| 688 | - $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
| 689 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
| 690 | - } |
|
| 691 | - |
|
| 692 | - /** |
|
| 693 | - * Wrap the given JSON selector. |
|
| 694 | - * |
|
| 695 | - * @param string $value |
|
| 696 | - * @return string |
|
| 697 | - */ |
|
| 698 | - protected function wrapJsonSelector($value) |
|
| 699 | - { |
|
| 700 | - $removeLast = strpos($value, ')'); |
|
| 701 | - $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
| 702 | - $path = explode('->', $value); |
|
| 703 | - $field = array_shift($path); |
|
| 704 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
| 705 | - return '"'.$part.'"'; |
|
| 706 | - })->implode('.')); |
|
| 652 | + if (strtolower($operator) == 'between') |
|
| 653 | + { |
|
| 654 | + $conditionString .= $key . ' >= ? and '; |
|
| 655 | + $conditionValues[] = $value1; |
|
| 656 | + |
|
| 657 | + $conditionString .= $key . ' <= ? {op} '; |
|
| 658 | + $conditionValues[] = $value2; |
|
| 659 | + } |
|
| 660 | + elseif (strtolower($operator) == 'in') |
|
| 661 | + { |
|
| 662 | + $conditionValues = array_merge($conditionValues, $value); |
|
| 663 | + $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
| 664 | + $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
|
| 665 | + } |
|
| 666 | + elseif (strtolower($operator) == 'null') |
|
| 667 | + { |
|
| 668 | + $conditionString .= $key . ' is null {op} '; |
|
| 669 | + } |
|
| 670 | + elseif (strtolower($operator) == 'not null') |
|
| 671 | + { |
|
| 672 | + $conditionString .= $key . ' is not null {op} '; |
|
| 673 | + } |
|
| 674 | + elseif (strtolower($operator) == 'has') |
|
| 675 | + { |
|
| 676 | + $sql = $model->withTrashed()->has($key)->toSql(); |
|
| 677 | + $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
| 678 | + $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
|
| 679 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 680 | + } |
|
| 681 | + else |
|
| 682 | + { |
|
| 683 | + $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
| 684 | + $conditionValues[] = $value; |
|
| 685 | + } |
|
| 686 | + } |
|
| 687 | + } |
|
| 688 | + $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
| 689 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
| 690 | + } |
|
| 691 | + |
|
| 692 | + /** |
|
| 693 | + * Wrap the given JSON selector. |
|
| 694 | + * |
|
| 695 | + * @param string $value |
|
| 696 | + * @return string |
|
| 697 | + */ |
|
| 698 | + protected function wrapJsonSelector($value) |
|
| 699 | + { |
|
| 700 | + $removeLast = strpos($value, ')'); |
|
| 701 | + $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
| 702 | + $path = explode('->', $value); |
|
| 703 | + $field = array_shift($path); |
|
| 704 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
| 705 | + return '"'.$part.'"'; |
|
| 706 | + })->implode('.')); |
|
| 707 | 707 | |
| 708 | - return $removeLast === false ? $result : $result . ')'; |
|
| 709 | - } |
|
| 710 | - |
|
| 711 | - /** |
|
| 712 | - * Abstract method that return the necessary |
|
| 713 | - * information (full model namespace) |
|
| 714 | - * needed to preform the previous actions. |
|
| 715 | - * |
|
| 716 | - * @return string |
|
| 717 | - */ |
|
| 718 | - abstract protected function getModel(); |
|
| 708 | + return $removeLast === false ? $result : $result . ')'; |
|
| 709 | + } |
|
| 710 | + |
|
| 711 | + /** |
|
| 712 | + * Abstract method that return the necessary |
|
| 713 | + * information (full model namespace) |
|
| 714 | + * needed to preform the previous actions. |
|
| 715 | + * |
|
| 716 | + * @return string |
|
| 717 | + */ |
|
| 718 | + abstract protected function getModel(); |
|
| 719 | 719 | } |
| 720 | 720 | \ No newline at end of file |
@@ -6,33 +6,33 @@ |
||
| 6 | 6 | |
| 7 | 7 | class CreateOauthClientsTable extends Migration |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Run the migrations. |
|
| 11 | - * |
|
| 12 | - * @return void |
|
| 13 | - */ |
|
| 14 | - public function up() |
|
| 15 | - { |
|
| 16 | - Schema::create('oauth_clients', function (Blueprint $table) { |
|
| 17 | - $table->increments('id'); |
|
| 18 | - $table->integer('user_id')->index()->nullable(); |
|
| 19 | - $table->string('name'); |
|
| 20 | - $table->string('secret', 100); |
|
| 21 | - $table->text('redirect'); |
|
| 22 | - $table->boolean('personal_access_client')->default(0); |
|
| 23 | - $table->boolean('password_client')->default(0); |
|
| 24 | - $table->boolean('revoked')->default(0); |
|
| 25 | - $table->timestamps(); |
|
| 26 | - }); |
|
| 27 | - } |
|
| 9 | + /** |
|
| 10 | + * Run the migrations. |
|
| 11 | + * |
|
| 12 | + * @return void |
|
| 13 | + */ |
|
| 14 | + public function up() |
|
| 15 | + { |
|
| 16 | + Schema::create('oauth_clients', function (Blueprint $table) { |
|
| 17 | + $table->increments('id'); |
|
| 18 | + $table->integer('user_id')->index()->nullable(); |
|
| 19 | + $table->string('name'); |
|
| 20 | + $table->string('secret', 100); |
|
| 21 | + $table->text('redirect'); |
|
| 22 | + $table->boolean('personal_access_client')->default(0); |
|
| 23 | + $table->boolean('password_client')->default(0); |
|
| 24 | + $table->boolean('revoked')->default(0); |
|
| 25 | + $table->timestamps(); |
|
| 26 | + }); |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Reverse the migrations. |
|
| 31 | - * |
|
| 32 | - * @return void |
|
| 33 | - */ |
|
| 34 | - public function down() |
|
| 35 | - { |
|
| 36 | - Schema::drop('oauth_clients'); |
|
| 37 | - } |
|
| 29 | + /** |
|
| 30 | + * Reverse the migrations. |
|
| 31 | + * |
|
| 32 | + * @return void |
|
| 33 | + */ |
|
| 34 | + public function down() |
|
| 35 | + { |
|
| 36 | + Schema::drop('oauth_clients'); |
|
| 37 | + } |
|
| 38 | 38 | } |
@@ -13,28 +13,28 @@ |
||
| 13 | 13 | public function up() |
| 14 | 14 | { |
| 15 | 15 | Schema::create('users', function (Blueprint $table) { |
| 16 | - $table->increments('id'); |
|
| 17 | - $table->string('name', 100)->nullable(); |
|
| 18 | - $table->string('email')->unique(); |
|
| 19 | - $table->string('password', 60)->nullable(); |
|
| 20 | - $table->boolean('blocked')->default(0); |
|
| 21 | - $table->softDeletes(); |
|
| 22 | - $table->rememberToken(); |
|
| 23 | - $table->timestamps(); |
|
| 24 | - }); |
|
| 16 | + $table->increments('id'); |
|
| 17 | + $table->string('name', 100)->nullable(); |
|
| 18 | + $table->string('email')->unique(); |
|
| 19 | + $table->string('password', 60)->nullable(); |
|
| 20 | + $table->boolean('blocked')->default(0); |
|
| 21 | + $table->softDeletes(); |
|
| 22 | + $table->rememberToken(); |
|
| 23 | + $table->timestamps(); |
|
| 24 | + }); |
|
| 25 | 25 | |
| 26 | 26 | /** |
| 27 | 27 | * Create Default users. |
| 28 | 28 | */ |
| 29 | 29 | \DB::table('users')->insertGetId( |
| 30 | - [ |
|
| 30 | + [ |
|
| 31 | 31 | 'name' => 'Admin', |
| 32 | 32 | 'email' => '[email protected]', |
| 33 | 33 | 'password' => bcrypt('123456'), |
| 34 | 34 | 'created_at' => \DB::raw('NOW()'), |
| 35 | 35 | 'updated_at' => \DB::raw('NOW()') |
| 36 | 36 | ] |
| 37 | - ); |
|
| 37 | + ); |
|
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | /** |
@@ -6,82 +6,82 @@ |
||
| 6 | 6 | |
| 7 | 7 | class OauthClientsTableSeeder extends Seeder |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Run the database seeds. |
|
| 11 | - * |
|
| 12 | - * @return void |
|
| 13 | - */ |
|
| 14 | - public function run() |
|
| 15 | - { |
|
| 16 | - /** |
|
| 17 | - * Insert the permissions related to oauthClients table. |
|
| 18 | - */ |
|
| 19 | - \DB::table('permissions')->insert( |
|
| 20 | - [ |
|
| 21 | - /** |
|
| 22 | - * Users model permissions. |
|
| 23 | - */ |
|
| 24 | - [ |
|
| 25 | - 'name' => 'list', |
|
| 26 | - 'model' => 'oauthClients', |
|
| 27 | - 'created_at' => \DB::raw('NOW()'), |
|
| 28 | - 'updated_at' => \DB::raw('NOW()') |
|
| 29 | - ], |
|
| 30 | - [ |
|
| 31 | - 'name' => 'find', |
|
| 32 | - 'model' => 'oauthClients', |
|
| 33 | - 'created_at' => \DB::raw('NOW()'), |
|
| 34 | - 'updated_at' => \DB::raw('NOW()') |
|
| 35 | - ], |
|
| 36 | - [ |
|
| 37 | - 'name' => 'search', |
|
| 38 | - 'model' => 'oauthClients', |
|
| 39 | - 'created_at' => \DB::raw('NOW()'), |
|
| 40 | - 'updated_at' => \DB::raw('NOW()') |
|
| 41 | - ], |
|
| 42 | - [ |
|
| 43 | - 'name' => 'paginate', |
|
| 44 | - 'model' => 'oauthClients', |
|
| 45 | - 'created_at' => \DB::raw('NOW()'), |
|
| 46 | - 'updated_at' => \DB::raw('NOW()') |
|
| 47 | - ], |
|
| 48 | - [ |
|
| 49 | - 'name' => 'revoke', |
|
| 50 | - 'model' => 'oauthClients', |
|
| 51 | - 'created_at' => \DB::raw('NOW()'), |
|
| 52 | - 'updated_at' => \DB::raw('NOW()') |
|
| 53 | - ], |
|
| 54 | - [ |
|
| 55 | - 'name' => 'unRevoke', |
|
| 56 | - 'model' => 'oauthClients', |
|
| 57 | - 'created_at' => \DB::raw('NOW()'), |
|
| 58 | - 'updated_at' => \DB::raw('NOW()') |
|
| 59 | - ], |
|
| 60 | - [ |
|
| 61 | - 'name' => 'first', |
|
| 62 | - 'model' => 'oauthClients', |
|
| 63 | - 'created_at' => \DB::raw('NOW()'), |
|
| 64 | - 'updated_at' => \DB::raw('NOW()') |
|
| 65 | - ], |
|
| 66 | - [ |
|
| 67 | - 'name' => 'findby', |
|
| 68 | - 'model' => 'oauthClients', |
|
| 69 | - 'created_at' => \DB::raw('NOW()'), |
|
| 70 | - 'updated_at' => \DB::raw('NOW()') |
|
| 71 | - ], |
|
| 72 | - [ |
|
| 73 | - 'name' => 'paginateby', |
|
| 74 | - 'model' => 'oauthClients', |
|
| 75 | - 'created_at' => \DB::raw('NOW()'), |
|
| 76 | - 'updated_at' => \DB::raw('NOW()') |
|
| 77 | - ], |
|
| 78 | - [ |
|
| 79 | - 'name' => 'save', |
|
| 80 | - 'model' => 'oauthClients', |
|
| 81 | - 'created_at' => \DB::raw('NOW()'), |
|
| 82 | - 'updated_at' => \DB::raw('NOW()') |
|
| 83 | - ] |
|
| 84 | - ] |
|
| 85 | - ); |
|
| 86 | - } |
|
| 9 | + /** |
|
| 10 | + * Run the database seeds. |
|
| 11 | + * |
|
| 12 | + * @return void |
|
| 13 | + */ |
|
| 14 | + public function run() |
|
| 15 | + { |
|
| 16 | + /** |
|
| 17 | + * Insert the permissions related to oauthClients table. |
|
| 18 | + */ |
|
| 19 | + \DB::table('permissions')->insert( |
|
| 20 | + [ |
|
| 21 | + /** |
|
| 22 | + * Users model permissions. |
|
| 23 | + */ |
|
| 24 | + [ |
|
| 25 | + 'name' => 'list', |
|
| 26 | + 'model' => 'oauthClients', |
|
| 27 | + 'created_at' => \DB::raw('NOW()'), |
|
| 28 | + 'updated_at' => \DB::raw('NOW()') |
|
| 29 | + ], |
|
| 30 | + [ |
|
| 31 | + 'name' => 'find', |
|
| 32 | + 'model' => 'oauthClients', |
|
| 33 | + 'created_at' => \DB::raw('NOW()'), |
|
| 34 | + 'updated_at' => \DB::raw('NOW()') |
|
| 35 | + ], |
|
| 36 | + [ |
|
| 37 | + 'name' => 'search', |
|
| 38 | + 'model' => 'oauthClients', |
|
| 39 | + 'created_at' => \DB::raw('NOW()'), |
|
| 40 | + 'updated_at' => \DB::raw('NOW()') |
|
| 41 | + ], |
|
| 42 | + [ |
|
| 43 | + 'name' => 'paginate', |
|
| 44 | + 'model' => 'oauthClients', |
|
| 45 | + 'created_at' => \DB::raw('NOW()'), |
|
| 46 | + 'updated_at' => \DB::raw('NOW()') |
|
| 47 | + ], |
|
| 48 | + [ |
|
| 49 | + 'name' => 'revoke', |
|
| 50 | + 'model' => 'oauthClients', |
|
| 51 | + 'created_at' => \DB::raw('NOW()'), |
|
| 52 | + 'updated_at' => \DB::raw('NOW()') |
|
| 53 | + ], |
|
| 54 | + [ |
|
| 55 | + 'name' => 'unRevoke', |
|
| 56 | + 'model' => 'oauthClients', |
|
| 57 | + 'created_at' => \DB::raw('NOW()'), |
|
| 58 | + 'updated_at' => \DB::raw('NOW()') |
|
| 59 | + ], |
|
| 60 | + [ |
|
| 61 | + 'name' => 'first', |
|
| 62 | + 'model' => 'oauthClients', |
|
| 63 | + 'created_at' => \DB::raw('NOW()'), |
|
| 64 | + 'updated_at' => \DB::raw('NOW()') |
|
| 65 | + ], |
|
| 66 | + [ |
|
| 67 | + 'name' => 'findby', |
|
| 68 | + 'model' => 'oauthClients', |
|
| 69 | + 'created_at' => \DB::raw('NOW()'), |
|
| 70 | + 'updated_at' => \DB::raw('NOW()') |
|
| 71 | + ], |
|
| 72 | + [ |
|
| 73 | + 'name' => 'paginateby', |
|
| 74 | + 'model' => 'oauthClients', |
|
| 75 | + 'created_at' => \DB::raw('NOW()'), |
|
| 76 | + 'updated_at' => \DB::raw('NOW()') |
|
| 77 | + ], |
|
| 78 | + [ |
|
| 79 | + 'name' => 'save', |
|
| 80 | + 'model' => 'oauthClients', |
|
| 81 | + 'created_at' => \DB::raw('NOW()'), |
|
| 82 | + 'updated_at' => \DB::raw('NOW()') |
|
| 83 | + ] |
|
| 84 | + ] |
|
| 85 | + ); |
|
| 86 | + } |
|
| 87 | 87 | } |
@@ -5,56 +5,56 @@ |
||
| 5 | 5 | */ |
| 6 | 6 | class AclUserObserver { |
| 7 | 7 | |
| 8 | - public function saving($model) |
|
| 9 | - { |
|
| 10 | - // |
|
| 11 | - } |
|
| 12 | - |
|
| 13 | - public function saved($model) |
|
| 14 | - { |
|
| 15 | - // |
|
| 16 | - } |
|
| 17 | - |
|
| 18 | - public function creating($model) |
|
| 19 | - { |
|
| 20 | - // |
|
| 21 | - } |
|
| 22 | - |
|
| 23 | - public function created($model) |
|
| 24 | - { |
|
| 25 | - // |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - public function updating($model) |
|
| 29 | - { |
|
| 30 | - // |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - public function updated($model) |
|
| 34 | - { |
|
| 35 | - // |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - public function deleting($model) |
|
| 39 | - { |
|
| 40 | - if ($model->getOriginal('id') == \Auth::id()) |
|
| 41 | - { |
|
| 42 | - \ErrorHandler::noPermissions(); |
|
| 43 | - } |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - public function deleted($model) |
|
| 47 | - { |
|
| 48 | - // |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - public function restoring($model) |
|
| 52 | - { |
|
| 53 | - $model->logs()->restore(); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - public function restored($model) |
|
| 57 | - { |
|
| 58 | - // |
|
| 59 | - } |
|
| 8 | + public function saving($model) |
|
| 9 | + { |
|
| 10 | + // |
|
| 11 | + } |
|
| 12 | + |
|
| 13 | + public function saved($model) |
|
| 14 | + { |
|
| 15 | + // |
|
| 16 | + } |
|
| 17 | + |
|
| 18 | + public function creating($model) |
|
| 19 | + { |
|
| 20 | + // |
|
| 21 | + } |
|
| 22 | + |
|
| 23 | + public function created($model) |
|
| 24 | + { |
|
| 25 | + // |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + public function updating($model) |
|
| 29 | + { |
|
| 30 | + // |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + public function updated($model) |
|
| 34 | + { |
|
| 35 | + // |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + public function deleting($model) |
|
| 39 | + { |
|
| 40 | + if ($model->getOriginal('id') == \Auth::id()) |
|
| 41 | + { |
|
| 42 | + \ErrorHandler::noPermissions(); |
|
| 43 | + } |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + public function deleted($model) |
|
| 47 | + { |
|
| 48 | + // |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + public function restoring($model) |
|
| 52 | + { |
|
| 53 | + $model->logs()->restore(); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + public function restored($model) |
|
| 57 | + { |
|
| 58 | + // |
|
| 59 | + } |
|
| 60 | 60 | } |
| 61 | 61 | \ No newline at end of file |
@@ -4,37 +4,37 @@ |
||
| 4 | 4 | |
| 5 | 5 | class OauthClientRepository extends AbstractRepository |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * Return the model full namespace. |
|
| 9 | - * |
|
| 10 | - * @return string |
|
| 11 | - */ |
|
| 12 | - protected function getModel() |
|
| 13 | - { |
|
| 14 | - return 'App\Modules\V1\Acl\OauthClient'; |
|
| 15 | - } |
|
| 7 | + /** |
|
| 8 | + * Return the model full namespace. |
|
| 9 | + * |
|
| 10 | + * @return string |
|
| 11 | + */ |
|
| 12 | + protected function getModel() |
|
| 13 | + { |
|
| 14 | + return 'App\Modules\V1\Acl\OauthClient'; |
|
| 15 | + } |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Revoke the given client. |
|
| 19 | - * |
|
| 20 | - * @param integer $clientId |
|
| 21 | - * @return void |
|
| 22 | - */ |
|
| 23 | - public function revoke($clientId) |
|
| 24 | - { |
|
| 25 | - $client = $this->find($clientId); |
|
| 26 | - $client->tokens()->update(['revoked' => true]); |
|
| 27 | - $this->save(['id'=> $clientId, 'revoked' => true]); |
|
| 28 | - } |
|
| 17 | + /** |
|
| 18 | + * Revoke the given client. |
|
| 19 | + * |
|
| 20 | + * @param integer $clientId |
|
| 21 | + * @return void |
|
| 22 | + */ |
|
| 23 | + public function revoke($clientId) |
|
| 24 | + { |
|
| 25 | + $client = $this->find($clientId); |
|
| 26 | + $client->tokens()->update(['revoked' => true]); |
|
| 27 | + $this->save(['id'=> $clientId, 'revoked' => true]); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Un revoke the given client. |
|
| 32 | - * |
|
| 33 | - * @param integer $clientId |
|
| 34 | - * @return void |
|
| 35 | - */ |
|
| 36 | - public function unRevoke($clientId) |
|
| 37 | - { |
|
| 38 | - $this->save(['id'=> $clientId, 'revoked' => false]); |
|
| 39 | - } |
|
| 30 | + /** |
|
| 31 | + * Un revoke the given client. |
|
| 32 | + * |
|
| 33 | + * @param integer $clientId |
|
| 34 | + * @return void |
|
| 35 | + */ |
|
| 36 | + public function unRevoke($clientId) |
|
| 37 | + { |
|
| 38 | + $this->save(['id'=> $clientId, 'revoked' => false]); |
|
| 39 | + } |
|
| 40 | 40 | } |
@@ -8,250 +8,250 @@ |
||
| 8 | 8 | |
| 9 | 9 | class UsersController extends BaseApiController |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * The name of the model that is used by the base api controller |
|
| 13 | - * to preform actions like (add, edit ... etc). |
|
| 14 | - * @var string |
|
| 15 | - */ |
|
| 16 | - protected $model = 'users'; |
|
| 11 | + /** |
|
| 12 | + * The name of the model that is used by the base api controller |
|
| 13 | + * to preform actions like (add, edit ... etc). |
|
| 14 | + * @var string |
|
| 15 | + */ |
|
| 16 | + protected $model = 'users'; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * List of all route actions that the base api controller |
|
| 20 | - * will skip permissions check for them. |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - protected $skipPermissionCheck = ['account', 'logout', 'changePassword', 'saveProfile', 'account']; |
|
| 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 | 24 | |
| 25 | - /** |
|
| 26 | - * List of all route actions that the base api controller |
|
| 27 | - * will skip login check for them. |
|
| 28 | - * @var array |
|
| 29 | - */ |
|
| 30 | - protected $skipLoginCheck = ['login', 'loginSocial', 'register', 'sendreset', 'resetpassword', 'refreshtoken']; |
|
| 25 | + /** |
|
| 26 | + * List of all route actions that the base api controller |
|
| 27 | + * will skip login check for them. |
|
| 28 | + * @var array |
|
| 29 | + */ |
|
| 30 | + protected $skipLoginCheck = ['login', 'loginSocial', 'register', 'sendreset', 'resetpassword', 'refreshtoken']; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * The validations rules used by the base api controller |
|
| 34 | - * to check before add. |
|
| 35 | - * @var array |
|
| 36 | - */ |
|
| 37 | - protected $validationRules = [ |
|
| 38 | - 'name' => 'nullable|string', |
|
| 39 | - 'email' => 'required|email|unique:users,email,{id}', |
|
| 40 | - 'password' => 'nullable|min:6' |
|
| 41 | - ]; |
|
| 32 | + /** |
|
| 33 | + * The validations rules used by the base api controller |
|
| 34 | + * to check before add. |
|
| 35 | + * @var array |
|
| 36 | + */ |
|
| 37 | + protected $validationRules = [ |
|
| 38 | + 'name' => 'nullable|string', |
|
| 39 | + 'email' => 'required|email|unique:users,email,{id}', |
|
| 40 | + 'password' => 'nullable|min:6' |
|
| 41 | + ]; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * The loginProxy implementation. |
|
| 45 | - * |
|
| 46 | - * @var \App\Modules\V1\Acl\Proxy\LoginProxy |
|
| 47 | - */ |
|
| 48 | - protected $loginProxy; |
|
| 43 | + /** |
|
| 44 | + * The loginProxy implementation. |
|
| 45 | + * |
|
| 46 | + * @var \App\Modules\V1\Acl\Proxy\LoginProxy |
|
| 47 | + */ |
|
| 48 | + protected $loginProxy; |
|
| 49 | 49 | |
| 50 | - public function __construct(LoginProxy $loginProxy) |
|
| 51 | - { |
|
| 52 | - $this->loginProxy = $loginProxy; |
|
| 53 | - parent::__construct(); |
|
| 54 | - } |
|
| 50 | + public function __construct(LoginProxy $loginProxy) |
|
| 51 | + { |
|
| 52 | + $this->loginProxy = $loginProxy; |
|
| 53 | + parent::__construct(); |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Return the logged in user account. |
|
| 58 | - * |
|
| 59 | - * @return \Illuminate\Http\Response |
|
| 60 | - */ |
|
| 61 | - public function account() |
|
| 62 | - { |
|
| 63 | - return \Response::json($this->repo->account($this->relations), 200); |
|
| 64 | - } |
|
| 56 | + /** |
|
| 57 | + * Return the logged in user account. |
|
| 58 | + * |
|
| 59 | + * @return \Illuminate\Http\Response |
|
| 60 | + */ |
|
| 61 | + public function account() |
|
| 62 | + { |
|
| 63 | + return \Response::json($this->repo->account($this->relations), 200); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * Block the user. |
|
| 68 | - * |
|
| 69 | - * @param integer $id Id of the user. |
|
| 70 | - * @return \Illuminate\Http\Response |
|
| 71 | - */ |
|
| 72 | - public function block($id) |
|
| 73 | - { |
|
| 74 | - return \Response::json($this->repo->block($id), 200); |
|
| 75 | - } |
|
| 66 | + /** |
|
| 67 | + * Block the user. |
|
| 68 | + * |
|
| 69 | + * @param integer $id Id of the user. |
|
| 70 | + * @return \Illuminate\Http\Response |
|
| 71 | + */ |
|
| 72 | + public function block($id) |
|
| 73 | + { |
|
| 74 | + return \Response::json($this->repo->block($id), 200); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * Unblock the user. |
|
| 79 | - * |
|
| 80 | - * @param integer $id Id of the user. |
|
| 81 | - * @return \Illuminate\Http\Response |
|
| 82 | - */ |
|
| 83 | - public function unblock($id) |
|
| 84 | - { |
|
| 85 | - return \Response::json($this->repo->unblock($id), 200); |
|
| 86 | - } |
|
| 77 | + /** |
|
| 78 | + * Unblock the user. |
|
| 79 | + * |
|
| 80 | + * @param integer $id Id of the user. |
|
| 81 | + * @return \Illuminate\Http\Response |
|
| 82 | + */ |
|
| 83 | + public function unblock($id) |
|
| 84 | + { |
|
| 85 | + return \Response::json($this->repo->unblock($id), 200); |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * Logout the user. |
|
| 90 | - * |
|
| 91 | - * @return \Illuminate\Http\Response |
|
| 92 | - */ |
|
| 93 | - public function logout() |
|
| 94 | - { |
|
| 95 | - return \Response::json($this->loginProxy->logout(), 200); |
|
| 96 | - } |
|
| 88 | + /** |
|
| 89 | + * Logout the user. |
|
| 90 | + * |
|
| 91 | + * @return \Illuminate\Http\Response |
|
| 92 | + */ |
|
| 93 | + public function logout() |
|
| 94 | + { |
|
| 95 | + return \Response::json($this->loginProxy->logout(), 200); |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - /** |
|
| 99 | - * Handle a registration request. |
|
| 100 | - * |
|
| 101 | - * @param \Illuminate\Http\Request $request |
|
| 102 | - * @return \Illuminate\Http\Response |
|
| 103 | - */ |
|
| 104 | - public function register(Request $request) |
|
| 105 | - { |
|
| 106 | - $this->validate($request, [ |
|
| 107 | - 'name' => 'nullable|string', |
|
| 108 | - 'email' => 'required|email|unique:users,email,{id}', |
|
| 109 | - 'password' => 'required|min:6' |
|
| 110 | - ]); |
|
| 98 | + /** |
|
| 99 | + * Handle a registration request. |
|
| 100 | + * |
|
| 101 | + * @param \Illuminate\Http\Request $request |
|
| 102 | + * @return \Illuminate\Http\Response |
|
| 103 | + */ |
|
| 104 | + public function register(Request $request) |
|
| 105 | + { |
|
| 106 | + $this->validate($request, [ |
|
| 107 | + 'name' => 'nullable|string', |
|
| 108 | + 'email' => 'required|email|unique:users,email,{id}', |
|
| 109 | + 'password' => 'required|min:6' |
|
| 110 | + ]); |
|
| 111 | 111 | |
| 112 | - return \Response::json($this->repo->register($request->only('name', 'email', 'password')), 200); |
|
| 113 | - } |
|
| 112 | + return \Response::json($this->repo->register($request->only('name', 'email', 'password')), 200); |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * Handle a login request to the application. |
|
| 117 | - * |
|
| 118 | - * @param \Illuminate\Http\Request $request |
|
| 119 | - * @return \Illuminate\Http\Response |
|
| 120 | - */ |
|
| 121 | - public function login(Request $request) |
|
| 122 | - { |
|
| 123 | - $this->validate($request, [ |
|
| 124 | - 'email' => 'required|email', |
|
| 125 | - 'password' => 'required|min:6' |
|
| 126 | - ]); |
|
| 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 | + ]); |
|
| 127 | 127 | |
| 128 | - return \Response::json($this->loginProxy->login($request->only('email', 'password')), 200); |
|
| 129 | - } |
|
| 128 | + return \Response::json($this->loginProxy->login($request->only('email', 'password')), 200); |
|
| 129 | + } |
|
| 130 | 130 | |
| 131 | - /** |
|
| 132 | - * Handle a social login request of the none admin to the application. |
|
| 133 | - * |
|
| 134 | - * @param \Illuminate\Http\Request $request |
|
| 135 | - * @return \Illuminate\Http\Response |
|
| 136 | - */ |
|
| 137 | - public function loginSocial(Request $request) |
|
| 138 | - { |
|
| 139 | - $this->validate($request, [ |
|
| 140 | - 'auth_code' => 'required_without:access_token', |
|
| 141 | - 'access_token' => 'required_without:auth_code', |
|
| 142 | - 'type' => 'required|in:facebook,google' |
|
| 143 | - ]); |
|
| 131 | + /** |
|
| 132 | + * Handle a social login request of the none admin to the application. |
|
| 133 | + * |
|
| 134 | + * @param \Illuminate\Http\Request $request |
|
| 135 | + * @return \Illuminate\Http\Response |
|
| 136 | + */ |
|
| 137 | + public function loginSocial(Request $request) |
|
| 138 | + { |
|
| 139 | + $this->validate($request, [ |
|
| 140 | + 'auth_code' => 'required_without:access_token', |
|
| 141 | + 'access_token' => 'required_without:auth_code', |
|
| 142 | + 'type' => 'required|in:facebook,google' |
|
| 143 | + ]); |
|
| 144 | 144 | |
| 145 | - return \Response::json($this->repo->loginSocial($request->only('auth_code', 'access_token', 'type')), 200); |
|
| 146 | - } |
|
| 145 | + return \Response::json($this->repo->loginSocial($request->only('auth_code', 'access_token', 'type')), 200); |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | - /** |
|
| 149 | - * Assign the given groups to the given user. |
|
| 150 | - * |
|
| 151 | - * @param \Illuminate\Http\Request $request |
|
| 152 | - * @return \Illuminate\Http\Response |
|
| 153 | - */ |
|
| 154 | - public function assigngroups(Request $request) |
|
| 155 | - { |
|
| 156 | - $this->validate($request, [ |
|
| 157 | - 'group_ids' => 'required|exists:groups,id', |
|
| 158 | - 'user_id' => 'required|exists:users,id' |
|
| 159 | - ]); |
|
| 148 | + /** |
|
| 149 | + * Assign the given groups to the given user. |
|
| 150 | + * |
|
| 151 | + * @param \Illuminate\Http\Request $request |
|
| 152 | + * @return \Illuminate\Http\Response |
|
| 153 | + */ |
|
| 154 | + public function assigngroups(Request $request) |
|
| 155 | + { |
|
| 156 | + $this->validate($request, [ |
|
| 157 | + 'group_ids' => 'required|exists:groups,id', |
|
| 158 | + 'user_id' => 'required|exists:users,id' |
|
| 159 | + ]); |
|
| 160 | 160 | |
| 161 | - return \Response::json($this->repo->assignGroups($request->get('user_id'), $request->get('group_ids')), 200); |
|
| 162 | - } |
|
| 161 | + return \Response::json($this->repo->assignGroups($request->get('user_id'), $request->get('group_ids')), 200); |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - /** |
|
| 165 | - * Send a reset link to the given user. |
|
| 166 | - * |
|
| 167 | - * @param \Illuminate\Http\Request $request |
|
| 168 | - * @return \Illuminate\Http\Response |
|
| 169 | - */ |
|
| 170 | - public function sendreset(Request $request) |
|
| 171 | - { |
|
| 172 | - $this->validate($request, ['email' => 'required|email']); |
|
| 164 | + /** |
|
| 165 | + * Send a reset link to the given user. |
|
| 166 | + * |
|
| 167 | + * @param \Illuminate\Http\Request $request |
|
| 168 | + * @return \Illuminate\Http\Response |
|
| 169 | + */ |
|
| 170 | + public function sendreset(Request $request) |
|
| 171 | + { |
|
| 172 | + $this->validate($request, ['email' => 'required|email']); |
|
| 173 | 173 | |
| 174 | - return \Response::json($this->repo->sendReset($request->only('email')), 200); |
|
| 175 | - } |
|
| 174 | + return \Response::json($this->repo->sendReset($request->only('email')), 200); |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | - /** |
|
| 178 | - * Reset the given user's password. |
|
| 179 | - * |
|
| 180 | - * @param \Illuminate\Http\Request $request |
|
| 181 | - * @return \Illuminate\Http\Response |
|
| 182 | - */ |
|
| 183 | - public function resetpassword(Request $request) |
|
| 184 | - { |
|
| 185 | - $this->validate($request, [ |
|
| 186 | - 'token' => 'required', |
|
| 187 | - 'email' => 'required|email', |
|
| 188 | - 'password' => 'required|confirmed|min:6', |
|
| 189 | - 'password_confirmation' => 'required', |
|
| 190 | - ]); |
|
| 177 | + /** |
|
| 178 | + * Reset the given user's password. |
|
| 179 | + * |
|
| 180 | + * @param \Illuminate\Http\Request $request |
|
| 181 | + * @return \Illuminate\Http\Response |
|
| 182 | + */ |
|
| 183 | + public function resetpassword(Request $request) |
|
| 184 | + { |
|
| 185 | + $this->validate($request, [ |
|
| 186 | + 'token' => 'required', |
|
| 187 | + 'email' => 'required|email', |
|
| 188 | + 'password' => 'required|confirmed|min:6', |
|
| 189 | + 'password_confirmation' => 'required', |
|
| 190 | + ]); |
|
| 191 | 191 | |
| 192 | - return \Response::json($this->repo->resetPassword($request->only('email', 'password', 'password_confirmation', 'token')), 200); |
|
| 193 | - } |
|
| 192 | + return \Response::json($this->repo->resetPassword($request->only('email', 'password', 'password_confirmation', 'token')), 200); |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | - /** |
|
| 196 | - * Change the logged in user password. |
|
| 197 | - * |
|
| 198 | - * @param \Illuminate\Http\Request $request |
|
| 199 | - * @return \Illuminate\Http\Response |
|
| 200 | - */ |
|
| 201 | - public function changePassword(Request $request) |
|
| 202 | - { |
|
| 203 | - $this->validate($request, [ |
|
| 204 | - 'old_password' => 'required', |
|
| 205 | - 'password' => 'required|confirmed|min:6', |
|
| 206 | - 'password_confirmation' => 'required', |
|
| 207 | - ]); |
|
| 195 | + /** |
|
| 196 | + * Change the logged in user password. |
|
| 197 | + * |
|
| 198 | + * @param \Illuminate\Http\Request $request |
|
| 199 | + * @return \Illuminate\Http\Response |
|
| 200 | + */ |
|
| 201 | + public function changePassword(Request $request) |
|
| 202 | + { |
|
| 203 | + $this->validate($request, [ |
|
| 204 | + 'old_password' => 'required', |
|
| 205 | + 'password' => 'required|confirmed|min:6', |
|
| 206 | + 'password_confirmation' => 'required', |
|
| 207 | + ]); |
|
| 208 | 208 | |
| 209 | - return \Response::json($this->repo->changePassword($request->only('old_password', 'password', 'password_confirmation')), 200); |
|
| 210 | - } |
|
| 209 | + return \Response::json($this->repo->changePassword($request->only('old_password', 'password', 'password_confirmation')), 200); |
|
| 210 | + } |
|
| 211 | 211 | |
| 212 | - /** |
|
| 213 | - * Refresh the expired login token. |
|
| 214 | - * |
|
| 215 | - * @param \Illuminate\Http\Request $request |
|
| 216 | - * @return \Illuminate\Http\Response |
|
| 217 | - */ |
|
| 218 | - public function refreshtoken(Request $request) |
|
| 219 | - { |
|
| 220 | - $this->validate($request, [ |
|
| 221 | - 'refreshtoken' => 'required', |
|
| 222 | - ]); |
|
| 212 | + /** |
|
| 213 | + * Refresh the expired login token. |
|
| 214 | + * |
|
| 215 | + * @param \Illuminate\Http\Request $request |
|
| 216 | + * @return \Illuminate\Http\Response |
|
| 217 | + */ |
|
| 218 | + public function refreshtoken(Request $request) |
|
| 219 | + { |
|
| 220 | + $this->validate($request, [ |
|
| 221 | + 'refreshtoken' => 'required', |
|
| 222 | + ]); |
|
| 223 | 223 | |
| 224 | - return \Response::json($this->loginProxy->refreshtoken($request->get('refreshtoken')), 200); |
|
| 225 | - } |
|
| 224 | + return \Response::json($this->loginProxy->refreshtoken($request->get('refreshtoken')), 200); |
|
| 225 | + } |
|
| 226 | 226 | |
| 227 | - /** |
|
| 228 | - * Paginate all users with in the given group. |
|
| 229 | - * |
|
| 230 | - * @param \Illuminate\Http\Request $request |
|
| 231 | - * @param string $groupName The name of the requested group. |
|
| 232 | - * @param integer $perPage Number of rows per page default 15. |
|
| 233 | - * @param string $sortBy The name of the column to sort by. |
|
| 234 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
| 235 | - * @return \Illuminate\Http\Response |
|
| 236 | - */ |
|
| 237 | - public function group(Request $request, $groupName, $perPage = false, $sortBy = 'created_at', $desc = 1) |
|
| 238 | - { |
|
| 239 | - return \Response::json($this->repo->group($request->all(), $groupName, $this->relations, $perPage, $sortBy, $desc), 200); |
|
| 240 | - } |
|
| 227 | + /** |
|
| 228 | + * Paginate all users with in the given group. |
|
| 229 | + * |
|
| 230 | + * @param \Illuminate\Http\Request $request |
|
| 231 | + * @param string $groupName The name of the requested group. |
|
| 232 | + * @param integer $perPage Number of rows per page default 15. |
|
| 233 | + * @param string $sortBy The name of the column to sort by. |
|
| 234 | + * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
| 235 | + * @return \Illuminate\Http\Response |
|
| 236 | + */ |
|
| 237 | + public function group(Request $request, $groupName, $perPage = false, $sortBy = 'created_at', $desc = 1) |
|
| 238 | + { |
|
| 239 | + return \Response::json($this->repo->group($request->all(), $groupName, $this->relations, $perPage, $sortBy, $desc), 200); |
|
| 240 | + } |
|
| 241 | 241 | |
| 242 | - /** |
|
| 243 | - * Save the given data to the logged in user. |
|
| 244 | - * |
|
| 245 | - * @param \Illuminate\Http\Request $request |
|
| 246 | - * @return \Illuminate\Http\Response |
|
| 247 | - */ |
|
| 248 | - public function saveProfile(Request $request) |
|
| 249 | - { |
|
| 250 | - $this->validate($request, [ |
|
| 251 | - 'name' => 'nullable|string', |
|
| 252 | - 'email' => 'required|email|unique:users,email,' . \Auth::id() |
|
| 253 | - ]); |
|
| 242 | + /** |
|
| 243 | + * Save the given data to the logged in user. |
|
| 244 | + * |
|
| 245 | + * @param \Illuminate\Http\Request $request |
|
| 246 | + * @return \Illuminate\Http\Response |
|
| 247 | + */ |
|
| 248 | + public function saveProfile(Request $request) |
|
| 249 | + { |
|
| 250 | + $this->validate($request, [ |
|
| 251 | + 'name' => 'nullable|string', |
|
| 252 | + 'email' => 'required|email|unique:users,email,' . \Auth::id() |
|
| 253 | + ]); |
|
| 254 | 254 | |
| 255 | - return \Response::json($this->repo->saveProfile($request->only('name', 'email')), 200); |
|
| 256 | - } |
|
| 255 | + return \Response::json($this->repo->saveProfile($request->only('name', 'email')), 200); |
|
| 256 | + } |
|
| 257 | 257 | } |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | * to preform actions like (add, edit ... etc). |
| 14 | 14 | * @var string |
| 15 | 15 | */ |
| 16 | - protected $model = 'users'; |
|
| 16 | + protected $model = 'users'; |
|
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | 19 | * List of all route actions that the base api controller |
@@ -27,14 +27,14 @@ discard block |
||
| 27 | 27 | * will skip login check for them. |
| 28 | 28 | * @var array |
| 29 | 29 | */ |
| 30 | - protected $skipLoginCheck = ['login', 'loginSocial', 'register', 'sendreset', 'resetpassword', 'refreshtoken']; |
|
| 30 | + protected $skipLoginCheck = ['login', 'loginSocial', 'register', 'sendreset', 'resetpassword', 'refreshtoken']; |
|
| 31 | 31 | |
| 32 | 32 | /** |
| 33 | 33 | * The validations rules used by the base api controller |
| 34 | 34 | * to check before add. |
| 35 | 35 | * @var array |
| 36 | 36 | */ |
| 37 | - protected $validationRules = [ |
|
| 37 | + protected $validationRules = [ |
|
| 38 | 38 | 'name' => 'nullable|string', |
| 39 | 39 | 'email' => 'required|email|unique:users,email,{id}', |
| 40 | 40 | 'password' => 'nullable|min:6' |
@@ -249,7 +249,7 @@ discard block |
||
| 249 | 249 | { |
| 250 | 250 | $this->validate($request, [ |
| 251 | 251 | 'name' => 'nullable|string', |
| 252 | - 'email' => 'required|email|unique:users,email,' . \Auth::id() |
|
| 252 | + 'email' => 'required|email|unique:users,email,'.\Auth::id() |
|
| 253 | 253 | ]); |
| 254 | 254 | |
| 255 | 255 | return \Response::json($this->repo->saveProfile($request->only('name', 'email')), 200); |
@@ -8,44 +8,44 @@ |
||
| 8 | 8 | |
| 9 | 9 | class OauthClientsController extends BaseApiController |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * The name of the model that is used by the base api controller |
|
| 13 | - * to preform actions like (add, edit ... etc). |
|
| 14 | - * @var string |
|
| 15 | - */ |
|
| 16 | - protected $model = 'oauthClients'; |
|
| 11 | + /** |
|
| 12 | + * The name of the model that is used by the base api controller |
|
| 13 | + * to preform actions like (add, edit ... etc). |
|
| 14 | + * @var string |
|
| 15 | + */ |
|
| 16 | + protected $model = 'oauthClients'; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * The validations rules used by the base api controller |
|
| 20 | - * to check before add. |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - protected $validationRules = [ |
|
| 24 | - 'name' => 'required|max:255', |
|
| 25 | - 'redirect' => 'required|url', |
|
| 26 | - 'user_id' => 'required|exists:users,id', |
|
| 27 | - 'revoked' => 'boolean' |
|
| 28 | - ]; |
|
| 18 | + /** |
|
| 19 | + * The validations rules used by the base api controller |
|
| 20 | + * to check before add. |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + protected $validationRules = [ |
|
| 24 | + 'name' => 'required|max:255', |
|
| 25 | + 'redirect' => 'required|url', |
|
| 26 | + 'user_id' => 'required|exists:users,id', |
|
| 27 | + 'revoked' => 'boolean' |
|
| 28 | + ]; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Revoke the given client. |
|
| 32 | - * |
|
| 33 | - * @param integer $clientId Id of the client |
|
| 34 | - * @return \Illuminate\Http\Response |
|
| 35 | - */ |
|
| 36 | - public function revoke($clientId) |
|
| 37 | - { |
|
| 38 | - return \Response::json($this->repo->revoke($clientId), 200); |
|
| 39 | - } |
|
| 30 | + /** |
|
| 31 | + * Revoke the given client. |
|
| 32 | + * |
|
| 33 | + * @param integer $clientId Id of the client |
|
| 34 | + * @return \Illuminate\Http\Response |
|
| 35 | + */ |
|
| 36 | + public function revoke($clientId) |
|
| 37 | + { |
|
| 38 | + return \Response::json($this->repo->revoke($clientId), 200); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Un revoke the given client. |
|
| 43 | - * |
|
| 44 | - * @param integer $clientId Id of the client |
|
| 45 | - * @return \Illuminate\Http\Response |
|
| 46 | - */ |
|
| 47 | - public function unRevoke($clientId) |
|
| 48 | - { |
|
| 49 | - return \Response::json($this->repo->unRevoke($clientId), 200); |
|
| 50 | - } |
|
| 41 | + /** |
|
| 42 | + * Un revoke the given client. |
|
| 43 | + * |
|
| 44 | + * @param integer $clientId Id of the client |
|
| 45 | + * @return \Illuminate\Http\Response |
|
| 46 | + */ |
|
| 47 | + public function unRevoke($clientId) |
|
| 48 | + { |
|
| 49 | + return \Response::json($this->repo->unRevoke($clientId), 200); |
|
| 50 | + } |
|
| 51 | 51 | } |
@@ -13,14 +13,14 @@ |
||
| 13 | 13 | * to preform actions like (add, edit ... etc). |
| 14 | 14 | * @var string |
| 15 | 15 | */ |
| 16 | - protected $model = 'oauthClients'; |
|
| 16 | + protected $model = 'oauthClients'; |
|
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | 19 | * The validations rules used by the base api controller |
| 20 | 20 | * to check before add. |
| 21 | 21 | * @var array |
| 22 | 22 | */ |
| 23 | - protected $validationRules = [ |
|
| 23 | + protected $validationRules = [ |
|
| 24 | 24 | 'name' => 'required|max:255', |
| 25 | 25 | 'redirect' => 'required|url', |
| 26 | 26 | 'user_id' => 'required|exists:users,id', |