`
zccst
  • 浏览: 3291991 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Yii->user(当前用户)相关

    博客分类:
  • YII
 
阅读更多
作者:zccst

常用的用法是
class Controller extends CController
{
    public $user = null;
    $this->user = Yii:app()->user;
}

this->user->isGuest;
this->user->id;
this->user->name;

还有设置session
this->user->setStatus('xx');
this->user->getStatus('xx');

查看一下手册后,发现user的属性和方法还真多。

详细如下:
CWebUser represents the persistent state for a Web application user.

CWebUser is used as an application component whose ID is 'user'. Therefore, at any place one can access the user state via

Yii::app()->user.

CWebUser should be used together with an identity which implements the actual authentication algorithm.

A typical authentication process using CWebUser is as follows:
1.The user provides information needed for authentication.
2.An identity instance is created with the user-provided information.
3.Call IUserIdentity::authenticate to check if the identity is valid.
4.If valid, call CWebUser::login to login the user, and Redirect the user browser to returnUrl.
5.If not valid, retrieve the error code or message from the identity instance and display it.


The property id and name are both identifiers for the user. The former is mainly used internally (e.g. primary key), while the

latter is for display purpose (e.g. username). The id property is a unique identifier for a user that is persistent during the

whole user session. It can be a username, or something else, depending on the implementation of the identity class.

Both id and name are persistent during the user session. Besides, an identity may have additional persistent data which can be

accessed by calling getState. Note, when cookie-based authentication is enabled, all these persistent data will be stored in

cookie. Therefore, do not store password or other sensitive data in the persistent storage. Instead, you should store them

directly in session on the server side if needed.


1,属性
allowAutoLogin      boolean        whether to enable cookie-based login. CWebUser
authTimeout         integer        timeout in seconds after which user is logged out if inactive. CWebUser
autoRenewCookie     boolean        whether to automatically renew the identity cookie each time a page is requested. CWebUser
autoUpdateFlash     boolean        whether to automatically update the validity of flash messages. CWebUser
behaviors           array          the behaviors that should be attached to this component. CApplicationComponent
flashes             array          Returns all flash messages. CWebUser
guestName           string         the name for a guest user. CWebUser
id                  mixed          the unique identifier for the user. CWebUser
identityCookie      array          the property values (in name-value pairs) used to initialize the identity cookie. CWebUser
isGuest             boolean        whether the current application user is a guest. CWebUser
isInitialized       boolean        Checks if this application component bas been initialized. CApplicationComponent
loginUrl            string|array   the URL for login. CWebUser
name                string         Returns the unique identifier for the user (e.g. username). CWebUser
returnUrl           string         Returns the URL that the user should be redirected to after successful login. CWebUser
stateKeyPrefix      string         a prefix for the name of the session variables storing user session data. CWebUser



2,方法
最基本的方法
除了call, get, isset, set, unset方法之外,还有
getIsInitialized() Checks if this application component bas been initialized. //一般不需要检查
checkAccess() Performs access check for this user.   //检查用户可以访问的操作
方法原型:public boolean checkAccess(string $operation, array $params=array ( ), boolean $allowCaching=true)


基本方法
getId() Returns the unique identifier for the user. If null, it means the user is a guest.
setId() Sets the unique identifier for the user. If null, it means the user is a guest.
getName() Returns the unique identifier for the user (e.g. username).
setName() Sets the unique identifier for the user (e.g. username).
setReturnUrl() Sets the URL that the user should be redirected to after login.
getReturnUrl() Returns the URL that the user should be redirected to after successful login.
canGetProperty() Determines whether a property can be read.
canSetProperty() Determines whether a property can be set.


登陆相关
login() Logs in a user. CWebUser
loginRequired() Redirects the user browser to the login page.//该方法非常好用
logout()
getIsGuest()


增加行为相关
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component.
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component.


session相关
setState() Stores a variable in user session. // 是基于cookie-based authentication,所以不应存一些如密码等敏感信息
getState() Returns the value of a variable that is stored in user session.
hasState() Returns a value indicating whether there is a state of the specified name.
clearStates() Clears all user identity information from persistent storage.
setStateKeyPrefix() Sets a prefix for the name of the session variables storing user session data.
getStateKeyPrefix() Returns a prefix for the name of the session variables storing user session data.


flash相关
hasFlash() Determines whether the specified flash message exists
getFlash() Returns a flash message.
setFlash() Stores a flash message.
getFlashes() Returns all flash messages.


事件相关
raiseEvent() Raises an event.
hasEvent() Determines whether an event is defined.
hasEventHandler() Checks whether the named event has attached handlers.
getEventHandlers() Returns the list of attached event handlers for an event.
attachEventHandler() Attaches an event handler to an event.
detachEventHandler() Detaches an existing event handler.



如果您觉得本文的内容对您的学习有所帮助,您可以微信:
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics