SecurityHelper   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 1
cbo 9
dl 0
loc 42
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A amLoggedInAs() 0 22 1
A seeIsGranted() 0 6 1
1
<?php
2
namespace AppBundle\Helper;
3
4
use Symfony\Component\BrowserKit\Cookie;
5
use Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken;
6
7
class SecurityHelper extends AbstractSymfonyHelper
8
{
9
    /**
10
     * Pretend user is logged in
11
     *
12
     * @param $username
13
     */
14
    public function amLoggedInAs($username)
15
    {
16
        $firewallContext = 'main';
17
18
        /** @var \AppBundle\Entity\Repository\UserRepository $userRepository */
19
        $userRepository = $this->getSymfonyModule()->grabService('app.entity.repository.user');
20
21
        /** @var \Symfony\Component\HttpFoundation\Session\Session $session */
22
        $session = $this->getSymfonyModule()->grabService('session');
23
24
        // Fetch user
25
        $user = $userRepository->findOneBy(['login' => $username]);
26
27
        // Create token and save to session and cookie
28
        $token = new PostAuthenticationGuardToken($user, $firewallContext, $user->getRoles());
0 ignored issues
show
Bug introduced by
It seems like $user defined by $userRepository->findOne...('login' => $username)) on line 25 can be null; however, Symfony\Component\Securi...ardToken::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
29
30
        $session->set('_security_'.$firewallContext, serialize($token));
31
        $session->save();
32
33
        $cookieJar = $this->getSymfonyModule()->client->getCookieJar();
34
        $cookieJar->set(new Cookie($session->getName(), $session->getId()));
35
    }
36
37
    /**
38
     * Checks if the attributes are granted against the current authentication token
39
     *
40
     * @param mixed $attributes
41
     */
42
    public function seeIsGranted($attributes)
43
    {
44
        /** @var \Symfony\Component\Security\Core\Authorization\AuthorizationChecker $authorizationChecker */
45
        $authorizationChecker = $this->getSymfonyModule()->grabService('security.authorization_checker');
46
        $this->assertTrue($authorizationChecker->isGranted($attributes));
47
    }
48
}
49