Add tokens routes
This commit is contained in:
		
							
								
								
									
										108
									
								
								moneymgr_backend/src/services/tokens_service.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										108
									
								
								moneymgr_backend/src/services/tokens_service.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,108 @@
 | 
			
		||||
use diesel::prelude::*;
 | 
			
		||||
 | 
			
		||||
use crate::connections::db_connection::db;
 | 
			
		||||
use crate::constants;
 | 
			
		||||
use crate::models::tokens::{NewToken, Token, TokenID};
 | 
			
		||||
use crate::models::users::UserID;
 | 
			
		||||
use crate::schema::token;
 | 
			
		||||
use crate::utils::rand_utils::rand_string;
 | 
			
		||||
use crate::utils::time_utils::time;
 | 
			
		||||
 | 
			
		||||
pub struct NewTokenInfo {
 | 
			
		||||
    pub user_id: UserID,
 | 
			
		||||
    pub name: String,
 | 
			
		||||
    pub max_inactivity: u32,
 | 
			
		||||
    pub ip_net: Option<ipnet::IpNet>,
 | 
			
		||||
    pub read_only: bool,
 | 
			
		||||
    pub right_account: bool,
 | 
			
		||||
    pub right_movement: bool,
 | 
			
		||||
    pub right_inbox: bool,
 | 
			
		||||
    pub right_attachment: bool,
 | 
			
		||||
    pub right_auth: bool,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Create a new token
 | 
			
		||||
pub async fn create(new_token: NewTokenInfo) -> anyhow::Result<Token> {
 | 
			
		||||
    let ip_net = new_token.ip_net.map(|i| i.to_string());
 | 
			
		||||
    let token = rand_string(constants::TOKENS_LEN);
 | 
			
		||||
    let t = NewToken {
 | 
			
		||||
        name: &new_token.name,
 | 
			
		||||
        user_id: new_token.user_id.0,
 | 
			
		||||
        time_create: time() as i64,
 | 
			
		||||
        time_used: time() as i64,
 | 
			
		||||
        max_inactivity: new_token.max_inactivity as i32,
 | 
			
		||||
        read_only: new_token.read_only,
 | 
			
		||||
        ip_net: ip_net.as_deref(),
 | 
			
		||||
        token_value: &token,
 | 
			
		||||
        right_auth: new_token.right_auth,
 | 
			
		||||
        right_account: new_token.right_account,
 | 
			
		||||
        right_movement: new_token.right_movement,
 | 
			
		||||
        right_inbox: new_token.right_inbox,
 | 
			
		||||
        right_attachment: new_token.right_attachment,
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    let res = diesel::insert_into(token::table)
 | 
			
		||||
        .values(&t)
 | 
			
		||||
        .get_result(&mut db()?)?;
 | 
			
		||||
 | 
			
		||||
    Ok(res)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Get a single token by its id
 | 
			
		||||
pub async fn get_by_id(token_id: TokenID) -> anyhow::Result<Token> {
 | 
			
		||||
    Ok(token::table
 | 
			
		||||
        .filter(token::dsl::id.eq(token_id.0))
 | 
			
		||||
        .get_result(&mut db()?)?)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Get a single token by its name
 | 
			
		||||
pub fn get_by_name(name: &str) -> anyhow::Result<Token> {
 | 
			
		||||
    Ok(token::table
 | 
			
		||||
        .filter(token::dsl::name.eq(name))
 | 
			
		||||
        .get_result(&mut db()?)?)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Get a single token by its value
 | 
			
		||||
pub async fn get_by_value(value: &str) -> anyhow::Result<Token> {
 | 
			
		||||
    Ok(token::table
 | 
			
		||||
        .filter(token::dsl::token_value.eq(value))
 | 
			
		||||
        .get_result(&mut db()?)?)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Get the token of a user
 | 
			
		||||
pub async fn get_list_user(id: UserID) -> anyhow::Result<Vec<Token>> {
 | 
			
		||||
    Ok(token::table
 | 
			
		||||
        .filter(token::dsl::user_id.eq(id.0))
 | 
			
		||||
        .get_results(&mut db()?)?)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Update last used value of a token
 | 
			
		||||
pub async fn update_time_used(token: &Token) -> anyhow::Result<()> {
 | 
			
		||||
    diesel::update(token::dsl::token.filter(token::dsl::id.eq(token.id().0)))
 | 
			
		||||
        .set(token::dsl::time_used.eq(time() as i64))
 | 
			
		||||
        .execute(&mut db()?)?;
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Delete the token of a user
 | 
			
		||||
pub async fn delete(user_id: UserID, token_id: TokenID) -> anyhow::Result<()> {
 | 
			
		||||
    diesel::delete(
 | 
			
		||||
        token::dsl::token.filter(
 | 
			
		||||
            token::dsl::id
 | 
			
		||||
                .eq(token_id.0)
 | 
			
		||||
                .and(token::dsl::user_id.eq(user_id.0)),
 | 
			
		||||
        ),
 | 
			
		||||
    )
 | 
			
		||||
    .execute(&mut db()?)?;
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Remove outdated token
 | 
			
		||||
pub async fn cleanup() -> anyhow::Result<()> {
 | 
			
		||||
    let query = format!(
 | 
			
		||||
        "DELETE from token where last_used + max_inactivity < {};",
 | 
			
		||||
        time()
 | 
			
		||||
    );
 | 
			
		||||
    diesel::sql_query(query).execute(&mut db()?)?;
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user