<commit_before><?php

namespace TRMN\medusaauth\auth\provider;

use \phpbb\language\language;

class medusa extends \phpbb\auth\provider\base
{
    /**
     * phpBB passwords manager
     *
     * @var \phpbb\passwords\manager
     */
    protected $passwords_manager;

    /**
     * MEDUSA Authentication Constructor
     *
     * @param       \phpbb\db\driver\driver_interface $db                Database object
     * @param       \phpbb\config\config              $config            Config object
     * @param       \phpbb\passwords\manager          $passwords_manager Passwords manager object
     * @param       \phpbb\user                       $user              User object
     */
    public function __construct(
        \phpbb\db\driver\driver_interface $db,
        \phpbb\config\config $config,
        \phpbb\passwords\manager $passwords_manager,
        \phpbb\user $user,
        \phpbb\language\language_file_loader $loader
    ) {
        $this->db = $db;
        $this->config = $config;
        $this->passwords_manager = $passwords_manager;
        $this->user = $user;

        // Load our language file
        $language = new language($loader);
        $language->add_lang('common', 'TRMN/medusaauth');
    }

    /**
     * {@inheritdoc}
     */
    public function login($username, $password)
    {
        global $phpbb_root_path, $phpEx;

        // Auth plugins get the password untrimmed.
        // For compatibility we trim() here.
        $password = trim($password);

        // do not allow empty password
        if (!$password) {
            return array(
                'status' => LOGIN_ERROR_PASSWORD,
                'error_msg' => 'NO_PASSWORD_SUPPLIED',
                'user_row' => array('user_id' => ANONYMOUS),
            );
        }

        if (!$username) {
            return array(
                'status' => LOGIN_ERROR_USERNAME,
                'error_msg' => 'LOGIN_ERROR_USERNAME',
                'user_row' => array('user_id' => ANONYMOUS),
            );
        }

        $username = strtolower($username);
        $ch = curl_init('https://medusa.trmn.org/oauth/token');
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,
            'username=' . $username . '&password=' . $password . '&grant_type=password&client_id=medusamobile');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $results = json_decode(curl_exec($ch));
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        if ($httpcode == "200") {
            // Good login, check to see if we have a profile

            $row = $this->_userExists($username);

            if ($row) {
                // User inactive...
                if ($row['user_type'] == USER_INACTIVE || $row['user_type'] == USER_IGNORE) {
                    return array(
                        'status' => LOGIN_ERROR_ACTIVE,
                        'error_msg' => 'ACTIVE_ERROR',
                        'user_row' => $row,
                    );
                }

                // Successful login... set user_login_attempts to zero...

                $medusa_user = $this->_getUserInfo($results->access_token);

                $forum_rank = $this->_lookUpRank($medusa_user->rank->grade, $medusa_user->branch);

                if (!is_null($forum_rank)) {
                    $row['user_rank'] = $forum_rank;
                }

                return array(
                    'status' => LOGIN_SUCCESS,
                    'error_msg' => false,
                    'user_row' => $row,
                );
            } else {
                // Good login, no profile exists

                // retrieve default group id<commit_msg>Only update user_rank if it's different<commit_after><?php

namespace TRMN\medusaauth\auth\provider;

use \phpbb\language\language;

class medusa extends \phpbb\auth\provider\base
{
    /**
     * phpBB passwords manager
     *
     * @var \phpbb\passwords\manager
     */
    protected $passwords_manager;

    /**
     * MEDUSA Authentication Constructor
     *
     * @param       \phpbb\db\driver\driver_interface $db                Database object
     * @param       \phpbb\config\config              $config            Config object
     * @param       \phpbb\passwords\manager          $passwords_manager Passwords manager object
     * @param       \phpbb\user                       $user              User object
     */
    public function __construct(
        \phpbb\db\driver\driver_interface $db,
        \phpbb\config\config $config,
        \phpbb\passwords\manager $passwords_manager,
        \phpbb\user $user,
        \phpbb\language\language_file_loader $loader
    ) {
        $this->db = $db;
        $this->config = $config;
        $this->passwords_manager = $passwords_manager;
        $this->user = $user;

        // Load our language file
        $language = new language($loader);
        $language->add_lang('common', 'TRMN/medusaauth');
    }

    /**
     * {@inheritdoc}
     */
    public function login($username, $password)
    {
        global $phpbb_root_path, $phpEx;

        // Auth plugins get the password untrimmed.
        // For compatibility we trim() here.
        $password = trim($password);

        // do not allow empty password
        if (!$password) {
            return array(
                'status' => LOGIN_ERROR_PASSWORD,
                'error_msg' => 'NO_PASSWORD_SUPPLIED',
                'user_row' => array('user_id' => ANONYMOUS),
            );
        }

        if (!$username) {
            return array(
                'status' => LOGIN_ERROR_USERNAME,
                'error_msg' => 'LOGIN_ERROR_USERNAME',
                'user_row' => array('user_id' => ANONYMOUS),
            );
        }

        $username = strtolower($username);
        $ch = curl_init('https://medusa.trmn.org/oauth/token');
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,
            'username=' . $username . '&password=' . $password . '&grant_type=password&client_id=medusamobile');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $results = json_decode(curl_exec($ch));
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        if ($httpcode == "200") {
            // Good login, check to see if we have a profile

            $row = $this->_userExists($username);

            if ($row) {
                // User inactive...
                if ($row['user_type'] == USER_INACTIVE || $row['user_type'] == USER_IGNORE) {
                    return array(
                        'status' => LOGIN_ERROR_ACTIVE,
                        'error_msg' => 'ACTIVE_ERROR',
                        'user_row' => $row,
                    );
                }

                // Successful login... set user_login_attempts to zero...

                $medusa_user = $this->_getUserInfo($results->access_token);

                $forum_rank = $this->_lookUpRank($medusa_user->rank->grade, $medusa_user->branch);

                if (!is_null($forum_rank) && $row['user_rank'] != $forum_rank) {
                    $row['user_rank'] = $forum_rank;
                }

                return array(
                    'status' => LOGIN_SUCCESS,
                    'error_msg' => false,
                    'user_row' => $row,
                );
            } else {
                // Good login, no profile exists

                // retrieve default group id
{'fraction_non_alphanumeric': 0.12307488050982475, 'fraction_numerical': 0.0010621348911311736, 'mean_word_length': 1.606574394463668, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 55, 'https://': 2, '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': '6578172', 'n_tokens_mistral': 2225, 'n_tokens_neox': 2104, 'n_words': 418}