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]
: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
- #authorize!(action, subject, *params) ⇒ Object
- #can?(action, subject, *params) ⇒ Boolean
- #cannot?(*args) ⇒ Boolean
- #deny!(payload) ⇒ Object
- #initialize(ctx) ⇒ Object
Instance Method Details
#authorize!(action, subject, *params) ⇒ Object
59 60 61 62 |
# File 'lib/allowy/access_control.rb', line 59 def (action, subject, *params) allowing, payload = (action, subject, *params) raise AccessDenied.new("Not authorized", action, subject, payload) if not allowing end |
#can?(action, subject, *params) ⇒ Boolean
50 51 52 53 |
# File 'lib/allowy/access_control.rb', line 50 def can?(action, subject, *params) allowing, _ = (action, subject, *params) allowing end |
#cannot?(*args) ⇒ 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 |