add last_access column for user table

This commit is contained in:
philipp 2023-05-10 08:57:20 +02:00
parent dab396640b
commit 8c38a2faf6
2 changed files with 19 additions and 4 deletions

View File

@ -5,7 +5,8 @@ CREATE TABLE IF NOT EXISTS "user" (
"is_cox" boolean NOT NULL DEFAULT FALSE,
"is_admin" boolean NOT NULL DEFAULT FALSE,
"is_guest" boolean NOT NULL DEFAULT TRUE,
"deleted" boolean NOT NULL DEFAULT FALSE
"deleted" boolean NOT NULL DEFAULT FALSE,
"last_access" DATETIME
);
CREATE TABLE IF NOT EXISTS "trip_type" (

View File

@ -5,7 +5,7 @@ use rocket::{
async_trait,
http::Status,
request::{self, FromRequest, Outcome},
Request,
Request, State,
};
use serde::{Deserialize, Serialize};
use sqlx::{FromRow, SqlitePool};
@ -149,6 +149,16 @@ ORDER BY name
.to_string()
}
pub async fn logged_in(&self, db: &SqlitePool) {
sqlx::query!(
"UPDATE user SET last_access = CURRENT_TIMESTAMP where id = ?",
self.id
)
.execute(db)
.await
.unwrap(); //Okay, because we can only create a User of a valid id
}
pub async fn delete(&self, db: &SqlitePool) {
sqlx::query!("UPDATE user SET deleted=1 WHERE id=?", self.id)
.execute(db)
@ -163,8 +173,12 @@ impl<'r> FromRequest<'r> for User {
async fn from_request(req: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
match req.cookies().get_private("loggedin_user") {
Some(user) => match serde_json::from_str(user.value()) {
Ok(user) => Outcome::Success(user),
Some(user) => match serde_json::from_str::<User>(user.value()) {
Ok(user) => {
let db = req.rocket().state::<SqlitePool>().unwrap();
user.logged_in(db).await;
Outcome::Success(user)
}
Err(_) => {
Outcome::Failure((Status::Unauthorized, LoginError::DeserializationError))
}