update user channel ids on creation, add argument for adding global admins
This commit is contained in:
parent
9f422706ea
commit
ca1a212188
@ -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;";
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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,12 +115,42 @@ pub struct Args {
|
||||
pub password: Option<String>,
|
||||
}
|
||||
|
||||
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<Sqlite>) -> 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<Sqlite>) -> 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<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()
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user