add last_access column for user table
This commit is contained in:
		@@ -5,7 +5,8 @@ CREATE TABLE IF NOT EXISTS "user" (
 | 
				
			|||||||
	"is_cox" boolean NOT NULL DEFAULT FALSE,
 | 
						"is_cox" boolean NOT NULL DEFAULT FALSE,
 | 
				
			||||||
	"is_admin" boolean NOT NULL DEFAULT FALSE,
 | 
						"is_admin" boolean NOT NULL DEFAULT FALSE,
 | 
				
			||||||
	"is_guest" boolean NOT NULL DEFAULT TRUE,
 | 
						"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" (
 | 
					CREATE TABLE IF NOT EXISTS "trip_type" (
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,7 +5,7 @@ use rocket::{
 | 
				
			|||||||
    async_trait,
 | 
					    async_trait,
 | 
				
			||||||
    http::Status,
 | 
					    http::Status,
 | 
				
			||||||
    request::{self, FromRequest, Outcome},
 | 
					    request::{self, FromRequest, Outcome},
 | 
				
			||||||
    Request,
 | 
					    Request, State,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
use serde::{Deserialize, Serialize};
 | 
					use serde::{Deserialize, Serialize};
 | 
				
			||||||
use sqlx::{FromRow, SqlitePool};
 | 
					use sqlx::{FromRow, SqlitePool};
 | 
				
			||||||
@@ -149,6 +149,16 @@ ORDER BY name
 | 
				
			|||||||
            .to_string()
 | 
					            .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) {
 | 
					    pub async fn delete(&self, db: &SqlitePool) {
 | 
				
			||||||
        sqlx::query!("UPDATE user SET deleted=1 WHERE id=?", self.id)
 | 
					        sqlx::query!("UPDATE user SET deleted=1 WHERE id=?", self.id)
 | 
				
			||||||
            .execute(db)
 | 
					            .execute(db)
 | 
				
			||||||
@@ -163,8 +173,12 @@ impl<'r> FromRequest<'r> for User {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    async fn from_request(req: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
 | 
					    async fn from_request(req: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
 | 
				
			||||||
        match req.cookies().get_private("loggedin_user") {
 | 
					        match req.cookies().get_private("loggedin_user") {
 | 
				
			||||||
            Some(user) => match serde_json::from_str(user.value()) {
 | 
					            Some(user) => match serde_json::from_str::<User>(user.value()) {
 | 
				
			||||||
                Ok(user) => Outcome::Success(user),
 | 
					                Ok(user) => {
 | 
				
			||||||
 | 
					                    let db = req.rocket().state::<SqlitePool>().unwrap();
 | 
				
			||||||
 | 
					                    user.logged_in(db).await;
 | 
				
			||||||
 | 
					                    Outcome::Success(user)
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
                Err(_) => {
 | 
					                Err(_) => {
 | 
				
			||||||
                    Outcome::Failure((Status::Unauthorized, LoginError::DeserializationError))
 | 
					                    Outcome::Failure((Status::Unauthorized, LoginError::DeserializationError))
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user