实现权限控制的一种方法

有时候开发项目时,需要用到权限控制,这里提供一种简单的方法,表单页面:

<table width="100%">
    <tbody>
    	<tr height="20" >
    	  <td align="left">
    	   <input name="auth[project-index]" value="1" id="c_project" type="checkbox" onclick="setC('project')" <?php if ($auth['project-index']) echo 'checked' ?> >项目管理</td>
    	 <td align="left">
            <input class="c_project" name="auth[project-add]" type="checkbox" value="1" <?php if ($auth['project-add']) echo 'checked' ?> ><span>添加</span>
    	<input class="c_project" name="auth[project-edit]" type="checkbox" value="1" <?php if ($auth['project-edit']) echo 'checked' ?> ><span>修改</span>
    	<input class="c_project" name="auth[project-del]" type="checkbox" value="1" <?php if ($auth['project-del']) echo 'checked' ?> ><span>删除</span>
            </td></tr>
    	<tr height="20">
    		<td align="left">
    		<input name="auth[dep-index]" value="1" id="c_dep" type="checkbox" onclick="setC('dep')" <?php if ($auth['dep-index']) echo 'checked' ?> >部门管理 </td>
    		<td align="left">
    			<input class="c_dep" name="auth[dep-add]" type="checkbox" value="1" <?php if ($auth['dep-add']) echo 'checked' ?> ><span>添加</span>
    			<input class="c_dep" name="auth[dep-edit]" type="checkbox" value="1" <?php if ($auth['dep-edit']) echo 'checked' ?> ><span>修改</span>
    			<input class="c_dep" name="auth[dep-del]" type="checkbox" value="1" <?php if ($auth['dep-del']) echo 'checked' ?> ><span>删除</span>
    		</td>
    	</tr>
    	<tr height="20">
    		<td align="left">
    		<input name="auth[user-index]" value="1" id="c_user" type="checkbox" onclick="setC('user')" <?php if ($auth['user-index']) echo 'checked' ?> >用户管理 </td>
    		<td align="left">
    			<input class="c_user" name="auth[user-add]" type="checkbox" value="1" <?php if ($auth['user-add']) echo 'checked' ?> ><span>添加</span>
    			<input class="c_user" name="auth[user-edit]" type="checkbox" value="1" <?php if ($auth['user-edit']) echo 'checked' ?> ><span>修改</span>
    			<input class="c_user" name="auth[user-del]" type="checkbox" value="1" <?php if ($auth['user-del']) echo 'checked' ?> ><span>删除</span>
    		</td>
    	</tr>
        </tbody>
</table>


实现选中多个的JS方法(不要忘记引入jquery.js文件)

function setC(c) {
 if($("#c_"+c).attr('checked')) {
  $(".c_"+c).attr("checked",true);
 } else {
  $(".c_"+c).attr("checked",false);
 }
}

假如选中项目添加和任务添加复选框,提交后用$_POST['auth']接收到的数据是这样的数组形式:array('project-add'=>1,'task-add'=>1)

用php函数转化成字符串:$auth = array2string($_post['auth']),存到角色表的auth字段中

/**
 * 将数组转换为字符串
 */
function array2string($data, $isformdata = 1)
{
    if ($data == '') return '';
    if ($isformdata) $data = new_stripslashes($data);
    return serialize($data);
}

/**
 * 返回经stripslashes处理过的字符串或数组
 */
function new_stripslashes($string)
{
    if (!is_array($string)) return stripslashes($string);
    foreach ($string as $key => $val) $string[$key] = new_stripslashes($val);
    return $string;
}

当这个用户登录时,获取它所属的角色的auth字段值$data['auth'],然后$UserInfo['auth'] = string2array($data['auth']),将字符串转化成数组

/**
 * 将字符串转换为数组
 */
function string2array($data)
{
    if ($data == '') return array();
    return unserialize($data);
}

假如现在要判断它是否具有任务添加的权限,则

if($checkAuth('task-add')){
   echo('具有任务添加的权限');
}
function checkAuth($string){
    global $UserInfo;
    if ($UserInfo['auth'][$string]){
        return true;
    }else {
        return false;
    }
}

转载于:https://my.oschina.net/u/2318591/blog/378788

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值