Merge pull request 'Fix users not cached on creation' (#87) from ldap-user-cache-on-creation into main
All checks were successful
continuous-integration/drone/push Build is passing

Reviewed-on: mentorenwahl/mentorenwahl#87
This commit is contained in:
Dominic Grimm 2022-03-07 12:05:28 +00:00
commit 5b32803880
5 changed files with 49 additions and 16 deletions

View file

@ -48,7 +48,12 @@ module Backend
rescue LDAP::Client::AuthError
true
end
user = Db::User.create!(username: input.username, role: input.role.to_s)
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
)
User.new(user)
end

View file

@ -111,11 +111,13 @@ module Backend
class UserCreateInput < GraphQL::BaseInputObject
getter username
getter role
getter admin
@[GraphQL::Field]
def initialize(
@username : String,
@role : UserRole
@role : UserRole,
@admin : Bool = false
)
end
end

View file

@ -0,0 +1,38 @@
# Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes.
# Copyright (C) 2022 Dominic Grimm
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
module Backend
module Worker
module Jobs
# Caches user data in redis cache
class CacheLdapUserJob < Mosquito::QueuedJob
params id : Int64
# :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)
else
Redis::CLIENT.del(key)
end
end
end
end
end
end

View file

@ -27,20 +27,7 @@ module Backend
.map(&.as(String).split(":")[2].to_i64)
.concat(Db::User.all.map(&.id.not_nil!))
.uniq!
.each do |id|
spawn do
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)
else
Redis::CLIENT.del(key)
end
end
end
Fiber.yield
.each { |id| CacheLdapUserJob.new(id).enqueue }
end
end
end

View file

@ -84,6 +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
puts "Done!"