如果用户继续,怎么办? Yii2

Posted

技术标签:

【中文标题】如果用户继续,怎么办? Yii2【英文标题】:if user is proceed, how? Yii2 【发布时间】:2016-05-12 15:00:11 【问题描述】:

我正在使用角色验证,我目前只使用 phprbca,还没有进入 databaserbca。

我的控制器上编码了一些自定义验证,如果他以管理员身份登录,我想让用户能够忽略某些规则。我怎么能这样做?

<?php
use app\models\User;
#Next line, how would i do it? or if (username=Richard)
if (userid=100)
    if($model->driver_identitynum!=null)
       $count = Drivers::find()->select('drivers.*')->joinWith('archives')->where('archive.DateCreated > DATE(CURDATE())')->andWhere(['archive.driver_identitynum' => $model->driver_identitynum])->count();

       if($count>0)
        $valid=false;  
       \Yii::$app->getSession()->setFlash('error', 'Can\'t proceed, already added today.'); 
       $model->addError('driver_identitynum',"El conductor ha salido $count veces.");
    

?>

我的用户模型:

<?php

namespace app\models;

class User extends \yii\base\Object implements \yii\web\IdentityInterface

    public $id;
    public $username;
    public $password;
    public $authKey;
    public $accessToken;

    private static $users = [
        '100' => [
            'id' => '100',
            'username' => 'admin',
            'password' => 'admin',
            'authKey' => 'test100key',
            'accessToken' => '100-token',

        ],
        '101' => [
            'id' => '101',
            'username' => 'demo',
            'password' => 'demo',
            'authKey' => 'test101key',
            'accessToken' => '101-token',
        ],
    ];

    /**
     * @inheritdoc
     */
    public static function findIdentity($id)
    
        return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
    

    /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken($token, $type = null)
    
        foreach (self::$users as $user) 
            if ($user['accessToken'] === $token) 
                return new static($user);
            
        

        return null;
    

    /**
     * Finds user by username
     *
     * @param  string      $username
     * @return static|null
     */
    public static function findByUsername($username)
    
        foreach (self::$users as $user) 
            if (strcasecmp($user['username'], $username) === 0) 
                return new static($user);
            
        

        return null;
    

    /**
     * @inheritdoc
     */
    public function getId()
    
        return $this->id;
    

    /**
     * @inheritdoc
     */
    public function getAuthKey()
    
        return $this->authKey;
    

    /**
     * @inheritdoc
     */
    public function validateAuthKey($authKey)
    
        return $this->authKey === $authKey;
    

    /**
     * Validates password
     *
     * @param  string  $password password to validate
     * @return boolean if password provided is valid for current user
     */
    public function validatePassword($password)
    
        return $this->password === $password;
    



【问题讨论】:

【参考方案1】:

如果您的用户拥有 rbac 权限 admin,则可以使用以下命令评估用户角色

Yii::$app-&gt;User-&gt;can('admin')

 if ( Yii::$app->User->can('admin') ) 
   //  your code fro admin 
 

【讨论】:

以上是关于如果用户继续,怎么办? Yii2的主要内容,如果未能解决你的问题,请参考以下文章

如何修改yii2 admin2的前端页面

yii2 判断一个url用户是不是有权限访问

Yii2 - 触发后从事件处理程序终止应用程序

YII2中自己写的自定义函数怎么调用

yii2 框架怎么调用vendor下的第三方

隐藏 Yii2 GridView 操作按钮