我是Yii2的新手,我尝试使AccessControl成功
但问题是我成功登录并重定向到其他页面
我的身份_attributes总是为null。如果我查看Yii::$app->user->isGuest
,则返回值始终为true
这是我的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
<?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');
}
}
我的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]);
}
}
,最后一个是我的配置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,
];
提前致谢。
答案 0 :(得分:2)
您在提问中提到Icon Id Number Description
Delete Icon 454545 Dog
232323 Cat
Delete Icon 121321 Horse
。在Yii2中,AccessControl是管理控制器内访问规则的特殊行为类:
http://www.yiiframework.com/doc-2.0/yii-filters-accesscontrol.html
我在你的代码中没有看到AccessControl
。
反正。
最有可能的问题是你的User类的实现。
查看代码,我可以想象表结构是:AccessControl
。
如果是,则方法user_id (PK), user_name, user_password
返回变量
(getId()
)从未初始化。但Yii使用此方法将当前用户存储在会话中。在您的情况下,它应该返回$this->id
。
如果您希望$this->user_id
正常工作,您应该正确实施remember me
和getAuthKey
。
以下是详细信息: http://www.yiiframework.com/doc-2.0/guide-security-authentication.html
如果这没有帮助,那么显示将身份验证数据传递给validateAuthKey
的表结构和视图代码
答案 1 :(得分:0)
看起来你应该检查
NSString *str = @"hello";
NSLog(@"Pointer = %p", str); // Prints @"hello"'s address ad
[self doSomthingWithString:&str];
- (void)doSomthingWithString:(NSString **)str {
NSLog(@"Pointer = %p", *str); // Prints @"hello"'s address again
NSString *other = @"world";
NSLog(@"Pointer = %p", *other); // Prints @"world"'s address
*str = other;
NSLog(@"Pointer = %p", *str); // Prints @"world"'s address again
}