如何检索用户权限?(CRUD 系统)

huangapple go评论95阅读模式
英文:

How to retrieve user permissions? (crud system)

问题

我找不到任何好的方法来获取我的用户权限。

我使用了多个SQL查询来根据他们的角色获取用户数据,但我不喜欢每次用户点击某物时都要询问权限的想法...但我也不喜欢从SQL获取所有权限并将其全部设置在 "session" 中的想法。
至少有12列(权限)。我使用4个表,users、users_role、roles和最后的permissions。

//... 
$permissions = sqlSelect($conn, '
SELECT * FROM permissions
INNER JOIN roles
ON permissions.role_id = roles.id
INNER JOIN users_role
ON permissions.role_id = users_role.role_id
WHERE users_role.user_id = ?', 'i', $row['id']);

$user_permissions = array();

while ($permission_row = $permissions->fetch_assoc()) {
  unset($permission_row['id_role']);
  $user_permissions[] = $permission_row;
}

$_SESSION['logged'] = true;
$_SESSION['user_permissions'] = $user_permissions;

//...
英文:

I can't find any good method to get my user's permissions

I used multiple SQL queries to get the user data based on their role but I don't like the idea of asking the permission every time the user clicks on something... but I also dislike the idea of getting all the permissions from SQL and set it all in the "session"
There are at least 12 columns (permissions). I use 4 tables, users, users_role, roles and finally permissions

//... 
$permissions = sqlSelect($conn, '
SELECT * FROM permissions
INNER JOIN roles
ON permissions.role_id = roles.id
INNER JOIN users_role
ON permissions.role_id = users_role.role_id
WHERE users_role.user_id = ?', 'i', $row['id']);

$user_permissions = array();

while ($permission_row = $permissions->fetch_assoc()) {
  unset($permission_row['id_role']);
  $user_permissions[] = $permission_row;
}

$_SESSION['logged'] = true;
$_SESSION['user_permissions'] = $user_permissions;

//...

答案1

得分: 0

你需要使用会话。
首先从MySQL中检索权限并将它们存储在会话中。
然后使用会话来检查权限。
最后,当用户退出登录时,清除该会话。

英文:

you need to use session.
first retrieve permissions from mysql and store them in a session.
then use session to check permissions.
and finally when a user logged out. clear this session.

huangapple
  • 本文由 发表于 2023年7月23日 13:41:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76746774.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定