<commit_before><?php

namespace core\controllers;

use Yii;
use yii\web\BadRequestHttpException;
use core\components\Controller;
use core\models\PasswordResetRequestForm;
use core\models\ResetPasswordForm;
use yii\filters\AccessControl;
use core\models\LoginForm;

/**
 * Class DefaultController
 * @package core\controllers
 */
class DefaultController extends Controller
{
	/**
	 * @var \core\Module
	 */
	public $module;

	private $loginAttemptsVar = '__LoginAttemptsCount';

	public function behaviors()
	{
		return [
			'access' => [
				'class' => AccessControl::className(),
				'only' => ['logout'],
				'rules' => [
					[
						'actions' => ['logout'],
						'allow' => true,
						'roles' => ['@'],
					],
				],
			],
		];
	}

	public function actions()
	{
		return [
			'error' => [
				'class' => 'yii\web\ErrorAction',
				'view' => 'error'
			],
			'captcha' => [
				'class' => 'yii\captcha\CaptchaAction',
				'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
			],
		];
	}

    /**
     * @param \yii\base\Action $event
     * @return bool
     */
    public function beforeAction($event)
    {
		if(Yii::$app->user->isGuest) {
			$this->layout = self::LOGIN_LAYOUT;
		} else {
            $this->layout = self::MAIN_LAYOUT;
		}
        return true;
    }

    /**
     * Shows the login form
     *
     * @return string|\yii\web\Response
     */
	public function actionLogin()
	{
		if (!\Yii::$app->user->isGuest) {
			$this->goHome();
		}

		$model = new LoginForm();
		//make the captcha required if the unsuccessful attempts are more of thee
		if ($this->getLoginAttempts() >= $this->module->attemptsBeforeCaptcha) {
			$model->scenario = 'withCaptcha';
		}

		if ($model->load($_POST) and $model->login()) {
			$this->setLoginAttempts(0); //if login is successful, reset the attempts
			return $this->goBack();
		}
		//if login is not successful, increase the attempts
		$this->setLoginAttempts($this->getLoginAttempts() + 1);

		return $this->render('login', [
			'model' => $model,
		]);<commit_msg>Changing the controller so it can be reused in the frontend too<commit_after><?php

namespace core\controllers;

use Yii;
use yii\web\BadRequestHttpException;
use core\components\Controller;
use core\models\PasswordResetRequestForm;
use core\models\ResetPasswordForm;
use yii\filters\AccessControl;
use core\models\LoginForm;

/**
 * Class DefaultController
 * @package core\controllers
 */
class DefaultController extends Controller
{
	/**
	 * @var \core\Module
	 */
	public $module;

    /**
     * @var int the number of login attempts done.
     */

    private $loginAttemptsVar = '__LoginAttemptsCount';

	public function behaviors()
	{
		return [
			'access' => [
				'class' => AccessControl::className(),
				//'only' => ['logout', 'login', 'request-password-reset', 'reset-password'],
				'rules' => [
                    [
                        'actions' => ['login', 'request-password-reset', 'reset-password'],
                        'allow' => true,
                        'roles' => ['?'],
                    ],
                    [
						'actions' => ['logout'],
						'allow' => true,
						'roles' => ['@'],
					],
				],
			],
		];
	}

	public function actions()
	{
		return [
			'error' => [
				'class' => 'yii\web\ErrorAction',
				'view' => 'error'
			],
			'captcha' => [
				'class' => 'yii\captcha\CaptchaAction',
				'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
			],
		];
	}

    /**
     * @param \yii\base\Action $event
     * @return bool
     */
    public function beforeAction($event)
    {
		if(Yii::$app->user->isGuest) {
			$this->layout = self::LOGIN_LAYOUT;
		} else {
            $this->layout = self::MAIN_LAYOUT;
		}
        return parent::beforeAction($event);
    }

    /**
     * Shows the login form
     *
     * @return string|\yii\web\Response
     */
	public function actionLogin()
	{
		$model = new LoginForm();
		//make the captcha required if the unsuccessful attempts are more of thee
		if ($this->getLoginAttempts() >= $this->module->attemptsBeforeCaptcha) {
			$model->scenario = 'withCaptcha';
		}

        if(Yii::$app->request->post()) {
            if($model->load($_POST) && $model->login()) {
                $this->setLoginAttempts(0); //if login is successful, reset the attempts
                return $this->goBack();
            } else {
                //if login is not successful, increase the attempts
                $this->setLoginAttempts($this->getLoginAttempts() + 1);
            }
		}

		return $this->render('login', [
			'model' => $model,
		]);
{'fraction_non_alphanumeric': 0.16433260393873086, 'fraction_numerical': 0.00087527352297593, 'mean_word_length': 2.518860662047729, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 71, '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': '5276825', 'n_tokens_mistral': 1669, 'n_tokens_neox': 1454, 'n_words': 304}