Module: Allowy::AccessControl

Extended by:
ActiveSupport::Concern
Defined in:
lib/allowy/access_control.rb

Overview

This module provides the interface for implementing the access control actions. In order to use it, mix it into a plain Ruby class and define methods ending with ‘?`. For example:

@example
class PageAccess
  include Allowy::AccessControl

  def view?(page)
    page and page.wiki? and context.user_signed_in?
  end
end

And then you can check the permissions from a controller:

@example
def show
  @page = Page.find params[:id]
  authorize! :view, @page
end

You can also check the permissions outside of the controller, but you need an object that includes ‘Allowy::Context` class:

@example
class CucumberContext
  include Allowy::Context
  attr_accessor :current_user

  def initialize(user)
    @current_user = user
  end
end

CucumberContext.new(that_user).can?(:create, Blog)
CucumberContext.new(that_user).should be_able_to :create, Blog

Instance Method Summary collapse

Instance Method Details

#authorize!(action, subject, *params) ⇒ Object

Raises:



59
60
61
62
# File 'lib/allowy/access_control.rb', line 59

def authorize!(action, subject, *params)
  allowing, payload = check_permission(action, subject, *params)
  raise AccessDenied.new("Not authorized", action, subject, payload) if not allowing
end

#can?(action, subject, *params) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/allowy/access_control.rb', line 50

def can?(action, subject, *params)
  allowing, _ = check_permission(action, subject, *params)
  allowing
end

#cannot?(*args) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/allowy/access_control.rb', line 55

def cannot?(*args)
  not can?(*args)
end

#deny!(payload) ⇒ Object



64
65
66
# File 'lib/allowy/access_control.rb', line 64

def deny!(payload)
  throw(:deny, payload)
end

#initialize(ctx) ⇒ Object



46
47
48
# File 'lib/allowy/access_control.rb', line 46

def initialize(ctx)
  @context = ctx
end