The Yii2 base projects, Basic and Advanced, comes with a simpleUser authentication and Login system included.
It provides two users, Admin and Demo, which are hard coded into the User model. It is left to the developer to wire this up into a database or whatever other persistent system you are planning.
I recently updated a project through composer and found that I could no longer access any of the Admin functions in my application. On investigation, I found that the Identity object was not being set properly and therefore isGuest was always true.
Cause and Effect
Eventually, I found that it was caused by two things;
Firstly, the Session cookie was corrupted and need to be deleted through the browser.
Secondly, I started getting naming conflicts between the User data model and the User base class Yiiwebuser. Why oh why has the Yii team called a base class ‘User’? Won’t this lead to on-going naming conflicts, even given namespaces???
Rename Your User Model
The simple answer is to rename your User class. It’s only a couple of changes in the Basic application.
- Change the Model name to Users
- Within the Users model change the class name to Users
- In the LoginForm model, change the reference in the getUser function from $this->_user = User::findByUsername($this->username); to $this->_user = Users::findByUsername($this->username);