用户角色权限mysql设计,最佳用户角色权限数据库设计实践?

博客讨论了如何为Web应用设计数据库,特别是涉及用户角色和权限的部分。提出了两种方案:一是创建USER_PERMISSIONS表来存储权限类型;二是通过代码级别管理权限。作者提到,选择取决于需求的灵活性,他们自己实现了一个包含用户、角色和权限的多对多关系模型,以适应不断增长的需求。但不建议在应用中硬编码权限或角色到权限的映射。
摘要由CSDN通过智能技术生成

I want to design database for a web app in which user can access particular tabs based on the permissions given to a role.

What I have done so far is I created two tables USER_TABLE and USER_ROLES.

USER_TABLE has below fields:

id (primary key)

user_name

password

first_name

last_name

created_date

role_id_fk (foreign key)

USER_ROLES has below fields:

id (primary key)

role_name (e.g. ADMIN, TAB1_USER, TAB2_USER)

created_date

Here, the user having role_name "ADMIN" can see all the tabs, other users can access specific tabs only.

My question is do I need to create a table USER_PERMISSIONS having foreign key in USER_ROLES table with below fields:

id (primary key)

permission_type (ALL, TAB1, TAB2....)

or should I manage this at my code level? What would be the cons and pros of both approaches?

解决方案

As krokodilko wrote in his comment, it depends on the level of flexibility you need.

I have implemented role based permissions for one of my clients as follows:

User (user id (PK), user name (unique), password (salted and hashed!), first name, last name, phone etc')

Role (role id (PK), role name (unique), role description)

Permission (permission id (PK), permission name (unique)) - the tabs / screens / actions goes here

User To Role (user id, role id) - PK is both columns combined

Role to Permission (role id, permission id) - PK is both columns combined

But my requirement was to be as flexible as possible, and it is a system that is still growing (6 years and counting).

I guess a lot of applications can have the user to role as a one to many relationship, instead of a many to many like in my case, but I wouldn't go hard coding permissions or role to permissions in any application.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值