From e2f20c02722804d840642f2bcbe09459d4bd3616 Mon Sep 17 00:00:00 2001 From: Allen Date: Tue, 15 Feb 2022 18:46:38 -0800 Subject: [PATCH] prep for rule changes there should be a better way to model the logic proposed by checking against the polar ruleset work will go in a diff branch --- src/handlers/login.rs | 4 ++-- src/handlers/user.rs | 16 ++++++++-------- src/models/user.rs | 35 ++++++++++++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/handlers/login.rs b/src/handlers/login.rs index a58a9d2..d3258e8 100644 --- a/src/handlers/login.rs +++ b/src/handlers/login.rs @@ -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, @@ -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` diff --git a/src/handlers/user.rs b/src/handlers/user.rs index 337076c..459cfb3 100644 --- a/src/handlers/user.rs +++ b/src/handlers/user.rs @@ -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( @@ -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` @@ -73,10 +73,10 @@ pub(crate) struct UpdateUserForm { message = "Can only contain letters, numbers, dashes (-), periods (.), and underscores (_)" ) )] - name: Option, + pub(crate) name: Option, #[validate(email(message = "Must be a valid email address."))] - email: Option, - role: Option, + pub(crate) email: Option, + pub(crate) role: Option, #[validate( length( min = 8, @@ -88,7 +88,7 @@ pub(crate) struct UpdateUserForm { message = "Must be alphanumeric and contain at least one number." ) )] - password: Option, + pub(crate) password: Option, } /// The form input for `PUT /user/verify` @@ -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` diff --git a/src/models/user.rs b/src/models/user.rs index b1d7685..7da53e0 100644 --- a/src/models/user.rs +++ b/src/models/user.rs @@ -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( @@ -33,14 +33,47 @@ pub(crate) struct User { pub(crate) id: Uuid, pub(crate) created_at: DateTime, pub(crate) updated_at: DateTime, + #[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, + pub(crate) created_at: Option>, + pub(crate) updated_at: Option>, + #[polar(attribute)] + pub(crate) email: Option, + #[polar(attribute)] + pub(crate) role: Option, + #[polar(attribute)] + pub(crate) password: Option, + pub(crate) verified: Option, +} + +impl From 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);