`
zccst
  • 浏览: 3322434 次
  • 性别: 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.



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

相关推荐

    vk-app-template:使用在服务器端的 yii2 php 框架和客户端的backbone.js 上编写的样板启动您的 vk iframe 应用程序

    写在 yii2/backbone 上的 vk 应用模板 如何设置 克隆; chmod 0777 运行时; chmod 0777 网络/资产; 创建数据库; ... 通过在终端输入“./yii migrate”来创建用户表;...\Yii:$app->user->identiry->las

    Yii2 RBAC 集成了用户管理模块和自写的权限管理模块,前端页面采用H+框架。-Yii2-Rbac.zip

    5. **权限检查**:在控制器或视图中,可以使用`Yii::$app->user->can`方法检查当前用户是否具有执行特定操作的权限。 6. **自定义权限管理模块**:如果默认的RBAC组件不能满足需求,可以编写自定义模块,扩展其功能...

    Yii 的登录流程

    此时,可以调用`Yii::app()->user->login`方法使用户登录系统。 接下来我们深入探讨`Yii::app()->user->login`方法的具体实现。该方法定义在`CWebUser`类中,主要负责以下几个关键步骤: 1. **更改用户身份**:...

    Yii获取当前url和域名的方法

    echo Yii::app()->user->returnUrl; ``` 这通常用于重定向用户回到他们之前访问的页面。 获取根目录地址(不包括域名)则使用: ```php echo Yii::app()->homeUrl; ``` 而获取基于基URL的路径(例如,不含域名的...

    Yii2-GridView 中让关联字段带搜索和排序功能示例

    ->andFilterWhere(['user_id' => $this->user_id]) ->andFilterWhere(['customer_id' => $this->customer_id]) ->andFilterWhere(['order_time' => $this->order_time]) ->andFilterWhere(['pay_time' => $this-...

    yii框架 rbac扩展

    3. 使用:在控制器中,可以使用`Yii::app()->user->checkAccess()`方法来检查当前用户是否有执行某个操作的权限。例如: ```php if (Yii::app()->user->checkAccess('updatePost', array('postId' => $id))) { // ...

    内置有Rbac权限的YII2框架源码

    7. **权限检查**:在控制器或视图中,可以使用`can()`方法检查当前用户是否有执行某个操作的权限,如`if (Yii::$app->user->can('editPost')) { ... }`。 8. **数据库表结构**:使用`DbManager`时,框架会自动创建`...

    Yii Rbac 资源包

    6. **使用 RBAC**:在控制器或视图中,你可以使用 `Yii::$app->user->can()` 方法检查当前用户是否有执行特定操作的权限。如果用户有权限,方法将返回 `true`,反之则返回 `false`。 7. **动态添加和管理**:在运行...

    yii2.0整合阿里云oss删除单个文件的方法

    $operator = Yii::$app->user->id; if ($owner != $operator) { $res['errmsg'] = '您删除的图片不存在'; return $res; } $trans = Yii::$app->db->beginTransaction(); try { $delstatus = Yii::$app->db...

    Yii框架通过请求组件处理get,post请求的方法分析

    5. 获取用户IP地址等其他相关信息,使用`$request->userIP`。 以上就是Yii框架通过请求组件处理GET和POST请求的核心知识点和操作技巧。为了更好地利用Yii框架开发高质量的PHP Web应用,开发者可以参考本文所述方法...

    Yii分页用法实例详解

    Yii框架是一个高性能的PHP开发框架,广泛用于Web应用的...开发者在应用Yii框架开发Web应用时,应充分考虑分页的用户体验和系统性能,充分利用Yii框架提供的分页组件和相关参数配置,为用户提供流畅且高效的分页体验。

    Yii框架扩展CGridView增加导出CSV功能的方法

    if (Yii::app()->user->checkAccess('export')) { echo '<div class="toolBar">'; echo CHtml::beginForm(CHtml::normalizeUrl(array('actionExport')), 'post'); // 添加其他隐藏字段以传递必要参数 echo C...

    Yii2 rbac权限控制操作步骤实例教程

    跳转到`/admin/assignment/index`,在这里为当前用户分配刚创建的角色,而不是直接分配权限。这样,用户就拥有了“用户管理”的角色权限。 6. **菜单管理(Menu)**: 如果希望“用户管理”显示在后台菜单上,...

    php页面防重复提交方法总结

    Yii::app()->user->id . Yii::app()->user->name . Yii::app()->user->mihome : $uniqueid; $token = md5("wms_check_repeat" . $uniqueid); $time = time(); if (isset($_SESSION['token']) && !empty($_...

    yii2.0框架场景的简单使用示例

    `$this->context`可以获取当前的Module、Controller和Action;通过配置可以设置默认的Controller和Action;验证码、分页组件、自定义用户认证模型以及文件上传等都是Yii2.0框架常用的组件,它们都有丰富的API和示例...

    Yii使用find findAll查找出指定字段的实现方法

    $user = User::model()->find(array( 'select' => array('username', 'id', 'email'), 'order' => 'id DESC', 'condition' => 'id=:id', 'params' => array(':id' => $id), )); ``` 这里的 `condition` ...

    YII实现分页的方法

    $userList = User::model()->findAll($criteria); // 查询所有数据 $this->render('test', array('list' => $userList, 'pages' => $pager)); // 渲染视图 } ``` 这段代码实现了以下几个关键步骤: 1. **创建查询...

    解决yii2左侧菜单子级无法高亮问题的方法

    'items' => MenuHelper::getAssignedMenu(Yii::$app->user->id, null, $callback), ]); ``` 这样,我们就能够确保左侧菜单能够根据当前控制器高亮相应的菜单项,包括其父级菜单项。需要注意的是,文章中也提到了...

    yii实现model添加默认值的方法(2种方法)

    $this->user_id = Yii::app()->user->id; // 设置默认值为当前用户ID return true; // 返回true以确保数据能被保存 } ``` 在这个示例中,`user_id`字段将被设置为当前用户的身份ID。`beforeSave()`方法必须返回`...

    Yii全局函数用法示例

    2. **app()**: 这个函数返回`Yii::app()`,它是Yii框架的核心组件,提供了对应用程序级别的属性和方法的访问,如用户身份(`Yii::app()->user`)、参数(`Yii::app()->params`)等。 3. **cs()**: 这个函数返回`Yii...

Global site tag (gtag.js) - Google Analytics