From ca1a212188447415bb897795c9d317c8cd5ccd5d Mon Sep 17 00:00:00 2001 From: jb-alvarado Date: Wed, 19 Jun 2024 16:51:43 +0200 Subject: [PATCH] update user channel ids on creation, add argument for adding global admins --- ffplayout/src/db/handles.rs | 16 +++++++ ffplayout/src/db/models.rs | 9 ++++ ffplayout/src/utils/args_parse.rs | 77 ++++++++++++++++++++----------- ffplayout/src/utils/channels.rs | 21 +++++++++ 4 files changed, 95 insertions(+), 28 deletions(-) diff --git a/ffplayout/src/db/handles.rs b/ffplayout/src/db/handles.rs index c2e80146..6f3825bf 100644 --- a/ffplayout/src/db/handles.rs +++ b/ffplayout/src/db/handles.rs @@ -317,6 +317,12 @@ pub async fn select_user(conn: &Pool, id: i32) -> Result) -> Result, sqlx::Error> { + let query = "SELECT id, mail, username, role_id, channel_ids FROM user WHERE role_id = 1"; + + sqlx::query_as(query).fetch_all(conn).await +} + pub async fn select_users(conn: &Pool) -> Result, sqlx::Error> { let query = "SELECT id, username FROM user"; @@ -366,6 +372,16 @@ pub async fn update_user( sqlx::query(&query).bind(id).execute(conn).await } +pub async fn update_user_channel( + conn: &Pool, + id: i32, + ids: String, +) -> Result { + let query = format!("UPDATE user SET channel_ids = $2 WHERE id = $1"); + + sqlx::query(&query).bind(id).bind(ids).execute(conn).await +} + pub async fn delete_user(conn: &Pool, id: i32) -> Result { let query = "DELETE FROM user WHERE id = $1;"; diff --git a/ffplayout/src/db/models.rs b/ffplayout/src/db/models.rs index 1556f0be..bae714cd 100644 --- a/ffplayout/src/db/models.rs +++ b/ffplayout/src/db/models.rs @@ -249,6 +249,15 @@ pub struct Channel { pub utc_offset: i32, } +impl Channel { + pub fn default() -> Self { + Self { + id: 1, + ..Default::default() + } + } +} + #[derive(Clone, Debug, Deserialize, Serialize, sqlx::FromRow)] pub struct Configuration { pub id: i32, diff --git a/ffplayout/src/utils/args_parse.rs b/ffplayout/src/utils/args_parse.rs index bc403cab..2695ce8a 100644 --- a/ffplayout/src/utils/args_parse.rs +++ b/ffplayout/src/utils/args_parse.rs @@ -10,7 +10,7 @@ use sqlx::{Pool, Sqlite}; use crate::db::{ handles::{self, insert_user}, - models::{GlobalSettings, User}, + models::{Channel, GlobalSettings, User}, }; use crate::utils::{advanced_config::AdvancedConfig, config::PlayoutConfig}; use crate::ARGS; @@ -27,6 +27,9 @@ pub struct Args { )] pub init: bool, + #[clap(short, long, help = "Add a global admin user")] + pub add: bool, + #[clap(long, env, help = "path to database file")] pub db: Option, @@ -112,12 +115,42 @@ pub struct Args { pub password: Option, } +fn global_user(args: &mut Args) { + let mut user = String::new(); + let mut mail = String::new(); + + print!("Global admin: "); + stdout().flush().unwrap(); + + stdin() + .read_line(&mut user) + .expect("Did not enter a correct name?"); + + args.username = Some(user.trim().to_string()); + + print!("Password: "); + stdout().flush().unwrap(); + let password = read_password(); + + args.password = password.ok(); + + print!("Mail: "); + stdout().flush().unwrap(); + + stdin() + .read_line(&mut mail) + .expect("Did not enter a correct name?"); + + args.mail = Some(mail.trim().to_string()); +} + pub async fn run_args(pool: &Pool) -> Result<(), i32> { + let channels = handles::select_all_channels(pool).await; let mut args = ARGS.clone(); if args.init { - let mut user = String::new(); - let mut mail = String::new(); + let check_user = handles::select_user(pool, 1).await; + let mut storage = String::new(); let mut playlist = String::new(); let mut logging = String::new(); @@ -133,29 +166,9 @@ pub async fn run_args(pool: &Pool) -> Result<(), i32> { shared_storage: false, }; - print!("Global admin: "); - stdout().flush().unwrap(); - - stdin() - .read_line(&mut user) - .expect("Did not enter a correct name?"); - - args.username = Some(user.trim().to_string()); - - print!("Password: "); - stdout().flush().unwrap(); - let password = read_password(); - - args.password = password.ok(); - - print!("Mail: "); - stdout().flush().unwrap(); - - stdin() - .read_line(&mut mail) - .expect("Did not enter a correct name?"); - - args.mail = Some(mail.trim().to_string()); + if check_user.is_err() { + global_user(&mut args); + } print!("Storage path [/var/lib/ffplayout/tv-media]: "); stdout().flush().unwrap(); @@ -238,6 +251,10 @@ pub async fn run_args(pool: &Pool) -> Result<(), i32> { println!("Set global settings..."); } + if args.add { + global_user(&mut args); + } + if let Some(username) = args.username { if args.mail.is_none() || args.password.is_none() { eprintln!("Mail/password missing!"); @@ -250,7 +267,11 @@ pub async fn run_args(pool: &Pool) -> Result<(), i32> { username: username.clone(), password: args.password.unwrap(), role_id: Some(1), - channel_ids: vec![1], + channel_ids: channels + .unwrap_or(vec![Channel::default()]) + .iter() + .map(|c| c.id) + .collect(), token: None, }; @@ -265,7 +286,7 @@ pub async fn run_args(pool: &Pool) -> Result<(), i32> { } if ARGS.list_channels { - match handles::select_all_channels(pool).await { + match channels { Ok(channels) => { let chl = channels .iter() diff --git a/ffplayout/src/utils/channels.rs b/ffplayout/src/utils/channels.rs index 64e8586f..7edc1697 100644 --- a/ffplayout/src/utils/channels.rs +++ b/ffplayout/src/utils/channels.rs @@ -3,6 +3,7 @@ use std::{ sync::{Arc, Mutex}, }; +use log::*; use sqlx::{Pool, Sqlite}; use super::logging::MailQueue; @@ -36,6 +37,26 @@ pub async fn create_channel( mqs.push(m_queue.clone()); } + if let Ok(channels) = handles::select_all_channels(conn).await { + if let Ok(admins) = handles::select_global_admins(conn).await { + for admin in admins { + if let Err(e) = handles::update_user_channel( + conn, + admin.id, + channels + .iter() + .map(|c| c.id.to_string()) + .collect::>() + .join(","), + ) + .await + { + error!("Update global admin: {e}"); + }; + } + } + } + Ok(channel) }