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 ( c097a6...9587da )
by Steeven
02:27
created

User::loggedIn()   A

Complexity

Conditions 6
Paths 11

Size

Total Lines 34
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 16
nc 11
nop 0
dl 0
loc 34
rs 9.1111
c 0
b 0
f 0
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::$app
33
     *
34
     * @var string
35
     */
36
    protected $app = 'app';
37
38
    /**
39
     * User::__construct
40
     *
41
     * @throws \O2System\Spl\Exceptions\RuntimeException
42
     */
43
    public function __construct()
44
    {
45
        parent::__construct();
46
47
        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

47
        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...
48
            $this->setConfig($config->getArrayCopy());
49
        }
50
51
        if ( ! models('users')) {
52
            throw new RuntimeException('ACL_E_UNDEFINED_USERS_MODEL');
53
        }
54
    }
55
56
    // ------------------------------------------------------------------------
57
58
    /**
59
     * User::setApp
60
     *
61
     * @param string $app
62
     *
63
     * @return static
64
     */
65
    public function setApp($app)
66
    {
67
        if($app = modules()->getApp($app)) {
0 ignored issues
show
Bug introduced by
The method getApp() does not exist on O2System\Framework\Conta...s\DataStructures\Module. ( Ignorable by Annotation )

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

67
        if($app = modules()->/** @scrutinizer ignore-call */ getApp($app)) {

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...
68
            $this->app = $app;
0 ignored issues
show
Documentation Bug introduced by
It seems like $app can also be of type true. However, the property $app is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
69
        }
70
71
        return $this;
72
    }
73
74
    // ------------------------------------------------------------------------
75
76
    /**
77
     * User::authenticate
78
     *
79
     * @param string $username
80
     * @param string $password
81
     *
82
     * @return bool
83
     */
84
    public function authenticate($username, $password)
85
    {
86
        if ($user = $this->find($username)) {
87
            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...
88
                if ($this->passwordVerify($password, $user->account->password)) {
89
                    if ($this->passwordRehash($password)) {
90
                        $user->account->update([
91
                            'id'       => $user->id,
0 ignored issues
show
Bug introduced by
The property id does not seem to exist on O2System\Database\DataObjects\Result.
Loading history...
Bug introduced by
The property id does not seem to exist on O2System\Framework\Models\Sql\DataObjects\Result.
Loading history...
92
                            'password' => $this->passwordHash($password),
93
                        ]);
94
                    }
95
96
                    $account = $user->account->getArrayCopy();
97
                }
98
            } 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...
99
                $account = $user->getArrayCopy();
100
            }
101
102
            if (isset($account)) {
103
                foreach ($account as $key => $value) {
104
                    if (strpos($key, 'record') !== false) {
105
                        unset($account[ $key ]);
106
                    } elseif (in_array($key,
107
                        ['password', 'pin', 'token', 'sso', 'id_sys_user', 'id_sys_module', 'id_sys_module_role'])) {
108
                        unset($account[ $key ]);
109
                    }
110
                }
111
112
                $this->login($account);
113
114
                return true;
115
            }
116
        }
117
118
        return false;
119
    }
120
121
    // ------------------------------------------------------------------------
122
123
    /**
124
     * User::find
125
     *
126
     * @param string $username
127
     *
128
     * @return bool|mixed|\O2System\Database\DataObjects\Result|\O2System\Framework\Models\Sql\DataObjects\Result
129
     */
130
    public function find($username)
