AdminAction.class.php 3.5 KB
<?php
Class AdminAction extends CommonAction{
	private $tab;
	public function __construct() {
		parent::__construct();
		$this->tab='Admin';
		// $this->rotab='Role';
	}
	
	public function index()
	{
		$Admin=D($this->tab);
		import('ORG.Util.Pages');
		$map['id']=array('neq',1);
		$count = $Admin->where($map)->count();
		$Page = new Pages($count,20);
		$field=array('id','username','tel','email','name','isshow','ltime','lip','addtime','updatatime');
		$lists = $Admin->field($field)->where($map)->page($Page->nowPage.','.$Page->listRows)->order(array('id'=>'desc'))->select();
		$show = $Page->show();
		$this->assign('page',$show);
		$this->assign('lists',$lists);
		$this->display();
	}

	public function edit()
	{
		$Admin=D($this->tab);
		$data=$Admin->where('id='.I('id'))->find();
		if($this->isPost()){
			//添加用户
			if(!$Admin->create()){
				$this->error($Admin->getError());
			}else{
				if(!$Admin->password){	//修改账号,如果没有输入密码则删除password
					unset($Admin->password);
				}
				$lastid=$Admin->where('id='.I('id'))->save();
				if($lastid>0){
					$this->success('系统用户修改成功',U('Admin/index'));
				}else{
					$this->error('系统用户修改失败');
				}
			}
		}else{
			$this->assign('data',$data);
			$this->display(); 
		}
	}
	
	public function add()
	{
		$Admin=D($this->tab);
		if($this->isPost()){
			//添加用户
			if(!$Admin->create()){
				$this->error($Admin->getError());
			}else{
				$depth = $this->getDepth();	//获取depth=array('depth','pid')
				$Admin->depth = $depth['depth'];
				$Admin->pid = $depth['pid'];
				$lastid=$Admin->add();
				if($lastid>0){
					$this->success('系统用户添加成功',U('Admin/index'));
				}else{
					$this->error('系统用户添加失败');
				}
			}
		}else{
			$this->display(); 
		}
	}
/**
 * 获取当前登录账号的depth+id
 * @return array('id','depth')
 */
	private function getDepth(){
		$m = M($this->tab);
		$map['id'] = array('eq',cookie('ADMIN_KEY'));
		$r = $m->field('id,depth')->where($map)->find();
		$back['depth'] = $r['depth'].','.$r['id'];
		$back['pid'] = $r['id'];
		return $back;
	}

	public function delete()
	{
		$where['id']=I('id',0,'intval');
		$Admin=D($this->tab);
		$data=$Admin->where($where)->find();
		if(!$data){
			$this->error('信息有误!');
		}
		if($data['id'] == cookie('ADMIN_KEY')){
			$this->error('不允许删除当前使用的管理员帐号');
		}elseif($data['id'] == 1){
			$this->error('该账号不允许删除!');
		}else{
			$count=$Admin->where($where)->delete();
			if($count){
				$this->delAuthAccess($data['id']);
				$this->success('管理员删除成功',U('Admin/index'));
			}else{
				$this->error('管理员删除失败');
			}
		}
	}
/**
 * 删除会员时删除相应的角色分配
 * @param  int $uid 用户id
 */
	private function delAuthAccess($uid){
		if($uid){
			$m = M('Authgroupaccess');
			$map['uid'] = array('eq',$uid);
			$r = $m->where($map)->delete();
		}
	}
/**
 * 修改分组开启属性
**/
	public function editattr()
	{
		$table=$this->_post('table');
		$m=M($table);
		$where['id']=array('eq',$this->_post('id'));
		$data[$this->_post('f')]=$this->_post('status');
		$count=$m->where($where)->data($data)->save();
		if($count)
		{
			$msg['success']=true;
			$msg['msg']='状态已修改';
		}
		else
		{
			$msg['success']=false;
			$msg['msg']='状态修改失败';
		}
		echo json_encode($msg);
	}
}
?>