mentorenwahl/docker/backend/src/backend/db/user.cr
Dominic Grimm 5bc10f8aaf
Some checks failed
continuous-integration/drone/push Build is failing
Added worker
2022-01-23 09:12:57 +01:00

47 lines
1.2 KiB
Crystal

require "CrystalEmail"
module Backend
module Db
class User < Granite::Base
table users
has_one :admin
has_one :teacher
has_one :student
column id : Int64, primary: true
column firstname : String
column lastname : String
column email : String
column password : String
column role : String
column blocked : Bool = false
validate :email, "needs to be an email address" do |user|
CrystalEmail::Rfc5322::Public.validates?(user.email)
end
validate :role, "needs to be a valid role" do |user|
UserRole.parse?(user.role).in?(UserRole.values)
end
validate :role, "user external needs to be a valid role" do |user|
if user.admin.nil? && user.teacher.nil? && user.student.nil?
true
else
!!case UserRole.parse(user.role)
when UserRole::Admin
user.admin && user.teacher.nil? && user.student.nil?
when UserRole::Teacher
user.admin.nil? && user.teacher && user.student.nil?
when UserRole::Student
user.admin.nil? && user.teacher.nil? && user.student
else
false
end
end
end
end
end
end