131
    {
132
        $column = 'username';
133
        if (is_numeric($username)) {
134
            $column = 'id';
135
        } elseif (filter_var($username, FILTER_VALIDATE_EMAIL)) {
136
            $column = 'email';
137
        } elseif (preg_match($this->config[ 'msisdnRegex' ], $username)) {
138
            $column = 'msisdn';
139
        }
140
141
        if ($user = models('users')->findWhere([
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

141
        if ($user = models('users')->/** @scrutinizer ignore-call */ findWhere([

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...
142
            $column => $username
143
        ], 1)) {
144
            return $user;
145
        }
146
147
        return false;
148
    }
149
150
    // ------------------------------------------------------------------------
151
152
    /**
153
     * User::loggedIn
154
     *
155
     * @return bool
156
     * @throws \Psr\Cache\InvalidArgumentException
157
     */
158
    public function loggedIn()
159
    {
160
        if (parent::loggedIn()) {
161
            $account = new Account($_SESSION[ 'account' ]);
162
163
            if ($user = models('users')->findWhere(['username' => $account->username], 1)) {
164
                // Store Account Profile
165
                if ($profile = $user->profile) {
0 ignored issues
show
Bug introduced by
The property profile does not seem to exist on O2System\Framework\Models\Sql\DataObjects\Result.
Loading history...
Bug introduced by
The property profile does not seem to exist on O2System\Database\DataObjects\Result.
Loading history...
166
                    $account->store('profile', $profile);
167
                }
168
169
                // Store Account Role
170
                if ($role = $user->role) {
0 ignored issues
show
Bug introduced by
The property role does not seem to exist on O2System\Framework\Models\Sql\DataObjects\Result.
Loading history...
Bug introduced by
The property role does not seem to exist on O2System\Database\DataObjects\Result.
Loading history...
171
                    $account->store('role', new Role([
172
                        'label'       => $role->label,
173
                        'description' => $role->description,
174
                        'code'        => $role->code,
175
                        'authorities' => $role->authorities,
176
                    ]));
177
                }
178
            }
179
            
180
            // Store Globals Account
181
            globals()->store('account', $account);
182
183
            // Store Presenter Account
184
            if (services()->has('view')) {
185
                presenter()->store('account', $account);
186
            }
187
188
            return true;
189
        }
190
191
        return false;
192
    }
193
194
    // ------------------------------------------------------------------------
195
196
    /**
197
     * User::forceLogin
198
     *
199
     * @param string $username
200
     * @param string $column
201
     *
202
     * @return bool
203
     */
204
    public function forceLogin($username, $column = 'username')
205
    {
206
        if (is_numeric($username)) {
207
            $column = 'id';
208
        } elseif (filter_var($username, FILTER_VALIDATE_EMAIL)) {
209
            $column = 'email';
210
        } elseif (preg_match($this->config[ 'msisdnRegex' ], $username)) {
211
            $column = 'msisdn';
212
        } elseif (strpos($username, 'token-') !== false) {
213
            $username = str_replace('token-', '', $username);
214
            $column = 'token';
215
        } elseif (strpos($username, 'sso-') !== false) {
216
            $username = str_replace('sso-', '', $username);
217
            $column = 'sso';
218
        }
219
220
        if ($account = models('users')->findWhere([$column => $username], 1)) {
221
            $account = $account->getArrayCopy();
222
223
            foreach ($account as $key => $value) {
224
                if (strpos($key, 'record') !== false) {
225
                    unset($account[ $key ]);
226
                } elseif (in_array($key, ['password', 'pin', 'token', 'sso'])) {
227
                    unset($account[ $key ]);
228
                }
229
            }
230
231
            if ($column === 'token') {
232
                models('users')->update([
0 ignored issues
show
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

232
                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\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

232
                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...
233
                    'id'    => $account[ 'id' ],
234
                    'token' => null,
235
                ]);
236
            }
237
238
            $this->login($account);
239
240
            return true;
241
        }
242
243
        return false;
244
    }
245
246
    // ------------------------------------------------------------------------
247
248
    /**
249
     * User::getIframeCode
250
     *
251
     * @return string
252
     * @throws \Psr\Cache\InvalidArgumentException
253
     */
254
    public function getIframeCode()
255
    {
256
        if ($this->signedOn() && $this->loggedIn() === false) {
257
            return '<iframe id="sign-on-iframe" width="1" height="1" src="' . rtrim($this->config[ 'sso' ][ 'server' ],
258
                    '/') . '" style="display: none; visibility: hidden;"></iframe>';
259
        }
260
261
        return '';
262
    }
263
}