<commit_before><?php

/*
 * HiPanel core package
 *
 * @link      https://hipanel.com/
 * @package   hipanel-core
 * @license   BSD-3-Clause
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
 */

namespace hipanel\components;

use Yii;
use yii\base\InvalidCallException;
use yii\base\InvalidConfigException;

class User extends \yii\web\User
{
    public $isGuestAllowed = false;

    /**
     * @var string the seller login
     */
    public $seller;

    public function init()
    {
        parent::init();
        if (empty($this->seller)) {
            throw new InvalidConfigException('User "seller" must be set');
        }
    }

    public function not($key)
    {
        $identity = $this->getIdentity();
        if (!$identity) {
            throw new InvalidCallException();
        }

        return $identity->not($key);
    }

    public function is($key)
    {
        $identity = $this->getIdentity();
        if (!$identity) {
            throw new InvalidCallException();
        }

        return $identity->is($key);
    }
    /**
     * @inheritdoc
     * XXX fixes redirect loop when identity is set but the object is empty
     * @return bool
     */
    public function getIsGuest()
    {
        return empty($this->getIdentity()->id);
    }

    /**
     * Prepares authorization data.
     * Redirects to authorization if necessary.
     * @return array
     */
    public function getAuthData()
    {
        if ($this->isGuest) {
            if ($this->isGuestAllowed) {
                return [];
            } else {
                Yii::$app->response->redirect('/site/login');
                Yii::$app->end();
            }
        }

        $token = $this->identity->getAccessToken();
        if (empty($token)) {
            /// this is very important line
            /// without this line - redirect loop
            $this->logout();

            Yii::$app->response->redirect('/site/login');
            Yii::$app->end();
        }

        return ['access_token' => $token];
    }
}<commit_msg>improved redirect to login in User component<commit_after><?php

/*
 * HiPanel core package
 *
 * @link      https://hipanel.com/
 * @package   hipanel-core
 * @license   BSD-3-Clause
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
 */

namespace hipanel\components;

use Yii;
use yii\base\InvalidCallException;
use yii\base\InvalidConfigException;

class User extends \yii\web\User
{
    public $isGuestAllowed = false;

    /**
     * @var string the seller login
     */
    public $seller;

    public function init()
    {
        parent::init();
        if (empty($this->seller)) {
            throw new InvalidConfigException('User "seller" must be set');
        }
    }

    public function not($key)
    {
        $identity = $this->getIdentity();
        if (!$identity) {
            throw new InvalidCallException();
        }

        return $identity->not($key);
    }

    public function is($key)
    {
        $identity = $this->getIdentity();
        if (!$identity) {
            throw new InvalidCallException();
        }

        return $identity->is($key);
    }
    /**
     * @inheritdoc
     * XXX fixes redirect loop when identity is set but the object is empty
     * @return bool
     */
    public function getIsGuest()
    {
        return empty($this->getIdentity()->id);
    }

    /**
     * Prepares authorization data.
     * Redirects to authorization if necessary.
     * @return array
     */
    public function getAuthData()
    {
        if ($this->isGuest) {
            if ($this->isGuestAllowed) {
                return [];
            } else {
                $this->redirectLogin();
            }
        }

        $token = $this->identity->getAccessToken();
        if (empty($token)) {
            /// logout() is very important here, else - redirect loop
            $this->logout();
            $this->redirectLogin();
        }

        return ['access_token' => $token];
    }

    public function redirectLogin()
    {
        $this->loginRequired();
        Yii::$app->end();
    }
}
{'fraction_non_alphanumeric': 0.13608748481166463, 'fraction_numerical': 0.004374240583232078, 'mean_word_length': 1.823045267489712, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, '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': '2324367', 'n_tokens_mistral': 1198, 'n_tokens_neox': 1163, 'n_words': 281}