user_info.rb 842 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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
  7. User.find_by(id: Thread.current[:user_id])
  8. end
  9. def self.current_user_id=(user_id)
  10. Thread.current[:user_id] = user_id
  11. Thread.current[:assets] = UserInfo::Assets.new(user_id)
  12. end
  13. def self.assets
  14. Thread.current[:assets]
  15. end
  16. def self.ensure_current_user_id
  17. if UserInfo.current_user_id.nil?
  18. UserInfo.current_user_id = 1
  19. reset_current_user_id = true
  20. end
  21. yield
  22. ensure
  23. UserInfo.current_user_id = nil if reset_current_user_id
  24. end
  25. def self.with_user_id(user_id)
  26. old_user_id = current_user_id
  27. self.current_user_id = user_id
  28. yield
  29. ensure
  30. self.current_user_id = old_user_id
  31. end
  32. end