Fixed jobs
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

This commit is contained in:
Dominic Grimm 2022-03-08 08:15:35 +01:00
parent 37a60a89ed
commit d235ca3620
No known key found for this signature in database
GPG key ID: 27C59510125F3C8A
9 changed files with 41 additions and 32 deletions

View file

@ -62,7 +62,7 @@ shards:
mosquito:
git: https://github.com/mosquito-cr/mosquito.git
version: 0.11.2
version: 1.0.0.rc1+git.commit.afd53dd241447b60ece9232b6c71669b192baaa4
openssl_ext:
git: https://github.com/spider-gazelle/openssl_ext.git

View file

@ -46,6 +46,7 @@ dependencies:
github: juanedi/micrate
mosquito:
github: mosquito-cr/mosquito
branch: master
quartz_mailer:
github: amberframework/quartz-mailer
kilt:

View file

@ -44,16 +44,12 @@ module Backend
context.admin!
raise "LDAP user does not exist" if check_ldap && begin
!Ldap.user(Ldap::Constructor.uid(input.username))
!Ldap::User.from_username(input.username)
rescue LDAP::Client::AuthError
true
end
user = Db::User.create!(username: input.username, role: input.role.to_s, admin: input.admin)
Redis::CLIENT.set(
"ldap:user:#{user.id.not_nil!}",
Ldap.user(Ldap::Constructor.uid(user.username)).to_json,
(Backend.config.ldap.cache_refresh_interval * 2).minutes.to_i
)
Worker::Jobs::CacheLdapUserJob.new(user.id.not_nil!.to_i).enqueue
User.new(user)
end

View file

@ -35,23 +35,6 @@ module Backend
LDAP::Client.new(TCPSocket.new(Backend.config.ldap.host, Backend.config.ldap.port))
end
# Queries the LDAP server for a user
#
# NOTE: Returns raw LDAP data
def raw_user(dn : String) : User::Raw
CLIENT.connection do |client|
client
.authenticate(Backend.config.ldap.bind_dn, Backend.config.ldap.bind_password)
.search(base: dn)
.first
end
end
# Queries the LDAP server for a user
def user(dn : String) : User
User.from_raw(raw_user(dn))
end
# Checks if credentials are valid
def authenticate?(dn : String, password : String) : Bool
!!CLIENT.connection(&.authenticate(dn, password))

View file

@ -52,6 +52,33 @@ module Backend
email: raw["mail"].first
)
end
# Creates user data from LDAP DN entry
def self.from_dn(dn : String) : self
from_raw(
CLIENT.connection do |client|
client
.authenticate(Backend.config.ldap.bind_dn, Backend.config.ldap.bind_password)
.search(base: dn)
.first
end
)
end
# Creates user data from LDAP username
def self.from_username(username : String) : self
from_dn(Ldap::Constructor.uid(username))
end
# Creates user data from DB entry
def self.from_db(user : Db::User) : self
from_username(user.username)
end
# Creates user data from DB entry index
def self.from_index(id : Int32) : self
from_db(Db::User.find!(id))
end
end
end
end

View file

@ -19,16 +19,18 @@ module Backend
module Jobs
# Caches user data in redis cache
class CacheLdapUserJob < Mosquito::QueuedJob
params id : Int64
params id : Int32
# :ditto:
def perform : Nil
key = "ldap:user:#{id}"
user = Db::User.find(id)
if user
ldap_user = Ldap.user(Ldap::Constructor.uid(user.username))
Redis::CLIENT.set(key, ldap_user.to_json, (Backend.config.ldap.cache_refresh_interval * 2).minutes.to_i)
log "Caching user ##{id}..."
ldap_user = Ldap::User.from_username(user.username)
Redis::CLIENT.set(key, ldap_user.to_json)
else
log "User ##{id} not found. Deleting cache..."
Redis::CLIENT.del(key)
end
end

View file

@ -24,8 +24,8 @@ module Backend
# :ditto:
def perform : Nil
Redis::CLIENT.keys("ldap:user:*")
.map(&.as(String).split(":")[2].to_i64)
.concat(Db::User.all.map(&.id.not_nil!))
.map(&.as(String).split(":")[2].to_i)
.concat(Db::User.all.map(&.id.not_nil!.to_i))
.uniq!
.each do |id|
spawn do

View file

@ -32,8 +32,8 @@ module Backend
fail
end
ldap_user = Ldap.user(Ldap::Constructor.uid(user.username))
log "Sending teacher registration email to #{ldap_user.email} (#{user.id})"
ldap_user = Ldap::User.from_username(user.username)
log "Sending teacher registration email to #{ldap_user.email} ##{user.id}"
Mailers::TeacherRegistrationMailer.new(ldap_user).deliver
channel.send(nil)

View file

@ -84,7 +84,7 @@ cli = Commander::Command.new do |cmd|
abort unless (gets(chomp: true) || "").strip.downcase == "y"
user = Backend::Db::User.create!(username: username, role: role.to_s, admin: opts.bool["admin"])
Backend::Worker::Jobs::CacheLdapUserJob.new(user.id.not_nil!).enqueue
Backend::Worker::Jobs::CacheLdapUserJob.new(user.id.not_nil!.to_i).enqueue
puts "Done!"