Skip to content
This repository was archived by the owner on Sep 8, 2019. It is now read-only.

Commit 182e27f

Browse files
committed
Initial framework for permissions system.
1 parent e55255c commit 182e27f

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- This file should undo anything in `up.sql`
2+
DROP TABLE user_access;
3+
DROP TABLE access;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
-- Your SQL goes here
2+
CREATE TABLE access (
3+
id SERIAL PRIMARY KEY,
4+
access_name VARCHAR(255) NOT NULL
5+
);
6+
7+
INSERT INTO access (access_name) VALUES
8+
("SearchUser"),
9+
("GetUser"),
10+
("CreateUser"),
11+
("UpdateUser"),
12+
("DeleteUser");
13+
14+
CREATE TABLE user_access (
15+
permission_id SERIAL PRIMARY KEY,
16+
access_id BIGINT UNSIGNED NOT NULL,
17+
user_id BIGINT UNSIGNED NOT NULL,
18+
FOREIGN KEY (access_id)
19+
REFERENCES access(id)
20+
ON DELETE CASCADE,
21+
FOREIGN KEY (user_id)
22+
REFERENCES users(id)
23+
ON DELETE CASCADE,
24+
permission_level VARCHAR(255)
25+
);

backend/src/access/models.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
use diesel::Queryable;
2+
use serde::Deserialize;
3+
use serde::Serialize;
4+
5+
use super::schema::{access, user_access};
6+
7+
use crate::errors::{WebdevError, WebdevErrorKind};
8+
9+
use crate::users::models::{User, UserList};
10+
11+
#[derive(Queryable, Serialize, Deserialize)]
12+
pub struct Access {
13+
pub id: u64,
14+
pub access_name: String,
15+
}
16+
17+
#[derive(Insertable, Serialize, Deserialize)]
18+
#[table_name = "access"]
19+
pub struct NewAccess {
20+
pub access_name: String,
21+
}
22+
23+
pub enum AccessRequest {
24+
CreateAccess(NewAccess), //new access type of some name to be created
25+
GetAccess(u64), //id of access name searched
26+
DeleteAccess(u64), //if of access to be deleted
27+
RenameAccess(Access), //Contains id to be changed to new access_name
28+
}
29+
30+
impl AccessRequest {
31+
fn from_rouille(request: &rouille::Request) -> Result<AccessRequest, WebdevError> {
32+
33+
}
34+
}
35+
36+
37+
38+
#[derive(Queryable, Serialize, Deserialize)]
39+
pub struct UserAccess {
40+
pub permission_id: u64,
41+
pub access_id: u64,
42+
pub user_id: u64,
43+
pub permission_level: Option<String>,
44+
}
45+
46+
#[derive(Insertable, Serialize, Deserialize)]
47+
#[table_name = "user_access"]
48+
pub struct NewUserAccess {
49+
pub access_id: u64,
50+
pub user_id: u64,
51+
pub permission_level: Option<String>,
52+
}
53+
54+
#[derive(AsChangeset, Serialize, Deserialize)]
55+
#[table_name = "user_access"]
56+
pub struct PartialUserAccess {
57+
pub access_id: u64,
58+
pub user_id: u64,
59+
pub permission_level: Option<Option<String>>,
60+
}
61+
62+
pub enum UserAccessRequest {
63+
SearchAccess(UserList), //list of users with access id or (?) name
64+
HasAccess(UserAccess), //entry allowing user of user_id to perform action of action_id
65+
CreateAccess(NewUserAccess), //entry to add to database
66+
UpdateAccess(u64, PartialUserAccess), //entry to update with new information
67+
DeleteUserAccess(u64), //entry to delete from database
68+
}
69+
70+
impl UserAccessRequest {
71+
fn from_rouille(request: &rouille::Request) -> Result<UserAccessRequest, WebdevError> {
72+
73+
}
74+
}

backend/src/access/schema.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
table! {
2+
access (id) {
3+
id -> Unsigned<Bigint>,
4+
access_name -> Varchar,
5+
}
6+
}
7+
8+
table! {
9+
user_access (permission_id) {
10+
permission_id -> Unsigned<Integer>,
11+
access_id -> Unsigned<Bigint>,
12+
user_id -> Unsigned<Bigint>,
13+
permission_level -> Nullable<Varchar>,
14+
}
15+
}
16+
17+
use crate::users::schema::users;
18+
19+
joinable!(user_access -> access (access_id));
20+
joinable!(user_access -> users (user_id));
21+
22+
allow_tables_to_appear_in_same_query!(
23+
access,
24+
user_access,
25+
users,
26+
);

0 commit comments

Comments
 (0)