authd/src/user.cr

75 lines
1.5 KiB
Crystal
Raw Normal View History

2019-05-29 15:30:23 +02:00
require "json"
2020-01-22 14:43:58 +01:00
require "uuid"
require "./token.cr"
class AuthD::User
include JSON::Serializable
enum PermissionLevel
None
Read
Edit
Admin
def to_json(o)
to_s.downcase.to_json o
end
end
2020-01-22 01:55:57 +01:00
class Contact
include JSON::Serializable
2020-01-22 14:43:58 +01:00
# the activation key is removed once the user is validated
property activation_key : String?
property email : String?
property phone : String?
2020-01-22 01:55:57 +01:00
def initialize(@email = nil, @phone = nil)
2020-01-22 14:43:58 +01:00
@activation_key = UUID.random.to_s
2020-01-22 01:55:57 +01:00
end
end
# Public.
property login : String
property uid : Int32
property profile : JSON::Any?
# Private.
property contact : Contact
property password_hash : String
property password_renew_key : String?
2020-01-22 01:55:57 +01:00
# service => resource => permission level
property permissions : Hash(String, Hash(String, PermissionLevel))
property configuration : Hash(String, Hash(String, JSON::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)
2020-01-22 01:55:57 +01:00
@contact = Contact.new
@permissions = Hash(String, Hash(String, PermissionLevel)).new
@configuration = Hash(String, Hash(String, JSON::Any)).new
end
class Public
include JSON::Serializable
property login : String
property uid : Int32
property profile : JSON::Any?
def initialize(@uid, @login, @profile)
end
end
def to_public : Public
Public.new @uid, @login, @profile
end
end