Class: DecisionAgent::Auth::CustomAdapter

Inherits:
RbacAdapter
  • Object
show all
Defined in:
lib/decision_agent/auth/rbac_adapter.rb

Overview

Custom adapter that allows users to provide their own logic via blocks/procs

Instance Method Summary collapse

Constructor Details

#initialize(can_proc: nil, has_role_proc: nil, active_proc: nil, user_id_proc: nil, user_email_proc: nil) ⇒ CustomAdapter

Returns a new instance of CustomAdapter.



221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/decision_agent/auth/rbac_adapter.rb', line 221

def initialize(
  can_proc: nil,
  has_role_proc: nil,
  active_proc: nil,
  user_id_proc: nil,
  user_email_proc: nil
)
  super()
  @can_proc = can_proc
  @has_role_proc = has_role_proc
  @active_proc = active_proc
  @user_id_proc = user_id_proc
  @user_email_proc = user_email_proc
end

Instance Method Details

#active?(user) ⇒ Boolean

Returns:

  • (Boolean)


253
254
255
256
257
258
259
260
261
# File 'lib/decision_agent/auth/rbac_adapter.rb', line 253

def active?(user)
  return false unless user

  if @active_proc
    @active_proc.call(user)
  else
    super
  end
end

#can?(user, permission, resource = nil) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


236
237
238
239
240
241
242
243
# File 'lib/decision_agent/auth/rbac_adapter.rb', line 236

def can?(user, permission, resource = nil)
  return false unless user
  return false unless active?(user)

  raise NotImplementedError, "CustomAdapter requires can_proc to be provided" unless @can_proc

  @can_proc.call(user, permission, resource)
end

#has_role?(user, role) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


245
246
247
248
249
250
251
# File 'lib/decision_agent/auth/rbac_adapter.rb', line 245

def has_role?(user, role)
  return false unless user

  raise NotImplementedError, "CustomAdapter requires has_role_proc to be provided" unless @has_role_proc

  @has_role_proc.call(user, role)
end

#user_email(user) ⇒ Object



271
272
273
274
275
276
277
# File 'lib/decision_agent/auth/rbac_adapter.rb', line 271

def user_email(user)
  if @user_email_proc
    @user_email_proc.call(user)
  else
    super
  end
end

#user_id(user) ⇒ Object



263
264
265
266
267
268
269
# File 'lib/decision_agent/auth/rbac_adapter.rb', line 263

def user_id(user)
  if @user_id_proc
    @user_id_proc.call(user)
  else
    super
  end
end