【发布时间】:2020-06-18 01:51:24
【问题描述】:
我是新的 sql 东西,所以请原谅我提出愚蠢的问题。我正在为大学项目创建我的第一个现实生活应用程序。
在其核心,它需要处理数以千计的用户,除非被授予权限,否则这些用户应该无法读取或写入彼此的数据。就像 Linux 对用户和组所做的那样。
在我尝试过的以下架构中,如果用户具有读取权限,则用户可以查看(读取)和编辑(写入)其他用户。(r=2 w=1 r+w=3)。
例如,如果cgroup_1 是 admin,cgroup_2 是 manager,unixperm 是 32,那么这意味着 admin 组中的用户可以读+写(3),managers 组中的用户只能读(2)
create table cgroups
(
id int unsigned primary key auto_increment,
title varchar(100) not null unique,
cunixperm tinyint unsigned not null default 32 ,# r=2 w=1
cgroup_1 int unsigned not null default 1 references cgroups (id) on delete cascade on update cascade,
cgroup_2 int unsigned references cgroups (id) on delete cascade on update cascade
);
create table users
(
id int unsigned auto_increment primary key,
username varchar(255) not null unique,
cunixperm tinyint unsigned not null default 30, # r=2 w=1 3=r+w
cgroup_1 int unsigned default 1 not null references cgroups (id) on delete cascade on update cascade ,
cgroup_2 int unsigned references cgroups (id) on delete cascade on update cascade
);
create table many_users_in_many_cgroups
(
user_id int unsigned references users(id),
cgroup_id int unsigned references cgroups(id),
primary key (user_id,cgroup_id)
);
insert into cgroups(title)
values ('admins'),('managers'),('writers');
insert into users(username, cunixperm, cgroup_1, cgroup_2)
values ('user1',30,1,null),
('user2',30,1,2),
('user3',22,2,2),
('user4',02,3,3);
insert into many_users_in_many_cgroups
values (1,1),(2,2),(3,3),(4,4);
现在假设用户 2 已经登录到我的应用程序,我怎样才能只显示他具有读取 (2) 或读取 + 写入 (3) 权限的用户行。
如果上述架构(可能)不合适,请给我一个合适方案的示例
我目前正在使用 MariaDB,但也为其他人提供解决方案。
【问题讨论】: