<?php



namespace Admin\Controller;



use Common\Controller\AdminbaseController;



class UserController extends AdminbaseController{



	protected $users_model,$role_model;



	



	function _initialize() {



		parent::_initialize();



		$this->users_model = D("Common/Users");



		$this->role_model = D("Common/Role");



	}



	function index(){



		$count=$this->users_model->where(array("user_type"=>1))->count();



		$page = $this->page($count, 20);



		$users = $this->users_model



		->where(array("user_type"=>1))



		->order("create_time DESC")



		->limit($page->firstRow . ',' . $page->listRows)



		->select();



		



		$roles_src=$this->role_model->select();



		$roles=array();



		foreach ($roles_src as $r){



			$roleid=$r['id'];



			$roles["$roleid"]=$r;



		}



		$this->assign("page", $page->show('Admin'));



		$this->assign("roles",$roles);



		$this->assign("users",$users);



		$this->display();



	}



	



	



	function add(){



		$roles=$this->role_model->where("status=1")->order("id desc")->select();



		$this->assign("roles",$roles);



		$this->display();



	}



	



	function add_post(){



		if(IS_POST){



			if(!empty($_POST['role_id']) && is_array($_POST['role_id'])){



				$role_ids=$_POST['role_id'];



				unset($_POST['role_id']);



				if ($this->users_model->create()) {



					$result=$this->users_model->add();



					if ($result!==false) {



						$role_user_model=M("RoleUser");



						foreach ($role_ids as $role_id){



							$role_user_model->add(array("role_id"=>$role_id,"user_id"=>$result));



						}



						$this->success("添加成功！", U("user/index"));



					} else {



						$this->error("添加失败！");



					}



				} else {



					$this->error($this->users_model->getError());



				}



			}else{



				$this->error("请为此用户指定角色！");



			}



			



		}



	}



	



	



	function edit(){



		$id= intval(I("get.id"));



		$roles=$this->role_model->where("status=1")->order("id desc")->select();



		$this->assign("roles",$roles);



		$role_user_model=M("RoleUser");



		$role_ids=$role_user_model->where(array("user_id"=>$id))->getField("role_id",true);



		$this->assign("role_ids",$role_ids);



			



		$user=$this->users_model->where(array("id"=>$id))->find();



		$this->assign($user);



		$this->display();



	}



	



	function edit_post(){



		if (IS_POST) {



			if(!empty($_POST['role_id']) && is_array($_POST['role_id'])){



				if(empty($_POST['user_pass'])){



					unset($_POST['user_pass']);



				}



				$role_ids=$_POST['role_id'];



				unset($_POST['role_id']);



				if ($this->users_model->create()) {



					$result=$this->users_model->save();



					if ($result!==false) {



						$uid=intval($_POST['id']);



						$role_user_model=M("RoleUser");



						$role_user_model->where(array("user_id"=>$uid))->delete();



						foreach ($role_ids as $role_id){



							$role_user_model->add(array("role_id"=>$role_id,"user_id"=>$uid));



						}



						$this->success("保存成功！");



					} else {



						$this->error("保存失败！");



					}



				} else {



					$this->error($this->users_model->getError());



				}



			}else{



				$this->error("请为此用户指定角色！");



			}



			



		}



	}



	



	/**



	 *  删除



	 */



	function delete(){



		$id = intval(I("get.id"));



		if($id==1){



			$this->error("最高管理员不能删除！");



		}



		



		if ($this->users_model->where("id=$id")->delete()!==false) {



			M("RoleUser")->where(array("user_id"=>$id))->delete();



			$this->success("删除成功！");



		} else {



			$this->error("删除失败！");



		}



	}



	



	



	function userinfo(){



		$id=get_current_admin_id();



		$user=$this->users_model->where(array("id"=>$id))->find();



		$this->assign($user);



		$this->display();



	}



	



