dqpkea9486 2016-04-14 15:57
浏览 148

Yii2基本模板登录逻辑不起作用

I'm a newbie Yii2 developer. I've started to create my own website based on Basic Yii2 template. And after creating registration and authorization logic I ran into some issues.

  1. I create new user. It's added to User table into my db right as it should.
  2. I go to login page and Log in with all needed information. It works too.
  3. But when it log me in it redirects me with $this->goHome() method. By default it's actionIndex in my SiteController and this page is still denied by rules.

My DB scheme is:

$this->createTable('user', [
        'id' => Schema::TYPE_PK,
        'username' => Schema::TYPE_STRING . ' NOT NULL',
        'email' => Schema::TYPE_STRING . ' NOT NULL',
        'password_hash' => Schema::TYPE_STRING . ' NOT NULL',
        'role' => Schema::TYPE_SMALLINT . ' NOT NULL',
    ]);

I set my identity class:

'user' => [
    'identityClass' => 'app\models\User',
    'enableAutoLogin' => true,
]

My User class extends ActiveRecord and implements IdentityInterface.

public function getId()
{
    return $this->id;
}
public static function findIdentity($id)
{
    self::find()
        ->select('*')
        ->where(['id' => $id])
        ->one();
}

SiteController behavior method:

public function behaviors()
{
    return [
        'access' => [
            'class' => AccessControl::className(),
            'rules' => [
                [
                    'allow' => true,
                    'roles' => ['@'],
                ],
                [
                    'actions' => ['login'],
                    'allow' => true,
                    'roles' => ['?']
                ]
            ],
            'denyCallback' => function ($rule, $action) {
                echo 'DENIED ACTION: ' . $action->actionMethod;
                echo ' IS GUEST: ' . Yii::$app->user->isGuest;
                echo ' SESSION IS: ' . Yii::$app->user->enableSession;
            }
        ],
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'logout' => ['post'],
            ],
        ],
    ];
}

As you can see I've done some var_dumps in my denyCallback function. After I'm logged in it says:

IS DENIED: actionIndex IS GUEST: 1 SESSION IS: 1
  • "Why is isGuest attribute set to 1 even when I'm logged in as user?
  • "How can I fix my access logic?

Also here's some logs: After login i have: SELECT * FROM "user" WHERE "username"='Test' After redirect with goHome(): SELECT * FROM "user" WHERE "id"=1

And in session there's an Id: __id 1 Finally if I change my guests' rule to :

[
    'actions' => ['login', 'index'],
    'allow' => true,
    'roles' => ['?']
]

it shows me the page I need

Let me know if I should provide more information. Thanks in advance.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 touchsocket udp组播
    • ¥20 MAC怎么安装Silverlight 插件?以及安装了怎么启用
    • ¥15 VS2012中查询语句无法填入解析,数值传不进去
    • ¥15 gis系统开发出现命名空间“ESRI.ArcGIS”中不存在类型或命名空间名“Analyst3D”报错
    • ¥15 怎么让ai定时给我发信息 c#或者python
    • ¥15 scrapy的Error
    • ¥15 RBF-VSG姚凤军论文复现问题
    • ¥30 开发一个APP商城在制作tabbar的时候运行不了代码没有检查出错误,但是显示不出tabbar,以下为运行结果,如何解决?
    • ¥15 多网卡服务器中winform如何绑定指定网卡
    • ¥15 关于#python#pandas#的问题,想要实现:多个TXT导入Excel,进行分列,不同txt之间都从第一行开始,请各位专家解答!