update user channel ids on creation, add argument for adding global admins

This commit is contained in:
jb-alvarado 2024-06-19 16:51:43 +02:00
parent 9f422706ea
commit ca1a212188
4 changed files with 95 additions and 28 deletions

View File

@ -317,6 +317,12 @@ pub async fn select_user(conn: &Pool<Sqlite>, id: i32) -> Result<User, sqlx::Err
sqlx::query_as(query).bind(id).fetch_one(conn).await
}
pub async fn select_global_admins(conn: &Pool<Sqlite>) -> Result<Vec<User>, 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<Sqlite>) -> Result<Vec<User>, 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<Sqlite>,
id: i32,
ids: String,
) -> Result<SqliteQueryResult, sqlx::Error> {
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<Sqlite>, id: i32) -> Result<SqliteQueryResult, sqlx::Error> {
let query = "DELETE FROM user WHERE id = $1;";

View File

@ -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,

View File

@ -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<PathBuf>,
@ -112,26 +115,9 @@ pub struct Args {
pub password: Option<String>,
}
pub async fn run_args(pool: &Pool<Sqlite>) -> Result<(), i32> {
let mut args = ARGS.clone();
if args.init {
fn global_user(args: &mut Args) {
let mut user = String::new();
let mut mail = String::new();
let mut storage = String::new();
let mut playlist = String::new();
let mut logging = String::new();
let mut hls = String::new();
let mut shared_store = String::new();
let mut global = GlobalSettings {
id: 0,
secret: None,
hls_path: String::new(),
playlist_path: String::new(),
storage_path: String::new(),
logging_path: String::new(),
shared_storage: false,
};
print!("Global admin: ");
stdout().flush().unwrap();
@ -156,6 +142,33 @@ pub async fn run_args(pool: &Pool<Sqlite>) -> Result<(), i32> {
.expect("Did not enter a correct name?");
args.mail = Some(mail.trim().to_string());
}
pub async fn run_args(pool: &Pool<Sqlite>) -> Result<(), i32> {
let channels = handles::select_all_channels(pool).await;
let mut args = ARGS.clone();
if args.init {
let check_user = handles::select_user(pool, 1).await;
let mut storage = String::new();
let mut playlist = String::new();
let mut logging = String::new();
let mut hls = String::new();
let mut shared_store = String::new();
let mut global = GlobalSettings {
id: 0,
secret: None,
hls_path: String::new(),
playlist_path: String::new(),
storage_path: String::new(),
logging_path: String::new(),
shared_storage: false,
};
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<Sqlite>) -> 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<Sqlite>) -> 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<Sqlite>) -> Result<(), i32> {
}
if ARGS.list_channels {
match handles::select_all_channels(pool).await {
match channels {
Ok(channels) => {
let chl = channels
.iter()

View File

@ -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::<Vec<String>>()
.join(","),
)
.await
{
error!("Update global admin: {e}");
};
}
}
}
Ok(channel)
}