Class: Wallaby::ModelAuthorizationProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/interfaces/wallaby/model_authorization_provider.rb

Overview

Model Authorizer interface.

Since:

  • wallaby-5.2.0

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ModelAuthorizationProvider

Returns a new instance of ModelAuthorizationProvider.

Parameters:

  • options (Hash) (defaults to: {})

Since:

  • wallaby-5.2.0



39
40
41
# File 'lib/interfaces/wallaby/model_authorization_provider.rb', line 39

def initialize(options = {})
  @options = options || {}
end

Class Attribute Details

.provider_nameString/Symbol

This is the provider name (e.g. ‘:default`/`:cancancan`/`:pundit`) that can be set in Wallaby::ModelAuthorizer subclasses’s Wallaby::ModelAuthorizer.provider_name.

Returns:

  • (String/Symbol)

    provider name

Since:

  • wallaby-5.2.0



15
16
17
# File 'lib/interfaces/wallaby/model_authorization_provider.rb', line 15

def provider_name
  @provider_name ||= name.demodulize.gsub(/(Authorization)?Provider/, EMPTY_STRING).underscore
end

Instance Attribute Details

#optionsHash (readonly)

Returns:

  • (Hash)

Since:

  • wallaby-5.2.0



36
37
38
# File 'lib/interfaces/wallaby/model_authorization_provider.rb', line 36

def options
  @options
end

Class Method Details

.available?(_context) ⇒ Boolean

Note:

Template method to check and see if current provider is in used.

Parameters:

  • _context (ActionController::Base, ActionView::Base)

Returns:

  • (Boolean)

Raises:

Since:

  • wallaby-5.2.0



22
23
24
# File 'lib/interfaces/wallaby/model_authorization_provider.rb', line 22

def available?(_context)
  raise NotImplemented
end

.options_from(_context) ⇒ Object

Note:

Template method to get the required data from context.

Parameters:

  • _context (ActionController::Base, ActionView::Base)

Raises:

Since:

  • wallaby-5.2.0



29
30
31
# File 'lib/interfaces/wallaby/model_authorization_provider.rb', line 29

def options_from(_context)
  raise NotImplemented
end

Instance Method Details

#accessible_for(_action, _scope) ⇒ Object

Note:

It can be overridden in subclasses for customization purpose.

This is the template method to restrict user’s access to certain scope.

Parameters:

  • _action (Symbol, String)
  • _scope (Object)

Raises:

Since:

  • wallaby-5.2.0



80
81
82
# File 'lib/interfaces/wallaby/model_authorization_provider.rb', line 80

def accessible_for(_action, _scope)
  raise NotImplemented
end

#attributes_for(_action, _subject) ⇒ Object

Note:

It can be overridden in subclasses for customization purpose.

This is the template method to restrict user’s modification to certain fields of given subject.

Parameters:

  • _action (Symbol, String)
  • _subject (Object)

Raises:

Since:

  • wallaby-5.2.0



89
90
91
# File 'lib/interfaces/wallaby/model_authorization_provider.rb', line 89

def attributes_for(_action, _subject)
  raise NotImplemented
end

#authorize(_action, _subject) ⇒ Object

Note:

It can be overridden in subclasses for customization purpose.

This is the template method to check user’s permission for given action on given subject.

Parameters:

  • _action (Symbol, String)
  • _subject (Object, Class)

Raises:

Since:

  • wallaby-5.2.0



53
54
55
# File 'lib/interfaces/wallaby/model_authorization_provider.rb', line 53

def authorize(_action, _subject)
  raise NotImplemented
end

#authorized?(_action, _subject) ⇒ Boolean

Note:

It can be overridden in subclasses for customization purpose.

This is the template method to check if user has permission for given action on given subject.

Parameters:

  • _action (Symbol, String)
  • _subject (Object, Class)

Returns:

  • (Boolean)

Raises:

Since:

  • wallaby-5.2.0



62
63
64
# File 'lib/interfaces/wallaby/model_authorization_provider.rb', line 62

def authorized?(_action, _subject)
  raise NotImplemented
end

#permit_params(_action, _subject) ⇒ Object

Note:

It can be overridden in subclasses for customization purpose.

This is the template method to restrict user’s mass assignment to certain fields of given subject.

Parameters:

  • _action (Symbol, String)
  • _subject (Object)

Raises:

Since:

  • wallaby-5.2.0



98
99
100
# File 'lib/interfaces/wallaby/model_authorization_provider.rb', line 98

def permit_params(_action, _subject)
  raise NotImplemented
end

#unauthorized?(action, subject) ⇒ Boolean

Note:

It can be overridden in subclasses for customization purpose.

This is the template method to check if user has no permission for given action on given subject.

Parameters:

  • action (Symbol, String)
  • subject (Object, Class)

Returns:

  • (Boolean)

Raises:

Since:

  • wallaby-5.2.0



71
72
73
# File 'lib/interfaces/wallaby/model_authorization_provider.rb', line 71

def unauthorized?(action, subject)
  !authorized?(action, subject)
end

#userObject?

Returns user object.

Returns:

  • (Object, nil)

    user object

Since:

  • wallaby-5.2.0



44
45
46
# File 'lib/interfaces/wallaby/model_authorization_provider.rb', line 44

def user
  options[:user]
end