Julio Montoya vor 11 Jahren
Ursprung
Commit
eb681afd96

+ 1 - 0
main/inc/global.inc.php

@@ -557,6 +557,7 @@ $app->before(
         //var_dump($app['security']->isGranted('IS_AUTHENTICATED_FULLY'));
 
         if ($app['security']->isGranted('IS_AUTHENTICATED_FULLY')) {
+
             $token = $app['security']->getToken();
             if (null !== $token) {
                 $user = $token->getUser();

+ 2 - 1
main/inc/lib/login_redirection.class.php

@@ -3,6 +3,7 @@
 /**
  * When a user login, the function LoginRedirection::redirect is called.
  * When this function is called all user info has already been registered in $_user session variable
+ * @todo Use the LoginSuccessHandler
  * */
 Class LoginRedirection {
 
@@ -60,4 +61,4 @@ Class LoginRedirection {
         exit();
     }
 
-}
+}

+ 2 - 2
main/inc/lib/redirect.class.php

@@ -6,7 +6,7 @@
  * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  * @license see /license.txt
  *
- * @todo use $app->redirect
+ * @todo use the LoginSuccessHandler class
  */
 class Redirect {
 
@@ -102,4 +102,4 @@ class Redirect {
         header("Location: $url");
         exit;
     }
-}
+}

+ 69 - 0
main/inc/services.php

@@ -70,6 +70,8 @@ $app->register(new Silex\Provider\SecurityServiceProvider(), array(
     )
 ));
 
+// Registering Password encoder
+// @todo fix harcoded sha1 value
 use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
 $app['security.encoder.digest'] = $app->share(function($app) {
     // use the sha1 algorithm
@@ -78,6 +80,72 @@ $app['security.encoder.digest'] = $app->share(function($app) {
     return new MessageDigestPasswordEncoder('sha1', false, 1);
 });
 
+use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
+use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
+use Symfony\Component\Security\Core\SecurityContext;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\RedirectResponse;
+use Symfony\Component\Routing\Router;
+use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
+
+class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface
+{
+    protected $router;
+    protected $security;
+
+    /**
+     * @param UrlGeneratorInterface $urlGenerator
+     * @param SecurityContext $security
+     */
+    public function __construct(UrlGeneratorInterface $urlGenerator, SecurityContext $security)
+    {
+        $this->router = $urlGenerator;
+        $this->security = $security;
+    }
+
+    /**
+     * @param Request $request
+     * @param TokenInterface $token
+     * @return null|RedirectResponse|\Symfony\Component\Security\Http\Authentication\Response
+     */
+    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
+    {
+        /*if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
+            $response = new RedirectResponse($this->router->generate('category_index'));
+        } elseif ($this->security->isGranted('ROLE_ADMIN')) {
+            $response = new RedirectResponse($this->router->generate('category_index'));
+        } elseif ($this->security->isGranted('ROLE_USER')) {
+            // redirect the user to where they were before the login process begun.
+            $referer_url = $request->headers->get('referer');
+            $response = new RedirectResponse($referer_url);
+        }*/
+
+        $response = null;
+        //$session = $request->getSession();
+        $pageAfterLogin = api_get_setting('page_after_login');
+
+        //error_log($session->get('page_after_login'));
+        if ($this->security->isGranted('ROLE_STUDENT') && !empty($pageAfterLogin)) {
+            $url = api_get_path(WEB_PUBLIC_PATH).$pageAfterLogin;
+            $response = new RedirectResponse($url);
+        }
+
+        // Redirect the user to where they were before the login process begun.
+        if (empty($response)) {
+            $refererUrl = $request->headers->get('referer');
+            $response = new RedirectResponse($refererUrl);
+        }
+
+        return $response;
+    }
+}
+
+// Registering success login redirection
+$app['security.authentication.success_handler.admin'] = $app->share(function($app) {
+    return new LoginSuccessHandler($app['url_generator'], $app['security']);
+});
+
+// Role hierarchy
 $app['security.role_hierarchy'] = array(
     'ROLE_ADMIN' => array('ROLE_QUESTION_MANAGER', 'ROLE_TEACHER', 'ROLE_ALLOWED_TO_SWITCH'),
     'ROLE_TEACHER' => array('ROLE_STUDENT'),
@@ -87,6 +155,7 @@ $app['security.role_hierarchy'] = array(
     'ROLE_ANONYMOUS' => array('ROLE_ANONYMOUS')
 );
 
+// Role rules
 $app['security.access_rules'] = array(
     array('^/admin/administrator', 'ROLE_ADMIN'),
     array('^/admin/questionmanager', 'ROLE_QUESTION_MANAGER'),