user_info.rb 764 B

12345678910111213141516171819202122232425262728293031323334353637
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module UserInfo
  3. def self.current_user_id
  4. Thread.current[:user_id]
  5. end
  6. def self.current_user_id=(user_id)
  7. Thread.current[:user_id] = user_id
  8. Thread.current[:assets] = UserInfo::Assets.new(user_id)
  9. end
  10. def self.assets
  11. Thread.current[:assets]
  12. end
  13. def self.ensure_current_user_id
  14. if UserInfo.current_user_id.nil?
  15. UserInfo.current_user_id = 1
  16. reset_current_user_id = true
  17. end
  18. yield
  19. ensure
  20. UserInfo.current_user_id = nil if reset_current_user_id
  21. end
  22. def self.with_user_id(user_id)
  23. old_user_id = current_user_id
  24. self.current_user_id = user_id
  25. yield
  26. ensure
  27. self.current_user_id = old_user_id
  28. end
  29. end