Yii登录验证和全局访问用户ID
注意:Yii默认的代码,Yii::app()->user->id返回的不是我们想要的用户ID,而是用户名。因此在useridentity类中要用一个变量来存储登录用户的ID,然后重载getID()方法,返回正确的用户ID。
Yii 有一个内置的验证/授权(auth)框架,用起来很方便,还能对其进行自定义,使其符合特殊的需求。
Yii auth 框架的核心是一个预定义的 用户(user)应用组件 它是一个实现了 IWebUser 接口的对象。此用户组件代表当前用户的持久性认证信息。我们可以通过Yii::app()->user在任何地方访问它。
使用此用户组件,我们可以通过 CWebUser::isGuest 检查检查一个用户是否登陆; 可以 登录(login) 或 注销(logout) 一个用户;我们可以通过CWebUser::checkAccess检查此用户是否可以执行特定的操作;还可以获取此用户的唯一标识(unique identifier)及其他持久性身份信息。
为了验证一个用户,我们定义一个有验证逻辑的身份类。这个身份类实现IUserIdentity 接口。
不同的类可能实现不同的验证方式(例如:OpenID,LDAP)。最好是继承 CUserIdentity,此类是居于用户名和密码的验证方式。
定义身份类的主要工作是实现IUserIdentity::authenticate方法。在用户会话中根据需要,身份类可能需要定义别的身份信息。
下面是个例子:
class UserIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
$record=User::model()->findByAttributes(array('username'=>$this->username));
if($record===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($record->password!==md5($this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id=$record->id;
$this->setState('title', $record->title);
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
} public function getId()
{
return $this->_id;
}
}
二、自己的操作
路径

<?php /**
* UserIdentity represents the data needed to identity a user.
* It contains the authentication method that checks if the provided
* data can identity the user.
*/
class UserIdentity extends CUserIdentity
{
private $_id;
/**
* Authenticates a user.
* The example implementation makes sure if the username and password
* are both 'demo'.
* In practical applications, this should be changed to authenticate
* against some persistent user identity storage (e.g. database).
* @return boolean whether authentication succeeds.
*/
public function authenticate()
{
$userInfo = Admin::model()->find('username=:name', array(':name'=>$this->username)); if($userInfo == NULL){
$this->errorCode=self::ERROR_USERNAME_INVALID;
return false;
} if($userInfo->password !== md5($this->password)){
$this->errorCode=self::ERROR_PASSWORD_INVALID;
return false;
}
$this->_id = $userInfo->admin_id;
$this->errorCode=self::ERROR_NONE;
return true; /* $users=array(
// username => password
'demo'=>'demo',
'admin'=>'admin',
);
if(!isset($users[$this->username]))
$this->errorCode=self::ERROR_USERNAME_INVALID;
elseif($users[$this->username]!==$this->password)
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
$this->errorCode=self::ERROR_NONE;
return !$this->errorCode;*/
} public function getId()
{
return $this->_id;
}
}
Yii登录验证和全局访问用户ID的更多相关文章
- 【异常】ssh无法登录验证,非root用户ssh本机无法成功
1 自己搭建的是伪分布式环境,需要以非root用户启动Hadoop集群,之前root已经配置了ssh免密登录,但是自己切换到hdfs用户重新生成了一套ssh key, 但是切换到hdfs始终无法成功登 ...
- 基于Ajax与用户认证系统的登录验证
一.登录页面 from django.contrib import admin from django.urls import path from blog import views urlpatte ...
- Java初学者作业——编写Java程序,实现用户登录验证。
返回本章节 返回作业目录 需求说明: 编写Java程序,实现用户登录验证. 若用户名与密码输入正确,则提示"登录成功,欢迎回来!",若用户名与密码不匹配,则提示"用户名和 ...
- Django 登录验证-自动重定向到登录页
Web项目有些场景需要做用户登录验证,以便访问不同页面. 方法一:login_required装饰器 适用于函数视图. from django.contrib.auth.decorators impo ...
- 关于postman的接口登录验证问题
1.shiro的接口登录问题 碰到需要接口登录验证的:访问项目接口地址login,找到cookie将Cookie数据放入postman的headers 中. 2.碰到 security的项目.首先把相 ...
- yii 用户登录验证(cwebuser) yii 用户登录 (记)
yii 的确是一个强大而臃肿的框架,简单的小项目,或者只做后台接口调用的项目,建议不要用. 今天记录一下yii使用中cwebuser(Yii::app()->user->login())登 ...
- 2-功能1:基于用户认证组件和Ajax实现登录验证(图片验证码)
1.登录页面的设计 (1)label标签的id属性 label标签的id属性,点击label标记,相当于点击了input框 bootstarp样式 class="form-group&quo ...
- django 从零开始 8 用户登录验证 待测
看文档 djang 自带一个用户登录验证的方法,不过有些看着懵逼,去网上找了一圈,发现很多都是照抄文档说明的,几乎没说啥原理 特别是 from django.contrib.auth import a ...
- android loginDemo +WebService用户登录验证
android loginDemo +WebService用户登录验证 本文是基于android4.0下的loginActivity Demo和android下的Webservice实现的.l ...
随机推荐
- React之JSX语法
1. JSX的介绍 JSX(JavaScript XML)——一种在React组件内部构建标签的类XML语法.react在不使用JSX的情况下一样可以工作,然而使用JSX可以提高组件的可读性,因此 ...
- Metasploit 内网渗透篇
0x01 reverse the shell File 通常做法是使用msfpayload生成一个backdoor.exe然后上传到目标机器执行.本地监听即可获得meterpreter shell. ...
- 【bzoj3298】[USACO 2011Open]cow checkers(博弈论)
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=3298 博弈论经典结论题,我也没什么好说的.matrix67大佬比我想得深入的多:捡石子 ...
- Effective C++ 条款12:复制对象时勿忘其每一个成分
void logCall(const std::string& funcName); class Customer { public: ... Customer (const Customer ...
- StrStr,判断一个字符串是不是另一个字符串的字串,并返回子串的位置
public int strStr(String haystack, String needle) { if(haystack == null || needle == null) { return ...
- PL/SQL通过修改配置文件的方式实现数据库的连接
http://jingyan.baidu.com/article/c74d600080632a0f6a595d80.html
- SessionUtils
import com.diyfintech.constant.Constant.SuperAdmin; import com.diyfintech.pojo.SysUser; import org.a ...
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource
二月 20, 2017 3:09:47 下午 org.apache.catalina.startup.SetAllPropertiesRule begin警告: [SetAllPropertiesRu ...
- LeetCode OJ:Sqrt(x)(平方根)
Implement int sqrt(int x). Compute and return the square root of x. 简单的二分法,注意mid应该选为long,否则容易溢出: cla ...
- 【linux】基础知识学习
[版本] 两种:内核版本 . 发行版本 内核版本从 www.kernel.org 查看 发行版本: 1.retHat, centOS 更稳定,更安全,适宜做企业服务器 2.ubuntu, Debian ...