authd/src/user.cr

77 lines
1.6 KiB
Crystal

require "cbor"
require "uuid"
require "./token.cr"
class AuthD::User
include CBOR::Serializable
enum PermissionLevel
None
Read
Edit
Admin
def to_cbor(o)
to_s.downcase.to_cbor o
end
end
class Contact
include CBOR::Serializable
# the activation key is removed once the user is validated
property activation_key : String?
property email : String?
property phone : String?
def initialize(@email = nil, @phone = nil)
@activation_key = UUID.random.to_s
end
end
# Public.
property login : String
property uid : Int32
property profile : Hash(String, CBOR::Any)?
# Private.
property contact : Contact
property password_hash : String
property password_renew_key : String?
# service => resource => permission level
property permissions : Hash(String, Hash(String, PermissionLevel))
property configuration : Hash(String, Hash(String, CBOR::Any))
property date_last_connection : Time? = nil
property date_registration : Time? = nil
def to_token
Token.new @login, @uid
end
def initialize(@uid, @login, @password_hash)
@contact = Contact.new
@permissions = Hash(String, Hash(String, PermissionLevel)).new
@configuration = Hash(String, Hash(String, CBOR::Any)).new
end
class Public
include CBOR::Serializable
property login : String
property uid : Int32
property profile : Hash(String, CBOR::Any)?
property date_registration : Time?
def initialize(@uid, @login, @profile, @date_registration)
end
end
def to_public : Public
Public.new @uid, @login, @profile, @date_registration
end
end