gpt4 book ai didi

php - 权限组无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-03 12:18:18 26 4
gpt4 key购买 nike

好的,我已经建立了一个基本的成员(member)系统,我的数据库中有组(成员、管理员和版主)。这些组有 3 个字段,id、name 和 permission。成员我将权限留空,管理员有 {"admin": 1"moderator": 1} 和 moderator 有 {"moderator": 1}。

我在 classes 文件夹中的 user.php 文件中有一个简单的函数

函数

 class User {
public function hasPermission($key) {
$group = $this->_db->query("SELECT * FROM groups WHERE id = ?", array($this->data()->group));

if($group->count()) {
$permissions = json_decode($group->first()->permissions, true);

if($permissions[$key] === 1) {
return true;
}
}

return false;
}
}

然后在文件 admin.php 中,我有一段简单的代码,如果登录的用户是管理员,则应该显示回显

ps: 我已经需要 init.php 文件,其中包含我的 classes/User.php 文件,这样我就不需要调用多个文件.

代码

<?php
}

if($user->hasPermission('admin')) {
echo '<p>You are a admin!</p>';

} else {
echo 'You need to <a href="login.php">log in</a> or <a href="register.php">register</a>!';
}

?>

当管理员登录时应该显示一个回显,不幸的是我只是得到一个空白页面。

问题

所以我的问题是,有没有人知道为什么这不起作用,因为我有管理员权限集用户登录,但在 admin.php 上什么也没有?

组表 enter image description here

用户表 enter image description here

好的,出于某种原因,您需要登录或注册! echo 显示它无法识别管理员何时登录。

我想做的就是允许不同的组访问不同的页面

最佳答案

好吧,因为我之前的回答被删除了..这是另一个问题。

您的问题是您在此处进行的类型安全比较:

if($permissions[$key] === true) {
return true;
}

您的数组由一个 json 对象 {"moderator": 1} 填充,在 php 中转换为 array('moderator' => 1)。您正在使用类型安全比较将 bool 值 true 与整数 1 进行比较。这将失败,因为类型不匹配。参见 http://php.net/manual/en/language.operators.comparison.php了解更多详情。

您可以通过使用类型不安全的比较或将您的 $permissions 转换为 bool 值来解决这个问题。

if ((bool)$permissions[$key] === true) // Both are now of type boolean and will be compared.

if ($permissions[$key] == true) // Will compare 1 and TRUE, which will result in TRUE.

关于php - 权限组无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21163698/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com