diff --git a/visions/server/migrations/01-initial-db/up.sql b/visions/server/migrations/01-initial-db/up.sql index c0ff1ae..679ab5d 100644 --- a/visions/server/migrations/01-initial-db/up.sql +++ b/visions/server/migrations/01-initial-db/up.sql @@ -3,8 +3,7 @@ CREATE TABLE users( name TEXT UNIQUE, password TEXT, admin BOOLEAN, - enabled BOOLEAN, - password_expires TEXT + state TEXT ); CREATE TABLE sessions( @@ -15,9 +14,9 @@ CREATE TABLE sessions( ); CREATE TABLE games( - uuid TEXT PRIMARY KEY, + id TEXT PRIMARY KEY, + type_ TEXT, gm TEXT, - game_type TEXT, name TEXT, FOREIGN KEY(gm) REFERENCES users(uuid) @@ -40,4 +39,3 @@ CREATE TABLE roles( FOREIGN KEY(game_id) REFERENCES games(uuid) ); -INSERT INTO users VALUES ('admin', 'admin', '', true, true, datetime('now')); diff --git a/visions/server/src/core.rs b/visions/server/src/core.rs index 64e3c4a..500f20a 100644 --- a/visions/server/src/core.rs +++ b/visions/server/src/core.rs @@ -192,14 +192,16 @@ impl Core { let games = self.0.read().await.db.games().await; match games { // Ok(games) => ok(games.into_iter().map(|g| Game::from(g)).collect()), - Ok(games) => ok(games.into_iter().map(|game| - GameOverview{ - id: game.id, - game_type: "".to_owned(), - game_name: game.name, - gm: game.gm, - players: game.players, - }).collect::>()), + Ok(games) => ok(games + .into_iter() + .map(|game| GameOverview { + id: game.id, + type_: "".to_owned(), + name: game.name, + gm: game.gm, + players: game.players, + }) + .collect::>()), Err(err) => fatal(err), } } @@ -330,7 +332,11 @@ impl Core { }; match state .db - .save_user(User{ password, state: AccountState::Normal, ..user }) + .save_user(User { + password, + state: AccountState::Normal, + ..user + }) .await { Ok(_) => ok(()), @@ -340,34 +346,26 @@ impl Core { pub async fn auth( &self, - _username: &str, - _password: &str, + username: &str, + password: &str, ) -> ResultExt { - /* let now = Utc::now(); let state = self.0.read().await; match state.db.user_by_username(username).await { - Ok(Some(row)) - if (row.password == password) && row.enabled && row.password_expires.0 <= now => - { - match state.db.create_session(&row.id).await { + Ok(Some(row)) if row.password == password => match row.state { + AccountState::Normal => match state.db.create_session(&row.id).await { Ok(session_id) => ok(AuthResponse::Success(session_id)), Err(err) => fatal(err), - } - } - Ok(Some(row)) - if (row.password == password) && row.enabled && row.password_expires.0 > now => - { - match state.db.create_session(&row.id).await { + }, + AccountState::PasswordReset(exp) => match state.db.create_session(&row.id).await { Ok(session_id) => ok(AuthResponse::Expired(session_id)), Err(err) => fatal(err), - } - } + }, + AccountState::Locked => error(AppError::AuthFailed), + }, Ok(_) => error(AppError::AuthFailed), Err(err) => fatal(err), } - */ - unimplemented!(); } pub async fn session( diff --git a/visions/server/src/database/disk_db.rs b/visions/server/src/database/disk_db.rs index d81c2b0..da26ef7 100644 --- a/visions/server/src/database/disk_db.rs +++ b/visions/server/src/database/disk_db.rs @@ -120,7 +120,6 @@ impl DiskDb { user.password, user.admin, user.state, - // format!("{}", expiration.format("%Y-%m-%d %H:%M:%S")), user.id.as_str(), )) .unwrap(); @@ -150,38 +149,28 @@ impl DiskDb { pub fn create_game( &self, - _gm: &UserId, - _game_type: &str, - _name: &str, + gm: &UserId, + game_type: &str, + name: &str, ) -> Result { - 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> { - /* - match game_id { - None => { - 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(), 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 save_game(&self, game: Game) -> Result<(), FatalError> { + let mut stmt = self + .conn + .prepare("UPDATE games SET gm=? type_=? name=? WHERE id=?") + .map_err(|err| FatalError::ConstructQueryFailure(format!("{}", err)))?; + stmt.execute((game.gm.as_str(), game.type_, game.name, game.id.as_str())) + .unwrap(); + Ok(()) } pub fn games(&self) -> Result, FatalError> { @@ -193,7 +182,8 @@ impl DiskDb { .query_map([], |row| { Ok(Game { 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(), players: vec![], }) @@ -380,7 +370,7 @@ pub async fn db_handler(db: DiskDb, requestor: Receiver) { Ok(users) => { tx.send(DatabaseResponse::Users(users)).await.unwrap(); } - _ => unimplemented!(), + _ => unimplemented!("request::Users"), } } } diff --git a/visions/server/src/handlers/user_management.rs b/visions/server/src/handlers/user_management.rs index a7c02b8..4123f1c 100644 --- a/visions/server/src/handlers/user_management.rs +++ b/visions/server/src/handlers/user_management.rs @@ -94,18 +94,14 @@ where } pub async fn check_password( - _core: Core, + core: Core, req: Json, ) -> ResultExt { let Json(AuthRequest { - username: _, - password: _, + username, + password, }) = req; - unimplemented!() - /* - match core.auth(&username, &password).await { - } - */ + core.auth(&username, &password).await } pub async fn get_user( diff --git a/visions/server/src/types.rs b/visions/server/src/types.rs index d423196..5153e9c 100644 --- a/visions/server/src/types.rs +++ b/visions/server/src/types.rs @@ -140,6 +140,7 @@ pub struct Player { #[typeshare] pub struct Game { pub id: GameId, + pub type_: String, pub name: String, pub gm: UserId, pub players: Vec, @@ -173,8 +174,8 @@ pub struct UserOverview { #[typeshare] pub struct GameOverview { pub id: GameId, - pub game_type: String, - pub game_name: String, + pub type_: String, + pub name: String, pub gm: UserId, pub players: Vec, }