use laterite_core::Event;
use serde::{Deserialize, Serialize};
use crate::service::RequestContext;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct SignedIn {
pub user_id: i64,
pub username: String,
pub ip_address: Option<String>,
pub user_agent: Option<String>,
}
impl SignedIn {
pub fn new(user_id: i64, username: impl Into<String>, ctx: &RequestContext) -> Self {
Self {
user_id,
username: username.into(),
ip_address: ctx.ip_address.clone(),
user_agent: ctx.user_agent.clone(),
}
}
}
impl Event for SignedIn {
const NAME: &'static str = "auth.signed_in";
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct SignInFailed {
pub user_id: Option<i64>,
pub username: String,
pub ip_address: Option<String>,
pub user_agent: Option<String>,
}
impl SignInFailed {
pub fn new(user_id: Option<i64>, username: impl Into<String>, ctx: &RequestContext) -> Self {
Self {
user_id,
username: username.into(),
ip_address: ctx.ip_address.clone(),
user_agent: ctx.user_agent.clone(),
}
}
}
impl Event for SignInFailed {
const NAME: &'static str = "auth.sign_in_failed";
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct LockedOut {
pub user_id: Option<i64>,
pub username: String,
pub ip_address: Option<String>,
pub user_agent: Option<String>,
}
impl LockedOut {
pub fn new(user_id: Option<i64>, username: impl Into<String>, ctx: &RequestContext) -> Self {
Self {
user_id,
username: username.into(),
ip_address: ctx.ip_address.clone(),
user_agent: ctx.user_agent.clone(),
}
}
}
impl Event for LockedOut {
const NAME: &'static str = "auth.locked_out";
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct SignedOut {
pub user_id: i64,
}
impl SignedOut {
pub fn new(user_id: i64) -> Self {
Self { user_id }
}
}
impl Event for SignedOut {
const NAME: &'static str = "auth.signed_out";
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct PasswordChanged {
pub user_id: i64,
pub changed_by: Option<i64>,
}
impl PasswordChanged {
pub fn new(user_id: i64, changed_by: Option<i64>) -> Self {
Self {
user_id,
changed_by,
}
}
pub fn by_owner(&self) -> bool {
self.changed_by == Some(self.user_id)
}
}
impl Event for PasswordChanged {
const NAME: &'static str = "auth.password_changed";
}