permission.rb 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Permission < ApplicationModel
  3. include ChecksClientNotification
  4. include ChecksHtmlSanitized
  5. include HasCollectionUpdate
  6. has_and_belongs_to_many :roles
  7. validates :name, presence: true
  8. store :preferences
  9. validates :note, length: { maximum: 500 }
  10. sanitized_html :note
  11. =begin
  12. permissions = Permission.with_parents('some_key.sub_key')
  13. returns
  14. ['some_key.sub_key', 'some_key']
  15. =end
  16. def self.with_parents(key)
  17. names = []
  18. part = ''
  19. key.split('.').each do |local_part|
  20. if part != ''
  21. part += '.'
  22. end
  23. part += local_part
  24. names.push part
  25. end
  26. names
  27. end
  28. def to_s
  29. name
  30. end
  31. def self.join_with(object, permissions)
  32. return object if !object.method_defined?(:permissions?)
  33. permissions = with_parents(permissions)
  34. object.joins(roles: :permissions)
  35. .where(roles: { active: true }, permissions: { name: permissions, active: true })
  36. end
  37. end