realign database queries and begin restoring check_password

This commit is contained in:
Savanni D'Gerinel 2025-01-11 12:43:13 -05:00
parent e19d97663d
commit b138e6da0a
5 changed files with 56 additions and 73 deletions

View File

@ -3,8 +3,7 @@ CREATE TABLE users(
name TEXT UNIQUE, name TEXT UNIQUE,
password TEXT, password TEXT,
admin BOOLEAN, admin BOOLEAN,
enabled BOOLEAN, state TEXT
password_expires TEXT
); );
CREATE TABLE sessions( CREATE TABLE sessions(
@ -15,9 +14,9 @@ CREATE TABLE sessions(
); );
CREATE TABLE games( CREATE TABLE games(
uuid TEXT PRIMARY KEY, id TEXT PRIMARY KEY,
type_ TEXT,
gm TEXT, gm TEXT,
game_type TEXT,
name TEXT, name TEXT,
FOREIGN KEY(gm) REFERENCES users(uuid) FOREIGN KEY(gm) REFERENCES users(uuid)
@ -40,4 +39,3 @@ CREATE TABLE roles(
FOREIGN KEY(game_id) REFERENCES games(uuid) FOREIGN KEY(game_id) REFERENCES games(uuid)
); );
INSERT INTO users VALUES ('admin', 'admin', '', true, true, datetime('now'));

View File

@ -192,14 +192,16 @@ impl Core {
let games = self.0.read().await.db.games().await; let games = self.0.read().await.db.games().await;
match games { match games {
// Ok(games) => ok(games.into_iter().map(|g| Game::from(g)).collect()), // Ok(games) => ok(games.into_iter().map(|g| Game::from(g)).collect()),
Ok(games) => ok(games.into_iter().map(|game| Ok(games) => ok(games
GameOverview{ .into_iter()
id: game.id, .map(|game| GameOverview {
game_type: "".to_owned(), id: game.id,
game_name: game.name, type_: "".to_owned(),
gm: game.gm, name: game.name,
players: game.players, gm: game.gm,
}).collect::<Vec<GameOverview>>()), players: game.players,
})
.collect::<Vec<GameOverview>>()),
Err(err) => fatal(err), Err(err) => fatal(err),
} }
} }
@ -330,7 +332,11 @@ impl Core {
}; };
match state match state
.db .db
.save_user(User{ password, state: AccountState::Normal, ..user }) .save_user(User {
password,
state: AccountState::Normal,
..user
})
.await .await
{ {
Ok(_) => ok(()), Ok(_) => ok(()),
@ -340,34 +346,26 @@ impl Core {
pub async fn auth( pub async fn auth(
&self, &self,
_username: &str, username: &str,
_password: &str, password: &str,
) -> ResultExt<AuthResponse, AppError, FatalError> { ) -> ResultExt<AuthResponse, AppError, FatalError> {
/*
let now = Utc::now(); let now = Utc::now();
let state = self.0.read().await; let state = self.0.read().await;
match state.db.user_by_username(username).await { match state.db.user_by_username(username).await {
Ok(Some(row)) Ok(Some(row)) if row.password == password => match row.state {
if (row.password == password) && row.enabled && row.password_expires.0 <= now => AccountState::Normal => match state.db.create_session(&row.id).await {
{
match state.db.create_session(&row.id).await {
Ok(session_id) => ok(AuthResponse::Success(session_id)), Ok(session_id) => ok(AuthResponse::Success(session_id)),
Err(err) => fatal(err), Err(err) => fatal(err),
} },
} AccountState::PasswordReset(exp) => match state.db.create_session(&row.id).await {
Ok(Some(row))
if (row.password == password) && row.enabled && row.password_expires.0 > now =>
{
match state.db.create_session(&row.id).await {
Ok(session_id) => ok(AuthResponse::Expired(session_id)), Ok(session_id) => ok(AuthResponse::Expired(session_id)),
Err(err) => fatal(err), Err(err) => fatal(err),
} },
} AccountState::Locked => error(AppError::AuthFailed),
},
Ok(_) => error(AppError::AuthFailed), Ok(_) => error(AppError::AuthFailed),
Err(err) => fatal(err), Err(err) => fatal(err),
} }
*/
unimplemented!();
} }
pub async fn session( pub async fn session(

View File

@ -120,7 +120,6 @@ impl DiskDb {
user.password, user.password,
user.admin, user.admin,
user.state, user.state,
// format!("{}", expiration.format("%Y-%m-%d %H:%M:%S")),
user.id.as_str(), user.id.as_str(),
)) ))
.unwrap(); .unwrap();
@ -150,38 +149,28 @@ impl DiskDb {
pub fn create_game( pub fn create_game(
&self, &self,
_gm: &UserId, gm: &UserId,
_game_type: &str, game_type: &str,
_name: &str, name: &str,
) -> Result<GameId, FatalError> { ) -> Result<GameId, FatalError> {
unimplemented!(); let game_id = GameId::new();
let mut stmt = self
.conn
.prepare("INSERT INTO games VALUES (?, ?, ?, ?)")
.map_err(|err| FatalError::ConstructQueryFailure(format!("{}", err)))?;
stmt.execute((game_id.as_str(), game_type, gm.as_str(), name))
.unwrap();
Ok(game_id)
} }
pub fn save_game(&self, _game: Game) -> Result<(), FatalError> { pub fn save_game(&self, game: Game) -> Result<(), FatalError> {
/* let mut stmt = self
match game_id { .conn
None => { .prepare("UPDATE games SET gm=? type_=? name=? WHERE id=?")
let game_id = GameId::new(); .map_err(|err| FatalError::ConstructQueryFailure(format!("{}", err)))?;
let mut stmt = self stmt.execute((game.gm.as_str(), game.type_, game.name, game.id.as_str()))
.conn .unwrap();
.prepare("INSERT INTO games VALUES (?, ?, ?, ?)") Ok(())
.map_err(|err| FatalError::ConstructQueryFailure(format!("{}", err)))?;
stmt.execute((game_id.as_str(), gm.as_str(), game_type, name))
.unwrap();
Ok(game_id)
}
Some(game_id) => {
let mut stmt = self
.conn
.prepare("UPDATE games SET gm=? game_type=? name=? WHERE uuid=?")
.map_err(|err| FatalError::ConstructQueryFailure(format!("{}", err)))?;
stmt.execute((gm.as_str(), game_type, name, game_id.as_str()))
.unwrap();
Ok(game_id)
}
}
*/
unimplemented!()
} }
pub fn games(&self) -> Result<Vec<Game>, FatalError> { pub fn games(&self) -> Result<Vec<Game>, FatalError> {
@ -193,7 +182,8 @@ impl DiskDb {
.query_map([], |row| { .query_map([], |row| {
Ok(Game { Ok(Game {
id: row.get(0).unwrap(), id: row.get(0).unwrap(),
gm: row.get(1).unwrap(), type_: row.get(1).unwrap(),
gm: row.get(2).unwrap(),
name: row.get(3).unwrap(), name: row.get(3).unwrap(),
players: vec![], players: vec![],
}) })
@ -380,7 +370,7 @@ pub async fn db_handler(db: DiskDb, requestor: Receiver<DatabaseRequest>) {
Ok(users) => { Ok(users) => {
tx.send(DatabaseResponse::Users(users)).await.unwrap(); tx.send(DatabaseResponse::Users(users)).await.unwrap();
} }
_ => unimplemented!(), _ => unimplemented!("request::Users"),
} }
} }
} }

View File

@ -94,18 +94,14 @@ where
} }
pub async fn check_password( pub async fn check_password(
_core: Core, core: Core,
req: Json<AuthRequest>, req: Json<AuthRequest>,
) -> ResultExt<AuthResponse, AppError, FatalError> { ) -> ResultExt<AuthResponse, AppError, FatalError> {
let Json(AuthRequest { let Json(AuthRequest {
username: _, username,
password: _, password,
}) = req; }) = req;
unimplemented!() core.auth(&username, &password).await
/*
match core.auth(&username, &password).await {
}
*/
} }
pub async fn get_user( pub async fn get_user(

View File

@ -140,6 +140,7 @@ pub struct Player {
#[typeshare] #[typeshare]
pub struct Game { pub struct Game {
pub id: GameId, pub id: GameId,
pub type_: String,
pub name: String, pub name: String,
pub gm: UserId, pub gm: UserId,
pub players: Vec<UserId>, pub players: Vec<UserId>,
@ -173,8 +174,8 @@ pub struct UserOverview {
#[typeshare] #[typeshare]
pub struct GameOverview { pub struct GameOverview {
pub id: GameId, pub id: GameId,
pub game_type: String, pub type_: String,
pub game_name: String, pub name: String,
pub gm: UserId, pub gm: UserId,
pub players: Vec<UserId>, pub players: Vec<UserId>,
} }