Skip to content
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

feat: group management API #479

Merged
merged 9 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ humantime = "2.1"
# match ipnetwork version from sqlx
ipnetwork = { version = "0.20", features = ["serde"] }
jsonwebtoken = "9.2"
ldap3 = "0.11"
ldap3 = { version = "0.11", default-features = false, features = ["tls"] }
lettre = { version = "0.11", features = ["tokio1", "tokio1-native-tls"] }
md4 = "0.10"
otpauth = "0.4"
Expand Down
3 changes: 2 additions & 1 deletion src/appstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ pub struct AppState {
}

impl AppState {
pub fn trigger_action(&self, event: AppEvent) {
pub(crate) fn trigger_action(&self, event: AppEvent) {
let event_name = event.name().to_owned();
match self.tx.send(event) {
Ok(()) => info!("Sent trigger {event_name}"),
Err(err) => error!("Error sending trigger {event_name}: {err}"),
}
}

/// Handle webhook events
async fn handle_triggers(pool: DbPool, mut rx: UnboundedReceiver<AppEvent>) {
let reqwest_client = Client::builder().user_agent("reqwest").build().unwrap();
Expand Down
16 changes: 9 additions & 7 deletions src/bin/defguard.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
use std::{
fs::read_to_string,
sync::{Arc, Mutex},
};

use secrecy::ExposeSecret;
use tokio::sync::{broadcast, mpsc::unbounded_channel};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

use defguard::{
auth::failed_login::FailedLoginMap,
config::{Command, DefGuardConfig},
Expand All @@ -10,13 +19,6 @@ use defguard::{
wireguard_stats_purge::run_periodic_stats_purge,
SERVER_CONFIG,
};
use secrecy::ExposeSecret;
use std::{
fs::read_to_string,
sync::{Arc, Mutex},
};
use tokio::sync::{broadcast, mpsc::unbounded_channel};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

#[macro_use]
extern crate tracing;
Expand Down
8 changes: 4 additions & 4 deletions src/db/models/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,11 +655,11 @@ mod test {
network.save(&pool).await.unwrap();

let mut user = User::new(
"testuser".to_string(),
"testuser",
Some("hunter2"),
"Tester".to_string(),
"Test".to_string(),
"test@test.com".to_string(),
"Tester",
"Test",
"test@test.com",
None,
);
user.save(&pool).await.unwrap();
Expand Down
12 changes: 6 additions & 6 deletions src/db/models/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct Group {

impl Group {
#[must_use]
pub fn new(name: &str) -> Self {
pub fn new<S: Into<String>>(name: S) -> Self {
Self {
id: None,
name: name.into(),
Expand Down Expand Up @@ -48,7 +48,7 @@ impl Group {
}
}

pub async fn fetch_all_members<'e, E>(&self, executor: E) -> Result<Vec<User>, SqlxError>
pub async fn members<'e, E>(&self, executor: E) -> Result<Vec<User>, SqlxError>
where
E: PgExecutor<'e>,
{
Expand Down Expand Up @@ -233,11 +233,11 @@ mod test {
group.save(&pool).await.unwrap();

let mut user = User::new(
"hpotter".into(),
"hpotter",
Some("pass123"),
"Potter".into(),
"Harry".into(),
"h.potter@hogwart.edu.uk".into(),
"Potter",
"Harry",
"h.potter@hogwart.edu.uk",
None,
);
user.save(&pool).await.unwrap();
Expand Down
8 changes: 4 additions & 4 deletions src/db/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,11 @@ mod test {
#[sqlx::test]
async fn test_user_info(pool: DbPool) {
let mut user = User::new(
"hpotter".into(),
"hpotter",
Some("pass123"),
"Potter".into(),
"Harry".into(),
"h.potter@hogwart.edu.uk".into(),
"Potter",
"Harry",
"h.potter@hogwart.edu.uk",
None,
);
user.save(&pool).await.unwrap();
Expand Down
34 changes: 17 additions & 17 deletions src/db/models/settings.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;

use model_derive::Model;
use sqlx::{query, Error as SqlxError, PgExecutor, Type};
use sqlx::{query, query_as, Error as SqlxError, PgExecutor, Type};
use struct_patch::Patch;

use super::DbPool;
Expand Down Expand Up @@ -46,7 +46,7 @@ pub struct Settings {
pub enrollment_welcome_email: Option<String>,
pub enrollment_welcome_email_subject: Option<String>,
pub enrollment_use_welcome_message_as_email: bool,
// Instance uuid needed for desktop client
// Instance UUID needed for desktop client
#[serde(skip)]
pub uuid: uuid::Uuid,
// LDAP
Expand Down Expand Up @@ -110,14 +110,7 @@ impl Settings {
}
}

#[derive(Debug, Serialize, Clone)]
pub struct SettingsBranding {
pub instance_name: String,
pub main_logo_url: String,
pub nav_logo_url: String,
}

#[derive(Debug, Serialize, Clone)]
#[derive(Serialize)]
pub struct SettingsEssentials {
pub instance_name: String,
pub main_logo_url: String,
Expand All @@ -129,11 +122,18 @@ pub struct SettingsEssentials {
}

impl SettingsEssentials {
pub async fn get_settings_essentials(pool: &DbPool) -> Result<Self, SqlxError> {
let res = sqlx::query_as!(SettingsEssentials, r#"
SELECT instance_name, main_logo_url, nav_logo_url, wireguard_enabled, webhooks_enabled, worker_enabled, openid_enabled FROM settings WHERE id = 1;
"#).fetch_one(pool).await?;
Ok(res)
pub(crate) async fn get_settings_essentials<'e, E>(executor: E) -> Result<Self, SqlxError>
where
E: PgExecutor<'e>,
{
query_as!(
SettingsEssentials,
"SELECT instance_name, main_logo_url, nav_logo_url, wireguard_enabled, \
webhooks_enabled, worker_enabled, openid_enabled \
FROM settings WHERE id = 1"
)
.fetch_one(executor)
.await
}
}

Expand All @@ -152,7 +152,7 @@ impl From<Settings> for SettingsEssentials {
}

mod defaults {
pub const WELCOME_MESSAGE: &str = "Dear {{ first_name }} {{ last_name }},
pub static WELCOME_MESSAGE: &str = "Dear {{ first_name }} {{ last_name }},

By completing the enrollment process, you now have now access to all company systems.

Expand Down Expand Up @@ -187,5 +187,5 @@ Sent by defguard {{ defguard_version }}
Star us on GitHub! https://github.com/defguard/defguard\
";

pub const WELCOME_EMAIL_SUBJECT: &str = "[defguard] Welcome message after enrollment";
pub static WELCOME_EMAIL_SUBJECT: &str = "[defguard] Welcome message after enrollment";
}
Loading
Loading