如何在 Web 应用程序中实现用户和组安全?
如果重要的话使用 php 。
如果您创建一个具有用户和组的网站。你把它放在网络应用程序的哪里?你是否只是在每个页面的顶部放置一个功能(伪):
如果有人在一个组中,那么他们可以看到此页面 或者 如果有人在这个组中,他们可以看到这个按钮
这肯定是错误的。我不想编辑网络应用程序代码只是为了更改谁可以按组查看内容。我不确定我应该做什么或如何实现这样的事情。
谢谢。
using php if that matters.
If you create a website that has users and groups. Where do you put this in the web application? Do you just put a function at the top of every page (pseudo):
if someone is in a group then they can see this page
or
if someone is in this group they can see this button
That sure seems wrong. I wouldn't want to edit the web app code just to change who can see what group-wise. I'm not sure what I should do or how to implement something like this.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 MySQL 中,我总是创建这 4 个表:users、user_groups、permissions 和 user_groups_permissions,它们使用外键链接。
所以,用户A可以属于用户组B,该用户组的权限在user_groups_permissions中。
现在,我只是对这 4 个表(或者更好,三个:用户、用户组权限和权限)执行 INNER JOIN,结果是用户拥有的权限。我们所需要的只是通过 INNER JOIN 选择
permissions.key
。现在,在处理请求之前,我需要检查 Client::has_permissin('send_post') 是否返回 true 。更好的是,还在每个用户组相关的功能之上。
注意:Client 是一个类,它在处理请求之前仅加载一次所有用户权限,然后在整个请求生命周期内使用该权限,而无需在该请求中多次访问数据库。对此类使用静态方法和 $permissions 属性,这样您就不需要通过应用程序类/方法/函数发送它的对象:)
In MySQL, I always create these 4 tables: users, user_groups, permissions and user_groups_permissions which are linked using Foreign Keys.
So, user A can be in a user group B, which this user group permissions are in user_groups_permissions.
Now, I just do a INNER JOIN on this 4 tables (or better, three: users, user_groups_permissions and permissions), the results are permissions that user have. all we need is selecting
permissions.key
by INNER JOIN.Now, before processing request, I need to check that Client::has_permissin('send_post') returns true or not. And better, also on top of each user-group-related function.
Note: Client is a class that loads all user permissions just one time, before processing request, and then uses that permissions for whole request-life-time, without needing to access to database several times in that request. Use static methods and $permissions property for this class so you never need to send it's object over your applications classes/methods/functions :)
您可以拥有一个实用程序函数,它接受用户 ID 和组代码并返回 true 或 false。
您可以在每个页面的顶部将该实用程序函数用作伪函数,并且相同的函数也可用于隐藏或显示页面中的部分。
如果您的 Web 应用程序采用 MVC,请将用户授权逻辑嵌入到您的控制器中。
You can have a utility function which takes user id and group code and return true or false.
You can use that utility function as pseudo at the top of each page and the same function also be used to hide or show sections in your page.
If your web application is in MVC, embed user authorization logic in your controller.