如何在sql中确定用户是否可以在la unix权限系统中读取/写入/执行文件



我正在尝试编写一个有文件和用户的应用程序。文件的权限应该与unix中的一样:读/写/执行。每个文件都属于一个所有者和一个组,每个文件都有"其他"用户(基于谁是所有者,谁属于一个组(。模式如下:

create table s_file(
file_id INTEGER PRIMARY KEY,
parent_id INTEGER default NULL,
name,
content BLOB DEFAULT NULL,
owner_id INTEGER NOT NULL,
group_id INTEGER NOT NULL,
read_owner BOOLEAN not null default 1,
write_owner BOOLEAN not null default 1,
execute_owner BOOLEAN not null default 1,
read_group BOOLEAN not null default 1,
write_group BOOLEAN not null default 0,
execute_group BOOLEAN not null default 1,
read_other BOOLEAN not null default 1,
write_other BOOLEAN not null default 0,
execute_other BOOLEAN not null default 1
);
create table s_user( uid INTEGER PRIMARY KEY, 
name NOT NULL, password NOT NULL);
create table s_group( gid INTEGER PRIMARY KEY, name NOT NULL);
create table s_user_in_group ( uid INTEGER NOT NULL, 
gid INTEGER NOT NULL);

insert into s_user(uid,name,password) values (1,'root','pw');
insert into s_user(uid,name,password) values (2,'gauss', 'pw');
insert into s_user(uid,name,password) values (3,'conway', 'pw');
insert into s_group(gid,name) values (1,'root');
insert into s_group(gid,name) values (2,'producer');
insert into s_group(gid,name) values (3,'gauss');
insert into s_group(gid,name) values (4,'conway');
insert into s_user_in_group(uid,gid) values (2,2),(2,3),(3,4);
insert into s_file(file_id,name,owner_id,group_id) values (1,'galois',1,1);
insert into s_file(file_id,parent_id,name,owner_id,group_id) values (2,1,'home',1,1);
insert into s_file(file_id,parent_id,name,owner_id,group_id,write_group) values (3,1,'models',1,2,1);
insert into s_file(file_id,parent_id,name,owner_id,group_id) values (4,2,'gauss',2,3);
insert into s_file(file_id,parent_id,name,owner_id,group_id) values (5,2,'conway',3,4);
insert into s_file(file_id,parent_id,name,owner_id,group_id,content) values (6,4,'boston.pfa',2,3,'cB');
insert into s_file(file_id,parent_id,name,owner_id,group_id,content) values (7,5,'iris.pfa',3,4,'cI');
insert into s_file(file_id,parent_id,name,owner_id,group_id,write_group,content) values (8,3,'boston.pfa',2,2,1,'CB');
insert into s_file(file_id,parent_id,name,owner_id,group_id,write_group,content) values (9,3,'iris.pfa',2,2,1,'CI');

以下是一些示例数据:文件树:

galois
|-- home
|   |-- gauss
|   |   +-- boston.pfa
|   +-- conway
|       +-- iris.pfa
+-- models
|-- boston.pfa
+-- iris.pfa

我想写一个查询,如果可能的话,在sqlite中,否则用python,看看例如,用户conway是否可以写/galois/models/boston.pfa根据我的示例数据,你可以在下面找到,这应该是不可能的,因为康威不是所有者,也不是群制作人,其他人也不允许写作。因此,如果在sql中可能的话,我试图做的是下表/条目:

file_id, user_id, can_write/can_execute/can_read

请在下面找到一些示例数据:

sqlite> select file_id, parent_id, name, owner_id, group_id, write_owner,   write_group,write_other from s_file;
1||galois|1|1|1|0|0
2|1|home|1|1|1|0|0
3|1|models|1|2|1|1|0
4|2|gauss|2|3|1|0|0
5|2|conway|3|4|1|0|0
6|4|boston.pfa|2|3|1|0|0
7|5|iris.pfa|3|4|1|0|0
8|3|boston.pfa|2|2|1|1|0
9|3|iris.pfa|2|2|1|1|0

sqlite> select uid,name from s_user;
1|root
2|gauss
3|conway

sqlite> select gid,name from s_group;
1|root
2|producer
3|gauss
4|conway

sqlite> select uid,gid from s_user_in_group;
2|2
2|3
3|4

有什么建议吗?

谢谢你的帮助!

编辑:根据@Mike的建议,我将按如下方式进行:1.创建视图

file_id, user_id, can_read, can_write, can_execute

具有"本地"权限

  1. 使用CTE来决定用户是否可以访问(la unix(特定文件:为此,用户必须有权执行文件上方的每个目录,直到根目录

我的问题是决定第一步是否可以只在sql中完成(那太好了(,或者我是否必须将其与python混合使用[在这种情况下,我还将实现步骤2。在python中]

或者,您对如何构建(模式(数据以实现此目标有了更好的想法?

再次感谢您的帮助!

我找到了一个解决方案:

create view if not exists user_rights as
select u.uid,f.file_id, 
case 
when f.owner_id = u.uid then f.read_owner
when u.uid = uig.uid then max(f.read_group) else max(f.read_other) end as     can_read,
case 
when f.owner_id = u.uid then f.write_owner
when u.uid = uig.uid then max(f.write_group) else max(f.write_other) end as can_write,
case 
when f.owner_id = u.uid then f.execute_owner
when u.uid = uig.uid then max(f.execute_group) else max(f.execute_other) end as can_execute
from s_user u,s_file f left join s_user_in_group uig on f.group_id = uig.gid group by u.uid,file_id;

完整的解决方案可以在这里找到:

https://github.com/orgesleka/unix-acl-sql

最新更新