http://blog.csdn.net/painsonline/article/details/7183679php
- <?php
-
- 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::DELETE;
- }
-
-
- public static function getInstance()
- {
- if (self::$instance === null){
- self::$instance = new include_purview();
- }
- return self::$instance;
- }
-
-
- 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;
- }
- }
調用示例session
PHP代碼
- @session_start();
- $_SESSION['role'] = 'user';
-
- $pruview = include_purview::getInstance();
- $pruview->usertype = $_SESSION['role'];
- $arr = $pruview->getpur();
- echo '該用戶的權限有:'.join(',',$arr)."\n";
- if (true === $pruview->check(include_purview::CREATE ))
- {
- create();
- }
- else
- {
- exit('您沒有權限!');
- }
- $pruview->delPur(include_purview::CREATE );
- $arr = $pruview->getpur();
- echo '該用戶的權限有:'.join(',',$arr)."\n";
- if (true === $pruview->check(include_purview::CREATE ))
- {
- create();
- }
- else
- {
- echo '您沒有權限!'."\n";
- }
- $pruview->addPur(include_purview::CREATE );
- $arr = $pruview->getpur();
- echo '該用戶的權限有:'.join(',',$arr)."\n";
- if (true === $pruview->check(include_purview::CREATE ))
- {
- create();
- }
- else
- {
- exit('您沒有權限!');
- }
-
- function create()
- {
- echo '執行了添加操做'."\n";
- }