<commit_before><?php
/**
 * Created by PhpStorm.
 * User: HBW
 * Date: 2016/4/28
 * Time: 9:56
 * To change this template use File | Setting | File Templates.
 */

namespace app\modules\core\helpers;

use app\models\User;
use Yii;

/**
 * Class UserHelper
 * @package app\modules\core\helpers
 *
 * @property $user
 * @property $userInstance
 * @property $userId
 * @property $userName
 */
class UserHelper
{
    /**
     * 获取用户实例，默认返回当前登录的用户实例
     *
     * @param int|string $userId
     * @return User|null
     */
    public static function getUserInstance($userId = 0)
    {
        if ($userId == 0) {
            return Yii::$app->user->identity;
        } else {
            return User::find()->where(['id' => $userId])->limit(1)->one();
        }
    }

    /**
     * 判断当前访客是否是管理员
     *
     * 统一封装在这有利于以后改动设计时不需要对项目进行很大改动
     *
     * @return bool
     */
    public static function isAdmin()
    {
        return Yii::$app->user->can('SuperAdmin');
    }

    /**
     * 获取用户ID，默认返回当前登录的用户ID
     *
     * @param string|null $userName
     * @return bool|int|string
     */
    public static function getUserId($userName = null)
    {
        if ($userName) {
            return User::find()->select(['id'])->where(['username' => $userName])->limit(1)->scalar();
        } else {
            return Yii::$app->user->id;
        }
    }

    /**
     * 获取用户名，默认返回当前登录用户的用户名
     *
     * @param int $userId
     * @param null $default
     * @param string $zero_name
     * @return bool|null|string
     */
    public static function getUserName($userId = -1, $default = null, $zero_name = 'System')
    {
        if ($userId == -1 && !Yii::$app->user->isGuest) {
            return self::getUserInstance()->username;
        } else if ($userId == 0) {
            return $zero_name;
        } else {
            $username = User::find()->select(['username'])->where(['id' => $userId])->limit(1)->scalar();
            if ($username) {
                return $username;
            }
        }
        return $default;
    }

    /**
     * 是否属于当前登录的用户，在一些修改和删除的按钮以及控制器上会用到
     *
     * @param $userId<commit_msg>调整获取用户id的逻辑<commit_after><?php
/**
 * Created by PhpStorm.
 * User: HBW
 * Date: 2016/4/28
 * Time: 9:56
 * To change this template use File | Setting | File Templates.
 */

namespace app\modules\core\helpers;

use app\models\User;
use Yii;

/**
 * Class UserHelper
 * @package app\modules\core\helpers
 *
 * @property $user
 * @property $userInstance
 * @property $userId
 * @property $userName
 */
class UserHelper
{
    /**
     * 获取用户实例，默认返回当前登录的用户实例
     *
     * @param int|string $userId
     * @return User|null
     */
    public static function getUserInstance($userId = 0)
    {
        if ($userId == 0) {
            return Yii::$app->user->identity;
        } else {
            return User::find()->where(['id' => $userId])->limit(1)->one();
        }
    }

    /**
     * 判断当前访客是否是管理员
     *
     * 统一封装在这有利于以后改动设计时不需要对项目进行很大改动
     *
     * @return bool
     */
    public static function isAdmin()
    {
        return Yii::$app->user->can('SuperAdmin');
    }

    /**
     * 获取用户ID，默认返回当前登录的用户ID
     *
     * @param string|null $userName
     * @return bool|int|string
     */
    /**
     * @param null $userName
     * @return false|int|null|string
     */
    public static function getUserId($userName = null)
    {
        if ($userName) {
            return User::find()->select(['id'])->where(['username' => $userName])->limit(1)->scalar();
        } else {
            if (Yii::$app->user->id == null) {
                return Yii::$app->user->id;
            } else {
                return 0;
            }
        }
    }

    /**
     * 获取用户名，默认返回当前登录用户的用户名
     *
     * @param int $userId
     * @param null $default
     * @param string $zero_name
     * @return bool|null|string
     */
    public static function getUserName($userId = -1, $default = null, $zero_name = 'System')
    {
        if ($userId == -1 && !Yii::$app->user->isGuest) {
            return self::getUserInstance()->username;
        } else if ($userId == 0) {
            return $zero_name;
        } else {
            $username = User::find()->select(['username'])->where(['id' => $userId])->limit(1)->scalar();
            if ($username) {
                return $username;
            }
        }
        return $default;
    }

    /**
     * 是否属于当前登录的用户，在一些修改和删除的按钮以及控制器上会用到
     *
     * @param $userId
{'fraction_non_alphanumeric': 0.1593974820143885, 'fraction_numerical': 0.008318345323741008, 'mean_word_length': 1.9424603174603174, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 51, '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': '5090238', 'n_tokens_mistral': 1582, 'n_tokens_neox': 1665, 'n_words': 278}