From 303885ef0d299a8e10276df7df6218db7b801552 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Mon, 20 Nov 2023 16:31:01 +0000 Subject: [PATCH] feat: added support for skynet trainees --- flake.nix | 6 ++++++ src/bin/update_groups.rs | 40 ++++++++++++++++++++++------------------ 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/flake.nix b/flake.nix index 1c6ac58..f8a0c10 100644 --- a/flake.nix +++ b/flake.nix @@ -56,6 +56,7 @@ # special categories of users USERS_ADMIN = lib.strings.concatStringsSep "," cfg.users.admin; USERS_COMMITTEE = lib.strings.concatStringsSep "," cfg.users.committee; + USERS_TRAINEE = lib.strings.concatStringsSep "," cfg.users.trainee; USERS_LIFETIME = lib.strings.concatStringsSep "," cfg.users.lifetime; USERS_BANNED = lib.strings.concatStringsSep "," cfg.users.banned; USERS_RESTRICTED = lib.strings.concatStringsSep "," cfg.users.restricted; @@ -141,6 +142,11 @@ default = []; description = "array of committee members"; }; + trainee = mkOption rec { + type = types.listOf types.str; + default = []; + description = "array of trainee admins"; + }; lifetime = mkOption rec { type = types.listOf types.str; default = []; diff --git a/src/bin/update_groups.rs b/src/bin/update_groups.rs index a3b0529..523090b 100644 --- a/src/bin/update_groups.rs +++ b/src/bin/update_groups.rs @@ -18,6 +18,7 @@ async fn update(config: &Config) -> tide::Result<()> { let mut users_tmp = HashSet::from([String::from("compsoc")]); let mut admins_tmp = HashSet::from([String::from("compsoc")]); let mut committee_tmp = HashSet::from([String::from("compsoc")]); + let mut trainees_tmp = HashSet::from([String::from("compsoc")]); if let Ok(x) = env::var("USERS_LIFETIME") { for user in x.split(',').collect::>() { @@ -30,22 +31,9 @@ async fn update(config: &Config) -> tide::Result<()> { users_tmp.insert(user); } - if let Ok(x) = env::var("USERS_ADMIN") { - // admins automatically get added as users - for user in x.split(',').collect::>() { - admins_tmp.insert(user.to_string()); - users_tmp.insert(user.to_string()); - } - } - - // read from teh env - if let Ok(x) = env::var("USERS_COMMITTEE") { - // committee automatically get added as users - for user in x.split(',').collect::>() { - committee_tmp.insert(user.to_string()); - users_tmp.insert(user.to_string()); - } - } + get_from_env(&mut users_tmp, &mut admins_tmp, "USERS_ADMIN"); + get_from_env(&mut users_tmp, &mut committee_tmp, "USERS_COMMITTEE"); + get_from_env(&mut users_tmp, &mut trainees_tmp, "USERS_TRAINEE"); // sorting makes it easier/faster if let Ok(x) = env::var("USERS_BANNED") { @@ -58,15 +46,27 @@ async fn update(config: &Config) -> tide::Result<()> { users, admins, committee, - } = get_secure(&db, &users_tmp, &admins_tmp, &committee_tmp).await; + trainees, + } = get_secure(&db, &users_tmp, &admins_tmp, &committee_tmp, &trainees_tmp).await; update_group(config, "skynet-users", &users, true).await?; update_group(config, "skynet-admins", &admins, true).await?; update_group(config, "skynet-committee", &committee, true).await?; + update_group(config, "skynet-trainees", &trainees, true).await?; Ok(()) } +fn get_from_env(users: &mut HashSet, other: &mut HashSet, key: &str) { + if let Ok(x) = env::var(key) { + // committee automatically get added as users + for user in x.split(',').collect::>() { + other.insert(user.to_string()); + users.insert(user.to_string()); + } + } +} + async fn from_csv(db: &Pool) -> Result, Box> { let mut uids = HashSet::new(); @@ -125,15 +125,19 @@ struct AccountsSecure { users: Vec, admins: Vec, committee: Vec, + trainees: Vec, } -async fn get_secure(db: &Pool, users: &HashSet, admins: &HashSet, committee: &HashSet) -> AccountsSecure { +async fn get_secure( + db: &Pool, users: &HashSet, admins: &HashSet, committee: &HashSet, trainees: &HashSet, +) -> AccountsSecure { // to avoid searching for teh same thing again. let mut cache = HashSet::new(); AccountsSecure { users: get_secure_sub(db, users, &mut cache).await, admins: get_secure_sub(db, admins, &mut cache).await, committee: get_secure_sub(db, committee, &mut cache).await, + trainees: get_secure_sub(db, trainees, &mut cache).await, } }