Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Plugins: Provide for authorization from main app

Plugins will have their own ability classes for their resources.
However, their hooks which execute in the main_app context will need to
access them. This commit provides away to do that. (#231 is almost there)
  • Loading branch information...
commit a51bdb459f06ca65d448c6ae84ef41e1468fa33d 1 parent a0eec3b
@mikldt mikldt authored
Showing with 25 additions and 1 deletion.
  1. +25 −1 app/controllers/application_controller.rb
View
26 app/controllers/application_controller.rb
@@ -5,6 +5,30 @@ class ApplicationController < ActionController::Base
before_filter :set_version
before_filter :compute_pending_moderation
+ # Current Ability for CanCan authorization
+ # This matches CanCan's code but is here to be explicit,
+ # since we modify @current_ability below for plugins.
+ def current_ability
+ @current_ability ||= ::Ability.new(current_user)
+ end
+
+ # Allow views in the main application to do authorization
+ # checks for plugins. This will be needed for plugin code
+ # that is included via UI hooks.
+ def use_plugin_ability(mod, &block)
+ @main_app_ability = @current_ability
+ @plugin_abilities = @plugin_abilities || {}
+ mod_sym = mod.name.to_sym
+ if @plugin_abilities[mod_sym].nil?
+ ability = (mod.name+"::Ability").constantize
+ @current_ability =
+ @plugin_abilities[mod_sym] ||= ability.new(current_user)
+ end
+ @current_ability = @plugin_abilities[mod_sym]
+ yield
+ @current_ability = @main_app_ability
+ end
+
# Expose a instance variable counting the number of pending submissions
# a user can moderate. 0 indicates no pending submissions.
# @pending_submissions_count
@@ -43,7 +67,7 @@ def check_for_initial_install
#Don't break for CanCan exceptions; send the user to the front page with a Flash error message
rescue_from CanCan::AccessDenied do |exception|
- redirect_to root_url, :flash => { :notice => exception.message }
+ redirect_to main_app.root_url, :flash => { :notice => exception.message }
end
# Authenticate using the current action and instance variables.
Please sign in to comment.
Something went wrong with that request. Please try again.