奇矩互动官方论坛's Archiver

lulu 发表于 2008-4-7 00:25

权限控制类

[code]<?php
/**
* Displays : 权限控制类
* Author   : phpox
* Date     : Fri Nov 30 13:13:48 CST 2007
*/

class include_purview
{
    /**
     * 类的唯一实例
     */
    private static $instance;
   
    /**
     * 权限常量
     */
    const SELECT = 0x1; //查询
    const CREATE = 0x2; //添加
    const EDIT   = 0x4; //修改
    const DELETE = 0x8; //删除
   
    /**
     * 角色
     */
    private $annoy = ''; //匿名用户
    private $user  = ''; //注册用户
    private $admin = ''; //管理用户
    private $usertype = 'annoy';
    private $hashtable = array(1=>'查询',2=>'添加',4=>'修改',8=>'删除');
   
    /**
     *
     */
    public function __set($name,$value)
    {
        if ($name == 'usertype')
        {
            if ($value != '')
            {
                $this->usertype = $value;
            }
        }
    }
   
    /**
     * 构造函数 给角色赋予权限
     */
    private function __construct()
    {
        $this->annoy = self::SELECT;
        $this->user = self::SELECT | self::CREATE;
        $this->admin = self::SELECT | self::CREATE | self::EDIT | self:ELETE;
    }
   
    /**
     * 获取类的唯一实例
     */
    public static function getInstance()
    {
        if (self:instance === null){
            self:instance = new include_purview();
        }
        return self:instance;
    }
   
    /**
     * 检查权限
     *
     * @param $usertype 角色
     * @param $purview 权限
     */
    public function check($purview)
    {
        if ($this->{$this->usertype} & $purview)
        {
            return true;
        }
        return false;
    }
   
    /**
     * 给角色加权限
     */
    public function addPur($purview)
    {
        $this->{$this->usertype} |= $purview;
    }
   
    /**
     * 给角色减权限
     */
    public function delPur($purview)
    {
        $this->{$this->usertype} ^= $purview;
    }
   
    /**
     * 返回角色拥有的权限
     */
    public function getPur()
    {
        $arr = array();
        foreach ($this->hashtable as $k => $v)
        {
            if ($k & $this->{$this->usertype})
            {
                $arr[] = $v;
            }
        }
        return $arr;
    }
}
?>[/code]

页: [1]

Powered by Discuz! Archiver 7.0.0  © 2001-2009 Comsenz Inc.