<?php
class Permission extends DBModel {
	public  $ignore = array();
	/*设置忽略的模块*/
	public function setIgnore($array=null){
		if (!defined('SUCCEED_VODCMS')){
			exit();
		}
		if (is_array($array)===false){
			throw new Exception('设置忽略模块时传入的参数不是一个数组!');
		}else{
			$this->ignore = $array;
		}
	}
	/*
	获取用户组拥有权限信息记录
	*/
	public function getAll($where=null){
		$array = array();
		$select = $this->_DB->select();
		$sql = $select->from(VODCMS_PRIVILEGES)->where($where)->toString();
		foreach ($this->_DB->fetAll($sql) as $row){
			$array[$row['group']][$row['module']][] = $row['method'];
		}
		return $array;
	}
	/*
	验证用户组是否拥有此模块的权限
	传入用户组名
	*/
	public function CheckPermission($group=null){
		if ($this->_Response->getSession('id')){
			return true;
		}
		if(is_null($group)===true){
			if ($this->_Response->getSession('group')){
				$group = $this->_Response->getCookie('group');
			}else{
				$group = 'Guest';
			}
		}
		$control = strtolower($_GET['controller']).'controller';
		foreach ($this->ignore as $row){
			//echo $row.'='.strtolower($_GET['controller']).'<br>';
			if (is_array($row)){	//如果是数组表明是忽略控制器下面的动作
				if (@array_shift(array_keys($row)) == @strtolower($_GET['controller'])){
					if (@array_shift($row) == $_GET['action']){
						return true;
					}
				}
			}elseif($row == strtolower($_GET['controller'])){
				return true;
			}
		}
		$data = $this->getAll(array('group'=> $group));
		if (count($data)<=0){
			$publicmod = $this->ignore;
		}else{
			$publicmod = array_keys($data[$group]);
		}
		$method = strtolower($this->_Response->getParam('method'));
		if ( in_array($control, $publicmod )){
			return true;
		}else{
			exit('您没有此功能访问权限');
		}
	}
}