feat: added support for skynet trainees

This commit is contained in:
silver 2023-11-20 16:31:01 +00:00
parent b0bc6f35c2
commit 303885ef0d
2 changed files with 28 additions and 18 deletions

View file

@ -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 = [];

View file

@ -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::<Vec<&str>>() {
@ -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::<Vec<&str>>() {
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::<Vec<&str>>() {
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<String>, other: &mut HashSet<String>, key: &str) {
if let Ok(x) = env::var(key) {
// committee automatically get added as users
for user in x.split(',').collect::<Vec<&str>>() {
other.insert(user.to_string());
users.insert(user.to_string());
}
}
}
async fn from_csv(db: &Pool<Sqlite>) -> Result<HashSet<String>, Box<dyn Error>> {
let mut uids = HashSet::new();
@ -125,15 +125,19 @@ struct AccountsSecure {
users: Vec<String>,
admins: Vec<String>,
committee: Vec<String>,
trainees: Vec<String>,
}
async fn get_secure(db: &Pool<Sqlite>, users: &HashSet<String>, admins: &HashSet<String>, committee: &HashSet<String>) -> AccountsSecure {
async fn get_secure(
db: &Pool<Sqlite>, users: &HashSet<String>, admins: &HashSet<String>, committee: &HashSet<String>, trainees: &HashSet<String>,
) -> 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,
}
}