Interim update
This commit is contained in:
29
Server/app/resources/users/Users.jl
Normal file
29
Server/app/resources/users/Users.jl
Normal file
@@ -0,0 +1,29 @@
|
||||
module Users
|
||||
|
||||
using SearchLight, SearchLight.Validation, Server.UsersValidator
|
||||
using SHA
|
||||
using Random
|
||||
|
||||
export User
|
||||
|
||||
Base.@kwdef mutable struct User <: AbstractModel
|
||||
### FIELDS
|
||||
id::DbId = DbId()
|
||||
email::String = ""
|
||||
password::String = ""
|
||||
google_id::String = ""
|
||||
confirmation_code::String = ""
|
||||
verified::Bool = false
|
||||
end
|
||||
|
||||
Validation.validator(u::Type{User}) = ModelValidator([
|
||||
ValidationRule(:email, UsersValidator.not_empty),
|
||||
ValidationRule(:email, UsersValidator.unique),
|
||||
ValidationRule(:password, UsersValidator.not_empty)
|
||||
])
|
||||
|
||||
function hash_password(password::String)
|
||||
sha256(password) |> bytes2hex
|
||||
end
|
||||
|
||||
end
|
21
Server/app/resources/users/UsersValidator.jl
Normal file
21
Server/app/resources/users/UsersValidator.jl
Normal file
@@ -0,0 +1,21 @@
|
||||
module UsersValidator
|
||||
|
||||
using SearchLight, SearchLight.Validation, SearchLight.QueryBuilder
|
||||
|
||||
function not_empty(field::Symbol, m::T, args::Vararg{Any})::ValidationResult where {T<:AbstractModel}
|
||||
isempty(getfield(m, field)) && return ValidationResult(invalid, :not_empty, "should not be empty")
|
||||
|
||||
ValidationResult(valid)
|
||||
end
|
||||
|
||||
function unique(field::Symbol, m::T, args::Vararg{Any})::ValidationResult where {T<:AbstractModel}
|
||||
ispersisted(m) && return ValidationResult(valid) # don't validate updates
|
||||
|
||||
if SearchLight.count(typeof(m), where("$field = ?", getfield(m, field))) > 0
|
||||
return ValidationResult(invalid, :unique, "is already used")
|
||||
end
|
||||
|
||||
ValidationResult(valid)
|
||||
end
|
||||
|
||||
end
|
Reference in New Issue
Block a user