Completed
Push — dev ( 2ed1fc...18be1d )
by Tristan
12:56 queued 06:13
created

BaseOidcUserProvider   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 208
Duplicated Lines 0 %

Test Coverage

Coverage 5.88%

Importance

Changes 0
Metric Value
wmc 28
eloc 60
dl 0
loc 208
ccs 4
cts 68
cp 0.0588
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getModel() 0 2 1
A validateCredentials() 0 4 1
A retrieveByToken() 0 3 1
A createUserFromCredentials() 0 13 5
A createModel() 0 4 1
A __construct() 0 3 1
A setModel() 0 4 1
C retrieveByCredentials() 0 66 15
A updateRememberToken() 0 1 1
A retrieveById() 0 6 1
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace App\Services\Auth;
4
5
use Illuminate\Contracts\Auth\UserProvider;
6
use Illuminate\Contracts\Auth\Authenticatable;
7
use App\Services\Auth\Contracts\OidcAuthenticatable;
8
use App\Services\Auth\Contracts\OidcUserValidator;
0 ignored issues
show
Bug introduced by
The type App\Services\Auth\Contracts\OidcUserValidator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use App\Models\Manager;
10
use App\Models\UserRole;
11
use App\Models\Applicant;
12
13
class BaseOidcUserProvider implements UserProvider {
0 ignored issues
show
Coding Style introduced by
Opening brace of a class must be on the line after the definition
Loading history...
Coding Style introduced by
Missing doc comment for class BaseOidcUserProvider
Loading history...
14
15
    /**
16
     * The Eloquent user model.
17
     *
18
     * @var string
19
     */
20
    protected $model;
21
22
    /**
23
     * The role new users should be created with
24
     *
25
     * @var string
26
     */
27
    protected $defaultRole;
28
29
30
31
    /**
32
     * Create a new user provider.
33
     *
34
     * @param  string  $model
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
35
     * @param  string  $defaultRole
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
36
     * @return void
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
37
     */
38 2
    public function __construct($model, $defaultRole) {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
39 2
        $this->model = $model;
40 2
        $this->defaultRole = $defaultRole;
41 2
    }
42
43
    /**
44
     * Retrieve a user by their unique identifier.
45
     *
46
     * @param  mixed  $identifier
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
47
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
48
     */
49
    public function retrieveById($identifier) {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
50
        $model = $this->createModel();
51
52
        return $model->newQuery()
0 ignored issues
show
Bug Best Practice introduced by
The expression return $model->newQuery(..., $identifier)->first() also could return the type Illuminate\Database\Eloquent\Model which is incompatible with the documented return type Illuminate\Contracts\Auth\Authenticatable|null.
Loading history...
53
                        ->where($model->getAuthIdentifierName(), $identifier)
0 ignored issues
show
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 24
Loading history...
54
                        ->first();
0 ignored issues
show
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 24
Loading history...
55
    }
56
57
    /**
58
     * Retrieve a user by their unique identifier and "remember me" token.
59
     *
60
     * @param  mixed  $identifier
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
61
     * @param  string  $token
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
62
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
63
     */
64
    public function retrieveByToken($identifier, $token) {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
65
        //TODO: Should we implement "remember me" tokens?
66
        return null;
67
    }
68
69
    /**
70
     * Update the "remember me" token for the given user in storage.
71
     *
72
     * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
73
     * @param  string  $token
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 37 spaces after parameter type; 2 found
Loading history...
74
     * @return void
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
75
     */
76
    public function updateRememberToken(Authenticatable $user, $token) {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
77
        //TODO: Should we implement "remember me" tokens?
78
    }
79
80
    /**
81
     * Retrieve a user by the given credentials.
82
     *
83
     * @param  array  $credentials
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
84
     * @return App\Services\Auth\Contracts\OidcAuthenticatable|null
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
Bug introduced by
The type App\Services\Auth\App\Se...cts\OidcAuthenticatable was not found. Did you mean App\Services\Auth\Contracts\OidcAuthenticatable? If so, make sure to prefix the type with \.
Loading history...
85
     */
86
    public function retrieveByCredentials(array $credentials) {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
87
        if (empty($credentials)) {
88
            return;
89
        }
90
        if (isset($credentials['iss']) && isset($credentials['sub'])) {
91
            // First we will try to find a user that matches the openid issuer
92
            // and sub code.
93
94
            $model = $this->createModel();
95
96
            $user = $model->findByOidcSub($credentials['iss'], $credentials['sub']);
97
98
            debugbar()->info("in Provider.retrieveByCredentials()");
99
            if ($user) {
100
                debugbar()->info("Provider found user:");
101
                debugbar()->info($user);
102
            }
103
104
            // If no user was found, use the provided credentials to create a
105
            // new user
106
            if ($user === null) {
107
108
                $user = $this->createUserFromCredentials($credentials);
109
                if ($user) {
110
                    //If a user was created successfully, save it to database
111
                    $user->save();
112
                }
113
114
                debugbar()->info("Provider created user:");
115
                debugbar()->info($user);
116
            }
117
118
            //If running in a local environment, and FORCE_ADMIN is true,
119
            //automatically set any logged in user to (temporarilly) be an admin
120
            if (env('APP_ENV') == 'local' && env('FORCE_ADMIN', false)) {
121
                $adminRole = UserRole::where('name', 'admin')->firstOrFail();
122
                $user->user_role_id = $adminRole->id;
123
                $user->user_role = $adminRole;
124
            }
125
126
            //Ensure the user has a proper profile associated with it
127
            //If now profile exists yet create one.
128
            //Admins should be givven an applicant and manager profile
129
            if ($user->user_role->name == 'applicant' ||
130
                $user->user_role->name == 'admin') {
0 ignored issues
show
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
131
                $applicantProfile = Applicant::where(['user_id' => $user->id])->first();
132
                if (!$applicantProfile) {
133
                    $applicantProfile = new Applicant();
134
                    $applicantProfile->user_id = $user->id;
135
                    $applicantProfile->save();
136
                }
137
138
            }
139
            if ($user->user_role->name == 'manager' ||
140
                $user->user_role->name == 'admin') {
0 ignored issues
show
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
141
                $managerProfile = Manager::where(['user_id' => $user->id])->first();
142
                if (!$managerProfile) {
143
                    $managerProfile = new Manager();
144
                    $managerProfile->user_id = $user->id;
145
                    $managerProfile->save();
146
                }
147
            }
148
149
            return $user;
150
        } else {
151
            return;
152
        }
153
    }
154
155
    /**
156
     * Create a new user object using the given credentials.
157
     *
158
     * @param  array  $credentials
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
159
     * @return App\Services\Auth\Contracts\OidcAuthenticatable|null
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
160
     */
161
    public function createUserFromCredentials(array $credentials) {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
162
        //At a minimum, email, iss and sub codes must be available.
163
        if (!isset($credentials['email']) || !isset($credentials['iss']) ||
164
                !isset($credentials['sub'])) {
0 ignored issues
show
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
Coding Style introduced by
Multi-line IF statement not indented correctly; expected 12 spaces but found 16
Loading history...
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
165
            return null;
166
        }
167
168
        $model = $this->createModel();
169
170
        $name = isset($credentials['name']) ? $credentials['name'] : "";
171
172
        return $model->createWithOidcCredentials($name, $credentials['email'],
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
Bug Best Practice introduced by
The expression return $model->createWit...'], $this->defaultRole) also could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the documented return type null|App\Services\Auth\A...cts\OidcAuthenticatable.
Loading history...
173
                $credentials['iss'], $credentials['sub'], $this->defaultRole);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
174
    }
175
176
    /**
177
     * Validate a user against the given credentials.
178
     *
179
     * @param  App\Services\Auth\Contracts\OidcAuthenticatable  $user
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
180
     * @param  array  $credentials
0 ignored issues
show
Coding Style introduced by
Expected 43 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
181
     * @return bool
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
182
     */
183
    public function validateCredentials(Authenticatable $user, array $credentials) {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
184
        debugbar()->info("in Provider.validateCredentials()");
185
186
        return $user instanceof Authenticatable;
187
        //$subMatches = $credentials['sub'] === $user->getSub($credentials['iss']);
188
        //return $subMatches;
189
    }
190
191
    /**
192
     * Create a new instance of the model.
193
     *
194
     * @return \Illuminate\Database\Eloquent\Model
195
     */
196
    public function createModel() {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
197
        $class = '\\' . ltrim($this->model, '\\');
198
199
        return new $class;
200
    }
201
202
    /**
203
     * Gets the name of the Eloquent user model.
204
     *
205
     * @return string
206
     */
207
    public function getModel() {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
208
        return $this->model;
209
    }
210
211
    /**
212
     * Sets the name of the Eloquent user model.
213
     *
214
     * @param  string  $model
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
215
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
216
     */
217
    public function setModel($model) {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
218
        $this->model = $model;
219
220
        return $this;
221
    }
222
223
}
224