assets.rb 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class UserInfo::Assets
  3. LEVEL_CUSTOMER = 1
  4. LEVEL_AGENT = 2
  5. LEVEL_ADMIN = 3
  6. attr_accessor :current_user_id, :level, :filter_attributes, :user
  7. def initialize(current_user_id)
  8. @current_user_id = current_user_id
  9. @user = User.find_by(id: current_user_id) if current_user_id.present?
  10. set_level
  11. end
  12. def admin?
  13. check_level?(UserInfo::Assets::LEVEL_ADMIN)
  14. end
  15. def agent?
  16. check_level?(UserInfo::Assets::LEVEL_AGENT)
  17. end
  18. def customer?
  19. check_level?(UserInfo::Assets::LEVEL_CUSTOMER)
  20. end
  21. def set_level
  22. if user.blank?
  23. self.level = nil
  24. return
  25. end
  26. self.level = UserInfo::Assets::LEVEL_CUSTOMER
  27. user.permissions_with_child_names.each do |permission|
  28. case permission
  29. when %r{^admin\.}
  30. self.level = UserInfo::Assets::LEVEL_ADMIN
  31. break
  32. when 'ticket.agent'
  33. self.level = UserInfo::Assets::LEVEL_AGENT
  34. end
  35. end
  36. end
  37. def check_level?(check)
  38. return true if user.blank?
  39. level >= check
  40. end
  41. end