登录认证
<无详细内容>
<?php
/*
*
* @copyright 2011
* @version $Id$
* @access public
* @property string $id
* @property string $account
* @property string $pwd
* @property string $lastquesttime
* @property string $lastip
* @property int $regtime
* @property int $accounttype
* @property string $oldaccount
* @property int $logintimes
* @property string $currentIP
* @property int $cid
*/
class Resposity extends AdminActiveRecord {
public $connectionPrefix = 'passport';
protected $list = null;
protected $passportConfig = null;
public $oldaccount = '';
/**
* Returns the static model of the specified AR class.
* @return Resposity the static model class
*/
public static function model($className = __class__) {
return parent::model ( $className );
}
/**
* Modify it if you need.
* @return mixed the primaryKey.
*/
public function primaryKey() {
return 'id';
}
/**
* @return array validation rules for model attributes.
*/
public function rules() {
// NOTE: you should only define rules for those attributes that
// will receive user inputs.s
return array (
array ('accounttype,cid', 'required' ),
array ('accounttype', 'in', 'range' => array ('0', '1', '2', '3', '4', '5', '6', '7' ) ),
array ('currentIP', 'ip')
);
}
/**
根据主帐号获得用户信息
**/
protected function getUserInfoByMainAccount($account,$cid){
$account=strtolower($account);
$cacheKey = hash ( 'md5', $account.'^]'.$cid.'^]'.'0' );
$user=UtilD::getCache('resposity',$cacheKey);
if(is_array($user)){
UtilD::clearNullOrEmptyValue ( $user );
}else{
$user=array();
}
if(count($user)){
return $user;
}else{
$model = $this->find ( 'account=:account AND cid=:cid', array (':account' => $account,':cid' => $cid ) );
if($model){
$user=$model->getAttributes();
unset($model);
}
UtilD::setCache('resposity',$cacheKey,$user);
return $user;
}
}
/**根据子帐号获得用户信息**/
protected function getUserInfoByChildAccount($subaccount,$cid,$accounttype){
if(!$this->validate(array('accounttype'), true)){
return -1000;
}
$subaccount=strtolower($subaccount);
$cacheKey = hash ( 'md5', $subaccount.'^]'.$cid.'^]'.$accounttype );
$userAccounts=UtilD::getCache('resposity',$cacheKey);
if(is_array($userAccounts)){
UtilD::clearNullOrEmptyValue($userAccounts);
}else{
$userAccounts=array();
}
if(!count($userAccounts)){
$accountRefer=new AccountRefer();
$model=$accountRefer->find ( 'subaccount=:subaccount AND cid=:cid AND accounttype=:accounttype', array (':subaccount' => $subaccount,':cid' => $this->cid,':accounttype'=> $accounttype) );
if($model){
$userAccounts=$model->getAttributes();
UtilD::setCache('resposity',$cacheKey,$userAccounts);
}
}
if(isset($userAccounts['account'])){
return $this->getUserInfoByMainAccount($userAccounts['account'],$userAccounts['cid']);
}
return array();
}
/**根据帐号类型获取用户数据**/
public function getUserInfoByAccount(){
if($this->accounttype==0){
return $this->getUserInfoByMainAccount($this->account,$this->cid);
}else{
return $this->getUserInfoByChildAccount($this->account,$this->cid,$this->accounttype);
}
}
/**
* 通过主帐号获取子帐号
*/
public function getSubAccountByMainAccount(){
$c=new CDbCriteria();
$c->addCondition('cid=:cid AND account=:account', 'AND');
$c->params=array(':cid'=>$this->cid, ':account'=>$this->account);
$refer=new AccountRefer();
$result=$refer->getListByPage(1, 30, $c);
$data=array();
if($result['count']>0){
foreach($result['data'] as $row){
$data[$row['accounttype']]=$row['subaccount'];
}
}
return $data;
}
/**
获得当前商户的操作等级
1 对自己添加的用户有完全权限,对其它机构添加的用户无权限
2 对自己添加的用户有完全权限,对accsessids指定机构的用户有查询权限
**/
public function getAccess(){
if ($this->passportConfig === null) {
$this->passportConfig = PassportConfig::model ()->getItemConfigById ( $this->cid );
}
$ip=Yii::app ()->request->getUserHostAddress();
$ips=CJSON::decode($passportConfig['iprouters']);
if(!in_array($ip,$ips)){
throw new CHttpException(403, '您没有权限访问此页面!');
}
}
/**修改名牌用户私有数据**/
public function saveUserAttributes($tickets,array $attributes){
try{
$user=$this->getUserAttributes($tickets);
if(empty($user) || !is_array($user)){
return -1001;
}
$data=array();
if($user[WebUserD::STORAGE_KEY]!=='[]'){
$data=CJSON::decode($user[WebUserD::STORAGE_KEY]);
}
if(!is_array($data)){
$data=array();
}
$attributes=array_change_key_case($attributes);
foreach($attributes as $key=>$value){
if(!is_array($value)){
$data[$key]=$value;
}else{
if(!isset($data[$key])){
$data[$key]=array();
}
$data[$key]=array_merge($data[$key],$attributes[$key]);
}
}
$user[WebUserD::STORAGE_KEY]=CJSON::encode($data);
$user['lastquesttime']=$_SERVER['REQUEST_TIME'];
$user['data']=CJSON::encode($data);
$this->setAttributes($user,false);
!$this->currentIP && $this->currentIP='127.0.0.1';
!$this->lastip && $this->lastip=$this->currentIP;
!$this->logintimes && $this->logintimes = 0;
$this->setIsNewRecord ( false );
if($this->save()){
UtilD::setCache('resposity', $tickets, $user);
}else{
return -1003;
}
}catch(Exception $ex){
return -1004;
}
}
/**修改帐号私有数据**/
public function saveUserAttributesByName( array $attributes){
$user=$this->getUserInfoByMainAccount($this->account,$this->cid);
if(empty($user)){
return -1001;
}
$cacheKey = hash ( 'md5', $user['account'].'^]'.$user['cid'].'^]'.'0' );
return $this-> saveUserAttributes($cacheKey,$attributes);
}
/**
@return array
根据令牌获得帐号全部数据
**/
public function getUserAttributes($tickets){
$t = hash ( 'md5', $tickets . '&' . $this->currentIP . '&' . $this->accounttype . '&' . $this->cid );
$ft=UtilD::getCache('resposity', $t);
if (! $ft) {
$ft = 0;
} else {
if ($ft > 4) { //如果1个ip 1分钟内连续5次获取失败
return -1005;
}
}
$user=UtilD::getCache('resposity',$tickets);
if(!$user){
$ft++;
UtilD::setCache('resposity', $t,$ft,60);
return -1001;
}
$user['lastquesttime']=$_SERVER['REQUEST_TIME'];
UtilD::setCache('resposity',$tickets,$user);
return $user;
}
/**添加主帐号**/
public function add(){
try{
if($this->accounttype!=0){
return -1000;
}
$this->account=strtolower($this->account);
//检查主帐号是否存在
$user=$this->getUserInfoByAccount();
if(is_array($user) && count($user)){
return -1006;
}
//开始保存数据
$this->setIsNewRecord ( true );
$this->lastquesttime=$_SERVER['REQUEST_TIME'];
$this->regtime=$_SERVER['REQUEST_TIME'];
$this->data='[]';
$this->pwd=hash('sha256',$this->pwd);
if(!$this->save()){
return -1007;
}else{
$user=$this->getAttributes();
$cacheKey = hash ( 'md5',$this->account.'^]'.$this->cid.'^]'.$this->accounttype );
UtilD::setCache('resposity', $cacheKey, $user);
}
}catch(Exception $ex){
return -1004;
}
}
/**关联子帐号**/
public function addChild($subaccount){
try{
//检查子是否有同名的帐号与主账号关联
$subaccount=strtolower($subaccount);
$user=$this->getUserInfoByChildAccount($subaccount,$this->cid,$this->accounttype);
if($user){
return -1008;
}
$user=$this->getUserInfoByMainAccount($this->account,$this->cid);
if(!$user){
return -1001;
}
//检查是否有类型的账号与主账号关联
$accountRefer=new AccountRefer();
$model=$accountRefer->find('pid=:pid AND cid=:cid AND accounttype=:accounttype', array (':pid'=>$user['id'],':cid'=>$this->cid,':accounttype'=>$this->accounttype));
if($model){
return -1009;
}
$this->account=strtolower($this->account);
$accountRefer->cid=$this->cid;
$accountRefer->setIsNewRecord ( true );
$accountRefer->pid=$user['id'];
$accountRefer->account=$user['account'];
$accountRefer->subaccount=$subaccount;
$accountRefer->accounttype=$this->accounttype;
if($accountRefer->save()){
$cacheKey = hash ( 'md5',$accountRefer->subaccount.'^]'.$accountRefer->cid.'^]'.$accountRefer->accounttype );
UtilD::setCache('resposity', $cacheKey, $accountRefer->getAttributes());
}else{
return -1010;
}
}catch(Exception $ex){
return -1004;
}
}
/** 修改密码**/
public function changePassword(){
try{
$user=$this->getUserInfoByAccount();//检查主帐号是否存在
if(!$user){
return -1001;
}
$this->setIsNewRecord ( false );
$this->pwd=hash('sha256',$this->pwd);
if($this->pwd!==$user['pwd']){
$this->setIsNewRecord ( false );
$user['pwd']=$this->pwd;
$this->setAttributes($user,false);
if($this->save()){
$cacheKey = hash ( 'md5', $user['account'].'^]'.$user['cid'].'^]'.'0' );
UtilD::setCache('resposity', $cacheKey, $user);
}else{
return -1011;
}
}
}catch(Exception $ex){
return -1004;
}
}
/**修改子帐号**/
public function repickAccount(){
try{
$accounts=$this->getUserInfoByChildAccount($this->oldaccount,$this->cid,$this->accounttype);
if(!$accounts){
return -1001;
}
$cacheKey = hash ( 'md5',$this->oldaccount.'^]'.$this->cid.'^]'.$this->accounttype );
$user=UtilD::getCache('resposity', $cacheKey);
if($this->oldaccount!==$this->account){
$accountRefer=new AccountRefer();
$accountRefer->setIsNewRecord ( false );
$accountRefer->setAttributes($user,false);
$accountRefer->subaccount=$this->account;
if($accountRefer->save()){
$user['subaccount']=$this->account;
UtilD::setCache('resposity', $cacheKey,array());
$cacheKey= hash ( 'md5',$user['subaccount'].'^]'.$this->cid.'^]'.$this->accounttype );
UtilD::setCache('resposity', $cacheKey,$user);
}else{
return -1012;
}
}
}catch(Exception $ex){
return -1004;
}
}
/**
删除主帐号
**/
public function deleteMainAccount(){
try{
$user=$this->getUserInfoByMainAccount($this->account,$this->cid);
if(!$user){
return -1001;
}
//检查是否有子帐号
$accountRefer=new AccountRefer();
$models=$accountRefer->findAll('pid=:pid', array (':pid'=>$user['id']));
if(is_array($models)&&count($models)){
foreach($models as $model){
$k= hash ( 'md5', $model->subaccount.'^]'.$model->cid.'^]'.$model->accounttype );//删除子帐号缓存
UtilD::setCache('resposity', $k,false);
unset($model);
}
unset($models);
}
//删除主帐号缓存
$cacheKey = hash ( 'md5', $this->account.'^]'.$this->cid.'^]'.'0' );
if($this->deleteByPk($user['id'])){
UtilD::setCache('resposity', $cacheKey,false);
}else{
return -1013;
}
}catch(Exception $ex){
return -1004;
}
}
/**删除子帐号**/
public function deleteChildAccount(){
try{
$user=$this->getUserInfoByChildAccount($this->account,$this->cid,$this->accounttype);
if(!$user){
return -1014;
}
//删除缓存
$cacheKey = hash ( 'md5', $this->account.'^]'.$this->cid.'^]'.$this->accounttype );
$child=UtilD::getCache('resposity',$cacheKey);
$accountRefer=new AccountRefer();
if($accountRefer->deleteByPk($child['id'])){
UtilD::setCache('resposity', $cacheKey,false);
}else{
return -1014;
}
}catch(Exception $ex){
return -1004;
}
}
public function login(){
try{
if(empty($this->currentIP)){
return -1017;
}
$tickets=hash ( 'md5', $this->account.'^]'.$this->cid.'^]'.$this->accounttype );
$t = hash ( 'md5', $tickets . '&' . $this->currentIP . '&' . $this->accounttype . '&' . $this->cid );
$ft=UtilD::getCache('resposity', $t);
if (! $ft) {
$ft = 0;
} else {
if ($ft > 4) { //如果1个ip 1分钟内连续5次获取失败
return -1015;
}
}
$user=$this->getUserInfoByAccount();
if(!$user){
$ft++;
UtilD::setCache('resposity', $t,$ft,60);
return -1001;
}
$this->pwd=hash('sha256',$this->pwd);
if($user['pwd']!==$this->pwd){
$ft++;
UtilD::setCache('resposity', $t,$ft,60);
return -1016;
}
$tickets=hash ( 'md5', $user['account'].'^]'.$user['cid'].'^]'.'0' );
//更新登录次数和最后请求时间
if(!isset($user['currentIP'])){
$user['currentIP']=$this->currentIP;
}
$user['lastip']=$user['currentIP'];
$user['currentIP']=$this->currentIP;
if(!isset($user['logintimes'])){
$user['logintimes']=0;
}
$user['logintimes']++;
$user['lastquesttime']=$_SERVER['REQUEST_TIME'];
$this->setAttributes($user,false);
if($this->save()){
UtilD::setCache('resposity', $tickets,$user);
return array('tickets'=>$tickets);
}else{
return -1017;
}
}catch(Exception $ex){
return -1004;
}
}
}
精彩图集
精彩文章






