mentorenwahl/docker/backend/src/backend/db/user.cr
Dominic Grimm e55127f0bf
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
Added LDAP login support
2022-02-06 16:42:08 +01:00

51 lines
1.2 KiB
Crystal

module Backend
module Db
class User < Granite::Base
table users
has_one :teacher
has_one :student
column id : Int64, primary: true
column username : String
column role : String
column admin : Bool = false
def firstname : String
Ldap.user(Ldap.uid(@username.not_nil!)).first["givenName"].first
end
def lastname : String
Ldap.user(Ldap.uid(@username.not_nil!)).first["sn"].first
end
def name : String
Ldap.user(Ldap.uid(@username.not_nil!)).first["cn"].first
end
def email : String
Ldap.user(Ldap.uid(@username.not_nil!)).first["mail"].first
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.teacher.nil? && user.student.nil?
true
else
!!case UserRole.parse(user.role)
when .teacher?
user.teacher && user.student.nil?
when .student?
user.teacher.nil? && user.student
else
false
end
end
end
end
end
end