CWebUser and CUserIdentity

I am creating an authentication module for my application and I am not fully understanding the relationship between CWebUser

and CUserIdentity

.

To set the user id to Yii::app()->user->id

I have to do this in my class UserIdentity

and create a method:

public function getId() {
    return $this->_id;
}

      

But set isAdmin

in Yii::app()->user->isAdmin

I need to create a method in the class WebUser

:

function getIsAdmin() {
    $user = $this->loadUser(Yii::app()->user->id);
    return intval($user->user_level_id) == AccountModule::USER_LEVEL_ADMIN;
}

      

Why can't I just create class methods UserIdentity

? What is the division of labor here?

+3


source to share


3 answers


A class UserIdentity

(UI) is like an ID card where the class WebUser

is the actual person, plus everything you know about them.

The UI class gives you authentication via database, webservices, text file, whatever. It lets you know what the key attributes are and allows you to manipulate them. However, the user can provide you with more information about what they are allowed to do, names, granular permissions, etc.

OK, the end of the metaphor

The UI class contains key information, so when requesting a user ID it will reference the User Identity class to get the ID for the user.

Anything not related to user identification or authentication is in the class WebUser



Delete everything?

Your example

You specified a function as an example getId

, but you can create it on WebUser

to override the default value that needs to be pulled out of state.

So not sure what you mean here.

+5


source


I love how the accepted answer used real life examples to make it easier to understand. However, I also like the way Chris explained it here with an example.



User information is stored in an instance of the CWebUser class and this is created when the application is initialized (that is: when the user first connects to the website), regardless of whether the user is logged in or not. By default, the user is set to "Guest". Authentication is managed by the CUserIdentity class, and this class verifies that the user is known and is a valid user. How this check will depend on your application, perhaps against a database or facebook, or against an ldap server, etc.

+2


source


And what are the benefits of using all these classes? I can do everything just according to the User model. If I installed the "login" script, the password will be verified during verification. If the validation is correct, I can set up the session with my own variable like this:

$model = new User("login");
$model->attributes = $_POST["User"];
if ($model->validate())
{
  Yii::app()->session["currentUser"] = $model;
}
else
{
  // .. show error
  unset(Yii::app()->session["currentUser"]);
}

      

In user model, I have static methods to check this variable

public static function isGuest()
{
  return isset(Yii::app()->session["currentUser"]);
}

public static function getCurrent()
{
  return Yii::app()->session["currentUser"];
}

      

And I can call it very short:

User::isGuest();
$model = User::getCurrent();
// instead of writing this:
Yii::app()->user->isGuest;

      

So why should I use such a complex class hierarchy that Yii offers? I have never understood this.

+2


source







All Articles