6
6
7
7
namespace Chamilo \CoreBundle \EventListener ;
8
8
9
+ use Chamilo \CoreBundle \Helpers \AccessUrlHelper ;
10
+ use Chamilo \CoreBundle \Helpers \UserHelper ;
9
11
use Chamilo \CoreBundle \Repository \LanguageRepository ;
12
+ use Chamilo \CoreBundle \Repository \Node \AccessUrlRepository ;
10
13
use Symfony \Component \HttpKernel \Event \ControllerEvent ;
11
- use Symfony \Component \Security \Core \Authentication \Token \Storage \TokenStorageInterface ;
12
- use Symfony \Component \Security \Core \User \UserInterface ;
13
14
use Symfony \Component \Serializer \SerializerInterface ;
14
15
use Twig \Environment ;
15
16
@@ -21,33 +22,32 @@ class TwigListener
21
22
public function __construct (
22
23
private readonly Environment $ twig ,
23
24
private readonly SerializerInterface $ serializer ,
24
- private readonly TokenStorageInterface $ tokenStorage ,
25
25
private readonly LanguageRepository $ languageRepository ,
26
+ private readonly UserHelper $ userHelper ,
27
+ private readonly AccessUrlHelper $ accessUrlHelper ,
26
28
) {}
27
29
28
30
public function __invoke (ControllerEvent $ event ): void
29
31
{
30
- $ request = $ event -> getRequest ();
31
- $ token = $ this ->tokenStorage -> getToken ();
32
+ $ currentAccessUrl = $ this -> accessUrlHelper -> getCurrent ();
33
+ $ user = $ this ->userHelper -> getCurrent ();
32
34
33
35
$ data = null ;
34
36
$ isAuth = false ;
35
- if (null !== $ token ) {
36
- $ user = $ token ->getUser ();
37
- if ($ user instanceof UserInterface) {
38
- $ data = $ this ->serializer ->serialize ($ user , 'jsonld ' , [
39
- 'groups ' => ['user_json:read ' ],
40
- ]);
41
- $ isAuth = true ;
42
- }
37
+ if ($ user ) {
38
+ $ data = $ this ->serializer ->serialize ($ user , 'jsonld ' , [
39
+ 'groups ' => ['user_json:read ' ],
40
+ ]);
41
+ $ isAuth = true ;
43
42
}
44
43
45
44
$ languages = $ this ->languageRepository ->getAllAvailable ()->getQuery ()->getArrayResult ();
46
45
47
46
// $this->twig->addGlobal('text_direction', api_get_text_direction());
48
47
$ this ->twig ->addGlobal ('is_authenticated ' , json_encode ($ isAuth ));
49
48
$ this ->twig ->addGlobal ('user_json ' , $ data ?? json_encode ([]));
50
- $ this ->twig ->addGlobal ('access_url_id ' , $ request ->getSession ()->get ('access_url_id ' ));
49
+ $ this ->twig ->addGlobal ('is_login_url ' , (int ) $ currentAccessUrl ->isLoginOnly ());
50
+ $ this ->twig ->addGlobal ('access_url_id ' , $ currentAccessUrl ->getId ());
51
51
$ this ->twig ->addGlobal ('languages_json ' , json_encode ($ languages ));
52
52
}
53
53
}
0 commit comments