-
Notifications
You must be signed in to change notification settings - Fork 1
Add client roles to zauth #328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
16c0c40
implement roles
xerbalind 563d13e
format
xerbalind a27fb04
Merge branch 'main' into roles
xerbalind ff4c986
fix tests
xerbalind fa8b4b2
put all the schemas in one file that can be autogenerated
xerbalind 00f62f3
remove todo
xerbalind ede7341
tests, tests, tests
xerbalind e37df12
refactoring of routes and renaming of functions
xerbalind f2ae227
add tests for new endpoint
xerbalind 1f203d6
use bulma tags to list roles with delete button
xerbalind File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| -- This file should undo anything in `up.sql` | ||
| DROP TABLE users_roles; | ||
| DROP TABLE roles; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| -- Your SQL goes here | ||
| CREATE TABLE roles ( | ||
| id SERIAL PRIMARY KEY, | ||
| name VARCHAR(255) NOT NULL UNIQUE, | ||
| description VARCHAR(255) NOT NULL, | ||
| client_id INTEGER REFERENCES clients(id) | ||
| ); | ||
|
|
||
| CREATE TABLE users_roles ( | ||
| user_id INTEGER REFERENCES users(id) ON DELETE CASCADE, | ||
| role_id INTEGER REFERENCES roles(id) ON DELETE CASCADE, | ||
| PRIMARY KEY (user_id, role_id) | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| 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/<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?; | ||
|
|
||
| let client = if let Some(id) = role.client_id { | ||
| Some(Client::find(id, &db).await?) | ||
| } else { | ||
| None | ||
| }; | ||
|
|
||
| Ok(template! { "roles/show_role.html"; | ||
| current_user: User = session.admin, | ||
| role: Role = role, | ||
| client: Option<Client> = client, | ||
| 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, ()), | ||
| }) | ||
| } | ||
|
|
||
| #[post("/roles/<role_id>/users", data = "<username>")] | ||
| pub async fn add_user<'r>( | ||
| username: Form<String>, | ||
| role_id: i32, | ||
| db: DbConn, | ||
| _session: AdminSession, | ||
| ) -> Result<impl Responder<'r, 'static>> { | ||
| let role = Role::find(role_id, &db).await?; | ||
| let user_result = User::find_by_username(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>/users/<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, ()), | ||
| }) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.