<commit_before><?php

/**
 * This file is covered by the AGPLv3 license, which can be found at the LICENSE file in the root of this project.
 * @copyright 2020 subtitulamos.tv
 */

namespace App\Controllers;

use App\Entities\User;
use App\Services\Auth;
use App\Services\Utils;
use Dflydev\FigCookies\FigRequestCookies;
use Dflydev\FigCookies\FigResponseCookies;
use Dflydev\FigCookies\SetCookie;
use Doctrine\ORM\EntityManager;

use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Respect\Validation\Validator as v;

class AccessController
{
    const MINIMUM_PASSWORD_LENGTH = 8;
    const MINIMUM_USER_LENGTH = 4;

    protected $container;

    // constructor receives container instance
    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    /**
     * Attempt to login a user
     *
     * @param ServerRequestInterface $request
     * @param ResponseInterface $response
     * @return ResponseInterface
     */
    public function login(ServerRequestInterface $request, ResponseInterface $response, EntityManager $em, Auth $auth)
    {
        $body = $request->getParsedBody();
        $username = $body['username'] ?? '';
        $password = $body['password'] ?? '';
        $remember = ($body['remember'] ?? '') == 'true';

        if (!$username || !$password) {
            return $response->withStatus(400);
        }

        if (\filter_var($username, \FILTER_VALIDATE_EMAIL)) {
            // Logging in with email
            $user = $em->getRepository('App:User')->findOneByEmail($username);
            $loginName = 'Correo electrónico';
        } else {
            $user = $em->getRepository('App:User')->findOneByUsername($username);
            $loginName = 'Usuario';
        }

        if (!$user->checkPassword($password)) {
            return Utils::jsonResponse($response, [$loginName.' o contraseña incorrectos'])->withStatus(403);
        }

        $token = $auth->log($user, $remember);
        if ($remember) {
            $response = FigResponseCookies::set($response, SetCookie::create('remember')->withPath('/')->withValue($token)->rememberForever());
        }

        return $response->withStatus(200);
    }

    public function register(ServerRequestInterface $request, ResponseInterface $response, EntityManager $em, Auth $auth)
    {
        $body = $request->getParsedBody();
        $username = $body['username'] ?? '';
        $password = $body['password'] ?? '';
        $password_confirmation = $body['password_confirmation'] ?? '';
        $email = $body['email'] ?? '';
        $terms = ($body['terms'] ?? false) == 'true';

        $errors = [];
        if (!$terms) {
            $errors[] = ['terms' => 'Debes aceptar los términos y condiciones'];
        }

        if (!v::alnum('_')->noWhitespace()->length(3, 24)->validate($username)) {<commit_msg>Fix login crash when user doesn't exist<commit_after><?php

/**
 * This file is covered by the AGPLv3 license, which can be found at the LICENSE file in the root of this project.
 * @copyright 2020 subtitulamos.tv
 */

namespace App\Controllers;

use App\Entities\User;
use App\Services\Auth;
use App\Services\Utils;
use Dflydev\FigCookies\FigRequestCookies;
use Dflydev\FigCookies\FigResponseCookies;
use Dflydev\FigCookies\SetCookie;
use Doctrine\ORM\EntityManager;

use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Respect\Validation\Validator as v;

class AccessController
{
    const MINIMUM_PASSWORD_LENGTH = 8;
    const MINIMUM_USER_LENGTH = 4;

    protected $container;

    // constructor receives container instance
    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    /**
     * Attempt to login a user
     *
     * @param ServerRequestInterface $request
     * @param ResponseInterface $response
     * @return ResponseInterface
     */
    public function login(ServerRequestInterface $request, ResponseInterface $response, EntityManager $em, Auth $auth)
    {
        $body = $request->getParsedBody();
        $username = $body['username'] ?? '';
        $password = $body['password'] ?? '';
        $remember = ($body['remember'] ?? '') == 'true';

        if (!$username || !$password) {
            return $response->withStatus(400);
        }

        if (\filter_var($username, \FILTER_VALIDATE_EMAIL)) {
            // Logging in with email
            $user = $em->getRepository('App:User')->findOneByEmail($username);
            $loginName = 'Correo electrónico';
        } else {
            $user = $em->getRepository('App:User')->findOneByUsername($username);
            $loginName = 'Usuario';
        }

        if (!$user || !$user->checkPassword($password)) {
            return Utils::jsonResponse($response, [$loginName.' o contraseña incorrectos'])->withStatus(403);
        }

        $token = $auth->log($user, $remember);
        if ($remember) {
            $response = FigResponseCookies::set($response, SetCookie::create('remember')->withPath('/')->withValue($token)->rememberForever());
        }

        return $response->withStatus(200);
    }

    public function register(ServerRequestInterface $request, ResponseInterface $response, EntityManager $em, Auth $auth)
    {
        $body = $request->getParsedBody();
        $username = $body['username'] ?? '';
        $password = $body['password'] ?? '';
        $password_confirmation = $body['password_confirmation'] ?? '';
        $email = $body['email'] ?? '';
        $terms = ($body['terms'] ?? false) == 'true';

        $errors = [];
        if (!$terms) {
            $errors[] = ['terms' => 'Debes aceptar los términos y condiciones'];
        }

        if (!v::alnum('_')->noWhitespace()->length(3, 24)->validate($username)) {
{'fraction_non_alphanumeric': 0.1466419169760378, 'fraction_numerical': 0.00641241984475194, 'mean_word_length': 3.0457337883959044, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 41, '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': '1366818', 'n_tokens_mistral': 1731, 'n_tokens_neox': 1651, 'n_words': 371}