Module: EasyAdmin::Permissions::UserExtensions
- Extended by:
- ActiveSupport::Concern
- Included in:
- AdminUser
- Defined in:
- lib/easy_admin/permissions/user_extensions.rb
Instance Method Summary collapse
-
#calculate_permissions(context: nil) ⇒ Object
Get all user permissions for a context.
-
#clear_permissions_cache! ⇒ Object
Clear permission cache.
-
#has_permission?(permission_name, context: nil) ⇒ Boolean
Check if user has specific permission.
-
#has_role?(role_name, context: nil) ⇒ Boolean
Check if user has specific role.
-
#remove_permission(permission_name, context: nil) ⇒ Object
Remove permission from cache.
-
#roles_for_context(context = nil) ⇒ Object
Get all roles for user in specific context.
-
#set_permission(permission_name, value = true, context: nil) ⇒ Object
Set user permissions directly in cache.
Instance Method Details
#calculate_permissions(context: nil) ⇒ Object
Get all user permissions for a context
40 41 42 43 44 45 46 47 |
# File 'lib/easy_admin/permissions/user_extensions.rb', line 40 def (context: nil) return [] unless EasyAdmin::Permissions.enabled? return [] unless respond_to?(:permissions_cache) return [] unless .present? # Return only permissions that are explicitly set to true .select { |k, v| v == "true" || v == true }.keys end |
#clear_permissions_cache! ⇒ Object
Clear permission cache
106 107 108 109 110 111 112 113 114 |
# File 'lib/easy_admin/permissions/user_extensions.rb', line 106 def if respond_to?(:permissions_cache=) if persisted? update_column(:permissions_cache, {}) else self. = {} end end end |
#has_permission?(permission_name, context: nil) ⇒ Boolean
Check if user has specific permission
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/easy_admin/permissions/user_extensions.rb', line 15 def (, context: nil) return false unless EasyAdmin::Permissions.enabled? return false unless respond_to?(:permissions_cache) return false unless .present? # For now, ignore context and just use flat permission structure = [.to_s] # Check if permission exists in cache (handle both string and boolean values) return true if == "true" || == true return false if == "false" || == false # If not explicitly set, default to false false end |
#has_role?(role_name, context: nil) ⇒ Boolean
Check if user has specific role
32 33 34 35 36 37 |
# File 'lib/easy_admin/permissions/user_extensions.rb', line 32 def has_role?(role_name, context: nil) user_roles.active.includes(:role).any? do |user_role| next false if context && user_role.context != context user_role.role.slug == role_name.to_s || user_role.role.name == role_name.to_s end end |
#remove_permission(permission_name, context: nil) ⇒ Object
Remove permission from cache
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/easy_admin/permissions/user_extensions.rb', line 74 def (, context: nil) return false unless EasyAdmin::Permissions.enabled? return false unless respond_to?(:permissions_cache=) context_key = context&.cache_key || 'global' current_cache = || {} if context && current_cache[context_key] current_cache[context_key].delete(.to_s) else current_cache.delete(.to_s) end if persisted? update_column(:permissions_cache, current_cache) else self. = current_cache end true end |
#roles_for_context(context = nil) ⇒ Object
Get all roles for user in specific context
97 98 99 100 101 102 103 |
# File 'lib/easy_admin/permissions/user_extensions.rb', line 97 def roles_for_context(context = nil) if context user_roles.active.for_context(context).includes(:role).map(&:role) else user_roles.active.global.includes(:role).map(&:role) end end |
#set_permission(permission_name, value = true, context: nil) ⇒ Object
Set user permissions directly in cache
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/easy_admin/permissions/user_extensions.rb', line 50 def (, value = true, context: nil) return false unless EasyAdmin::Permissions.enabled? return false unless respond_to?(:permissions_cache=) context_key = context&.cache_key || 'global' current_cache = || {} if context current_cache[context_key] ||= {} current_cache[context_key][.to_s] = value.to_s else current_cache[.to_s] = value.to_s end if persisted? update_column(:permissions_cache, current_cache) else self. = current_cache end true end |