-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroles_controller.rs
More file actions
179 lines (168 loc) · 4.36 KB
/
Copy pathroles_controller.rs
File metadata and controls
179 lines (168 loc) · 4.36 KB
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
use diesel::result::DatabaseErrorKind;
use rocket::form::Form;
use rocket::http::Status;
use rocket::response::status::Custom;
use rocket::response::{Redirect, Responder, status};
use rocket::serde::json::Json;
use std::fmt::Debug;
use crate::DbConn;
use crate::ephemeral::from_api::Api;
use crate::ephemeral::session::AdminSession;
use crate::errors::{Either, InternalError, Result, ZauthError};
use crate::models::client::Client;
use crate::models::role::{NewRole, Role};
use crate::models::user::User;
use crate::views::accepter::Accepter;
#[get("/roles?<error>")]
pub async fn list_roles<'r>(
error: Option<String>,
db: DbConn,
session: AdminSession,
) -> Result<impl Responder<'r, 'static>> {
let roles = Role::all(&db).await?;
let clients = Client::all(&db).await?;
Ok(Accepter {
html: template! {
"roles/index.html";
roles: Vec<Role> = roles.clone(),
clients: Vec<Client> = clients,
error: Option<String> = error,
current_user: User = session.admin,
},
json: Json(roles),
})
}
#[post("/roles", data = "<role>")]
pub async fn create_role<'r, 'a>(
role: Api<NewRole>,
db: DbConn,
_admin: AdminSession,
) -> Result<
Either<impl Responder<'a, 'static>, impl Responder<'r, 'static> + use<'r>>,
> {
let role = Role::create(role.into_inner(), &db).await;
match role {
Ok(role) => Ok(Either::Left(Accepter {
html: Redirect::to(uri!(list_roles(None::<String>))),
json: status::Created::new(String::from("/role")).body(Json(role)),
})),
Err(ZauthError::Internal(InternalError::DatabaseError(
diesel::result::Error::DatabaseError(
DatabaseErrorKind::UniqueViolation,
_,
),
))) => Ok(Either::Right(Accepter {
html: Redirect::to(uri!(list_roles(Some(
"role name already exists"
)))),
json: "role name already exists",
})),
Err(err) => Err(err),
}
}
#[get("/roles/add?<username>")]
pub async fn add_user_page<'r>(
username: Option<String>,
db: DbConn,
session: AdminSession,
) -> Result<impl Responder<'r, 'static>> {
let roles = Role::all(&db).await?;
Ok(template! {
"roles/add_user.html";
roles: Vec<Role> = roles,
username: String = username.unwrap_or_default(),
current_user: User = session.admin
})
}
#[get("/roles/<id>?<error>&<info>")]
pub async fn show_role_page<'r>(
id: i32,
error: Option<String>,
info: Option<String>,
session: AdminSession,
db: DbConn,
) -> Result<impl Responder<'r, 'static>> {
let role = Role::find(id, &db).await?;
let users = role.clone().users(&db).await?;
Ok(template! { "roles/show_role.html";
current_user: User = session.admin,
role: Role = role,
users: Vec<User> = users,
error: Option<String> = error,
info: Option<String> = info
})
}
#[delete("/roles/<id>")]
pub async fn delete_role<'r>(
id: i32,
_session: AdminSession,
db: DbConn,
) -> Result<impl Responder<'r, 'static>> {
let role = Role::find(id, &db).await?;
role.delete(&db).await?;
Ok(Accepter {
html: Redirect::to(uri!(list_roles(None::<String>))),
json: Custom(Status::NoContent, ()),
})
}
#[derive(FromForm)]
pub struct Mapping {
username: String,
role_id: i32,
}
#[post("/roles/mapping", data = "<form>")]
pub async fn add_user<'r>(
form: Form<Mapping>,
db: DbConn,
_session: AdminSession,
) -> Result<impl Responder<'r, 'static>> {
let role = Role::find(form.role_id, &db).await?;
let user_result = User::find_by_username(form.username.clone(), &db).await;
Ok(match user_result {
Ok(user) => {
role.add_user(user.id, &db).await?;
Accepter {
html: Redirect::to(uri!(show_role_page(
role.id,
None::<String>,
Some("user added")
))),
json: Custom(Status::Ok, ()),
}
},
Err(ZauthError::NotFound(_)) => Accepter {
html: Redirect::to(uri!(show_role_page(
role.id,
Some("user not found"),
None::<String>
))),
json: Custom(Status::NotFound, ()),
},
_ => Accepter {
html: Redirect::to(uri!(show_role_page(
role.id,
Some("error occured"),
None::<String>
))),
json: Custom(Status::InternalServerError, ()),
},
})
}
#[delete("/roles/<role_id>/mapping/<user_id>")]
pub async fn delete_user<'r>(
role_id: i32,
user_id: i32,
_session: AdminSession,
db: DbConn,
) -> Result<impl Responder<'r, 'static>> {
let role = Role::find(role_id, &db).await?;
role.remove_user(user_id, &db).await?;
Ok(Accepter {
html: Redirect::to(uri!(show_role_page(
role_id,
None::<String>,
Some("user deleted")
))),
json: Custom(Status::Ok, ()),
})
}