Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| CustomPasswordBroker | |
0.00% |
0 / 26 |
|
0.00% |
0 / 2 |
272 | |
0.00% |
0 / 1 |
| getUser | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
72 | |||
| getCredentials | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
72 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Services; |
| 4 | |
| 5 | use App\Http\Models\Admin\AdminUserInvitation; |
| 6 | use App\Http\Models\Auth\User; |
| 7 | use Illuminate\Auth\Passwords\PasswordBroker as BasePasswordBroker; |
| 8 | use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; |
| 9 | use Illuminate\Contracts\Support\Arrayable; |
| 10 | use Illuminate\Database\Eloquent\Builder; |
| 11 | use Illuminate\Support\Arr; |
| 12 | use Illuminate\Support\Str; |
| 13 | use UnexpectedValueException; |
| 14 | |
| 15 | class CustomPasswordBroker extends BasePasswordBroker |
| 16 | { |
| 17 | /** |
| 18 | * Get the user for the given credentials. |
| 19 | * |
| 20 | * |
| 21 | * @throws \UnexpectedValueException |
| 22 | */ |
| 23 | public function getUser(array $credentials) |
| 24 | { |
| 25 | $credentials = Arr::except($credentials, ['token']); |
| 26 | |
| 27 | if (empty($credentials) || |
| 28 | (count($credentials) === 1 && |
| 29 | array_key_exists('password', $credentials))) { |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | $query = User::query(); |
| 34 | |
| 35 | $user = $this->getCredentials($query, $credentials); |
| 36 | |
| 37 | if (!$user) { |
| 38 | $query = AdminUserInvitation::query(); |
| 39 | |
| 40 | $user = $this->getCredentials($query, $credentials); |
| 41 | } |
| 42 | |
| 43 | $user = strtolower($user->email) === strtolower($credentials['email']) ? $user : null; |
| 44 | |
| 45 | if ($user && ! $user instanceof CanResetPasswordContract) { |
| 46 | throw new UnexpectedValueException('User must implement CanResetPassword interface.'); |
| 47 | } |
| 48 | |
| 49 | return $user; |
| 50 | } |
| 51 | |
| 52 | private function getCredentials(Builder $query, array $credentials) |
| 53 | { |
| 54 | foreach ($credentials as $key => $value) { |
| 55 | if (Str::contains($key, 'password')) { |
| 56 | continue; |
| 57 | } |
| 58 | |
| 59 | if (is_array($value) || $value instanceof Arrayable) { |
| 60 | if ($key == 'email') { |
| 61 | foreach ($value as $key => $val) { |
| 62 | $query->where('email', 'like', strtolower($val)); |
| 63 | } |
| 64 | } else { |
| 65 | $query->whereIn($key, $value); |
| 66 | } |
| 67 | } else { |
| 68 | if ($key == 'email') { |
| 69 | $query->where($key, 'like', strtolower($value)); |
| 70 | } else { |
| 71 | $query->where($key, $value); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return $query->first(); |
| 77 | } |
| 78 | } |