Passed
Push — bugfix/relatives_not_saving ( 6485fa...f1e1c5 )
by Tristan
13:39
created

BaseOidcUserProvider   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 27
eloc 53
dl 0
loc 199
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A retrieveByToken() 0 3 1
A __construct() 0 3 1
A updateRememberToken() 0 1 1
A retrieveById() 0 6 1
A getModel() 0 2 1
A validateCredentials() 0 2 1
A createUserFromCredentials() 0 13 5
A createModel() 0 4 1
A setModel() 0 4 1
C retrieveByCredentials() 0 59 14
1
<?php
2
3
namespace App\Services\Auth;
4
5
use Illuminate\Support\Facades\App;
6
use Illuminate\Support\Facades\Config;
7
use Illuminate\Contracts\Auth\UserProvider;
8
use Illuminate\Contracts\Auth\Authenticatable;
9
use App\Services\Auth\Contracts\OidcAuthenticatable;
10
use App\Services\Auth\Contracts\OidcUserValidator;
11
use App\Models\Manager;
12
use App\Models\UserRole;
13
use App\Models\Applicant;
14
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
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
37
     * @param  string  $defaultRole
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
38
     * @return void
39
     */
40
    public function __construct($model, $defaultRole) {
2 ignored issues
show
introduced by
Method \App\Services\Auth\BaseOidcUserProvider::__construct() does not have parameter type hint for its parameter $model but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \App\Services\Auth\BaseOidcUserProvider::__construct() does not have parameter type hint for its parameter $defaultRole but it should be possible to add it based on @param annotation "string".
Loading history...
Coding Style introduced by
Type hint "string" missing for $model
Loading history...
Coding Style introduced by
Type hint "string" missing for $defaultRole
Loading history...
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
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
49
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
50
     */
51
    public function retrieveById($identifier) {
0 ignored issues
show
introduced by
Method \App\Services\Auth\BaseOidcUserProvider::retrieveById() does not have return type hint for its return value but it should be possible to add it based on @return annotation "\Illuminate\Contracts\Auth\Authenticatable|null".
Loading history...
52
        $model = $this->createModel();
53
54
        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...
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
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
63
     * @param  string  $token
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
64
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
65
     */
66
    public function retrieveByToken($identifier, $token) {
1 ignored issue
show
introduced by
Method \App\Services\Auth\BaseOidcUserProvider::retrieveByToken() does not have return type hint for its return value but it should be possible to add it based on @return annotation "\Illuminate\Contracts\Auth\Authenticatable|null".
Loading history...
Coding Style introduced by
Type hint "string" missing for $token
Loading history...
67
        //TODO: Should we implement "remember me" tokens?
68
        return null;
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
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
75
     * @param  string  $token
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 37 spaces after parameter type; 2 found
Loading history...
76
     * @return void
77
     */
78
    public function updateRememberToken(Authenticatable $user, $token) {
1 ignored issue
show
introduced by
Method \App\Services\Auth\BaseOidcUserProvider::updateRememberToken() does not have parameter type hint for its parameter $token but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \App\Services\Auth\BaseOidcUserProvider::updateRememberToken() does not have return type hint for its return value but it should be possible to add it based on @return annotation "void".
Loading history...
Coding Style introduced by
Type hint "string" missing for $token
Loading history...
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
introduced by
@param annotation of method \App\Services\Auth\BaseOidcUserProvider::retrieveByCredentials() does not specify type hint for items of its traversable parameter $credentials.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
86
     * @return App\Services\Auth\Contracts\OidcAuthenticatable|null
87
     */
88
    public function retrieveByCredentials(array $credentials) {
89
        if (empty($credentials)) {
90
            return;
0 ignored issues
show
Coding Style introduced by
Function return type is not void, but function is returning void here
Loading history...
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) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
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
introduced by
@param annotation of method \App\Services\Auth\BaseOidcUserProvider::createUserFromCredentials() does not specify type hint for items of its traversable parameter $credentials.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
154
     * @return App\Services\Auth\Contracts\OidcAuthenticatable|null
155
     */
156
    public function createUserFromCredentials(array $credentials) {
0 ignored issues
show
introduced by
Method \App\Services\Auth\BaseOidcUserProvider::createUserFromCredentials() does not have return type hint for its return value but it should be possible to add it based on @return annotation "App\Services\Auth\Contracts\OidcAuthenticatable|null".
Loading history...
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'],
0 ignored issues
show
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 Illuminate\Support\Facad...idcAuthenticatable|null.
Loading history...
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
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
175
     * @param  array  $credentials
2 ignored issues
show
introduced by
@param annotation of method \App\Services\Auth\BaseOidcUserProvider::validateCredentials() does not specify type hint for items of its traversable parameter $credentials.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 43 spaces after parameter type; 2 found
Loading history...
176
     * @return bool
1 ignored issue
show
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
177
     */
178
    public function validateCredentials(Authenticatable $user, array $credentials) {
0 ignored issues
show
introduced by
Method \App\Services\Auth\BaseOidcUserProvider::validateCredentials() does not have return type hint for its return value but it should be possible to add it based on @return annotation "bool".
Loading history...
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() {
0 ignored issues
show
introduced by
Method \App\Services\Auth\BaseOidcUserProvider::createModel() does not have return type hint for its return value but it should be possible to add it based on @return annotation "\Illuminate\Database\Eloquent\Model".
Loading history...
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() {
0 ignored issues
show
introduced by
Method \App\Services\Auth\BaseOidcUserProvider::getModel() does not have return type hint for its return value but it should be possible to add it based on @return annotation "string".
Loading history...
201
        return $this->model;
202
    }
203
204
    /**
205
     * Sets the name of the Eloquent user model.
206
     *
207
     * @param  string  $model
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
208
     * @return $this
209
     */
210
    public function setModel($model) {
1 ignored issue
show
introduced by
Method \App\Services\Auth\BaseOidcUserProvider::setModel() does not have parameter type hint for its parameter $model but it should be possible to add it based on @param annotation "string".
Loading history...
Coding Style introduced by
Type hint "string" missing for $model
Loading history...
211
        $this->model = $model;
212
213
        return $this;
214
    }
215
216
}
217