Passed
Push — master ( c7e265...e8c097 )
by Xavier
03:54
created

AuthExtension::auth()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 15
nc 2
nop 0
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
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
Bug introduced by
It seems like env('LDAP_HOST') can also be of type array; however, parameter $hostname of ldap_connect() does only seem to accept string, 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(/** @scrutinizer ignore-type */ env('LDAP_HOST'), 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');
48
        $this->administrators = explode(',', env('ADMINISTRATORS'));
0 ignored issues
show
Bug introduced by
It seems like env('ADMINISTRATORS') can also be of type array; however, parameter $string of explode() does only seem to accept string, 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

48
        $this->administrators = explode(',', /** @scrutinizer ignore-type */ env('ADMINISTRATORS'));
Loading history...
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
70
        if (isset($_SESSION['phpCAS']['user'])) {
71
            $logged = true;
72
            $username = $_SESSION['phpCAS']['user'];
73
            $query = ldap_search($this->ldap, $this->baseDN, "uid=$username");
74
            $name = ldap_get_entries($this->ldap, $query)[0]['displayname'][0];
75
            $isAdmin = (in_array($username, $this->administrators));
76
        }
77
78
        return (object)
79
        [
80
            'isLogged' => $logged,
81
            'isAdmin' =>  $isAdmin,
82
            'username' => $username,
83
            'name' => $name
84
        ];
85
    }
86
}
87