【发布时间】:2015-11-14 14:08:42
【问题描述】:
我目前在我的一个 php 类中有一个权限函数,然后在我的数据库中声明每个用户组的权限。
在我的 html 站点代码中,我包含一个如下所示的 if 语句,仅显示某些用户权限。
我遇到的唯一问题是,例如,讲师权限可以访问标准用户可以访问的所有内容以及讲师特定的任何内容。
这很好用,但是因为我还包括一个管理员权限,可以访问其他人可以访问的所有内容以及更多内容。
对于没有权限的人没有错误,对于有权访问所有内容的经理也没有错误。
但是,当我以讲师身份登录时,我得到未定义的“经理”变量,因为该组无权访问经理字段,但如果它无权查看该部分,我只想让它忽略它不显示未定义的变量。我的班级的打印屏幕、数据库设置和下面的代码。
当用户具有管理员权限时,仅在屏幕上显示 html 代码的示例,对于仅在 if 语句中写入讲师的讲师也是如此。
<?php if($user->hasPermission('manager')) { ?>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><span class="caret"></span> Manager Reports</a>
<ul class="dropdown-menu">
<li><a href="#">Financial Reports</a> </li>
<li><a href="#">Employee Report</a> </li>
<li><a href="#">Lesson Report</a> </li>
</ul>
</li>
<?php } ?>
我的 php 类中的 hasPermissions 函数
public function hasPermission($key){
$group = $this->_db->get('groups', array('ID', '=', $this->data()->group));
if($group->count()) {
$permissions = json_decode($group->first()->permissions, true);
if($permissions[$key] == true) {
return true;
}
}
return false;
}
【问题讨论】:
标签: php html if-statement permissions