Issues (32)

src/TwigExtension/AuthExtension.php (2 issues)

1
<?php
2
3
/*
4
 * @author  Xavier Chopin <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace App\TwigExtension;
11
12
use Symfony\Component\DependencyInjection\ContainerBuilder;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
use Twig_Extension;
15
use Twig_SimpleFunction;
16
17
class AuthExtension extends Twig_Extension
18
{
19
    /**
20
     *
21
     * @var resource LDAP Instance
22
     */
23
    protected $ldap;
24
25
    /**
26
     * @var string Base Distinguished Name
27
     */
28
    protected $baseDN;
29
30
    /**
31
     * @var array List of the super admin written in app/config/parameters.yml
32
     */
33
    protected $administrators;
34
35
    /**
36
     * AuthExtension constructor.
37
     *
38
     * @param ContainerInterface $container
39
     */
40
    public function __construct(ContainerInterface $container)
41
    {
42
        $ldapInstance = ldap_connect(env('LDAP_HOST'), env('LDAP_PORT'));
0 ignored issues
show
It seems like env('LDAP_PORT') can also be of type string and boolean; however, parameter $port of ldap_connect() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
        $ldapInstance = ldap_connect(env('LDAP_HOST'), /** @scrutinizer ignore-type */ env('LDAP_PORT'));
Loading history...
43
        ldap_set_option($ldapInstance, LDAP_OPT_PROTOCOL_VERSION, 3);
44
        ldap_set_option($ldapInstance, LDAP_OPT_REFERRALS, 0);
45
        $container->set('ldap', /** @scrutinizer ignore-type */ $ldapInstance);
46
        $this->ldap = $ldapInstance;
47
        $this->baseDN = env('LDAP_BASE_DN');
0 ignored issues
show
Documentation Bug introduced by
It seems like env('LDAP_BASE_DN') can also be of type boolean. However, the property $baseDN is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
48
        $this->administrators = explode(',', env('ADMINISTRATORS'));
49
    }
50
51
    public function getName()
52
    {
53
        return 'auth';
54
    }
55
56
    public function getFunctions()
57
    {
58
        return [
59
            new Twig_SimpleFunction('auth', [$this, 'auth'])
60
        ];
61
    }
62
63
    public function auth()
64
    {
65
        $name = null;
66
        $logged = false;
67
        $isAdmin = false;
68
        $username = null;
69
        $viewAsMode = false;
70
        $originalUsername = null;
71
72
        if (isset($_SESSION['phpCAS']['user'])) {
73
            $logged = true;
74
            $username = $_SESSION['phpCAS']['user'];
75
            $query = ldap_search($this->ldap, $this->baseDN, "uid=$username");
76
            $name = ldap_get_entries($this->ldap, $query)[0]['displayname'][0];
77
            $isAdmin = (in_array($username, $this->administrators));
78
79
            if (isset($_SESSION['username'])) {
80
                $viewAsMode = true;
81
                $originalUsername = $_SESSION['username'];
82
            }
83
        }
84
85
        return (object)
86
        [
87
            'isLogged' => $logged,
88
            'isAdmin' =>  $isAdmin,
89
            'username' => $username,
90
            'name' => $name,
91
            'viewAsMode' => $viewAsMode,
92
            'originalUsername' => $originalUsername
93
        ];
94
    }
95
}
96