简体   繁体   English

具有多个用户角色的数据库设计

[英]Design of the database with multiple user roles

Many of us have probably encountered this situation. 我们许多人可能都遇到过这种情况。 I want to build an app which supports multiple user roles and don't want to use gems for that process. 我想构建一个支持多个用户角色的应用程序,并且不想在该过程中使用gems。

I have 4 types of users: 我有4种类型的用户:

Admin
Teacher
Student
Librarian

Admin is simple and he would need only two columns, username and password . Admin很简单,他只需要两列, usernamepassword User table at the beginning contains only these values: 开头的用户表仅包含以下值:

username, password, admin, teacher, student, librarian

Columns admin, teacher, student, librarian are booleans which define does user have certain role. admin, teacher, student, librarian是布尔值,它们定义用户是否具有特定角色。 Admin would have admin to true and all other columns to false. Admin将admintrue ,所有其他列设置为false。

The problem is appearing when we introduce other columns which are specific to only one role. 当我们引入其他仅针对一个角色的列时,就会出现问题。 The we have these columns for user table: 我们为用户表提供了以下列:

id
username
password
admin
teacher
student
librarian
teacher_column1
teacher_column2
teacher_column3
student_column1
student_column2
student_column3
etc...

When user is created we need to fill a lot of columns with null values. 创建用户时,我们需要用空值填充很多列。 If we create a teacher, all columns which are needed only for librarian and student are filled with nulls. 如果我们创建教师,则只有图书管理员和学生所需的所有列都将填充为空。

On a couple of places when people asked about this, they were recommended to do STI so they would end with something like this: 在人们问这个问题的几个地方,建议他们做性传播感染,这样他们就可以像这样结束:

class User < ActiveRecord::Base

end

class Admin < User

end

class Teacher < User

end

class Student < User

end

This looks much better but would still introduce a lot of null values. 这看起来好多了,但仍会引入很多空值。

Other idea is to introduce 4 new tables for different user type details. 另一个想法是针对不同的用户类型详细信息引入4个新表。 Something like this: 像这样:

User table: 用户表:

id
username
password
admin
teacher
student
librarian

Teacher table: 老师桌:

user_id
teacher_column1
teacher_column2
teacher_column3

Student table: 学生桌:

user_id
username
password
student_column1
student_column2
student_column3

The same for Librarian and Admin. 图书馆员和管理员也一样。 In the process of user creation after we check role, new row in specific table for that user would be created. 在我们检查角色后的用户创建过程中,将在该用户的特定表中创建新行。

What do you think about this last approach? 您如何看待最后一种方法? Cons and Pros? 缺点和优点?

Would this be a correct way in creating a system with multiple user roles? 这是创建具有多个用户角色的系统的正确方法吗? Thank you. 谢谢。

Don't use different columns for each permission. 请勿为每个权限使用不同的列。

Create a single table for Users with these columns: 使用以下列为用户创建一个表:

username
password
user_type

user_type will be a value in %w{admin teacher student librarian} Then use single-table-inheritence like you mentioned in your post. user_type将是%w{admin teacher student librarian}一个值,然后像您在帖子中提到的那样使用单表继承。

Create a table for Role: 创建角色表:

name

Then create a table to tie roles to users: 然后创建一个表以将角色绑定到用户:

role_id
user_id

Create a table for Permission: 为权限创建表:

name

Then create a table to tie permissions to roles: 然后创建一个表以将权限绑定到角色:

permission_id
role_id

In the end you will have: 最后,您将获得:

  • 3 models with tables: User , Role , and Permission 3个带表的模型: UserRolePermission

  • 4 models without tables, inherting from User : Admin , Teacher , Student , and Librarian 4种不带表的模型,由User继承: AdminTeacherStudentLibrarian

  • 2 tables without models: users_roles and roles_permissions 2个没有模型的表: users_rolesroles_permissions

Also, if there are many columns that are unique to every user-type, then I would recommend using different model+tables for each and making the user in Role polymorphic. 另外,如果每种用户类型都有很多唯一的列,那么我建议为每个用户使用不同的模型+表,并使user处于“ Role多态”状态。

That way you can easily create new roles/permissions, modify the permissions of a particular role, and easily add/remove roles for a particular user. 这样,您可以轻松创建新角色/权限,修改特定角色的权限以及轻松添加/删除特定用户的角色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM