<commit_before><?php
include_once('User.php');
include_once('DbLayer.php');
class LoginService {
    const TOKEN_REQUEST = 1;
    const TOKEN_VALIDATION = 2;
    const TOKEN_ERROR_INVALID_USER_CREDENTIALS = 0;
    /**
     *
     */
    public function checkUser($username, $password) {
        if($username == null || $username == "" || $password == null || $password == ""){
            return null;
        }
        $projection = array(
        UsersContract::USERS_COLUMN_USERNAME,
        UsersContract::USERS_COLUMN_E_MAIL,
        UsersContract::USERS_COLUMN_ROLE
        );
        $tables = array(UsersContract::USERS_TABLE_NAME);
        $where = UsersContract::USERS_COLUMN_USERNAME . "=% AND " . 
            UsersContract::USERS_COLUMN_PASSWORD . "=%";
        $whereargs = array($username,$password);
        $row = self::getUserData($projection, $tables, $where, $whereargs);
        if($row != null){
            return self::createUser($row[UsersContract::USERS_COLUMN_USERNAME],
            $row[UsersContract::USERS_COLUMN_ROLE],
            $row[UsersContract::USERS_COLUMN_E_MAIL]);
        } else {
            return null;
        }
    }
    /**
     *
     * @return User $user
     */
    public function validateAccessToken($tokenString) {
        if($tokenString == null || $tokenString == "") {
            return null;
        }
        //TODO: Get details from DB
        $projection = array(
        UsersContract::USERS_TABLE_NAME . "." . UsersContract::USERS_COLUMN_USERNAME,
        UsersContract::USERS_COLUMN_E_MAIL,
        UsersContract::USERS_COLUMN_ROLE,
        UsersContract::ACCESS_TOKEN_TABLE_NAME . "." . UsersContract::ACCESS_TOKEN_COLUMN_LOGIN_TOKEN
        );
        $tables = array(UsersContract::USERS_TABLE_NAME, UsersContract::ACCESS_TOKEN_TABLE_NAME);
        $where = UsersContract::ACCESS_TOKEN_COLUMN_LOGIN_TOKEN . "=%";
        $whereargs = array(
        $tokenString
        );
        $row = self::getUserData($projection, $tables, $where, $whereargs);
        if($row != null){
            return new User(
            $row[UsersContract::USERS_COLUMN_USERNAME],
            $row[UsersContract::USERS_COLUMN_ROLE],
            $row[UsersContract::USERS_COLUMN_E_MAIL],
            new AccessToken($row[UsersContract::ACCESS_TOKEN_COLUMN_LOGIN_TOKEN])
            );
        } else {
            return null;
        }
    }

    static function getUserData($projection, $tables, $where, $whereargs){
        $dbLayer = new DbLayer();
        if($dbLayer->connect() == DbLayer::RESULT_DB_CONNECTION_SUCCESFUL){
            $data = $dbLayer->query($projection, $tables, $where, $whereargs);<commit_msg>Add correct user creation when already has access token<commit_after><?php
include_once('User.php');
include_once('DbLayer.php');
class LoginService {
    const TOKEN_REQUEST = 1;
    const TOKEN_VALIDATION = 2;
    const TOKEN_ERROR_INVALID_USER_CREDENTIALS = 0;
    /**
     *
     */
    public function checkUser($username, $password) {
        if($username == null || $username == "" || $password == null || $password == ""){
            return null;
        }
        $projection = array(
        UsersContract::USERS_COLUMN_USERNAME,
        UsersContract::USERS_COLUMN_E_MAIL,
        UsersContract::USERS_COLUMN_ROLE
        );
        $tables = array(UsersContract::USERS_TABLE_NAME);
        $where = UsersContract::USERS_COLUMN_USERNAME . "=% AND " . 
            UsersContract::USERS_COLUMN_PASSWORD . "=%";
        $whereargs = array($username,$password);
        $row = self::getUserData($projection, $tables, $where, $whereargs);
        if($row != null){
            return self::createUser($row[UsersContract::USERS_COLUMN_USERNAME],
            $row[UsersContract::USERS_COLUMN_ROLE],
            $row[UsersContract::USERS_COLUMN_E_MAIL]);
        } else {
            return null;
        }
    }
    /**
     *
     * @return User $user
     */
    public function validateAccessToken($tokenString) {
        if($tokenString == null || $tokenString == "") {
            return null;
        }
        //TODO: Get details from DB
        $projection = array(
        UsersContract::USERS_TABLE_NAME . "." . UsersContract::USERS_COLUMN_USERNAME,
        UsersContract::USERS_COLUMN_E_MAIL,
        UsersContract::USERS_COLUMN_ROLE,
        UsersContract::ACCESS_TOKEN_TABLE_NAME . "." . UsersContract::ACCESS_TOKEN_COLUMN_LOGIN_TOKEN
        );
        $tables = array(UsersContract::USERS_TABLE_NAME, UsersContract::ACCESS_TOKEN_TABLE_NAME);
        $where = UsersContract::ACCESS_TOKEN_COLUMN_LOGIN_TOKEN . "=%";
        $whereargs = array(
        $tokenString
        );
        $row = self::getUserData($projection, $tables, $where, $whereargs);
        if($row != null){
            return new User(
            $row[UsersContract::USERS_TABLE_NAME . "." . UsersContract::USERS_COLUMN_USERNAME],
            $row[UsersContract::USERS_COLUMN_ROLE],
            $row[UsersContract::USERS_COLUMN_E_MAIL],
            new AccessToken($row[UsersContract::ACCESS_TOKEN_TABLE_NAME . "." . UsersContract::ACCESS_TOKEN_COLUMN_LOGIN_TOKEN])
            );
        } else {
            return null;
        }
    }

    static function getUserData($projection, $tables, $where, $whereargs){
        $dbLayer = new DbLayer();
        if($dbLayer->connect() == DbLayer::RESULT_DB_CONNECTION_SUCCESFUL){
            $data = $dbLayer->query($projection, $tables, $where, $whereargs);
{'fraction_non_alphanumeric': 0.12644310060472788, 'fraction_numerical': 0.0010995052226498076, 'mean_word_length': 2.6953283683141502, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '4962464', 'n_tokens_mistral': 1638, 'n_tokens_neox': 1672, 'n_words': 247}