| Total Complexity | 27 |
| Total Lines | 199 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 15 | class BaseOidcUserProvider implements UserProvider { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The Eloquent user model. |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $model; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The role new users should be created with |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $defaultRole; |
||
| 30 | |||
| 31 | |||
| 32 | |||
| 33 | /** |
||
| 34 | * Create a new user provider. |
||
| 35 | * |
||
| 36 | * @param string $model |
||
|
2 ignored issues
–
show
|
|||
| 37 | * @param string $defaultRole |
||
|
2 ignored issues
–
show
|
|||
| 38 | * @return void |
||
| 39 | */ |
||
| 40 | public function __construct($model, $defaultRole) { |
||
|
2 ignored issues
–
show
|
|||
| 41 | $this->model = $model; |
||
| 42 | $this->defaultRole = $defaultRole; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Retrieve a user by their unique identifier. |
||
| 47 | * |
||
| 48 | * @param mixed $identifier |
||
|
2 ignored issues
–
show
|
|||
| 49 | * @return \Illuminate\Contracts\Auth\Authenticatable|null |
||
| 50 | */ |
||
| 51 | public function retrieveById($identifier) { |
||
| 52 | $model = $this->createModel(); |
||
| 53 | |||
| 54 | return $model->newQuery() |
||
| 55 | ->where($model->getAuthIdentifierName(), $identifier) |
||
| 56 | ->first(); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Retrieve a user by their unique identifier and "remember me" token. |
||
| 61 | * |
||
| 62 | * @param mixed $identifier |
||
|
1 ignored issue
–
show
|
|||
| 63 | * @param string $token |
||
|
2 ignored issues
–
show
|
|||
| 64 | * @return \Illuminate\Contracts\Auth\Authenticatable|null |
||
| 65 | */ |
||
| 66 | public function retrieveByToken($identifier, $token) { |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Update the "remember me" token for the given user in storage. |
||
| 73 | * |
||
| 74 | * @param \Illuminate\Contracts\Auth\Authenticatable $user |
||
|
2 ignored issues
–
show
|
|||
| 75 | * @param string $token |
||
|
2 ignored issues
–
show
|
|||
| 76 | * @return void |
||
| 77 | */ |
||
| 78 | public function updateRememberToken(Authenticatable $user, $token) { |
||
| 79 | //TODO: Should we implement "remember me" tokens? |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Retrieve a user by the given credentials. |
||
| 84 | * |
||
| 85 | * @param array $credentials |
||
|
2 ignored issues
–
show
|
|||
| 86 | * @return App\Services\Auth\Contracts\OidcAuthenticatable|null |
||
| 87 | */ |
||
| 88 | public function retrieveByCredentials(array $credentials) { |
||
| 89 | if (empty($credentials)) { |
||
| 90 | return; |
||
| 91 | } |
||
| 92 | if (isset($credentials['iss']) && isset($credentials['sub'])) { |
||
| 93 | // First we will try to find a user that matches the openid issuer |
||
| 94 | // and sub code. |
||
| 95 | |||
| 96 | $model = $this->createModel(); |
||
| 97 | |||
| 98 | $user = $model->findByOidcSub($credentials['iss'], $credentials['sub']); |
||
| 99 | |||
| 100 | // If no user was found, use the provided credentials to create a |
||
| 101 | // new user |
||
| 102 | if ($user === null) { |
||
| 103 | |||
| 104 | $user = $this->createUserFromCredentials($credentials); |
||
| 105 | if ($user) { |
||
| 106 | //If a user was created successfully, save it to database |
||
| 107 | $user->save(); |
||
| 108 | } |
||
| 109 | |||
| 110 | } |
||
| 111 | |||
| 112 | //If running in a local environment, and FORCE_ADMIN is true, |
||
| 113 | //automatically set any logged in user to (temporarilly) be an admin |
||
| 114 | if (App::environment() == 'local' && Config::get('app.force_admin')) { |
||
| 115 | $adminRole = UserRole::where('name', 'admin')->firstOrFail(); |
||
| 116 | $user->user_role_id = $adminRole->id; |
||
| 117 | // $user->user_role = $adminRole; |
||
| 118 | $user->save(); |
||
| 119 | } |
||
| 120 | |||
| 121 | //Ensure the user has a proper profile associated with it |
||
| 122 | //If now profile exists yet create one. |
||
| 123 | //Admins should be givven an applicant and manager profile |
||
| 124 | if ($user->user_role->name == 'applicant' || |
||
| 125 | $user->user_role->name == 'admin') { |
||
| 126 | $applicantProfile = Applicant::where(['user_id' => $user->id])->first(); |
||
| 127 | if (!$applicantProfile) { |
||
| 128 | $applicantProfile = new Applicant(); |
||
| 129 | $applicantProfile->user_id = $user->id; |
||
| 130 | $applicantProfile->save(); |
||
| 131 | } |
||
| 132 | |||
| 133 | } |
||
| 134 | if ($user->user_role->name == 'manager' || |
||
| 135 | $user->user_role->name == 'admin') { |
||
| 136 | $managerProfile = Manager::where(['user_id' => $user->id])->first(); |
||
| 137 | if (!$managerProfile) { |
||
| 138 | $managerProfile = new Manager(); |
||
| 139 | $managerProfile->user_id = $user->id; |
||
| 140 | $managerProfile->save(); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | return $user; |
||
| 145 | } else { |
||
| 146 | return; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Create a new user object using the given credentials. |
||
| 152 | * |
||
| 153 | * @param array $credentials |
||
|
2 ignored issues
–
show
|
|||
| 154 | * @return App\Services\Auth\Contracts\OidcAuthenticatable|null |
||
| 155 | */ |
||
| 156 | public function createUserFromCredentials(array $credentials) { |
||
| 157 | //At a minimum, email, iss and sub codes must be available. |
||
| 158 | if (!isset($credentials['email']) || !isset($credentials['iss']) || |
||
| 159 | !isset($credentials['sub'])) { |
||
| 160 | return null; |
||
| 161 | } |
||
| 162 | |||
| 163 | $model = $this->createModel(); |
||
| 164 | |||
| 165 | $name = isset($credentials['name']) ? $credentials['name'] : ""; |
||
| 166 | |||
| 167 | return $model->createWithOidcCredentials($name, $credentials['email'], |
||
| 168 | $credentials['iss'], $credentials['sub'], $this->defaultRole); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Validate a user against the given credentials. |
||
| 173 | * |
||
| 174 | * @param App\Services\Auth\Contracts\OidcAuthenticatable $user |
||
|
2 ignored issues
–
show
|
|||
| 175 | * @param array $credentials |
||
|
2 ignored issues
–
show
|
|||
| 176 | * @return bool |
||
|
1 ignored issue
–
show
|
|||
| 177 | */ |
||
| 178 | public function validateCredentials(Authenticatable $user, array $credentials) { |
||
| 179 | return $user instanceof Authenticatable; |
||
| 180 | //$subMatches = $credentials['sub'] === $user->getSub($credentials['iss']); |
||
| 181 | //return $subMatches; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Create a new instance of the model. |
||
| 186 | * |
||
| 187 | * @return \Illuminate\Database\Eloquent\Model |
||
| 188 | */ |
||
| 189 | public function createModel() { |
||
| 190 | $class = '\\' . ltrim($this->model, '\\'); |
||
| 191 | |||
| 192 | return new $class; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Gets the name of the Eloquent user model. |
||
| 197 | * |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | public function getModel() { |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Sets the name of the Eloquent user model. |
||
| 206 | * |
||
| 207 | * @param string $model |
||
|
2 ignored issues
–
show
|
|||
| 208 | * @return $this |
||
| 209 | */ |
||
| 210 | public function setModel($model) { |
||
| 214 | } |
||
| 215 | |||
| 217 |