首先,这听起来像是一个巨大的项目,我相信有一些框架可以为您做到这一点。但是,如果您尝试自己执行此操作,请继续阅读。
这可以通过多种方式完成。我会尽量详细。这需要 SQL 以及应用程序开发/软件工程知识。
第 1 步:设置数据库
您将需要以下表格:所有 id 都是自动递增的主键,其他字段可以是 varchar,但名称中包含日期的字段除外
- 会话 [id, uid, random_token, datecreated]
- resourcescope [消除,名称]
- 用户 [uid, first, last, email, username, salted_pwd]
- user_type [id、名称、描述]
- user_resourcescope [id, uid, rid] //userid和resourcescope之间的查找表
我更喜欢使用 Java 或 python,因为您可以使用依赖注入或装饰器。因此,在检查用户是否具有访问权限时,您不必编写大量代码。
将这一切付诸实践。
1. When a user signs up, you save them into a user database. Depending on the user type, you give them different permissions. Next, you save the user permissions inside the user_resourcescope table.
您现在应该拥有以下内容。
用户表
UID | first | last | email | username | salted_pwd | usertype
1 | james | iri | example@isp.com | jiri1928 | klasdjf8$kljs | 1
用户类型表
usetype_id | Name
1 | Basic users
2 | Searcher
资源范围表
rid | Name
1 | FindContent
2 | CreateContent
3 | DeleteContent
User_Resourcescope
id | uid | rid
1 | 1 | 1
2 | 1 | 3
会话
id | uid | random_token | datecreated
1 | 1 | ldkjfald882u3u | 1391274870322
每个资源代表系统内的一个请求。例如,
http://api.myapi.com/content/add - This would be associated with the ResourceScope CreateContent
http://api.myapi.com/content/delete- This would be associated with the ResourceScope CreateDelete
http://api.myapi.com/content/search - This would be associated with the ResourceScope SearchContent
当有人尝试创建内容时,您可以通过验证他们的会话信息来检查他们的信任是否正确,并通过检查 User_Resourcescope 表来检查他们是否具有正确的权限。
To prevent users from deleting content that is not theirs. Inside the content table you can add a creator field and put the user id associated with the content. And if someone try to delete content you can check their user id against the creator field.