From eed9bcdf8b48c9c8eb40334ca577a294b179c3be Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Sat, 15 Apr 2023 10:31:11 +0200 Subject: [PATCH] Can specify default clients --- src/controllers/admin_controller.rs | 35 ++++++++++++++++------------- src/data/client.rs | 20 +++++++++++++++++ 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/controllers/admin_controller.rs b/src/controllers/admin_controller.rs index 964946f..81ca9a0 100644 --- a/src/controllers/admin_controller.rs +++ b/src/controllers/admin_controller.rs @@ -42,8 +42,8 @@ pub async fn clients_route(user: CurrentUser, clients: web::Data) _p: BaseSettingsPage::get("Clients list", &user, None, None), clients: clients.cloned(), } - .render() - .unwrap(), + .render() + .unwrap(), ) } @@ -65,7 +65,7 @@ pub struct UpdateUserQuery { } pub async fn users_route( - user: CurrentUser, + admin: CurrentUser, users: web::Data>, update_query: Option>, logger: ActionLogger, @@ -197,7 +197,7 @@ pub async fn users_route( true => "Failed to create user!", false => "Failed to update user!", } - .to_string(), + .to_string(), ) } else { success = Some(match is_creating { @@ -225,23 +225,26 @@ pub async fn users_route( HttpResponse::Ok().body( UsersListTemplate { - _p: BaseSettingsPage::get("Users list", &user, danger, success), + _p: BaseSettingsPage::get("Users list", &admin, danger, success), users, } - .render() - .unwrap(), + .render() + .unwrap(), ) } -pub async fn create_user(user: CurrentUser, clients: web::Data) -> impl Responder { +pub async fn create_user(admin: CurrentUser, clients: web::Data) -> impl Responder { + let mut user = User::default(); + user.authorized_clients = Some(clients.get_default_clients().iter().map(|u| u.id.clone()).collect()); + HttpResponse::Ok().body( EditUserTemplate { - _p: BaseSettingsPage::get("Create a new user", user.deref(), None, None), - u: Default::default(), + _p: BaseSettingsPage::get("Create a new user", admin.deref(), None, None), + u: user, clients: clients.cloned(), } - .render() - .unwrap(), + .render() + .unwrap(), ) } @@ -251,7 +254,7 @@ pub struct EditUserQuery { } pub async fn edit_user( - user: CurrentUser, + admin: CurrentUser, clients: web::Data, users: web::Data>, query: web::Query, @@ -266,7 +269,7 @@ pub async fn edit_user( EditUserTemplate { _p: BaseSettingsPage::get( "Edit user account", - user.deref(), + admin.deref(), match edited_account.is_none() { true => Some("Could not find requested user!".to_string()), false => None, @@ -276,7 +279,7 @@ pub async fn edit_user( u: edited_account.unwrap_or_default(), clients: clients.cloned(), } - .render() - .unwrap(), + .render() + .unwrap(), ) } diff --git a/src/data/client.rs b/src/data/client.rs index 6f121f2..35ec1fc 100644 --- a/src/data/client.rs +++ b/src/data/client.rs @@ -6,11 +6,24 @@ pub struct ClientID(pub String); #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] pub struct Client { + /// The ID of the client pub id: ClientID, + + /// The human-readable name of the client pub name: String, + + /// A short description of the service provided by the client pub description: String, + + /// The secret used by the client to retrieve authenticated users information pub secret: String, + + /// The URI where the users should be redirected once authenticated pub redirect_uri: String, + + /// Specify if the client must be allowed by default for new account + #[serde(default = "bool::default")] + pub default: bool, } impl PartialEq for Client { @@ -33,6 +46,13 @@ impl EntityManager { None } + /// Get the list of default clients. + /// + /// i.e. the clients that are granted to new accounts by default + pub fn get_default_clients(&self) -> Vec<&Client> { + self.iter().filter(|u| u.default).collect() + } + pub fn apply_environment_variables(&mut self) { for c in self.iter_mut() { c.id = ClientID(apply_env_vars(&c.id.0));