Module: EasyAdmin::Permissions::UserExtensions

Extended by:
ActiveSupport::Concern
Included in:
AdminUser
Defined in:
lib/easy_admin/permissions/user_extensions.rb

Instance Method Summary collapse

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 calculate_permissions(context: nil)
  return [] unless EasyAdmin::Permissions.enabled?
  return [] unless respond_to?(:permissions_cache)
  return [] unless permissions_cache.present?

  # Return only permissions that are explicitly set to true
  permissions_cache.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 clear_permissions_cache!
  if respond_to?(:permissions_cache=)
    if persisted?
      update_column(:permissions_cache, {})
    else
      self.permissions_cache = {}
    end
  end
end

#has_permission?(permission_name, context: nil) ⇒ Boolean

Check if user has specific permission

Returns:

  • (Boolean)


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 has_permission?(permission_name, context: nil)
  return false unless EasyAdmin::Permissions.enabled?
  return false unless respond_to?(:permissions_cache)
  return false unless permissions_cache.present?

  # For now, ignore context and just use flat permission structure
  permission_value = permissions_cache[permission_name.to_s]
  
  # Check if permission exists in cache (handle both string and boolean values)
  return true if permission_value == "true" || permission_value == true
  return false if permission_value == "false" || permission_value == false
  
  # If not explicitly set, default to false
  false
end

#has_role?(role_name, context: nil) ⇒ Boolean

Check if user has specific role

Returns:

  • (Boolean)


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 remove_permission(permission_name, context: nil)
  return false unless EasyAdmin::Permissions.enabled?
  return false unless respond_to?(:permissions_cache=)

  context_key = context&.cache_key || 'global'
  current_cache = permissions_cache || {}
  
  if context && current_cache[context_key]
    current_cache[context_key].delete(permission_name.to_s)
  else
    current_cache.delete(permission_name.to_s)
  end
  
  if persisted?
    update_column(:permissions_cache, current_cache)
  else
    self.permissions_cache = 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 set_permission(permission_name, 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 = permissions_cache || {}
  
  if context
    current_cache[context_key] ||= {}
    current_cache[context_key][permission_name.to_s] = value.to_s
  else
    current_cache[permission_name.to_s] = value.to_s
  end
  
  if persisted?
    update_column(:permissions_cache, current_cache)
  else
    self.permissions_cache = current_cache
  end
  
  true
end