Completed
Push — feature/evo-2472-whoami ( c50209...9b4500 )
by Jan
30:40
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * airlock authkey based user provider
4
 */
5
6
namespace Graviton\SecurityBundle\Authentication\Provider;
7
8
use Graviton\SecurityBundle\Entities\SecurityContract;
9
use GravitonDyn\ContractBundle\Document\Contract;
10
use \Graviton\RestBundle\Model\ModelInterface;
11
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
12
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
13
use Symfony\Component\Security\Core\User\UserInterface;
14
use Symfony\Component\Security\Core\User\UserProviderInterface;
15
16
/**
17
 * Class AirlockAuthenticationKeyUserProvider
18
 *
19
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
20
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
21
 * @link     http://swisscom.ch
22
 */
23
class AirlockAuthenticationKeyUserProvider implements UserProviderInterface
24
{
25
    /**
26
     * @var \Graviton\RestBundle\Model\ModelInterface
27
     */
28
    private $documentModel;
29
30
    /**
31
     * @param \Graviton\RestBundle\Model\ModelInterface $contract contract to use as documentModel
32
     */
33
    public function __construct(ModelInterface $contract)
34
    {
35
        $this->documentModel = $contract;
36
    }
37
38
    /**
39
     * Finds a contract based on the provided ApiKey.
40
     *
41
     * @param string $apiKey key from airlock
42
     *
43
     * @return string
44
     */
45 View Code Duplication
    public function getUsernameForApiKey($apiKey)
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...
46
    {
47
        $contractId = '';
48
49
        /** @var \GravitonDyn\ContractBundle\Document\Contract $contract */
50
        $contract = $this->documentModel->getRepository()->findOneBy(array('number' => $apiKey));
51
52
        if ($contract instanceof Contract) {
0 ignored issues
show
Bug introduced by
The class GravitonDyn\ContractBundle\Document\Contract does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
53
            $contractId = $contract->getId();
54
        }
55
56
        return $contractId;
57
    }
58
59
    /**
60
     * Loads the user for the given username.
61
     *
62
     * This method must throw UsernameNotFoundException if the user is not
63
     * found.
64
     *
65
     * @param string $contractId contract id we need a username for
66
     *
67
     * @return \Symfony\Component\Security\Core\User\UserInterface
68
     *
69
     * @see \Symfony\Component\Security\Core\Exception\UsernameNotFoundException
70
     *
71
     * @throws \Symfony\Component\Security\Core\Exception\UsernameNotFoundException if the user is not found
72
     */
73
    public function loadUserByUsername($contractId)
74
    {
75
        // TODO [lapistano] to what is the contract to be mapped against??
76
77
        /** @var \GravitonDyn\ContractBundle\Document\Contract $contracts */
78
        $contract = $this->documentModel->find($contractId);
79
80
        if ($contract instanceof Contract) {
0 ignored issues
show
Bug introduced by
The class GravitonDyn\ContractBundle\Document\Contract does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
81
            // TODO [lapistano]: map the found contract to whatever ...
82
            return new SecurityContract($contract, $this->getContractRoles($contract));
0 ignored issues
show
Documentation introduced by
$this->getContractRoles($contract) is of type array<integer,string,{"0":"string"}>, but the function expects a array<integer,object<Sym...curity\Core\Role\Role>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83
        }
84
85
        throw new UsernameNotFoundException();
86
    }
87
88
    /**
89
     * Refreshes the user for the account interface.
90
     *
91
     * It is up to the implementation to decide if the user data should be
92
     * totally reloaded (e.g. from the database), or if the UserInterface
93
     * object can just be merged into some internal array of users / identity
94
     * map.
95
     *
96
     * @param \Symfony\Component\Security\Core\User\UserInterface $user user to refresh
97
     *
98
     * @return \Symfony\Component\Security\Core\User\UserInterface
99
     *
100
     * @throws \Symfony\Component\Security\Core\Exception\UnsupportedUserException if the account is not supported
101
     */
102
    public function refreshUser(UserInterface $user)
103
    {
104
        // this is used for storing authentication in the session
105
        // but in this example, the token is sent in each request,
106
        // so authentication can be stateless. Throwing this exception
107
        // is proper to make things stateless
108
        throw new UnsupportedUserException();
109
    }
110
111
    /**
112
     * Whether this provider supports the given user class.
113
     *
114
     * @param string $class class to check for support
115
     *
116
     * @return bool
117
     */
118
    public function supportsClass($class)
119
    {
120
        return $class instanceof \Symfony\Component\Security\Core\User\UserInterface;
121
    }
122
123
    /**
124
     * Decides the role set the provided contract has.
125
     *
126
     * @param Contract $contract provided contract
127
     *
128
     * @return string[]
129
     */
130
    private function getContractRoles(Contract $contract)
0 ignored issues
show
Unused Code introduced by
The parameter $contract is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
    {
132
        // TODO [lapistano]: implement the ability to decide what roles the contract entity haas.
133
134
        return array('ROLE_GRAVITON_USER');
135
    }
136
}
137