fix: generate i64 session ids instead of u64

image-uploads
Jef Roosens 2025-01-12 08:48:24 +01:00
parent 48b3191117
commit 7cf0e44b65
No known key found for this signature in database
GPG Key ID: 21FD3D77D56BAF49
9 changed files with 18 additions and 15 deletions

1
Cargo.lock generated
View File

@ -292,6 +292,7 @@ dependencies = [
"chrono", "chrono",
"r2d2", "r2d2",
"r2d2_sqlite", "r2d2_sqlite",
"rand",
"rusqlite", "rusqlite",
"serde", "serde",
"tera", "tera",

View File

@ -14,6 +14,7 @@ axum-extra = { version = "0.10.0", features = ["cookie"] }
chrono = { version = "0.4.39", features = ["serde"] } chrono = { version = "0.4.39", features = ["serde"] }
r2d2 = "0.8.10" r2d2 = "0.8.10"
r2d2_sqlite = "0.25.0" r2d2_sqlite = "0.25.0"
rand = "0.8.5"
# this dependency is needed soly because the r2d2_sqlite crate doesn't export # this dependency is needed soly because the r2d2_sqlite crate doesn't export
# the 'chrono' feature flag # the 'chrono' feature flag
rusqlite = { version = "0.32.1", features = ["chrono", "bundled"] } rusqlite = { version = "0.32.1", features = ["chrono", "bundled"] }

View File

@ -5,14 +5,14 @@ use super::{DbError, DbPool};
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct Comment { pub struct Comment {
id: i32, id: i64,
plant_id: i32, plant_id: i64,
comment: String, comment: String,
} }
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct NewComment { pub struct NewComment {
plant_id: i32, plant_id: i64,
comment: String, comment: String,
} }

View File

@ -64,8 +64,8 @@ impl FromSql for EventType {
#[derive(Serialize)] #[derive(Serialize)]
pub struct Event { pub struct Event {
id: i32, id: i64,
plant_id: i32, plant_id: i64,
event_type: EventType, event_type: EventType,
date: NaiveDate, date: NaiveDate,
description: String, description: String,
@ -85,7 +85,7 @@ impl Event {
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct NewEvent { pub struct NewEvent {
plant_id: i32, plant_id: i64,
event_type: EventType, event_type: EventType,
date: NaiveDate, date: NaiveDate,
description: String, description: String,

View File

@ -5,7 +5,7 @@ use super::{Comment, DbError, DbPool, Event};
#[derive(Serialize)] #[derive(Serialize)]
pub struct Plant { pub struct Plant {
id: i32, id: i64,
name: String, name: String,
species: String, species: String,
description: String, description: String,
@ -37,7 +37,7 @@ impl Plant {
Ok(plants?) Ok(plants?)
} }
pub fn by_id(pool: &DbPool, id: i32) -> Result<Option<Self>, DbError> { pub fn by_id(pool: &DbPool, id: i64) -> Result<Option<Self>, DbError> {
let conn = pool.get()?; let conn = pool.get()?;
let mut stmt = conn.prepare("select * from plants where id = $1")?; let mut stmt = conn.prepare("select * from plants where id = $1")?;

View File

@ -1,10 +1,11 @@
use argon2::password_hash::rand_core::{OsRng, RngCore}; use argon2::password_hash::rand_core::{OsRng, RngCore};
use rand::Rng;
use rusqlite::Row; use rusqlite::Row;
use super::{DbError, DbPool, User}; use super::{DbError, DbPool, User};
pub struct Session { pub struct Session {
pub id: u64, pub id: i64,
pub user_id: i32, pub user_id: i32,
} }
@ -16,7 +17,7 @@ impl Session {
}) })
} }
pub fn user_from_id(pool: &DbPool, id: u64) -> Result<Option<super::User>, DbError> { pub fn user_from_id(pool: &DbPool, id: i64) -> Result<Option<super::User>, DbError> {
let conn = pool.get()?; let conn = pool.get()?;
let mut stmt = conn.prepare("select users.* from sessions inner join users on sessions.user_id = users.id where sessions.id = $1")?; let mut stmt = conn.prepare("select users.* from sessions inner join users on sessions.user_id = users.id where sessions.id = $1")?;
@ -27,8 +28,8 @@ impl Session {
} }
} }
pub fn new_for_user(pool: &DbPool, user_id: i32) -> Result<Self, DbError> { pub fn new_for_user(pool: &DbPool, user_id: i64) -> Result<Self, DbError> {
let id: u64 = OsRng.next_u64(); let id: i64 = rand::thread_rng().gen();
let conn = pool.get()?; let conn = pool.get()?;
let mut stmt = let mut stmt =

View File

@ -9,7 +9,7 @@ use super::{DbError, DbPool};
#[derive(Serialize, Deserialize, Clone)] #[derive(Serialize, Deserialize, Clone)]
pub struct User { pub struct User {
pub id: i32, pub id: i64,
pub username: String, pub username: String,
pub password_hash: String, pub password_hash: String,
pub admin: bool, pub admin: bool,

View File

@ -22,7 +22,7 @@ pub fn logged_in_user(pool: &DbPool, headers: &HeaderMap) -> Result<Option<User>
if let Some(session_id) = jar if let Some(session_id) = jar
.get("session_id") .get("session_id")
.and_then(|c| c.value().parse::<u64>().ok()) .and_then(|c| c.value().parse::<i64>().ok())
{ {
Session::user_from_id(pool, session_id) Session::user_from_id(pool, session_id)
} else { } else {

View File

@ -20,7 +20,7 @@ pub fn app() -> axum::Router<crate::Context> {
async fn get_plant_page( async fn get_plant_page(
State(ctx): State<crate::Context>, State(ctx): State<crate::Context>,
headers: HeaderMap, headers: HeaderMap,
Path(plant_id): Path<i32>, Path(plant_id): Path<i64>,
) -> super::Result<Html<String>> { ) -> super::Result<Html<String>> {
let res = tokio::task::spawn_blocking(move || { let res = tokio::task::spawn_blocking(move || {
let plant = Plant::by_id(&ctx.pool, plant_id)?; let plant = Plant::by_id(&ctx.pool, plant_id)?;