diff --git a/ffplayout-api/src/api/routes.rs b/ffplayout-api/src/api/routes.rs index f284ed90..839e1eb3 100644 --- a/ffplayout-api/src/api/routes.rs +++ b/ffplayout-api/src/api/routes.rs @@ -232,6 +232,45 @@ async fn get_user( } } +/// **Get User by ID** +/// +/// ```BASH +/// curl -X GET 'http://127.0.0.1:8787/api/user/2' -H 'Content-Type: application/json' \ +/// -H 'Authorization: Bearer ' +/// ``` +#[get("/user/{name}")] +#[has_any_role("Role::Admin", type = "Role")] +async fn get_user_by_name( + pool: web::Data>, + name: web::Path, +) -> Result { + match handles::select_user(&pool.into_inner(), &name).await { + Ok(user) => Ok(web::Json(user)), + Err(e) => { + error!("{e}"); + Err(ServiceError::InternalServerError) + } + } +} + +// **Get all User** +/// +/// ```BASH +/// curl -X GET 'http://127.0.0.1:8787/api/users' -H 'Content-Type: application/json' \ +/// -H 'Authorization: Bearer ' +/// ``` +#[get("/users")] +#[has_any_role("Role::Admin", type = "Role")] +async fn get_users(pool: web::Data>) -> Result { + match handles::select_users(&pool.into_inner()).await { + Ok(users) => Ok(web::Json(users)), + Err(e) => { + error!("{e}"); + Err(ServiceError::InternalServerError) + } + } +} + /// **Update current User** /// /// ```BASH @@ -245,11 +284,20 @@ async fn update_user( id: web::Path, user: web::ReqData, data: web::Json, + role: AuthDetails, ) -> Result { - if id.into_inner() == user.id { + if id.into_inner() == user.id || role.has_role(&Role::Admin) { let mut fields = String::new(); + if !data.username.is_empty() { + fields.push_str(format!("username = '{}'", data.username).as_str()); + } + if let Some(mail) = data.mail.clone() { + if !fields.is_empty() { + fields.push_str(", "); + } + fields.push_str(format!("mail = '{mail}'").as_str()); } @@ -301,6 +349,27 @@ async fn add_user( } } +// **Delete User** +/// +/// ```BASH +/// curl -X GET 'http://127.0.0.1:8787/api/user/2' -H 'Content-Type: application/json' \ +/// -H 'Authorization: Bearer ' +/// ``` +#[delete("/user/{name}")] +#[has_any_role("Role::Admin", type = "Role")] +async fn remove_user( + pool: web::Data>, + name: web::Path, +) -> Result { + match handles::delete_user(&pool.into_inner(), &name).await { + Ok(_) => return Ok("Delete user success"), + Err(e) => { + error!("{e}"); + Err(ServiceError::InternalServerError) + } + } +} + /// #### ffpapi Settings /// /// **Get Settings from Channel** diff --git a/ffplayout-api/src/db/handles.rs b/ffplayout-api/src/db/handles.rs index 960a5592..04eefbdd 100644 --- a/ffplayout-api/src/db/handles.rs +++ b/ffplayout-api/src/db/handles.rs @@ -228,6 +228,18 @@ pub async fn select_user(conn: &Pool, user: &str) -> Result, id: i32) -> Result { + let query = "SELECT id, mail, username, role_id FROM user WHERE id = $1"; + + sqlx::query_as(query).bind(id).fetch_one(conn).await +} + +pub async fn select_users(conn: &Pool) -> Result, sqlx::Error> { + let query = "SELECT id, username FROM user"; + + sqlx::query_as(query).fetch_all(conn).await +} + pub async fn insert_user( conn: &Pool, user: User, @@ -260,6 +272,15 @@ pub async fn update_user( sqlx::query(&query).bind(id).execute(conn).await } +pub async fn delete_user( + conn: &Pool, + name: &str, +) -> Result { + let query = "DELETE FROM user WHERE username = $1;"; + + sqlx::query(query).bind(name).execute(conn).await +} + pub async fn select_presets(conn: &Pool, id: i32) -> Result, sqlx::Error> { let query = "SELECT * FROM presets WHERE channel_id = $1"; diff --git a/ffplayout-api/src/db/models.rs b/ffplayout-api/src/db/models.rs index 02c428b6..770df0c0 100644 --- a/ffplayout-api/src/db/models.rs +++ b/ffplayout-api/src/db/models.rs @@ -10,6 +10,7 @@ pub struct User { #[serde(skip_deserializing)] pub id: i32, #[sqlx(default)] + #[serde(skip_serializing_if = "Option::is_none")] pub mail: Option, pub username: String, #[sqlx(default)] diff --git a/ffplayout-api/src/main.rs b/ffplayout-api/src/main.rs index f5dacb2b..62d00eba 100644 --- a/ffplayout-api/src/main.rs +++ b/ffplayout-api/src/main.rs @@ -109,6 +109,9 @@ async fn main() -> std::io::Result<()> { .wrap(auth) .service(add_user) .service(get_user) + .service(get_user_by_name) + .service(get_users) + .service(remove_user) .service(get_playout_config) .service(update_playout_config) .service(add_preset)