Skip to content

Commit

Permalink
Had to resort to a somewhat less centralized/clean auth approach to a…
Browse files Browse the repository at this point in the history
…void repetitive AR calls
  • Loading branch information
August committed Apr 23, 2012
1 parent 3a22dfb commit b887123
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions app/controllers/contents_controller.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
class ContentsController < ApplicationController
before_filter :get_content_const, :only => [:new, :create]
before_filter { |controller| check_permissions(controller.action_name) }
before_filter :check_permissions, :only => [:new, :create]

#takes the action name being run as an argument an runs cancan's authorization routine
#TODO: Account for index and show actions - may need more definited Abilities
def check_permissions(action_name)
if action_name == "new" || action_name == "create"
authorize! :create, Content
elsif action_name == "edit" || action_name == "destroy" || action_name == "update"
authorize! [:update, :delete], Content
end
#Runs cancan permissions check for new and create actions
#Would have been nice to check all permissions here as we have access to both the action
#name and the params hash, but that would have required twice the AR calls
def check_create_permission
authorize! :create, Content
end

# Grab the constent object for the type of
Expand Down Expand Up @@ -77,6 +74,7 @@ def new
# GET /contents/1/edit
def edit
@content = Content.find(params[:id])
authorize! :update, @content
end

# POST /contents
Expand Down Expand Up @@ -110,7 +108,7 @@ def create
# PUT /contents/1.xml
def update
@content = Content.find(params[:id])

authorize! :update, @content

respond_to do |format|
if @content.update_attributes(params[:content])
Expand All @@ -131,6 +129,8 @@ def update
# DELETE /contents/1.xml
def destroy
@content = Content.find(params[:id])
authorize! :delete, @content

@content.destroy

respond_to do |format|
Expand Down

0 comments on commit b887123

Please sign in to comment.