|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Expressionengine\Coilpack\Auth; |
| 4 | + |
| 5 | +use Illuminate\Auth\EloquentUserProvider; |
| 6 | +use Illuminate\Contracts\Auth\Authenticatable; |
| 7 | + |
| 8 | +class CoilpackUserProvider extends EloquentUserProvider |
| 9 | +{ |
| 10 | + /** |
| 11 | + * Retrieve a user by the given credentials. |
| 12 | + * |
| 13 | + * @param array $credentials |
| 14 | + * @return \Illuminate\Contracts\Auth\Authenticatable|null |
| 15 | + */ |
| 16 | + public function retrieveByCredentials(array $credentials) |
| 17 | + { |
| 18 | + $key = array_key_exists('username', $credentials) ? 'username' : 'email'; |
| 19 | + $value = $credentials[$key]; |
| 20 | + |
| 21 | + return $this->newModelQuery() |
| 22 | + ->where('username', $value) |
| 23 | + ->when(strpos($value, '@'), function ($query) use ($value) { |
| 24 | + $query->orWhere('email', $value); |
| 25 | + }) |
| 26 | + ->first(); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Validate a user against the given credentials. |
| 31 | + * |
| 32 | + * @param \Illuminate\Contracts\Auth\Authenticatable $user |
| 33 | + * @param array $credentials |
| 34 | + * @return bool |
| 35 | + */ |
| 36 | + public function validateCredentials(Authenticatable $user, array $credentials) |
| 37 | + { |
| 38 | + ee()->load->library('auth'); |
| 39 | + |
| 40 | + $result = false; |
| 41 | + |
| 42 | + if (array_key_exists('username', $credentials)) { |
| 43 | + $result = ee()->auth->authenticate_username($credentials['username'], $credentials['password']); |
| 44 | + } elseif (array_key_exists('email', $credentials)) { |
| 45 | + $result = ee()->auth->authenticate_email($credentials['email'], $credentials['password']); |
| 46 | + } |
| 47 | + |
| 48 | + if (! $result) { |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + $result->start_session(); |
| 53 | + |
| 54 | + return $result !== false; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Retrieve a user by their unique identifier and "remember me" token. |
| 59 | + * |
| 60 | + * @param mixed $identifier |
| 61 | + * @param string $token |
| 62 | + * @return \Illuminate\Contracts\Auth\Authenticatable|null |
| 63 | + */ |
| 64 | + public function retrieveByToken($identifier, $token) |
| 65 | + { |
| 66 | + if (! ee()->remember->exists()) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + $model = $this->createModel(); |
| 71 | + |
| 72 | + return $this->newModelQuery($model)->where( |
| 73 | + $model->getAuthIdentifierName(), |
| 74 | + ee()->remember->data('member_id') |
| 75 | + )->first(); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Update the "remember me" token for the given user in storage. |
| 80 | + * |
| 81 | + * @param \Illuminate\Contracts\Auth\Authenticatable $user |
| 82 | + * @param string $token |
| 83 | + * @return void |
| 84 | + */ |
| 85 | + public function updateRememberToken(Authenticatable $user, $token) |
| 86 | + { |
| 87 | + ee()->remember->exists() ? ee()->remember->refresh() : ee()->remember->create(); |
| 88 | + } |
| 89 | +} |
0 commit comments