wtuyss 发表于 2017-3-27 09:46:02

PHP-权限控制类




[*]<?php
[*]/**
[*]* 权限控制类
[*]*/
[*]
[*]classinclude_purview
[*]{
[*]/**
[*] * 类的唯一实例
[*] */
[*]privatestatic$instance;
[*]
[*]/**
[*] * 权限常量
[*] */
[*]constSELECT = 0x1;//查询
[*]constCREATE = 0x2;//添加
[*]constEDIT = 0x4;//修改
[*]constDELETE=0x8;//删除
[*]
[*]/**
[*] * 角色
[*] */
[*]private$annoy='';//匿名用户
[*]private$user='';//注册用户
[*]private$admin='';//管理用户
[*]private$usertype='annoy';
[*]private$hashtable=array(1=>'查询',2=>'添加',4=>'修改',8=>'删除');
[*]
[*]/**
[*] *
[*] */
[*]publicfunction__set($name,$value)
[*] {
[*]if($name=='usertype')
[*] {
[*]if($value!='')
[*] {
[*]$this->usertype =$value;
[*] }
[*] }
[*] }
[*]
[*]/**
[*] * 构造函数 给角色赋予权限
[*] */
[*]privatefunction__construct()
[*] {
[*]$this->annoy = self::SELECT;
[*]$this->user = self::SELECT | self::CREATE;
[*]$this->admin = self::SELECT | self::CREATE | self::EDIT | self::DELETE;
[*] }
[*]
[*]/**
[*] * 获取类的唯一实例
[*] */
[*]publicstaticfunctiongetInstance()
[*] {
[*]if(self::$instance===null){
[*] self::$instance=newinclude_purview();
[*] }
[*]returnself::$instance;
[*] }
[*]
[*]/**
[*] * 检查权限
[*] *
[*] * @param $usertype 角色
[*] * @param $purview 权限
[*] */
[*]publicfunctioncheck($purview)
[*] {
[*]if($this->{$this->usertype}&$purview)
[*] {
[*]returntrue;
[*] }
[*]returnfalse;
[*] }
[*]
[*]/**
[*] * 给角色加权限
[*] */
[*]publicfunctionaddPur($purview)
[*] {
[*]$this->{$this->usertype}|=$purview;
[*] }
[*]
[*]/**
[*] * 给角色减权限
[*] */
[*]publicfunctiondelPur($purview)
[*] {
[*]$this->{$this->usertype}^=$purview;
[*] }
[*]
[*]/**
[*] * 返回角色拥有的权限
[*] */
[*]publicfunctiongetPur()
[*] {
[*]$arr=array();
[*]foreach($this->hashtableas$k=>$v)
[*] {
[*]if($k&$this->{$this->usertype})
[*] {
[*]$arr[] =$v;
[*] }
[*] }
[*]return$arr;
[*] }
[*]}

调用示例

PHP代码

[*]/**
[*]* 示例
[*]*/
[*]//在session中读用户组
[*]@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('您没有权限!');
[*]}
[*]
[*]//具体的操作
[*]functioncreate()
[*]{
[*]echo'执行了添加操作'."\n";
[*]}


页: [1]
查看完整版本: PHP-权限控制类