Completed
Pull Request — develop (#609)
by Narcotic
12:02 queued 07:18
created

AuthenticationProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 86
ccs 0
cts 14
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A loadUserByUsername() 0 14 4
A refreshUser() 0 8 1
A supportsClass() 0 4 1
A __construct() 0 5 1
1
<?php
2
/**
3
 * airlock authkey based consultant user provider
4
 */
5
6
namespace Graviton\SecurityBundle\Authentication\Provider;
7
8
use \Graviton\RestBundle\Model\ModelInterface;
9
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
10
use Symfony\Component\Security\Core\User\UserInterface;
11
use Symfony\Component\Security\Core\User\UserProviderInterface;
12
13
/**
14
 * Class AirlockAuthenticationKeyConsultantProvider
15
 *
16
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
17
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
18
 * @link     http://swisscom.ch
19
 */
20
class AuthenticationProvider implements UserProviderInterface
21
{
22
    /**
23
     * @var ModelInterface
24
     */
25
    private $documentModel;
26
27
    /**
28
     * @var String
29
     */
30
    private $queryField;
31
32
    /**
33
     * @param ModelInterface $model      The documentModel
34
     * @param string         $queryField Field to search by
35
     */
36
    public function __construct(ModelInterface $model, $queryField)
37
    {
38
        $this->documentModel = $model;
39
        $this->queryField = $queryField;
40
    }
41
42
    /**
43
     * Loads the user for the given username.
44
     *
45
     * This method must throw UsernameNotFoundException if the user is not
46
     * found.
47
     *
48
     * @param string $username the consultants username
49
     *
50
     * @return \Symfony\Component\Security\Core\User\UserInterface
51
     *
52
     * @see \Symfony\Component\Security\Core\Exception\UsernameNotFoundException
53
     *
54
     * @throws \Symfony\Component\Security\Core\Exception\UsernameNotFoundException if the user is not found
55
     */
56
    public function loadUserByUsername($username)
57
    {
58
        if (!$this->queryField || !$username) {
59
            return false;
60
        }
61
62
        $user = $this->documentModel->getRepository()->findOneBy(
63
            [
64
                $this->queryField => new \MongoRegex('/^'.preg_quote($username).'$/i')
65
            ]
66
        );
67
68
        return $user ? $user : false;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression $user ? $user : false; of type object|false adds false to the return on line 68 which is incompatible with the return type declared by the interface Symfony\Component\Securi...ace::loadUserByUsername of type Symfony\Component\Security\Core\User\UserInterface. It seems like you forgot to handle an error condition.
Loading history...
69
    }
70
71
    /**
72
     * Refreshes the user for the account interface.
73
     *
74
     * It is up to the implementation to decide if the user data should be
75
     * totally reloaded (e.g. from the database), or if the UserInterface
76
     * object can just be merged into some internal array of users / identity
77
     * map.
78
     *
79
     * @param \Symfony\Component\Security\Core\User\UserInterface $user user to refresh
80
     *
81
     * @return \Symfony\Component\Security\Core\User\UserInterface
82
     *
83
     * @throws \Symfony\Component\Security\Core\Exception\UnsupportedUserException if the account is not supported
84
     */
85
    public function refreshUser(UserInterface $user)
86
    {
87
        // this is used for storing authentication in the session
88
        // but in this example, the token is sent in each request,
89
        // so authentication can be stateless. Throwing this exception
90
        // is proper to make things stateless
91
        throw new UnsupportedUserException();
92
    }
93
94
    /**
95
     * Whether this provider supports the given user class.
96
     *
97
     * @param string $class class to check for support
98
     *
99
     * @return bool
100
     */
101
    public function supportsClass($class)
102
    {
103
        return $class instanceof \Symfony\Component\Security\Core\User\UserInterface;
104
    }
105
}
106