[英]Yii2 AccessControl
I am new in Yii2 and I try to make AccessControl and success but the problem is after I success for login and redirect to other page my Identity _attributes always are null.So if I check with Yii::$app->user->isGuest
the return value is always true 我是Yii2的新手,但我尝试使AccessControl取得成功,但问题是在我成功登录并重定向到其他页面后,我的Identity _attributes始终为null。因此,如果我使用Yii::$app->user->isGuest
返回值始终为真
this is my LoginHandler.php 这是我的LoginHandler.php
<?php
namespace app\models;
use Yii;
use yii\base\Model;
/**
* Login form
*/
class LoginHandler extends Model
{
public $user_name;
public $user_password;
public $rememberMe = true;
private $_user;
/**
* @inheritdoc
*/
public function rules()
{
return [
[['user_name', 'user_password'], 'required'],
[['user_name', 'user_password'], 'string', 'max' => 100],
['user_password','authenticate'],
];
}
public function authenticate($attribute, $params){
// return true;
}
public function login()
{
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
} else {
return false;
}
}
protected function getUser()
{
if ($this->_user === null) {
$this->_user = User::findByUsername($this->user_name);
}
return $this->_user;
}
}
LoginController 的LoginController
<?php
namespace backend\controllers;
use Yii;
use app\models\user;
use app\models\LoginHandler;
class LoginController extends \yii\web\Controller
{
public function actionIndex()
{
return $this->render('index');
}
public function actionSignin(){
$user = User::findByUsername('admin');
$model = new LoginHandler();
if(Yii::$app->request->post()){
$data = Yii::$app->request->post();
$model->attributes = $data;
if ($model->login()) {
return $this->redirect(['/user/test']);
}else{
die('test');
}
}
return $this->render('login');
}
}
My User.php as model 我的User.php作为模型
namespace app\models;
use Yii;
/**
* This is the model class for table "user".
*
* @property integer $user_id
* @property string $user_name
* @property string $user_password
*/
class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface{
public $id;
public $authKey;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'user';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['user_name', 'user_password'], 'required'],
[['user_name', 'user_password'], 'string', 'max' => 100]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'user_id' => 'User ID',
'user_name' => 'User Name',
'user_password' => 'User Password',
];
}
public static function findIdentity($id)
{
return static::findOne($id);
}
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['access_token' => $token]);
}
public function getId()
{
return $this->id;
}
public function getAuthKey()
{
return $this->authKey;
}
public function validateAuthKey($authKey)
{
return $this->authKey === $authKey;
}
public static function findByUsername($username){
return static::findOne(['user_name' => $username]);
}
}
and the last is my configuration main.php 最后是我的配置main.php
<?php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
return [
'id' => 'app-backend',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'backend\controllers',
'bootstrap' => ['log'],
'modules' => [],
'components' => [
'user' => [
'identityClass' => 'backend\models\User',
'loginUrl' => ['login/signin'],
'enableAutoLogin' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
],
'params' => $params,
];
Thanks in advance. 提前致谢。
You mentioned AccessControl
in your question. 您在问题中提到了AccessControl
。 In Yii2 AccessControl is the special behavior class to manage access rules inside controller: 在Yii2中,AccessControl是用于管理控制器内部访问规则的特殊行为类:
http://www.yiiframework.com/doc-2.0/yii-filters-accesscontrol.html http://www.yiiframework.com/doc-2.0/yii-filters-accesscontrol.html
and I don't see AccessControl
in you code. 而且我在您的代码中看不到AccessControl
。
Anyway. 无论如何。
Most probably the problem is in your implementation of User class. 问题很可能出在用户类的实现中。 Looking at your code I can imagine that the table structure is: user_id (PK), user_name, user_password
. 查看您的代码,我可以想象表结构为: user_id (PK), user_name, user_password
。 If so, then the method getId()
returns variable ( $this->id
) which is never initialized. 如果是这样,则方法getId()
返回从未初始化的变量( $this->id
)。 But this method is used by Yii to store current user in session. 但是Yii使用此方法在会话中存储当前用户。 In your case it should return $this->user_id
. 在您的情况下,它应该返回$this->user_id
。
And if you wish to make remember me
working, you should implement correctly getAuthKey
and validateAuthKey
too. 而且,如果您想让remember me
工作,则也应该正确实现getAuthKey
和validateAuthKey
。
Here is details: http://www.yiiframework.com/doc-2.0/guide-security-authentication.html 这是详细信息: http : //www.yiiframework.com/doc-2.0/guide-security-authentication.html
If this not helps, then show your table structure and code of view which pass authentication data to LoginController
如果这样做没有帮助,请显示将身份验证数据传递给LoginController
表结构和视图LoginController
看来您应该检查
Yii::$app->user->identity
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.