Completed
Push — master ( 26a679...63e609 )
by Patrick
11:16 queued 11s
created

AuthProvider::getTempUserByHash()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 9
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * AuthProvider class
4
 *
5
 * This file describes the AuthProvider Singleton
6
 *
7
 * PHP version 5 and 7
8
 *
9
 * @author Patrick Boyd / [email protected]
10
 * @copyright Copyright (c) 2015, Austin Artistic Reconstruction
11
 * @license http://www.apache.org/licenses/ Apache 2.0 License
12
 */
13
14
/**
15
 * Allow other classes to be loaded as needed
16
 */
17
require_once('Autoload.php');
18
19
/**
20
 * A Singleton class to abstract access to the authentication providers.
21
 *
22
 * This class is the primary method to access user data, login, and other authenication information.
23
 */
24
class AuthProvider extends Provider
25
{
26
    /**
27
     * Load the authentrication providers specified in the Settings $authProviders array
28
     *
29
     * @SuppressWarnings("StaticAccess")
30
     */
31
    protected function __construct()
32
    {
33
        $settings = \Settings::getInstance();
34
        $this->methods = $settings->getClassesByPropName('authProviders');
35
    }
36
37
    /**
38
     * Get the Auth\User class instance for the specified login
39
     *
40
     * Unlike the AuthProvider::login() function. This function will not impact the SESSION
41
     *
42
     * @param string $username The username of the User
43
     * @param string $password The password of the User
44
     *
45
     * @return Auth\User|false The User with the specified credentials or false if the credentials are not valid
46
     */
47
    public function getUserByLogin($username, $password)
48
    {
49
        $res = false;
50
        $count = count($this->methods);
51
        for($i = 0; $i < $count; $i++)
52
        {
53
            $res = $this->methods[$i]->login($username, $password);
54
            if($res !== false)
55
            {
56
                return $this->methods[$i]->getUser($res);
57
            }
58
        }
59
        return $res;
60
    }
61
62
    /**
63
     * Use the provided credetials to log the user on
64
     *
65
     * @param string $username The username of the User
66
     * @param string $password The password of the User
67
     *
68
     * @return true|false true if the login was successful, false otherwise
69
     */
70
    public function login($username, $password)
71
    {
72
        $res = false;
73
        $count = count($this->methods);
74
        for($i = 0; $i < $count; $i++)
75
        {
76
            $res = $this->methods[$i]->login($username, $password);
77
            if($res !== false)
78
            {
79
                if(isset($res['extended']) && isset($res['extended']['jpegphoto']))
80
                {
81
                    $res['extended']['jpegphoto']=true;
82
                }
83
                FlipSession::setVar('AuthMethod', get_class($this->methods[$i]));
84
                FlipSession::setVar('AuthData', $res);
85
                break;
86
            }
87
        }
88
        return $res;
89
    }
90
91
    /**
92
     * Determine if the user is still logged on from the session data
93
     *
94
     * @param stdClass $data The AuthData from the session
95
     * @param string $methodName The AuthMethod from the session
96
     *
97
     * @return true|false true if user is logged on, false otherwise
98
     */
99
    public function isLoggedIn($data, $methodName)
100
    {
101
        $auth = $this->getMethodByName($methodName);
102
        return $auth->isLoggedIn($data);
103
    }
104
105
    /**
106
     * Obtain the currently logged in user from the session data
107
     *
108
     * @param stdClass $data The AuthData from the session
109
     * @param string $methodName The AuthMethod from the session
110
     *
111
     * @return Auth\User|false The User instance if user is logged on, false otherwise
112
     */
113
    public function getUser($data, $methodName)
114
    {
115
        $auth = $this->getMethodByName($methodName);
116
        return $auth->getUser($data);
117
    }
118
119
    /**
120
     * Merge or set the returnValue as appropriate
121
     *
122
     * @param false|Auth\Group|Auth\User $returnValue The value to merge to
123
     * @param Auth\Group|Auth\User $res The value to merge from
124
     *
125
     * @return Auth\Group|false The merged returnValue
126
     */
127
    public function mergeResult(&$returnValue, $res)
128
    {
129
        if($res === false)
130
        {
131
            return;
132
        }
133
        if($returnValue === false)
134
        {
135
            $returnValue = $res;
136
            return;
137
        }
138
        $returnValue->merge($res);
139
    }
140
141
    /**
142
     * Get an Auth\Group by its name
143
     *
144
     * @param string $name The name of the group
145
     * @param string $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
146
     *
147
     * @return Auth\Group|false The Group instance if a group with that name exists, false otherwise
148
     */
149 View Code Duplication
    public function getGroupByName($name, $methodName = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
    {
151
        if($methodName === false)
152
        {
153
            return $this->callOnEach('getGroupByName', array($name));
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->callOnEach('getGr...ByName', array($name)); of type Auth\Group|Auth\User|false adds the type Auth\User to the return on line 153 which is incompatible with the return type documented by AuthProvider::getGroupByName of type Auth\Group|false.
Loading history...
154
        }
155
        $auth = $this->getMethodByName($methodName);
156
        return $auth->getGroupByName($name);
157
    }
158
159
    /**
160
     * Get an array of Auth\User from a filtered set
161
     *
162
     * @param Data\Filter|boolean $filter The filter conditions or false to retreive all
163
     * @param array|boolean $select The user fields to obtain or false to obtain all
164
     * @param integer|boolean $top The number of users to obtain or false to obtain all
165
     * @param integer|boolean $skip The number of users to skip or false to skip none
166
     * @param array|boolean $orderby The field to sort by and the method to sort or false to not sort
167
     * @param string|boolean $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
168
     *
169
     * @return array|boolean An array of Auth\User objects or false if no users were found
170
     */
171
    public function getUsersByFilter($filter, $select = false, $top = false, $skip = false, $orderby = false, $methodName = false)
172
    {
173
        return $this->callFunction($methodName, 'getUsersByFilter', array($filter, $select, $top, $skip, $orderby), 
174
                                    'current', false, array($this, 'mergeResult'));
175
    }
176
177
    /**
178
     * Get an array of Auth\PendingUser from a filtered set
179
     *
180
     * @param Data\Filter|boolean $filter The filter conditions or false to retreive all
181
     * @param array|boolean $select The user fields to obtain or false to obtain all
182
     * @param integer|boolean $top The number of users to obtain or false to obtain all
183
     * @param integer|boolean $skip The number of users to skip or false to skip none
184
     * @param array|boolean $orderby The field to sort by and the method to sort or false to not sort
185
     * @param string|boolean $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
186
     *
187
     * @return array|boolean An array of Auth\PendingUser objects or false if no pending users were found
188
     */
189
    public function getPendingUsersByFilter($filter, $select = false, $top = false, $skip = false, $orderby = false, $methodName = false)
190
    {
191
        return $this->callFunction($methodName, 'getPendingUsersByFilter', array($filter, $select, $top, $skip, $orderby),
192
                                    'pending', false, array($this, 'mergeResult'));
193
    }
194
195
    /**
196
     * Get an array of Auth\Group from a filtered set
197
     *
198
     * @param Data\Filter|false $filter The filter conditions or false to retreive all
199
     * @param array|false $select The group fields to obtain or false to obtain all
200
     * @param integer|false $top The number of groups to obtain or false to obtain all
201
     * @param integer|false $skip The number of groups to skip or false to skip none
202
     * @param array|false $orderby The field to sort by and the method to sort or false to not sort
203
     * @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
204
     *
205
     * @return array|false An array of Auth\Group objects or false if no pending users were found
206
     */
207
    public function getGroupsByFilter($filter, $select = false, $top = false, $skip = false, $orderby = false, $methodName = false)
208
    {
209
        return $this->callFunction($methodName, 'getGroupsByFilter', array($filter, $select, $top, $skip, $orderby),
210
                                    'current', false, array($this, 'mergeResult'));
211
    }
212
213
    /**
214
     * Get the number of currently active users on the system
215
     *
216
     * @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
217
     *
218
     * @return integer The number of currently active users on the system
219
     */
220
    public function getActiveUserCount($methodName = false)
221
    {
222
        if($methodName === false)
223
        {
224
            return $this->addFromEach('getActiveUserCount', 'current');
225
        }
226
        $auth = $this->getMethodByName($methodName);
227
        return $auth->getActiveUserCount();
228
    }
229
230
    /**
231
     * Get the number of currently pending users on the system
232
     *
233
     * @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
234
     *
235
     * @return integer The number of currently pending users on the system
236
     */
237
    public function getPendingUserCount($methodName = false)
238
    {
239
        if($methodName === false)
240
        {
241
            return $this->addFromEach('getPendingUserCount', 'pending');
242
        }
243
        $auth = $this->getMethodByName($methodName);
244
        return $auth->getPendingUserCount();
245
    }
246
247
    /**
248
     * Get the number of current groups on the system
249
     *
250
     * @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
251
     *
252
     * @return integer The number of current groups on the system
253
     */
254
    public function getGroupCount($methodName = false)
255
    {
256
        if($methodName === false)
257
        {
258
            return $this->addFromEach('getGroupCount', 'current');
259
        }
260
        $auth = $this->getMethodByName($methodName);
261
        return $auth->getGroupCount();
262
    }
263
264
    /**
265
     * Get the login links for all supplementary Authenitcation mechanisms
266
     *
267
     * This will return an array of links to any supplementary authentication mechanims. For example, Goodle is 
268
     * a supplementary authentication mechanism.
269
     *
270
     * @return array An array of suppmentary authentication mechanism links
271
     */
272
    public function getSupplementaryLinks()
273
    {
274
        $ret = array();
275
        $count = count($this->methods);
276 View Code Duplication
        for($i = 0; $i < $count; $i++)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
277
        {
278
            if($this->methods[$i]->supplement === false)
279
            {
280
                continue;
281
            }
282
283
            array_push($ret, $this->methods[$i]->getSupplementLink());
284
        }
285
        return $ret;
286
    }
287
288
    /**
289
     * Impersonate the user specified
290
     *
291
     * This will replace the user in the session with the specified user. In order
292
     * to undo this operation a user must logout.
293
     *
294
     * @param array|Auth\User $userArray Data representing the user
295
     */
296
    public function impersonateUser($userArray)
297
    {
298
        if(!is_object($userArray))
299
        {
300
            $userArray = new $userArray['class']($userArray);
301
        }
302
        \FlipSession::setUser($userArray);
303
    }
304
305
    /**
306
     * Get the pending user reresented by the supplied hash
307
     *
308
     * @param string $hash The hash value representing the Penging User
309
     * @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
310
     *
311
     * @return Auth\PendingUser|false The Auth\PendingUser instance or false if no user is matched by the provided hash
312
     */
313 View Code Duplication
    public function getTempUserByHash($hash, $methodName = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
314
    {
315
        if($methodName === false)
316
        {
317
            return $this->callOnEach('getTempUserByHash', array($hash), 'pending');
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->callOnEach('getTe...ray($hash), 'pending'); of type Auth\Group|Auth\User|false adds the type Auth\Group to the return on line 317 which is incompatible with the return type documented by AuthProvider::getTempUserByHash of type Auth\PendingUser|false.
Loading history...
318
        }
319
        $auth = $this->getMethodByName($methodName);
320
        return $auth->getTempUserByHash($hash);
321
    }
322
323
    /**
324
     * Create a pending user
325
     *
326
     * @param array $user An array of information about the user to create
327
     * @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
328
     *
329
     * @return boolean true if the user was successfully created. Otherwise false.
330
     */
331 View Code Duplication
    public function createPendingUser($user, $methodName = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
332
    {
333
        if($methodName === false)
334
        {
335
            $count = count($this->methods);
336
            for($i = 0; $i < $count; $i++)
337
            {
338
                if($this->methods[$i]->pending === false)
339
                {
340
                    continue;
341
                }
342
343
                $ret = $this->methods[$i]->createPendingUser($user);
344
                if($ret !== false)
345
                {
346
                    return true;
347
                }
348
            }
349
            return false;
350
        }
351
        $auth = $this->getMethodByName($methodName);
352
        return $auth->createPendingUser($user);
353
    }
354
355
    /**
356
     * Convert a Auth\PendingUser into an Auth\User
357
     *
358
     * This will allow a previously pending user the ability to log on in the future as an active user. It will also
359
     * have the side effect of logging the user on now.
360
     *
361
     * @param Auth\PendingUser $user The user to turn into a current user
362
     * @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
363
     *
364
     * @return boolean true if the user was successfully created. Otherwise false.
365
     */
366 View Code Duplication
    public function activatePendingUser($user, $methodName = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
367
    {
368
        if($methodName === false)
369
        {
370
            $count = count($this->methods);
371
            for($i = 0; $i < $count; $i++)
372
            {
373
                if($this->methods[$i]->current === false)
374
                {
375
                    continue;
376
                }
377
378
                $ret = $this->methods[$i]->activatePendingUser($user);
379
                if($ret !== false)
380
                {
381
                    $this->impersonateUser($ret);
382
                    return true;
383
                }
384
            }
385
            return false;
386
        }
387
        $auth = $this->getMethodByName($methodName);
388
        return $auth->activatePendingUser($user);
389
    }
390
391
    /**
392
     * Get a current user by a password reset hash
393
     *
394
     * @param string $hash The current password reset hash for the user
395
     * @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
396
     *
397
     * @return Auth\User|false The user if the password reset hash is valid. Otherwise false.
398
     */
399
    public function getUserByResetHash($hash, $methodName = false)
400
    {
401
        if($methodName === false)
402
        {
403
            return $this->callOnEach('getUserByResetHash', array($hash), 'current');
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->callOnEach('getUs...ray($hash), 'current'); of type Auth\Group|Auth\User|false adds the type Auth\Group to the return on line 403 which is incompatible with the return type documented by AuthProvider::getUserByResetHash of type Auth\User|false.
Loading history...
404
        }
405
        $auth = $this->getMethodByName($methodName);
406
        if($auth === false)
407
        {
408
            return $this->getUserByResetHash($hash, false);
409
        }
410
        return $auth->getUserByResetHash($hash);
411
    }
412
413
    /**
414
     * Get the Auth\Authenticator by host name
415
     *
416
     * @param string $host The host name used by the supplemental authentication mechanism
417
     *
418
     * @return Auth\Authenticator|false The Authenticator if the host is supported by a loaded Authenticator. Otherwise false.
419
     */
420
    public function getSuplementalProviderByHost($host)
421
    {
422
        $count = count($this->methods);
423 View Code Duplication
        for($i = 0; $i < $count; $i++)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
424
        {
425
            if($this->methods[$i]->supplement === false)
426
            {
427
                continue;
428
            }
429
430
            if($this->methods[$i]->getHostName() === $host)
431
            {
432
                return $this->methods[$i];
433
            }
434
        }
435
        return false;
436
    }
437
438
    /**
439
     * Delete any pending users that match the filter
440
     *
441
     * @param \Data\Filter|boolean $filter The filter to delete with or false to delete all
442
     * @param string|boolean $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
443
     *
444
     * @return boolean True if the users were deleted, false otherwise
445
     */
446
    public function deletePendingUsersByFilter($filter, $methodName = false)
447
    {
448
        $users = $this->getPendingUsersByFilter($filter, false, false, false, false, $methodName);
449
        if($users === false)
450
        {
451
            return false;
452
        }
453
        $count = count($users);
454
        for($i = 0; $i < $count; $i++)
455
        {
456
            $users[$i]->delete();
457
        }
458
        return true;
459
    }
460
461
    /**
462
     * Get the user by the one time access code
463
     *
464
     * @param string $key The user's access code
465
     * @param string|boolean $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
466
     *
467
     * @return boolean|\Auth\User The User specified by the access code or false otherwise
468
     */
469 View Code Duplication
    public function getUserByAccessCode($key, $methodName = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
470
    {
471
        if($methodName === false)
472
        {
473
            return $this->callOnEach('getUserByAccessCode', array($key), 'current');
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->callOnEach('getUs...rray($key), 'current'); of type Auth\Group|Auth\User|false adds the type Auth\Group to the return on line 473 which is incompatible with the return type documented by AuthProvider::getUserByAccessCode of type boolean|Auth\User.
Loading history...
474
        }
475
        $auth = $this->getMethodByName($methodName);
0 ignored issues
show
Bug introduced by
It seems like $methodName defined by parameter $methodName on line 469 can also be of type boolean; however, Provider::getMethodByName() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
476
        return $auth->getUserByAccessCode($key);
477
    }
478
}
479
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
480