Class: DecisionAgent::Auth::DeviseCanCanAdapter
- Inherits:
-
RbacAdapter
- Object
- RbacAdapter
- DecisionAgent::Auth::DeviseCanCanAdapter
- Defined in:
- lib/decision_agent/auth/rbac_adapter.rb
Overview
Adapter for Devise + CanCanCan integration
Instance Method Summary collapse
- #active?(user) ⇒ Boolean
- #can?(user, permission, resource = nil) ⇒ Boolean
- #has_role?(user, role) ⇒ Boolean
-
#initialize(ability_class: nil) ⇒ DeviseCanCanAdapter
constructor
A new instance of DeviseCanCanAdapter.
Methods inherited from RbacAdapter
Constructor Details
#initialize(ability_class: nil) ⇒ DeviseCanCanAdapter
Returns a new instance of DeviseCanCanAdapter.
95 96 97 98 |
# File 'lib/decision_agent/auth/rbac_adapter.rb', line 95 def initialize(ability_class: nil) super() @ability_class = ability_class end |
Instance Method Details
#active?(user) ⇒ Boolean
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/decision_agent/auth/rbac_adapter.rb', line 133 def active?(user) return false unless user # Devise typically uses active_for_authentication? or active? if user.respond_to?(:active_for_authentication?) user.active_for_authentication? elsif user.respond_to?(:active?) user.active? else true end end |
#can?(user, permission, resource = nil) ⇒ Boolean
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/decision_agent/auth/rbac_adapter.rb', line 100 def can?(user, , resource = nil) return false unless user return false unless active?(user) # CanCanCan uses :can? method with action and resource if user.respond_to?(:can?) # Map permission to CanCanCan action action = () user.can?(action, resource || Object) elsif @ability_class # Use Ability class if provided ability = @ability_class.new(user) action = () ability.can?(action, resource || Object) else false end end |
#has_role?(user, role) ⇒ Boolean
119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/decision_agent/auth/rbac_adapter.rb', line 119 def has_role?(user, role) return false unless user return false unless active?(user) # Check if user has role via CanCanCan roles or other methods if user.respond_to?(:has_role?) user.has_role?(role) elsif user.respond_to?(:roles) user.roles.any? { |r| r.to_s == role.to_s || r.name.to_s == role.to_s } else false end end |