Skip to content

Commit

Permalink
Finish auth! method and implement in the subscriptions controller.
Browse files Browse the repository at this point in the history
  • Loading branch information
bamnet committed Jul 7, 2012
1 parent 867f481 commit 4d8cb2f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
13 changes: 9 additions & 4 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ def check_for_initial_install
redirect_to root_url, :flash => { :notice => exception.message }
end

def auth
def auth!(options = {})
# action
# object
# allow_empty
action_map = {
'index' => :read,
'show' => :read,
Expand All @@ -43,14 +46,16 @@ def auth
'destroy' => :destroy,
}

test_action = (options[:action] || action_map[action_name])
allow_empty = (options[:allow_empty] || true)

var_name = controller_name
if action_name != 'index'
var_name = controller_name.singularize
end
object = instance_variable_get("@#{var_name}")
object = (options[:object] || instance_variable_get("@#{var_name}"))

test_action = action_map[action_name]
if object.is_a? Enumerable
if allow_empty && (object.is_a? Enumerable)
object.delete_if {|o| cannot?(test_action, o)}
else
if cannot?(test_action, object)
Expand Down
9 changes: 7 additions & 2 deletions app/controllers/subscriptions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class SubscriptionsController < ApplicationController
load_and_authorize_resource :except => [:new]
before_filter :get_screen

def get_screen
Expand All @@ -10,6 +9,7 @@ def get_screen
# GET /screen/:screen_id/subscriptions.xml
def index
@subscriptions = @screen.subscriptions.all
auth!

respond_to do |format|
format.html # index.html.erb
Expand All @@ -31,6 +31,7 @@ def manage
# GET /screen/:screen_id/subscriptions/1.xml
def show
@subscription = Subscription.find(params[:id])
auth!

respond_to do |format|
format.html # show.html.erb
Expand All @@ -44,7 +45,7 @@ def new
@subscription = Subscription.new
@subscription.screen = @screen
@subscription.field = @field
auth
auth!
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @subscription }
Expand All @@ -54,6 +55,7 @@ def new
# GET /screen/:screen_id/subscriptions/1/edit
def edit
@subscription = Subscription.find(params[:id])
auth!
end

# POST /screen/:screen_id/subscriptions
Expand All @@ -62,6 +64,7 @@ def create
@subscription = Subscription.new(params[:subscription])
@subscription.screen = @screen
@subscription.field = @field
auth!

respond_to do |format|
if @subscription.save
Expand All @@ -78,6 +81,7 @@ def create
# PUT /screen/:screen_id/subscriptions/1.xml
def update
@subscription = Subscription.find(params[:id])
auth!

respond_to do |format|
if @subscription.update_attributes(params[:subscription])
Expand All @@ -94,6 +98,7 @@ def update
# DELETE /screen/:screen_id/subscriptions/1.xml
def destroy
@subscription = Subscription.find(params[:id])
auth!
@subscription.destroy

respond_to do |format|
Expand Down
6 changes: 3 additions & 3 deletions test/unit/abilities/user/subscription_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ def setup

test "Screen user owner all access" do
ability = Ability.new(@katie)
assert ability.can?(:create, Subscription)
@subscription.screen = @kt_screen
assert ability.can?(:create, @subscription)

abilities = [:update, :delete, :read]
@subscription.screen = @kt_screen
abilities.each do |action|
assert ability.can?(action, @subscription)
end
Expand All @@ -28,10 +28,10 @@ def setup

test "Screen group owner all access" do
ability = Ability.new(@katie)
@subscription.screen = @wtg_screen
assert ability.can?(:create, @subscription)

abilities = [:update, :delete, :read]
@subscription.screen = @wtg_screen
abilities.each do |action|
assert ability.can?(action, @subscription)
end
Expand Down

0 comments on commit 4d8cb2f

Please sign in to comment.