-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcentralized.pro
140 lines (102 loc) · 3.37 KB
/
centralized.pro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
% --- state ---
:- dynamic userT/1. % user name
:- dynamic roleT/1. % role name
:- dynamic userRoleT/2. % user, role
:- dynamic fileT/1. % file name
:- dynamic permT/3. % role, operation, file
% import
:- ensure_loaded("extended.pro").
% --- utils ---
% rbacPrint(_, _) :- true.
rbacPrint(TEXT, CONTENT_OLD) :-
convertArray(CONTENT_OLD, CONTENT),
atom_concat("[TRADIT] ", TEXT, O1),
atom_concat(O1, "~n", O2),
ansi_format([fg(cyan)], O2, CONTENT).
% --- state-change rules ---
addUserT(USER) :- userT(USER).
addUserT(USER) :-
\+ userT(USER),
assert(userT(USER)),
rbacPrint("Create user ~a", [USER]).
addRoleT(ROLE) :- roleT(ROLE).
addRoleT(ROLE) :-
\+ roleT(ROLE),
assert(roleT(ROLE)),
rbacPrint("Create role ~a", [ROLE]).
addResourceT(FILE) :- fileT(FILE).
addResourceT(FILE) :-
\+ fileT(FILE),
assert(fileT(FILE)),
rbacPrint("Create file ~a", [FILE]).
assignUserToRoleT(USER, ROLE) :- userRoleT(USER, ROLE).
assignUserToRoleT(USER, ROLE) :-
userT(USER), roleT(ROLE), % checks
\+ userRoleT(USER, ROLE), % already member
assert(userRoleT(USER, ROLE)),
rbacPrint("Assign ~a to ~a", [USER, ROLE]).
assignPermissionToRoleT(_, [], _).
assignPermissionToRoleT(ROLE, [OP|OPs], FILE) :-
roleT(ROLE), fileT(FILE), operation(OP),
assert(permT(ROLE, OP, FILE)),
rbacPrint("Assign permission ~a for file ~a to role ~a", [OP, FILE, ROLE]),
assignPermissionToRoleT(ROLE, OPs, FILE).
revokePermissionFromRoleT(_, [], _).
revokePermissionFromRoleT(ROLE, [OP|OPs], FILE) :-
roleT(ROLE), fileT(FILE), operation(OP),
retractall(permT(ROLE, OP, FILE)),
rbacPrint("Assign permission ~a for file ~a to role ~a", [OP, FILE, ROLE]),
revokePermissionFromRoleT(ROLE, OPs, FILE).
revokeUserFromRoleT(USER, ROLE) :- \+ userRoleT(USER, ROLE).
revokeUserFromRoleT(USER, ROLE) :-
userT(USER), roleT(ROLE),
userRoleT(USER, ROLE),
retractall(userRoleT(USER, ROLE)),
rbacPrint("Revoke ~a from ~a", [USER, ROLE]).
deleteResourceT(FILE) :- \+ fileT(FILE).
deleteResourceT(FILE) :-
fileT(FILE),
% delete associated permissions
foreach(
permT(ROLE, OP, FILE),
revokePermissionFromRoleT(ROLE, [OP], FILE)
),
% delete allow
retractall(fileT(FILE)),
rbacPrint("Delete resource ~a", [FILE]).
deleteRoleT(ROLE) :- \+ roleT(ROLE).
deleteRoleT(ROLE) :-
roleT(ROLE),
% delete associated permissions
foreach(
permT(ROLE, OP, FILE),
revokePermissionFromRoleT(ROLE, [OP], FILE)
),
% delete members
foreach(
userRoleT(USER, ROLE),
revokeUserFromRoleT(USER, ROLE)
),
% delete role
retractall(roleT(ROLE)),
rbacPrint("Revoke role ~a", [ROLE]).
deleteUserT(USER) :- \+ userT(USER).
deleteUserT(USER) :-
userT(USER),
% delete memberships
foreach(
userRoleT(USER, ROLE),
revokeUserFromRoleT(USER, ROLE)
),
retractall(userT(USER)),
rbacPrint("Delete user ~a", [USER]).
canDoT(USER, OP, FILE) :-
userT(USER), operation(OP), fileT(FILE),
roleT(ROLE), userRoleT(USER, ROLE),
permT(ROLE, OP, FILE).
% --- queries ---
canDoT(USER, OP, FILE) :-
% checks
userT(USER), operation(OP), fileT(FILE),
% can do
roleT(ROLE), userRoleT(USER, ROLE), permT(ROLE, OP, FILE).