Revamed auth system

This commit is contained in:
2025-02-07 15:59:50 +01:00
parent 07493b83a5
commit 31aa9dc066
13 changed files with 118 additions and 59 deletions

View File

@@ -13,21 +13,21 @@ pub struct Member {
pub name: Name,
pub registration_token: Option<String>,
pub diploma: Option<String>,
pub swim_groups: SwimGroups,
pub groups: Groups,
pub roles: Roles,
}
bitflags! {
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct Groups: u64 {
const NONE = 1 << 0;
pub struct Roles: u64 {
const MEMBER = 1 << 0;
const KADER = 1 << 1;
const ZWEMZAKEN = 1 << 2;
const WEDSTRIJDEN = 1 << 3;
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct SwimGroups: u64 {
pub struct Groups: u64 {
const NONE = 1 << 0;
const A1 = 1 << 1;
@@ -76,15 +76,15 @@ bitflags! {
}
}
impl From<i64> for SwimGroups {
impl From<i64> for Groups {
fn from(value: i64) -> Self {
Self::from_bits(value as u64).unwrap_or(SwimGroups::NONE)
Self::from_bits(value as u64).unwrap_or(Groups::empty())
}
}
impl From<i64> for Groups {
impl From<i64> for Roles {
fn from(value: i64) -> Self {
Self::from_bits(value as u64).unwrap_or(Groups::NONE)
Self::from_bits(value as u64).unwrap_or(Roles::MEMBER)
}
}
@@ -99,8 +99,8 @@ impl From<DbMember> for Member {
},
registration_token: value.registration_token,
diploma: value.diploma,
swim_groups: value.swim_groups,
groups: value.groups,
roles: value.roles,
}
}
}
@@ -113,8 +113,8 @@ impl From<Member> for DbMember {
full_name: value.name.full,
registration_token: None,
diploma: value.diploma,
swim_groups: value.swim_groups,
groups: value.groups,
roles: value.roles,
}
}
}

View File

@@ -2,6 +2,7 @@ use chrono::{DateTime, Duration, Utc};
use crate::auth::generate_session_token;
#[derive(Debug)]
pub struct Session {
pub session_id: uuid::Uuid,
pub user_id: uuid::Uuid,
@@ -13,7 +14,7 @@ pub struct Session {
impl Session {
pub fn new(user_id: uuid::Uuid) -> Self {
let session_id = uuid::Uuid::new_v4();
let token = generate_session_token();
let token = format!("ses_{}", generate_session_token());
let created_at = Utc::now();
let expires_at = Utc::now() + Duration::days(7);

View File

@@ -1,5 +1,19 @@
use serde::Serialize;
#[derive(Serialize)]
pub struct User {
pub id: uuid::Uuid,
pub email: String,
pub admin: bool,
}
use crate::database::model::User as DbUser;
impl From<DbUser> for User {
fn from(db_user: DbUser) -> Self {
Self {
id: db_user.user_id,
email: db_user.email,
admin: db_user.admin,
}
}
}