	function userinfo_post(){



		if (IS_POST) {



			$_POST['id']=get_current_admin_id();



			$create_result=$this->users_model



			->field("user_login,user_email,last_login_ip,last_login_time,create_time,user_activation_key,user_status,role_id,score,user_type",true)//排除相关字段



			->create();



			if ($create_result) {



				if ($this->users_model->save()!==false) {



					$this->success("保存成功！");



				} else {



					$this->error("保存失败！");



				}



			} else {



				$this->error($this->users_model->getError());



			}



		}



	}



	



	    function ban(){



        $id=intval($_GET['id']);



    	if ($id) {



    		$rst = $this->users_model->where(array("id"=>$id,"user_type"=>1))->setField('user_status','0');



    		if ($rst) {



    			$this->success("管理员停用成功！", U("user/index"));



    		} else {



    			$this->error('管理员停用失败！');



    		}



    	} else {



    		$this->error('数据传入失败！');



    	}



    }



    



    function cancelban(){



    	$id=intval($_GET['id']);



    	if ($id) {



    		$rst = $this->users_model->where(array("id"=>$id,"user_type"=>1))->setField('user_status','1');



    		if ($rst) {



    			$this->success("管理员启用成功！", U("user/index"));



    		} else {



    			$this->error('管理员启用失败！');



    		}



    	} else {



    		$this->error('数据传入失败！');



    	}



    }



	

	/**

     * 操作日志管理

     * @author 顽皮蛋 <shf_l@163.com>

     */

	public function log(){



        $user = I('user');

        $ip = I('ip');

        if($ip)$map['ip'] = $ip;

        if($user)$map['user'] = $user;

        $count=M("admin_exec_log")->where($map)->count();

        $page = $this->page($count, 20);

        $lists = M("admin_exec_log")

        ->where($map)

        ->order("id DESC")

        ->limit($page->firstRow . ',' . $page->listRows)

        ->select();

        $this->assign('lists', $lists);

        $this->assign("page", $page->show('Admin'));

        $this->assign('url', $url);

        $this->assign('ip', $ip);

        $this->assign('user', $user);

        $this->display();

	}

	/**

     * 删除日志

     * @author 顽皮蛋 <shf_l@163.com>

     */

    public function deleteLog(){

        if(isset($_POST['ids'])){

            $ids = implode(",", $_POST['ids']);

            if (M('admin_exec_log')->where("id in ($ids)")->delete()!==false) {

                $this->success("删除成功！");

            }else{

                $this->error("删除失败！");

            }

        }else{

            $id = intval(I("get.id"));

            if (M('admin_exec_log')->delete($id)!==false) {

                $this->success("删除成功！");

            } else {

                $this->error("删除失败！");

            }

        }    	

    }



	/**

     * 后台登陆错误记录管理

     * @author 顽皮蛋 <shf_l@163.com>

     */

	public function logError(){



        $username = I('username');

        $map['username']    =   array('like', '%'.(string)$username.'%');

        $count=M("admin_login_error")->where($map)->count();

        $page = $this->page($count, 20);

        $lists = M("admin_login_error")

        ->where($map)

        ->order("id DESC")

        ->limit($page->firstRow . ',' . $page->listRows)

        ->select();

        $this->assign('lists', $lists);

        $this->assign("page", $page->show('Admin'));

        $this->assign('username', $username);

        $this->display();

	}

	/**

     * 删除登陆错误记录

     * @author 顽皮蛋 <shf_l@163.com>

     */

    public function deleteLogError(){

        if(isset($_POST['ids'])){

            $ids = implode(",", $_POST['ids']);

            if (M('admin_login_error')->where("id in ($ids)")->delete()!==false) {

                $this->success("删除成功！");

            }else{

                $this->error("删除失败！");

            }

        }else{

            $id = intval(I("get.id"));

            if (M('admin_login_error')->delete($id)!==false) {

                $this->success("删除成功！");

            } else {

                $this->error("删除失败！");

            }

        }    	

    }

}