mirror of
https://github.com/Blah-IM/blahrs.git
synced 2025-07-06 22:25:34 +00:00
feat(types,webapi): impl id_key/act_key for all APIs and update docs
This commit is contained in:
parent
fb76756482
commit
cb72d049e0
10 changed files with 426 additions and 330 deletions
|
@ -1,12 +1,17 @@
|
|||
use std::borrow::Borrow;
|
||||
use std::ops::DerefMut;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use anyhow::{ensure, Context, Result};
|
||||
use axum::http::StatusCode;
|
||||
use blah_types::{ServerPermission, UserKey};
|
||||
use parking_lot::Mutex;
|
||||
use rusqlite::{params, Connection, OpenFlags};
|
||||
use rusqlite::{params, Connection, OpenFlags, OptionalExtension};
|
||||
use serde::Deserialize;
|
||||
use serde_inline_default::serde_inline_default;
|
||||
|
||||
use crate::ApiError;
|
||||
|
||||
const DEFAULT_DATABASE_PATH: &str = "/var/lib/blahd/db.sqlite";
|
||||
|
||||
static INIT_SQL: &str = include_str!("../schema.sql");
|
||||
|
@ -97,6 +102,31 @@ impl Database {
|
|||
}
|
||||
}
|
||||
|
||||
pub trait ConnectionExt: Borrow<Connection> {
|
||||
fn get_user(&self, user: &UserKey) -> Result<(i64, ServerPermission), ApiError> {
|
||||
self.borrow()
|
||||
.query_row(
|
||||
r"
|
||||
SELECT `uid`, `permission`
|
||||
FROM `valid_user_act_key`
|
||||
WHERE (`id_key`, `act_key`) = (?, ?)
|
||||
",
|
||||
params![user.id_key, user.act_key],
|
||||
|row| Ok((row.get(0)?, row.get(1)?)),
|
||||
)
|
||||
.optional()?
|
||||
.ok_or_else(|| {
|
||||
error_response!(
|
||||
StatusCode::NOT_FOUND,
|
||||
"not_found",
|
||||
"the user does not exist",
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl ConnectionExt for Connection {}
|
||||
|
||||
#[test]
|
||||
fn init_sql_valid() {
|
||||
let conn = Connection::open_in_memory().unwrap();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue