简体   繁体   English

如何在mysql中为用户角色分配和区域分配设计数据库结构?

[英]How to design database structure in mysql for user role assignment and area assignment?

Recently my company assign me a project that will mange my company marketing team. 最近,我的公司为我分配了一个项目,该项目将管理我的公司营销团队。 I am in trouble or fail to decide about database structure. 我遇到麻烦或无法决定数据库结构。

In this project have Access Role that will be following: 在此项目中,将具有以下访问角色:

1) CMO (Chief Marketing officer) 1)首席营销官(首席营销官)

2) ------------General Manager 2)------------总经理

3) ------------------Regional Manager 3)------------------区域经理

4) ------------------------Zonal Manager/State Head 4)------------------------区域经理/国家元首

5) ------------------------------Assistance Manager 5)------------------------------协助经理

6) ------------------------------------Area Manager 6)------------------------------------区域经理

7) ------------------------------------------User 7)------------------------------------------用户

Access level will be according to mention above role hierarchy. 访问级别将根据上面提到的角色层次结构进行。
For example CMO have full access, Regional Manager have all access except CMO and so on. 例如,CMO具有完全访问权限,区域经理具有除CMO之外的所有访问权限,依此类推。

Now second main things is Region (Region will make after combining multiple zone/state) Zone/State (It will be make after combining multiple city ) Area/City 现在第二件事是地区(区域将在合并多个区域/州之后创建)区域/州(将在合并多个城市之后将创建)区域/城市

Now all user will be added by default in any area Then admin assign a role and a region or state or area Finally user can access in system according to their role and their assign zone/state/city/area 现在,默认情况下将所有用户添加到任何区域中。然后,管理员分配角色以及区域或州或区域。最后,用户可以根据其角色及其分配的区域/州/城市/区域来访问系统

if any user access role is Regional Manager and assign a region like Region1 Now user will be able to access all state/zone and their city which comes in region1 如果有任何用户访问角色是“区域经理”并分配了“ Region1”之类的区域,则该用户现在将能够访问“ region1”中的所有州/地区及其城市

Please help me to design database structure that manage these all things? 请帮助我设计管理所有这些东西的数据库结构吗?

Create a table in your database titled 'users'. 在数据库中创建一个名为“用户”的表。 Here you could create multiple tables or one. 在这里您可以创建多个表或一个表。 Basically, you want to create a role field and region field within your table/tables. 基本上,您想在表中创建角色字段和区域字段。 When a user signs in, they will be granted access based on their role (ie 1 is a gm, 7 is a user)/region. 当用户登录时,将根据其角色(即1是gm,7是用户)/区域为他们授予访问权限。

There are a lot of prebuilt solutions in many languages for what you want to do. 有许多您想要做的以多种语言编写的预构建解决方案。 I would recommend searching for one. 我建议搜索一个。

To represent the hierarchy you could create your roles table with a parent_role_id column meaning that other roles can 'sit under' the parent. 为了表示层次结构,您可以使用parent_role_id列创建角色表,这意味着其他角色可以“坐在”父项下。

For example: 例如:

CREATE TABLE `roles` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `role` varchar(45) NOT NULL,
  `parent_role_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `parent_role_id` (`parent_role_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

If I wanted to add a CMO they would keep this field null as nobody is above them. 如果我要添加CMO,则由于没有人在其上方,因此该字段将保持为空。 An Area Manager however would have parent id of 5 (assuming that the CMO is 1) as they are under the Assistance Manager. 但是,区域经理在“助理经理”下的父ID为5 (假设CMO为1)。 This enables you to build a folder like structure using just one table. 这使您可以仅使用一个表来构建类似于结构的文件夹。

The other users to location relationships could be done in many ways. 其他用户与位置的关系可以通过许多方式来完成。 I think the easiest would be via a number of many to many relationships, such as: 我认为最简单的方法是通过许多对许多关系,例如:

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `username` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `user_roles`(
  `user_id` int(11) NOT NULL,
  `role_id` int(11) NOT NULL,
  PRIMARY KEY (`user_id`,`role_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `user_region` (
  `user_id` int(11) NOT NULL,
  `region_id` int(11) NOT NULL,
  PRIMARY KEY (`user_id`,`region_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

If you are looking for these to be used for governing authorization (aka permissions) then a more test and flexible solution may be an Access Control List (ACL) depending on the language you are using. 如果您正在寻找这些用于管理授权(也称为权限)的东西,那么根据您所使用的语言,访问控制列表(ACL)可能是一个更加测试和灵活的解决方案。

http://en.wikipedia.org/wiki/Access_control_list http://en.wikipedia.org/wiki/Access_control_list

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

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