Skip to content

Commit

Permalink
prep for rule changes
Browse files Browse the repository at this point in the history
there should be a better way to model the logic proposed by checking against the polar ruleset
work will go in a diff branch
  • Loading branch information
fairingrey committed Feb 16, 2022
1 parent c923879 commit e2f20c0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/handlers/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub(crate) struct LoginForm {
message = "Can only contain letters, numbers, dashes (-), periods (.), and underscores (_)"
)
)]
name: String,
pub(crate) name: String,
#[validate(
length(
min = 8,
Expand All @@ -48,7 +48,7 @@ pub(crate) struct LoginForm {
message = "Must be alphanumeric and contain at least one number."
)
)]
password: String,
pub(crate) password: String,
}

/// Handler for `POST /login`
Expand Down
16 changes: 8 additions & 8 deletions src/handlers/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ pub(crate) struct CreateUserForm {
message = "Can only contain letters, numbers, dashes (-), periods (.), and underscores (_)"
)
)]
name: String,
pub(crate) name: String,
/// The provided email.
#[validate(email(message = "Must be a valid email address."))]
email: String,
pub(crate) email: String,
/// The provided password.
#[validate(
length(
Expand All @@ -56,7 +56,7 @@ pub(crate) struct CreateUserForm {
message = "Must be alphanumeric and contain at least one number."
)
)]
password: String,
pub(crate) password: String,
}

/// The form input for `PUT /user/:id`
Expand All @@ -73,10 +73,10 @@ pub(crate) struct UpdateUserForm {
message = "Can only contain letters, numbers, dashes (-), periods (.), and underscores (_)"
)
)]
name: Option<String>,
pub(crate) name: Option<String>,
#[validate(email(message = "Must be a valid email address."))]
email: Option<String>,
role: Option<Role>,
pub(crate) email: Option<String>,
pub(crate) role: Option<Role>,
#[validate(
length(
min = 8,
Expand All @@ -88,7 +88,7 @@ pub(crate) struct UpdateUserForm {
message = "Must be alphanumeric and contain at least one number."
)
)]
password: Option<String>,
pub(crate) password: Option<String>,
}

/// The form input for `PUT /user/verify`
Expand All @@ -98,7 +98,7 @@ pub(crate) struct VerifyForm {
equal = 32,
message = "Length of this key must be exactly 32 characters."
))]
key: String,
pub(crate) key: String,
}

/// The response output for `GET /user/:name`
Expand Down
35 changes: 34 additions & 1 deletion src/models/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use sqlx::types::chrono::{DateTime, Utc};
use std::fmt;
use uuid::Uuid;

use crate::impl_redis_rv;
use crate::{handlers::UpdateUserForm, impl_redis_rv};

/// User roles
#[derive(
Expand Down Expand Up @@ -33,14 +33,47 @@ pub(crate) struct User {
pub(crate) id: Uuid,
pub(crate) created_at: DateTime<Utc>,
pub(crate) updated_at: DateTime<Utc>,
#[polar(attribute)]
pub(crate) name: String,
#[polar(attribute)]
pub(crate) email: String,
#[polar(attribute)]
pub(crate) role: Role,
/// The password in hashed PHC form, as represented in the database
#[polar(attribute)]
pub(crate) password: String,
#[polar(attribute)]
pub(crate) verified: bool,
}

/// User model but all fields are options
#[derive(Debug, Clone, oso::PolarClass, Serialize, Deserialize)]
pub(crate) struct UserOptional {
#[polar(attribute)]
pub(crate) name: Option<String>,
pub(crate) created_at: Option<DateTime<Utc>>,
pub(crate) updated_at: Option<DateTime<Utc>>,
#[polar(attribute)]
pub(crate) email: Option<String>,
#[polar(attribute)]
pub(crate) role: Option<Role>,
#[polar(attribute)]
pub(crate) password: Option<String>,
pub(crate) verified: Option<bool>,
}

impl From<UpdateUserForm> for UserOptional {
fn from(form: UpdateUserForm) -> Self {
Self {
name: form.name,
email: form.email,
created_at: None,
updated_at: None,
role: form.role,
password: form.password,
verified: None,
}
}
}

impl_redis_rv!(User, Role);

0 comments on commit e2f20c0

Please sign in to comment.