|
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')); |
|
|
|
|
|
|
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')); |
|
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
|
|
|
|