Yii CUserIdentity vs a User Model

做~自己de王妃 提交于 2019-12-06 07:02:40

First off, do go ahead and create a User model, you will need it.

With that out of the way: CUserIdentity represents the concept of "who the user is", while the User model represents "information about a user of my application". CUserIdentity is applicable in all cases where there is more than one kind of user (i.e. guest), while the User model is only applicable when you are storing information about the users yourself. Admittedly, in most cases both will be applicable and this is what creates the confusion.

Usually, the relation between the two is that CUserIdentity, in order to answer questions such as "who the user is", "is the user allowed to access this resource" etc. queries the User model from the database to get the information it needs to answer these questions. This relationship between the two concepts is also documented in the definitive guide to Yii. After e.g. authenticating the user, it would expose some or all of the information on the User model through its own properties (which you would have to define).

To give an example of a scenario where there would be no User model, think about a website that lets you log in using your LDAP user account. When the CUserIdentity::authenticate method is called, the component would authenticate the credentials against the LDAP server and after a successful authentication would again grab any other relevant information and expose it through its own properties.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!