GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( cf9260...b52382 )
by
unknown
03:19
created

User::authenticate()   C

Complexity

Conditions 13
Paths 104

Size

Total Lines 44
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 27
nc 104
nop 2
dl 0
loc 44
rs 6.5833
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of the O2System Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System\Framework\Libraries\AccessControl;
15
16
// ------------------------------------------------------------------------
17
18
use O2System\Framework\Http\Message\ServerRequest;
19
use O2System\Security\Authentication\User\Account;
20
use O2System\Security\Authentication\User\Authorities;
21
use O2System\Security\Authentication\User\Authority;
22
use O2System\Security\Authentication\User\Role;
23
use O2System\Spl\Exceptions\RuntimeException;
24
25
/**
26
 * Class User
27
 * @package O2System\Framework\Libraries\AccessControl
28
 */
29
class User extends \O2System\Security\Authentication\User
30
{
31
    /**
32
     * User::__construct
33
     *
34
     * @throws \O2System\Spl\Exceptions\RuntimeException
35
     */
36
    public function __construct()
37
    {
38
        parent::__construct();
39
40
        if ($config = config()->loadFile('AccessControl', true)) {
0 ignored issues
show
Bug introduced by
The method loadFile() does not exist on O2System\Kernel\Datastructures\Config. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
        if ($config = config()->/** @scrutinizer ignore-call */ loadFile('AccessControl', true)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
41
            $this->setConfig($config->getArrayCopy());
42
        }
43
44
        if ( ! models('users')) {
45
            throw new RuntimeException('ACL_E_UNDEFINED_USERS_MODEL');
46
        }
47
    }
48
49
    // ------------------------------------------------------------------------
50
51
    /**
52
     * User::authenticate
53
     *
54
     * @param string $username
55
     * @param string $password
56
     *
57
     * @return bool
58
     */
59
    public function authenticate($username, $password)
60
    {
61
        $column = 'username';
62
        if (is_numeric($username)) {
63
            $column = 'id';
64
        } elseif (filter_var($username, FILTER_VALIDATE_EMAIL)) {
65
            $column = 'email';
66
        } elseif (preg_match($this->config[ 'msisdnRegex' ], $username)) {
67
            $column = 'msisdn';
68
        }
69
70
        if ($user = models('users')->findWhere([$column => $username], 1)) {
0 ignored issues
show
Bug introduced by
The method findWhere() does not exist on O2System\Framework\Containers\Models. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        if ($user = models('users')->/** @scrutinizer ignore-call */ findWhere([$column => $username], 1)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
            if ($user->account) {
0 ignored issues
show
Bug introduced by
The property account does not seem to exist on O2System\Database\DataObjects\Result.
Loading history...
Bug introduced by
The property account does not seem to exist on O2System\Framework\Models\Sql\DataObjects\Result.
Loading history...
72
                if ($this->passwordVerify($password, $user->account->password)) {
73
                    if ($this->passwordRehash($password)) {
74
                        $user->account->update([
75
                            'id'       => $user->id,
0 ignored issues
show
Bug introduced by
The property id does not seem to exist on O2System\Framework\Models\Sql\DataObjects\Result.
Loading history...
Bug introduced by
The property id does not seem to exist on O2System\Database\DataObjects\Result.
Loading history...
76
                            'password' => $this->passwordHash($password),
77
                        ]);
78
                    }
79
80
                    $account = $user->account->getArrayCopy();
81
                }
82
            } elseif ($this->passwordVerify($password, $user->password)) {
0 ignored issues
show
Bug introduced by
The property password does not seem to exist on O2System\Database\DataObjects\Result.
Loading history...
Bug introduced by
The property password does not seem to exist on O2System\Framework\Models\Sql\DataObjects\Result.
Loading history...
83
                $account = $user;
84
            }
85
86
            if (isset($account)) {
87
                foreach ($account as $key => $value) {
88
                    if (strpos($key, 'record') !== false) {
89
                        unset($account[ $key ]);
90
                    } elseif (in_array($key,
91
                        ['password', 'pin', 'token', 'sso', 'id_sys_user', 'id_sys_module', 'id_sys_module_role'])) {
92
                        unset($account[ $key ]);
93
                    }
94
                }
95
96
                $this->login($account);
97
98
                return true;
99
            }
100
        }
101
102
        return false;
103
    }
104
105
    // ------------------------------------------------------------------------
106
107
    /**
108
     * User::loggedIn
109
     *
110
     * @return bool
111
     * @throws \O2System\Psr\Cache\InvalidArgumentException
112
     */
113
    public function loggedIn()
114
    {
115
        if (parent::loggedIn()) {
116
            $account = new Account($_SESSION[ 'account' ]);
117
118
            if ($user = models('users')->findWhere(['username' => $account->username], 1)) {
119
                // Store Account Profile
120
                if ($profile = $user->profile) {
0 ignored issues
show
Bug introduced by
The property profile does not seem to exist on O2System\Database\DataObjects\Result.
Loading history...
Bug introduced by
The property profile does not seem to exist on O2System\Framework\Models\Sql\DataObjects\Result.
Loading history...
121
                    $account->store('profile', $profile);
122
                }
123
124
                // Store Account Role
125
                if ($role = $user->role) {
0 ignored issues
show
Bug introduced by
The property role does not seem to exist on O2System\Database\DataObjects\Result.
Loading history...
Bug introduced by
The property role does not seem to exist on O2System\Framework\Models\Sql\DataObjects\Result.
Loading history...
126
                    $account->store('role', new Role([
127
                        'label'       => $role->label,
128
                        'description' => $role->description,
129
                        'code'        => $role->code,
130
                        'authorities' => $role->authorities,
131
                    ]));
132
                }
133
            }
134
            
135
            // Store Globals Account
136
            globals()->store('account', $account);
137
138
            // Store Presenter Account
139
            if (services()->has('view')) {
140
                presenter()->store('account', $account);
141
            }
142
143
            return true;
144
        }
145
146
        return false;
147
    }
148
149
    // ------------------------------------------------------------------------
150
151
    /**
152
     * User::forceLogin
153
     *
154
     * @param string $username
155
     * @param string $column
156
     *
157
     * @return bool
158
     */
159
    public function forceLogin($username, $column = 'username')
160
    {
161
        if (is_numeric($username)) {
162
            $column = 'id';
163
        } elseif (filter_var($username, FILTER_VALIDATE_EMAIL)) {
164
            $column = 'email';
165
        } elseif (preg_match($this->config[ 'msisdnRegex' ], $username)) {
166
            $column = 'msisdn';
167
        } elseif (strpos($username, 'token-') !== false) {
168
            $username = str_replace('token-', '', $username);
169
            $column = 'token';
170
        } elseif (strpos($username, 'sso-') !== false) {
171
            $username = str_replace('sso-', '', $username);
172
            $column = 'sso';
173
        }
174
175
        if ($account = models('users')->findWhere([$column => $username], 1)) {
176
            $account = $account->getArrayCopy();
177
178
            foreach ($account as $key => $value) {
179
                if (strpos($key, 'record') !== false) {
180
                    unset($account[ $key ]);
181
                } elseif (in_array($key, ['password', 'pin', 'token', 'sso'])) {
182
                    unset($account[ $key ]);
183
                }
184
            }
185
186
            if ($column === 'token') {
187
                models('users')->update([
0 ignored issues
show
Bug introduced by
The method update() does not exist on O2System\Framework\Containers\Models. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

187
                models('users')->/** @scrutinizer ignore-call */ update([

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method update() does not exist on O2System\Framework\Models\NoSql\Model. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

187
                models('users')->/** @scrutinizer ignore-call */ update([

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
188
                    'id'    => $account[ 'id' ],
189
                    'token' => null,
190
                ]);
191
            }
192
193
            $this->login($account);
194
195
            return true;
196
        }
197
198
        return false;
199
    }
200
201
    // ------------------------------------------------------------------------
202
203
    /**
204
     * User::authorize
205
     *
206
     * @param \O2System\Framework\Http\Message\ServerRequest $request
207
     *
208
     * @return bool
209
     */
210
    public function authorize(ServerRequest $request)
211
    {
212
        if (isset($GLOBALS[ 'account' ][ 'role' ])) {
213
            $uriSegments = $request->getUri()->getSegments()->getString();
214
            $role = $GLOBALS[ 'account' ][ 'role' ];
215
            if (in_array($role->code, ['DEVELOPER', 'ADMINISTRATOR'])) {
216
                globals()->store('authority', new Authority([
217
                    'permission' => 'GRANTED',
218
                    'privileges' => '11111111',
219
                ]));
220
221
                return true;
222
            } elseif ($role->authorities instanceof Authorities) {
223
                if ($role->authorities->exists($uriSegments)) {
224
                    $authority = $role->authorities->getAuthority($uriSegments);
225
226
                    globals()->store('authority', $authority);
227
228
                    return $authority->getPermission();
229
                }
230
231
                globals()->store('authority', new Authority([
232
                    'permission' => 'DENIED',
233
                    'privileges' => '00000000',
234
                ]));
235
236
                return false;
237
            }
238
        }
239
240
        return false;
241
    }
242
243
    // ------------------------------------------------------------------------
244
245
    /**
246
     * User::getIframeCode
247
     *
248
     * @return string
249
     * @throws \O2System\Psr\Cache\InvalidArgumentException
250
     */
251
    public function getIframeCode()
252
    {
253
        if ($this->signedOn() && $this->loggedIn() === false) {
254
            return '<iframe id="sign-on-iframe" width="1" height="1" src="' . rtrim($this->config[ 'sso' ][ 'server' ],
255
                    '/') . '" style="display: none; visibility: hidden;"></iframe>';
256
        }
257
258
        return '';
259
    }
260
}