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.

LdapUserProvider::loadUserByUsername()   A
last analyzed

Complexity

Conditions 2
Paths 7

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 2
nc 7
nop 1
1
<?php
2
namespace SOG\Dashboard\Authentication;
3
4
use SOG\Dashboard\LdapAdapter;
5
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
6
use Symfony\Component\Security\Core\User\UserInterface;
7
use Symfony\Component\Security\Core\User\UserProviderInterface;
8
use Zend\Ldap\Exception\LdapException;
9
use Zend\Ldap\Ldap;
10
11
/**
12
 * This class maps a requested user to its LDAP entry.
13
 * The implementation follows https://github.com/DerManoMann/ldap-auth-service-provider - thank you!
14
 *
15
 * Class LdapUserProvider
16
 * @package SOG\Dashboard\Authentication
17
 */
18
class LdapUserProvider implements UserProviderInterface
19
{
20
    /**
21
     * @var LdapAdapter The LDAP resource
22
     */
23
    private $ldap;
24
25
    public function __construct(LdapAdapter $ldap)
26
    {
27
        $this->ldap = $ldap;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function refreshUser(UserInterface $user)
34
    {
35
        // This could be implemented in a different manner (session?) to reduce server load
36
        return $this->loadUserByUsername($user->getUsername());
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function loadUserByUsername($username)
43
    {
44
        try {
45
            // first retrieve the full DN, this might throw an exception
46
            $dn = $this->ldap->getCanonicalAccountName($username, LDAP::ACCTNAME_FORM_DN);
47
            // then get all associated attributes
48
            $attributes = $this->ldap->getEntry($dn);
49
            // also get the groups the user is member of
50
            $memberships = $this->ldap->getMemberships($dn, ['cn', 'ou', 'mailinglistId'])->toArray();
51
            // and maybe the owned groups
52
            $ownerships = $this->ldap->getOwnedGroups($dn)->toArray();
53
            // assign the user's roles
54
            $roles = $this->getRoles($dn, $ownerships);
55
            return new LdapUser($username, null, $attributes, $roles, $memberships, $ownerships);
0 ignored issues
show
Bug introduced by
It seems like $attributes defined by $this->ldap->getEntry($dn) on line 48 can also be of type null; however, SOG\Dashboard\Authentica...LdapUser::__construct() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
56
        } catch (LdapException $ex) {
57
          throw new UsernameNotFoundException($ex->getMessage().'Der Login war nicht erfolgreich, bitte überprüfe deinen Benutzernamen und Passwort.');
58
          $logger = $this->get('logger');
0 ignored issues
show
Unused Code introduced by
$logger = $this->get('logger'); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
59
          $logger->error(($ex->getMessage()));
60
        }
61
    }
62
63
    /**
64
     * Infer the rules of the given user by checking the LDAP resource. This can be extended to accommodate for
65
     * ROLE_ADMIN or other cases
66
     *
67
     * @param string $user_dn The user DN for which to infer the rules
68
     * @param array $ownerships The owned groups for the user DN
69
     * @return array The roles of the given user
70
     */
71
    private function getRoles($user_dn, $ownerships)
0 ignored issues
show
Unused Code introduced by
The parameter $user_dn 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...
72
    {
73
        $roles = [];
74
        if (count($ownerships) > 0) {
75
            $roles[] = 'ROLE_GROUP_ADMIN';
76
        } else {
77
            $roles[] = 'ROLE_USER';
78
        }
79
        return $roles;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function supportsClass($class)
86
    {
87
        return ($class === '\\SOG\\Dashboard\\Authentication\\LdapUser');
88
    }
89
90
}
91