add: littleweb api + scopes
This commit is contained in:
parent
c4de17058b
commit
3fc0872867
9 changed files with 598 additions and 11 deletions
|
@ -26,6 +26,7 @@ mod questions;
|
|||
mod reactions;
|
||||
mod reports;
|
||||
mod requests;
|
||||
mod services;
|
||||
mod stackblocks;
|
||||
mod stacks;
|
||||
mod uploads;
|
||||
|
|
130
crates/core/src/database/services.rs
Normal file
130
crates/core/src/database/services.rs
Normal file
|
@ -0,0 +1,130 @@
|
|||
use crate::model::{
|
||||
auth::User,
|
||||
littleweb::{Service, ServiceFsEntry},
|
||||
permissions::{FinePermission, SecondaryPermission},
|
||||
Error, Result,
|
||||
};
|
||||
use crate::{auto_method, DataManager};
|
||||
use oiseau::{cache::Cache, execute, get, params, query_rows, PostgresRow};
|
||||
|
||||
impl DataManager {
|
||||
/// Get a [`Service`] from an SQL row.
|
||||
pub(crate) fn get_service_from_row(x: &PostgresRow) -> Service {
|
||||
Service {
|
||||
id: get!(x->0(i64)) as usize,
|
||||
created: get!(x->1(i64)) as usize,
|
||||
owner: get!(x->2(i64)) as usize,
|
||||
name: get!(x->3(String)),
|
||||
files: serde_json::from_str(&get!(x->4(String))).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
auto_method!(get_service_by_id(usize as i64)@get_service_from_row -> "SELECT * FROM services WHERE id = $1" --name="service" --returns=Service --cache-key-tmpl="atto.service:{}");
|
||||
|
||||
/// Get all services by user.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `id` - the ID of the user to fetch services for
|
||||
pub async fn get_services_by_user(&self, id: usize) -> Result<Vec<Service>> {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = query_rows!(
|
||||
&conn,
|
||||
"SELECT * FROM services WHERE owner = $1 ORDER BY created DESC",
|
||||
&[&(id as i64)],
|
||||
|x| { Self::get_service_from_row(x) }
|
||||
);
|
||||
|
||||
if res.is_err() {
|
||||
return Err(Error::GeneralNotFound("service".to_string()));
|
||||
}
|
||||
|
||||
Ok(res.unwrap())
|
||||
}
|
||||
|
||||
const MAXIMUM_FREE_SERVICES: usize = 5;
|
||||
|
||||
/// Create a new service in the database.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `data` - a mock [`Service`] object to insert
|
||||
pub async fn create_service(&self, data: Service) -> Result<Service> {
|
||||
// check values
|
||||
if data.name.len() < 2 {
|
||||
return Err(Error::DataTooShort("name".to_string()));
|
||||
} else if data.name.len() > 128 {
|
||||
return Err(Error::DataTooLong("name".to_string()));
|
||||
}
|
||||
|
||||
// check number of services
|
||||
let owner = self.get_user_by_id(data.owner).await?;
|
||||
|
||||
if !owner.permissions.check(FinePermission::SUPPORTER) {
|
||||
let services = self.get_services_by_user(data.owner).await?;
|
||||
|
||||
if services.len() >= Self::MAXIMUM_FREE_SERVICES {
|
||||
return Err(Error::MiscError(
|
||||
"You already have the maximum number of services you can have".to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// ...
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = execute!(
|
||||
&conn,
|
||||
"INSERT INTO services VALUES ($1, $2, $3, $4, $5)",
|
||||
params![
|
||||
&(data.id as i64),
|
||||
&(data.created as i64),
|
||||
&(data.owner as i64),
|
||||
&data.name,
|
||||
&serde_json::to_string(&data.files).unwrap(),
|
||||
]
|
||||
);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
pub async fn delete_service(&self, id: usize, user: &User) -> Result<()> {
|
||||
let service = self.get_service_by_id(id).await?;
|
||||
|
||||
// check user permission
|
||||
if user.id != service.owner
|
||||
&& !user
|
||||
.secondary_permissions
|
||||
.check(SecondaryPermission::MANAGE_SERVICES)
|
||||
{
|
||||
return Err(Error::NotAllowed);
|
||||
}
|
||||
|
||||
// ...
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = execute!(&conn, "DELETE FROM services WHERE id = $1", &[&(id as i64)]);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
// ...
|
||||
self.0.1.remove(format!("atto.service:{}", id)).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
auto_method!(update_service_files(Vec<ServiceFsEntry>)@get_service_by_id:FinePermission::MANAGE_USERS; -> "UPDATE services SET data = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.service:{}");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue