21,891
社区成员
发帖
与我相关
我的任务
分享
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* 获取当前用户所在组的权限并生成一个权限文件缓存在文件系统中
*/
class Right{
//用户所在组的ID
private $group_id = "";
//权限表的名称
private $right_table = "";
//当前用户所在用户组的名称
private $group_name = "admin";
//存放查询的用户组权限数据位置
private $cache_dir = "application/rightcache";
public function __construct($group_id = 1,$right_table = 'right')
{
$this->CI =& get_instance();
$this->group_id = $group_id;
$this->right_table = $right_table;
if( ! $this->judge_group_cache_file_exists())
{
$db = $this->CI->load->database();
$this->get_group_for_all_right();
}
}
/**
* 判断当前用户所在的用户组的缓存数据文件是不是存在
*
* @return string
*/
public function judge_group_cache_file_exists()
{
$file = $this->cache_dir.'/'.md5($this->group_name).'.php';
$result = @file_get_contents($file);
return $result;
}
/**
* 取出该用户组对系统所有模块的权限
*
*/
public function get_group_for_all_right()
{
$r = $this->CI->db->get_where($this->right_table,array('group_id' => $this->group_id));
$data = $r->result_array();
$r->free_result();
$right = array();
foreach($r->result_array() as $row)
{
@$right[$row['mod_id']] += pow(2,$row['right_id']);
}
$this->set_file_cache($right);
}
/**
* 调整从数据库中查出的权限数组为需要的格式
* 格式:
* array = (
* [mod_id_1] = 2^权限ID_1 + .... 2^权限ID_N,
* [mod_id_2] = 2^权限ID_1 + .... 2^权限ID_N,
* ......
* [mod_id_N] = 2^权限ID_1 + .... 2^权限ID_N
* )
* 然后写入指定的权限缓存文件中
* @param array $right
* @return number $int
*/
public function set_file_cache($right)
{
$right='<?php
$array='.var_export($right,TRUE).';';
$file = $this->cache_dir.'/'.md5($this->group_name).'.php';
$int = file_put_contents($file, $right);
return $int;
}
/**
* 权限在数据库中的相关信息变更时触发更新权限缓存文件
*
*/
public function update_right_file_cache()
{
$this->get_group_for_all_right();
}
}
// END Right Class
/* End of file Right.php */
/* Location: ./application/libraries/Right.php */