66namespace EzSystems \RecommendationBundle \EventListener ;
77
88use eZ \Publish \API \Repository \UserService ;
9+ use Symfony \Component \Security \Core \Authentication \Token \TokenInterface ;
910use Symfony \Component \Security \Core \Authorization \AuthorizationChecker ;
1011use Symfony \Component \Security \Http \Event \InteractiveLoginEvent ;
1112use Symfony \Component \HttpFoundation \Session \Session ;
@@ -75,34 +76,35 @@ public function setCustomerId($value)
7576
7677 public function onSecurityInteractiveLogin (InteractiveLoginEvent $ event )
7778 {
78- if (
79- $ this ->authorizationChecker ->isGranted ('IS_AUTHENTICATED_FULLY ' ) // user has just logged in
80- || $ this ->authorizationChecker ->isGranted ('IS_AUTHENTICATED_REMEMBERED ' ) // user has logged in using remember_me cookie
79+ if (!$ this ->authorizationChecker ->isGranted ('IS_AUTHENTICATED_FULLY ' ) // user has just logged in
80+ || !$ this ->authorizationChecker ->isGranted ('IS_AUTHENTICATED_REMEMBERED ' ) // user has logged in using remember_me cookie
8181 ) {
82- if (!$ event ->getRequest ()->cookies ->has ('yc-session-id ' )) {
83- $ event ->getRequest ()->cookies ->set ('yc-session-id ' , $ this ->session ->getId ());
84- }
82+ return ;
83+ }
8584
86- $ notificationUri = sprintf ($ this ->getNotificationEndpoint () . '%s/%s/%s ' ,
87- 'login ' ,
88- $ event ->getRequest ()->cookies ->get ('yc-session-id ' ),
89- $ this ->userService ->loadUserByLogin ($ event ->getAuthenticationToken ()->getUsername ())->id
90- );
85+ if (!$ event ->getRequest ()->cookies ->has ('yc-session-id ' )) {
86+ $ event ->getRequest ()->cookies ->set ('yc-session-id ' , $ this ->session ->getId ());
87+ }
9188
92- if (isset ($ this ->logger )) {
93- $ this ->logger ->debug (sprintf ('Send login event notification to YooChoose: %s ' , $ notificationUri ));
94- }
89+ $ notificationUri = sprintf ($ this ->getNotificationEndpoint () . '%s/%s/%s ' ,
90+ 'login ' ,
91+ $ event ->getRequest ()->cookies ->get ('yc-session-id ' ),
92+ $ this ->getUser ($ event ->getAuthenticationToken ())
93+ );
9594
96- try {
97- $ response = $ this ->guzzleClient ->get ($ notificationUri );
95+ if (isset ($ this ->logger )) {
96+ $ this ->logger ->debug (sprintf ('Send login event notification to YooChoose: %s ' , $ notificationUri ));
97+ }
98+
99+ try {
100+ $ response = $ this ->guzzleClient ->get ($ notificationUri );
98101
99- if (isset ($ this ->logger )) {
100- $ this ->logger ->debug (sprintf ('Got %s from YooChoose login event notification ' , $ response ->getStatusCode ()));
101- }
102- } catch (RequestException $ e ) {
103- if (isset ($ this ->logger )) {
104- $ this ->logger ->error (sprintf ('YooChoose login event notification error: %s ' , $ e ->getMessage ()));
105- }
102+ if (isset ($ this ->logger )) {
103+ $ this ->logger ->debug (sprintf ('Got %s from YooChoose login event notification ' , $ response ->getStatusCode ()));
104+ }
105+ } catch (RequestException $ e ) {
106+ if (isset ($ this ->logger )) {
107+ $ this ->logger ->error (sprintf ('YooChoose login event notification error: %s ' , $ e ->getMessage ()));
106108 }
107109 }
108110 }
@@ -120,4 +122,24 @@ private function getNotificationEndpoint()
120122 $ this ->options ['customerId ' ]
121123 );
122124 }
125+
126+ /**
127+ * Returns current username or ApiUser id.
128+ *
129+ * @param TokenInterface $authenticationToken
130+ *
131+ * @return int|string
132+ */
133+ private function getUser (TokenInterface $ authenticationToken )
134+ {
135+ $ user = $ authenticationToken ->getUser ();
136+
137+ if (is_string ($ user )) {
138+ return $ user ;
139+ } elseif (method_exists ($ user , 'getAPIUser ' )) {
140+ return $ user ->getAPIUser ()->id ;
141+ }
142+
143+ return $ authenticationToken ->getUsername ();
144+ }
123145}
0 commit comments