这里做的是:基于角色的访问控制(role-based access control),在rbac中,权限与角色相关联,用户通过成为适当角色的成员而得到这些角色的权限。这就极大地简化了权限的管理。在一个组织中,角色是为了完成各种工作而创造,用户则依据它的责任和资格来被指派相应的角色,用户可以很容易地从一个角色被指派到另一个角色。角色可依新的需求和系统的合并而赋予新的权限,而权限也可根据需要而从某角色中回收。角色与角色的关系可以建立起来以囊括更广泛的客观情况。
先介绍下表结构:
create table `sp_auth` (
`auth_id` smallint(5) unsigned not null auto_increment,
`auth_name` varchar(30) not null comment '权限名称',
`action_name` varchar(30) not null comment '权限代码',
`desc` varchar(120) not null default '' comment '权限描述',
`pid` smallint(5) unsigned not null default '0' comment '上级权限id',
`sort_id` smallint(5) unsigned not null default '0' comment '权限排序值',
`add_time` int(11) unsigned not null default '0' comment '添加时间',
`update_time` timestamp not null default current_timestamp on update current_timestamp comment '更新时间',
`is_delete` tinyint(1) unsigned not null default '0' comment '是否删除(0 未删除 | 1 已删除)',
primary key (`auth_id`),
unique key `action_name` (`action_name`),
key `pid` (`pid`),
key `add_time` (`add_time`),
key `is_delete` (`is_delete`),
key `controller_name` (`controller_name`(6)),
key `auth_name` (`auth_name`(16)),
key `sort_id` (`sort_id`)
) engine=myisam auto_increment=113 default charset=utf8 comment='权限表';
处理的方法:
//打印无限极树形结构菜单展示
function _resorts($data, $pid=0)
{
$ret = array();
foreach ($data as $k => $v) {
if($v['pid'] == $pid) {
$v['children'] = _resorts($data, $v['auth_id']);
$ret[] = $v;
}
}
return $ret;
}
//打印二级菜单的方法
function getmenushow($data)
{
$ret = array();
if (!is_array($data)) {
return false;
}
foreach ($data as $key => $val) {
if ($val['pid'] == 0) {
//再次遍历,将第二级别的放在作为其子菜单
foreach ($data as $k => $v) {
if ($v['pid'] == $val['auth_id']) {
$val['children'][] = $v;
}
}
$ret[] = $val;
}
}
return $ret;
}
这样就能够获取展示的菜单数据。
