Skip to content

Commit 23df01c

Browse files
authored
Handle LDAP dry run in demo
2 parents 3e6efad + 4c7c3fc commit 23df01c

1 file changed

Lines changed: 71 additions & 3 deletions

File tree

crates/defguard_core/src/handlers/settings.rs

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ use crate::{
2424
enterprise::{
2525
db::models::enterprise_settings::EnterpriseSettings,
2626
handlers::LicenseInfo,
27-
ldap::{LDAPConnection, sync::Authority},
27+
ldap::{
28+
LDAPConnection,
29+
sync::{Authority, LdapDryRunAction, LdapDryRunResult, LdapDryRunUser},
30+
},
2831
license::update_cached_license,
2932
},
3033
error::WebError,
@@ -258,14 +261,71 @@ pub(crate) async fn test_ldap_settings(_admin: AdminRole, _license: LicenseInfo)
258261
}
259262
}
260263

264+
fn demo_ldap_dry_run_result() -> LdapDryRunResult {
265+
LdapDryRunResult {
266+
defguard: vec![
267+
LdapDryRunUser {
268+
username: "j.smith".to_string(),
269+
email: "j.smith@example.com".to_string(),
270+
first_name: "John".to_string(),
271+
last_name: "Smith".to_string(),
272+
action: LdapDryRunAction::Add,
273+
},
274+
LdapDryRunUser {
275+
username: "a.johnson".to_string(),
276+
email: "a.johnson@example.com".to_string(),
277+
first_name: "Anna".to_string(),
278+
last_name: "Johnson".to_string(),
279+
action: LdapDryRunAction::Add,
280+
},
281+
LdapDryRunUser {
282+
username: "p.brown".to_string(),
283+
email: "p.brown@example.com".to_string(),
284+
first_name: "Peter".to_string(),
285+
last_name: "Brown".to_string(),
286+
action: LdapDryRunAction::Remove,
287+
},
288+
],
289+
ldap: vec![
290+
LdapDryRunUser {
291+
username: "m.davis".to_string(),
292+
email: "m.davis@example.com".to_string(),
293+
first_name: "Maria".to_string(),
294+
last_name: "Davis".to_string(),
295+
action: LdapDryRunAction::Add,
296+
},
297+
LdapDryRunUser {
298+
username: "t.wilson".to_string(),
299+
email: "t.wilson@example.com".to_string(),
300+
first_name: "Thomas".to_string(),
301+
last_name: "Wilson".to_string(),
302+
action: LdapDryRunAction::Remove,
303+
},
304+
LdapDryRunUser {
305+
username: "k.miller".to_string(),
306+
email: "k.miller@example.com".to_string(),
307+
first_name: "Kate".to_string(),
308+
last_name: "Miller".to_string(),
309+
action: LdapDryRunAction::Remove,
310+
},
311+
],
312+
}
313+
}
314+
261315
/// Tests the LDAP connection using the provided (not yet saved) settings.
262316
pub(crate) async fn test_submitted_ldap_settings(
263317
_admin: AdminRole,
264318
_license: LicenseInfo,
265-
Json(settings): Json<Settings>,
319+
Json(_settings): Json<Settings>,
266320
) -> ApiResult {
267321
debug!("Testing LDAP connection with provided settings");
268-
match LDAPConnection::create_with_settings(settings).await {
322+
if server_config().is_demo_mode {
323+
return Ok(ApiResponse::json(
324+
demo_ldap_dry_run_result(),
325+
StatusCode::OK,
326+
));
327+
}
328+
match LDAPConnection::create_with_settings(_settings).await {
269329
Ok(_) => {
270330
debug!("LDAP connected successfully");
271331
Ok(ApiResponse::with_status(StatusCode::OK))
@@ -286,6 +346,14 @@ pub(crate) async fn ldap_dry_run(
286346
Json(settings): Json<Settings>,
287347
) -> ApiResult {
288348
debug!("Performing LDAP dry run with provided settings");
349+
350+
if server_config().is_demo_mode {
351+
return Ok(ApiResponse::json(
352+
demo_ldap_dry_run_result(),
353+
StatusCode::OK,
354+
));
355+
}
356+
289357
let authority = if settings.ldap_is_authoritative {
290358
Authority::LDAP
291359
} else {

0 commit comments

Comments
 (0)