-
Notifications
You must be signed in to change notification settings - Fork 0
/
access_control.rs
352 lines (300 loc) · 7.81 KB
/
access_control.rs
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#![allow(unused)]
mod data
{
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct UserId(pub String);
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct PostId(pub String);
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct GroupId(pub String);
#[derive(Debug)]
pub struct User
{
pub user_id: UserId,
pub username: String,
pub display_name: String,
}
#[derive(Debug)]
pub struct Group
{
pub group_id: GroupId,
pub group_name: String,
pub description: String,
}
#[derive(Debug)]
pub struct Post
{
pub post_id: PostId,
pub author_id: UserId,
pub group_id: Option<GroupId>,
pub privacy: PostPrivacy,
pub title: String,
pub content: String,
}
#[derive(Debug)]
pub enum PostPrivacy
{
Public,
Private,
GroupRead,
GroupEdit,
}
}
mod raw_query
{
use super::data::*;
pub struct DbError;
pub fn get_user_info(user_id: &UserId) -> Result<User, DbError>
{
todo!()
}
pub fn get_user_groups(user_id: &UserId) -> Result<Vec<Group>, DbError>
{
todo!()
}
pub fn user_is_admin(user_id: &UserId) -> Result<bool, DbError>
{
todo!()
}
pub fn get_post_info(post_id: &PostId) -> Result<Post, DbError>
{
todo!()
}
}
mod named_query
{
use mononym::*;
use super::{
data::*,
raw_query::{
self,
DbError,
},
};
exists! {
ExistUser(user: User) => UserHasId(user_id: UserId);
ExistGroups(groups: Vec<Group>) => UserInGroups(user_id: UserId);
ExistPost(post: Post) => PostHasId(post_id: PostId);
}
proof! {
UserIsAdmin(user_id: UserId);
}
pub fn get_user_info<UserIdVal: HasType<UserId>>(
seed: impl Seed,
user_id: Named<UserIdVal, UserId>,
) -> Result<ExistUser<impl HasType<User>, UserIdVal>, DbError>
{
let user = raw_query::get_user_info(user_id.value())?;
Ok(new_exist_user(seed, user))
}
pub fn get_user_groups<UserIdVal: HasType<UserId>>(
seed: impl Seed,
user_id: &Named<UserIdVal, UserId>,
) -> Result<ExistGroups<impl HasType<Vec<Group>>, UserIdVal>, DbError>
{
let groups = raw_query::get_user_groups(user_id.value())?;
Ok(new_exist_groups(seed, groups))
}
pub fn user_is_admin<UserIdVal: HasType<UserId>>(
user_id: Named<UserIdVal, UserId>
) -> Result<Option<UserIsAdmin<UserIdVal>>, DbError>
{
let is_admin = raw_query::user_is_admin(user_id.value())?;
if is_admin {
Ok(Some(UserIsAdmin::new()))
} else {
Ok(None)
}
}
pub fn get_post_info<PostIdVal: HasType<PostId>>(
seed: impl Seed,
post_id: &Named<PostIdVal, PostId>,
) -> Result<ExistPost<impl HasType<Post>, PostIdVal>, DbError>
{
let post = raw_query::get_post_info(post_id.value())?;
Ok(new_exist_post(seed, post))
}
}
mod privacy
{
use mononym::*;
use super::{
data::*,
named_query::*,
};
pub struct Public;
pub struct Private;
pub struct GroupRead;
pub struct GroupEdit;
proof! {
PostHasPrivacy<Privacy>(post_id: PostId);
}
pub enum SomePostPrivacy<PostIdVal: HasType<PostId>>
{
Public(PostHasPrivacy<Public, PostIdVal>),
Private(PostHasPrivacy<Private, PostIdVal>),
GroupRead(PostHasPrivacy<GroupRead, PostIdVal>),
GroupEdit(PostHasPrivacy<GroupEdit, PostIdVal>),
}
pub fn check_post_privacy<
UserIdVal: HasType<UserId>,
PostIdVal: HasType<PostId>,
PostVal: HasType<Post>,
>(
post: &Named<PostVal, Post>,
_post_has_id: &PostHasId<PostVal, PostIdVal>,
) -> SomePostPrivacy<PostIdVal>
{
match post.value().privacy {
PostPrivacy::Public => SomePostPrivacy::Public(PostHasPrivacy::new()),
PostPrivacy::Private => SomePostPrivacy::Private(PostHasPrivacy::new()),
PostPrivacy::GroupRead => {
SomePostPrivacy::GroupRead(PostHasPrivacy::new())
}
PostPrivacy::GroupEdit => {
SomePostPrivacy::GroupEdit(PostHasPrivacy::new())
}
}
}
}
mod access_control
{
use mononym::*;
use super::{
data::*,
named_query::*,
privacy::*,
};
proof! {
UserCanReadPost(post_id: PostId, user_id: UserId);
UserCanEditPost(post_id: PostId, user_id: UserId);
UserIsAuthor(post_id: PostId, user_id: UserId);
UserInGroup(group_id: GroupId, user_id: UserId);
}
exists! {
ExistPostGroup(group_id: GroupId) => PostInGroup(post_id: PostId);
}
pub fn check_user_is_author<
UserIdVal: HasType<UserId>,
PostIdVal: HasType<PostId>,
PostVal: HasType<Post>,
>(
user_id: &Named<UserIdVal, UserId>,
post: &Named<PostVal, Post>,
_post_has_id: &PostHasId<PostVal, PostIdVal>,
) -> Option<UserIsAuthor<PostIdVal, UserIdVal>>
{
if &post.value().author_id == user_id.value() {
Some(UserIsAuthor::new())
} else {
None
}
}
pub fn check_user_in_group<
UserIdVal: HasType<UserId>,
GroupIdVal: HasType<GroupId>,
GroupsVal: HasType<Vec<Group>>,
>(
user_id: &Named<UserIdVal, UserId>,
group_id: &Named<GroupIdVal, GroupId>,
groups: &Named<GroupsVal, Vec<Group>>,
_user_in_groups: &UserInGroups<GroupsVal, UserIdVal>,
) -> Option<UserInGroup<GroupIdVal, UserIdVal>>
{
for group in groups.value().iter() {
if &group.group_id == group_id.value() {
return Some(UserInGroup::new());
}
}
None
}
pub fn get_post_group<PostIdVal: HasType<PostId>, PostVal: HasType<Post>>(
seed: impl Seed,
post: &Named<PostVal, Post>,
_post_has_id: &PostHasId<PostVal, PostIdVal>,
) -> Option<ExistPostGroup<impl HasType<GroupId>, PostIdVal>>
{
post
.value()
.group_id
.as_ref()
.map(move |group_id| new_exist_post_group(seed, group_id.clone()))
}
pub fn check_post_in_group<
GroupIdVal: HasType<GroupId>,
PostIdVal: HasType<PostId>,
PostVal: HasType<Post>,
>(
group_id: &Named<GroupIdVal, GroupId>,
post: &Named<PostVal, Post>,
_post_has_id: &PostHasId<PostVal, PostIdVal>,
) -> Option<PostInGroup<GroupIdVal, PostIdVal>>
{
if post.value().group_id.as_ref() == Some(group_id.value()) {
Some(PostInGroup::new())
} else {
None
}
}
pub fn author_can_edit_post<
UserIdVal: HasType<UserId>,
PostIdVal: HasType<PostId>,
>(
_user_is_author: &UserIsAuthor<PostIdVal, UserIdVal>
) -> UserCanEditPost<PostIdVal, UserIdVal>
{
UserCanEditPost::new()
}
pub fn can_edit_also_can_read<
UserIdVal: HasType<UserId>,
PostIdVal: HasType<PostId>,
>(
_can_edit: &UserCanEditPost<PostIdVal, UserIdVal>
) -> UserCanReadPost<PostIdVal, UserIdVal>
{
UserCanReadPost::new()
}
pub fn anyone_can_read_public_post<
UserIdVal: HasType<UserId>,
PostIdVal: HasType<PostId>,
>(
_post_is_public: &PostHasPrivacy<Public, PostIdVal>
) -> UserCanReadPost<PostIdVal, UserIdVal>
{
UserCanReadPost::new()
}
pub fn admin_can_edit_any_post<
UserIdVal: HasType<UserId>,
PostIdVal: HasType<PostId>,
>(
_user_is_admin: &UserIsAdmin<UserIdVal>
) -> UserCanEditPost<PostIdVal, UserIdVal>
{
UserCanEditPost::new()
}
pub fn group_member_can_read_post_with_group_read_privacy<
UserIdVal: HasType<UserId>,
PostIdVal: HasType<PostId>,
GroupIdVal: HasType<GroupId>,
>(
_user_in_group: &UserInGroup<GroupIdVal, UserIdVal>,
_post_in_group: &PostInGroup<GroupIdVal, PostIdVal>,
_post_has_group_read_privacy: &PostHasPrivacy<GroupRead, PostIdVal>,
) -> UserCanReadPost<PostIdVal, UserIdVal>
{
UserCanReadPost::new()
}
pub fn group_member_can_edit_post_with_group_edit_privacy<
UserIdVal: HasType<UserId>,
PostIdVal: HasType<PostId>,
GroupIdVal: HasType<GroupId>,
>(
_user_in_group: &UserInGroup<GroupIdVal, UserIdVal>,
_post_in_group: &PostInGroup<GroupIdVal, PostIdVal>,
_post_has_group_read_privacy: &PostHasPrivacy<GroupEdit, PostIdVal>,
) -> UserCanEditPost<PostIdVal, UserIdVal>
{
UserCanEditPost::new()
}
}
fn main() {}