<commit_before><?php

namespace Site\Controller;

use Api\Library\Shared\Communicate\Communicate;
use Api\Library\Shared\Palaso\Exception\UserUnauthorizedException;
use Api\Model\Shared\Command\UserCommands;
use Api\Model\Shared\UserModel;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Core\Security;

defined('ENVIRONMENT') or exit('No direct script access allowed');

class Auth extends App
{
    // return status
    const LOGIN_FAIL = 'loginFail';
    const LOGIN_FAIL_USER_UNAUTHORIZED = 'loginFailUserUnauthorized';
    const LOGIN_SUCCESS = 'loginSuccess';

    public function view(Request $request, Application $app, $appName, $resetPasswordKey = '')
    {
        switch ($appName) {
            /** @noinspection PhpMissingBreakStatementInspection */
            case 'reset_password':
                $user = new UserModel();
                if (!$user->readByProperty('resetPasswordKey', $resetPasswordKey)) {
                    $app['session']->getFlashBag()->add('errorMessage', 'Your password reset cannot be completed. Please try again.');

                    return $app->redirect('/auth/login');
                }

                if (!$user->hasForgottenPassword(false)) {
                    $app['session']->getFlashBag()->add('errorMessage', 'Your password reset cannot be completed. It may have expired. Please try again.');

                    return $app->redirect('/auth/login');
                }

                // no break; - intentional fall through to next case
            case 'forgot_password':
            case 'login':
                $this->setupAngularAppVariables($app, $appName);
                $this->setupAuthView($request, $app);

                return $this->renderPage($app, 'angular-app');
                break;
            default:
                return $this->renderPage($app, $appName);
        }
    }

    public function forgotPassword(Request $request, Application $app)
    {
        $usernameOrEmail = UserCommands::sanitizeInput($request->request->get('_username'));
        $user = new UserModel();
        if (!$user->readByUsernameOrEmail($usernameOrEmail)) {
            $app['session']->getFlashBag()->add('errorMessage', 'User not found.');
            return $this->view($request, $app, 'forgot_password');
        } else if (!$user->active) {
            $app['session']->getFlashBag()->add('errorMessage', 'Access denied.');
            return $this->view($request, $app, 'forgot_password');
        }

        if (!$user->hasRoleOnSite($this->website)) {
            $user->siteRole[$this->website->domain] = $this->website->userDefaultSiteRole;<commit_msg>Redirect to projects page when logged in user visits login page<commit_after><?php

namespace Site\Controller;

use Api\Library\Shared\Communicate\Communicate;
use Api\Library\Shared\Palaso\Exception\UserUnauthorizedException;
use Api\Model\Shared\Command\UserCommands;
use Api\Model\Shared\UserModel;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Core\Security;

defined('ENVIRONMENT') or exit('No direct script access allowed');

class Auth extends App
{
    // return status
    const LOGIN_FAIL = 'loginFail';
    const LOGIN_FAIL_USER_UNAUTHORIZED = 'loginFailUserUnauthorized';
    const LOGIN_SUCCESS = 'loginSuccess';

    public function view(Request $request, Application $app, $appName, $resetPasswordKey = '')
    {
        switch ($appName) {
            /** @noinspection PhpMissingBreakStatementInspection */
            case 'reset_password':
                $user = new UserModel();
                if (!$user->readByProperty('resetPasswordKey', $resetPasswordKey)) {
                    $app['session']->getFlashBag()->add('errorMessage', 'Your password reset cannot be completed. Please try again.');

                    return $app->redirect('/auth/login');
                }

                if (!$user->hasForgottenPassword(false)) {
                    $app['session']->getFlashBag()->add('errorMessage', 'Your password reset cannot be completed. It may have expired. Please try again.');

                    return $app->redirect('/auth/login');
                }

                // no break; - intentional fall through to next case
            case 'forgot_password':
            case 'login':
                if($this->isLoggedIn($app)) {
                    return $app->redirect('/app/projects');
                }
                $this->setupAngularAppVariables($app, $appName);
                $this->setupAuthView($request, $app);

                return $this->renderPage($app, 'angular-app');
                break;
            default:
                return $this->renderPage($app, $appName);
        }
    }

    public function forgotPassword(Request $request, Application $app)
    {
        $usernameOrEmail = UserCommands::sanitizeInput($request->request->get('_username'));
        $user = new UserModel();
        if (!$user->readByUsernameOrEmail($usernameOrEmail)) {
            $app['session']->getFlashBag()->add('errorMessage', 'User not found.');
            return $this->view($request, $app, 'forgot_password');
        } else if (!$user->active) {
            $app['session']->getFlashBag()->add('errorMessage', 'Access denied.');
            return $this->view($request, $app, 'forgot_password');
        }

        if (!$user->hasRoleOnSite($this->website)) {
            $user->siteRole[$this->website->domain] = $this->website->userDefaultSiteRole;
{'fraction_non_alphanumeric': 0.13618677042801555, 'fraction_numerical': 0.0, 'mean_word_length': 2.634318766066838, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 63, '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': '6247380', 'n_tokens_mistral': 1517, 'n_tokens_neox': 1476, 'n_words': 316}