如何管理用户访问和用户权限
我正在使用 PHP + MySql 开发一个应用程序。在我的应用程序中,我有一个用户表、一个关系表(朋友、关注、订阅)和一个帖子表。用户的主要操作是:
- 用户有朋友
- 用户可以发布条目 用户
- 可以查看朋友条目
- 最后 用户可以阻止特定朋友查看条目
如果用户 A 是用户 B 的朋友,那么用户 A 可以看到用户 B 的所有条目。但是,例如,用户 B 可以将访问权限限制为仅少数朋友。现在的问题是:如何管理这些权限?我正在考虑一个表来存储每个被阻止查看特定条目的用户,但是一旦单个用户可以拥有多个朋友,这将不是一个好主意。那么,我该如何解决这个问题呢?有人可以告诉我如何开始吗?也许是在 Google 上搜索的正确术语或类似内容的链接。
I am working on an application with PHP + MySql. In my application I have a table for users, a table for relationships (friends, following, subscribed) and a table for posts. The main actions for users are:
- A user has friends
- A user can make post entries
- A user can see the friends entries
- And finally a user can block entries viewing for specific friends
If user A is friends with user B, then user A can see all entries from user B. But user B can restrict access to only a few friends for example. Now the query is: how can I manage these permissions? I was thinking of a table that stores each user that is blocked for viewing an specific entry, but this would't be a good idea once a single user can have several friends. So, how can I solve this? Can someone show me how to start? Maybe the right terms for searching on Google or a link for something similar.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你走在正确的轨道上。您将需要使用链接表。您将从表 users 开始。每个用户都有一个 id。然后创建一个表users_friends。该表由两个 ID 组成:user_id 和friend_id。最后一个表是users_restricted,它也由两个id组成,user_id和restricted_id。
例如,
这表示用户 1 和 2 是朋友,用户 2 和 3 是朋友。 (假设如果用户 1 是用户 2 的朋友,那么用户 2 也是用户 1 的朋友)
现在,即使用户 1 和用户 2 是朋友,用户 2 也在受限列表中,这意味着不允许用户 2 查看用户1 的条目。
您可以看到表通过 ids 链接,所有 id 都来自 users 表。这也可以扩展到与条目相关。只需通过 ID 引用条目即可。
要阻止用户访问特定条目,您将拥有下表。
现在,用户 1 已创建 2 个条目(条目 1 和条目 2),用户 2 已创建 2 个条目(条目 3 和条目 4)。另一个表将保存限制。
这表示用户 2 无法查看条目 1。
要使条目对用户 2 可见,您的语句将如下所示。
该语句选择除仅限用户 2 的条目之外的所有条目信息。
You are on the right track. You are going to want to use linked tables. You would start with a table users. Each user has an id. Then create a table users_friends. This table would consist of two ids, user_id and friend_id. The last table would be users_restricted which would also consist of two ids, user_id and restricted_id.
For example
This says user 1 and 2 are friends and users 2 and 3 are friends. (This assumes that if user 1 is friends with user 2 then user 2 is also friends with user 1)
Now even though user 1 and user 2 are friends, user 2 is in the restricted list meaning don't allow user 2 to view user 1's entries.
You can see that tables are linked via ids and all the ids come from the users table. This can be expanded to relate to entries as well. Just refer to entries by their id.
To have users blocked for specific entries you would have the following tables.
Now user 1 has made 2 entries (entry 1 and entry 2) and user 2 has made 2 entries (entry 3 and entry 4). Another table would hold the restrictions.
This would say user 2 cannot view entry 1.
To get the entries visible to user 2 your statement would look something like this.
The statement selects all the entry information excluding entries restricted to user 2.
您可以开始使用下表。
第一个表是用户表(正如 Jason.Wolfsmith 建议的那样)
第二个表可以是这样的。
该表将包含允许查看的条目的权限和名称。 1 - 限制某些条目; 0 - 允许全部。
在权限列中,数据类型可以设置为SET('1','0'),条目中的数据类型可以设置为NULL。
因此,用户 1 不允许用户 2 查看条目 1。 (entry1 和entry3 来自条目表)。
You can start using following tables.
The first table is users table (as Jason.Wolfsmith suggested)
The second table can be like this.
This table will contain permission and name of entries that should be allow for view. 1 - restrict some entries; 0 - allow all.
In the column permission data type might be set as SET('1','0') and data type in entries NULL.
Thus, user1 don't allow to view entry1 to user2. (entry1 and entry3 are from entries table).