<commit_before><?php
namespace AppBundle\Controller;

use AppBundle\Entity as EntityDir;
use AppBundle\Form as FormDir;
use AppBundle\Model\Email;
use AppBundle\Service\Client\RestClient;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;

/**
* @Route("user")
*/
class UserController extends AbstractController
{
    /**
     * Landing page to let the user access the app and selecting a password
     * 
     * Used for both user activation (Step1) or password reset. The controller logic is very similar
     * 
     * @Route("/{action}/{token}", name="user_activate", defaults={ "action" = "activate"}, requirements={
     *   "action" = "(activate|password-reset)"
     * })
     * @Template()
     */
    public function activateUserAction(Request $request, $action, $token)
    {
        $restClient = $this->get('restClient'); /* @var $restClient RestClient */
        $translator = $this->get('translator');
        
        // check $token is correct
        try {
            $user = $this->get('restClient')->loadUserByToken($token); /* @var $user EntityDir\User*/
        } catch (\Exception $e) {
            throw new \AppBundle\Exception\DisplayableException('This link is not working or has already been used');
        }
        
        if (!$user->isTokenSentInTheLastHours(EntityDir\User::TOKEN_EXPIRE_HOURS)) {
            switch ($action) {
                case 'activate':
                    return $this->render('AppBundle:User:activateTokenExpired.html.twig', [
                        'token'=>$token, 
                        'tokenExpireHours' => EntityDir\User::TOKEN_EXPIRE_HOURS,
                    ]);
                    
                case 'password-reset':
                    return $this->render('AppBundle:User:passwordResetTokenExpired.html.twig', [
                        'token'=>$token, 
                        'tokenExpireHours' => EntityDir\User::TOKEN_EXPIRE_HOURS,
                    ]);
            }
        }
        
        // define form and template that differs depending on the action (activate or password-reset)
        switch ($action) {
            case 'activate':
                $formType = new FormDir\SetPasswordType([
                    'passwordMismatchMessage' => $translator->trans('password.validation.passwordMismatch', [], 'user-activate')
                ]);
                $template = 'AppBundle:User:activate.html.twig';
                break;
            
            case 'password-reset':
                $formType = new FormDir\ResetPasswordType([
                    'passwordMismatchMessage' => $this->get('translator')->trans('password.validation.passwordMismatch', [], 'password-reset')
                ]);
                $template = 'AppBundle:User:passwordReset.html.twig';
                break;
            
            default:
                return $this->createNotFoundException("action $action not defined ");
        }
        
        $form = $this->createForm($formType, $user);
        
        $form->handleRequest($request);
        if ($form->isValid()) {
            
            // login user into API
            //TODO try move at the beginning
            $this->get('deputyprovider')->login(['token'=>$token]);
            
            // set password for user
            $restClient->put('user/' . $user->getId() . '/set-password', json_encode([
                'password_plain' => <PASSWORD>(),
                'set_active' => true,
                'send_email' => false //not sent on this "landing" pages
            ]));

            // log in user into CLIENT
            $clientToken = new UsernamePasswordToken($user, null, "secured_area", $user->getRoles());
            $this->get("security.context")->setToken($clientToken); //now the user is logged in

            $session = $this->get('session');
            $session->set('_security_secured_area', serialize($clientToken));

             //$request = $this->get("request");
             //$event = new InteractiveLoginEvent($request, $clientToken);
             //$this->get("event_dispatcher")->dispatch("security.interactive_login", $event);

             // the following should not be triggered
             return $this->redirect($this->generateUrl('user_details'));
        }

        return $this->render($template, [
            'token'=>$token, 
            'form' => $form->createView(),
            'userRole' => $user->getRole()['role']
        ]);<commit_msg>dd-1163 fix redirect after resetting password<commit_after><?php
namespace AppBundle\Controller;

use AppBundle\Entity as EntityDir;
use AppBundle\Form as FormDir;
use AppBundle\Model\Email;
use AppBundle\Service\Client\RestClient;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;

/**
* @Route("user")
*/
class UserController extends AbstractController
{
    /**
     * Landing page to let the user access the app and selecting a password
     * 
     * Used for both user activation (Step1) or password reset. The controller logic is very similar
     * 
     * @Route("/{action}/{token}", name="user_activate", defaults={ "action" = "activate"}, requirements={
     *   "action" = "(activate|password-reset)"
     * })
     * @Template()
     */
    public function activateUserAction(Request $request, $action, $token)
    {
        $restClient = $this->get('restClient'); /* @var $restClient RestClient */
        $translator = $this->get('translator');
        
        // check $token is correct
        try {
            $user = $this->get('restClient')->loadUserByToken($token); /* @var $user EntityDir\User*/
        } catch (\Exception $e) {
            throw new \AppBundle\Exception\DisplayableException('This link is not working or has already been used');
        }
        
        if (!$user->isTokenSentInTheLastHours(EntityDir\User::TOKEN_EXPIRE_HOURS)) {
            if ('activate' == $action) {
                return $this->render('AppBundle:User:activateTokenExpired.html.twig', [
                    'token'=>$token, 
                    'tokenExpireHours' => EntityDir\User::TOKEN_EXPIRE_HOURS,
                ]);
            } else { // password-reset
                return $this->render('AppBundle:User:passwordResetTokenExpired.html.twig', [
                    'token'=>$token, 
                    'tokenExpireHours' => EntityDir\User::TOKEN_EXPIRE_HOURS,
                ]);
            }
        }
        
        // define form and template that differs depending on the action (activate or password-reset)
        if ('activate' == $action) {
            $formType = new FormDir\SetPasswordType([
                'passwordMismatchMessage' => $translator->trans('password.validation.passwordMismatch', [], 'user-activate')
            ]);
            $template = 'AppBundle:User:activate.html.twig';
        } else { // 'password-reset'
            $formType = new FormDir\ResetPasswordType([
                'passwordMismatchMessage' => $this->get('translator')->trans('password.validation.passwordMismatch', [], 'password-reset')
            ]);
            $template = 'AppBundle:User:passwordReset.html.twig';
        }
        
        $form = $this->createForm($formType, $user);
        
        $form->handleRequest($request);
        if ($form->isValid()) {
            
            // login user into API
            //TODO try move at the beginning
            $this->get('deputyprovider')->login(['token'=>$token]);
            
            // set password for user
            $restClient->put('user/' . $user->getId() . '/set-password', json_encode([
                'password_plain' => $<PASSWORD>(),
                'set_active' => true,
                'send_email' => false //not sent on this "landing" pages
            ]));

            // log in user into CLIENT
            $clientToken = new UsernamePasswordToken($user, null, "secured_area", $user->getRoles());
            $this->get("security.context")->setToken($clientToken); //now the user is logged in

            $session = $this->get('session');
            $session->set('_security_secured_area', serialize($clientToken));

             //$request = $this->get("request");
             //$event = new InteractiveLoginEvent($request, $clientToken);
             //$this->get("event_dispatcher")->dispatch("security.interactive_login", $event);

            if ('activate' == $action) { // go to second step
                $redirectUrl = $this->generateUrl('user_details');
            } else { // password reset : go to homepage depending on the user role
                $redirectUrl = $this->get('redirectorService')->getUserFirstPage(false);
            }
            
             // the following should not be triggered
            return $this->redirect($redirectUrl);
        }

        return $this->render($template, [
            'token'=>$token, 
            'form' => $form->createView(),
            'userRole' => $user->getRole()['role']
        ]);
{'fraction_non_alphanumeric': 0.13630600169061707, 'fraction_numerical': 0.0006339814032121725, 'mean_word_length': 2.169792364367046, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 94, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '663915', 'n_tokens_mistral': 2498, 'n_tokens_neox': 2431, 'n_words': 513}