Permalink
Browse files

Move the code to namespace CAS

  • Loading branch information...
nackd committed Nov 21, 2010
1 parent dbdc59f commit c70940f7ef7632e9df2d35dbbcb1b2d6ec4a2248
@@ -1,14 +1,16 @@
# Load CAS authentication configuration
-CAS_CONFIG = YAML.load_file("#{File.dirname(__FILE__)}/../cas.yml")[RAILS_ENV]
+module CAS
+ CONFIG = YAML.load_file("#{File.dirname(__FILE__)}/../cas.yml")[RAILS_ENV]
+end
-if CAS_CONFIG['enabled']
+if CAS::CONFIG['enabled']
# Basic CAS client configuration
require 'casclient'
require 'casclient/frameworks/rails/filter'
CASClient::Frameworks::Rails::Filter.configure(
- :cas_base_url => CAS_CONFIG['url'],
+ :cas_base_url => CAS::CONFIG['url'],
:enable_single_sign_out => true
)
end
View
@@ -5,8 +5,8 @@
end
require 'redmine'
-require 'cas_account_controller_patch'
-require 'cas_application_controller_patch'
+require 'cas/account_controller_patch'
+require 'cas/application_controller_patch'
Redmine::Plugin.register :redmine_cas do
name 'CAS Web Authentication'
@@ -0,0 +1,48 @@
+require 'casclient'
+require 'casclient/frameworks/rails/filter'
+require 'dispatcher'
+require_dependency 'account_controller'
+
+# Patches Redmine's AccountController dinamically. Manages login and logout
+# through CAS.
+module CAS
+ module AccountControllerPatch
+ def self.included(base) # :nodoc:
+ base.send(:include, InstanceMethods)
+
+ base.class_eval do
+ unloadable # Mark as unloadable so it is reloaded in development
+
+ alias_method_chain :login, :cas
+ alias_method_chain :logout, :cas
+ end
+ end
+
+ module InstanceMethods
+ def login_with_cas
+ if CAS::CONFIG['enabled']
+ if params[:ticket]
+ redirect_back_or_default :controller => 'my', :action => 'page'
+ else
+ CASClient::Frameworks::Rails::Filter::redirect_to_cas_for_authentication(self)
+ end
+ else
+ login_without_cas
+ end
+ end
+
+ def logout_with_cas
+ if CAS::CONFIG['enabled']
+ self.logged_user = nil
+ CASClient::Frameworks::Rails::Filter::logout(self, home_url)
+ else
+ logout_without_cas
+ end
+ end
+ end
+ end
+end
+
+Dispatcher.to_prepare do
+ AccountController.send(:include, CAS::AccountControllerPatch)
+end
@@ -0,0 +1,49 @@
+require 'casclient'
+require 'casclient/frameworks/rails/filter'
+require 'dispatcher'
+require_dependency 'application_controller'
+
+# Patches Redmine's ApplicationController dinamically. Prepends a CAS gatewaying
+# filter.
+module CAS
+ module ApplicationControllerPatch
+ def self.included(base) # :nodoc:
+ base.send(:include, InstanceMethods)
+
+ base.class_eval do
+ unloadable # Mark as unloadable so it is reloaded in development
+
+ prepend_before_filter :cas_filter, :set_user_id
+ end
+ end
+
+ module InstanceMethods
+ def cas_filter
+ if CAS::CONFIG['enabled'] and !['atom', 'xml', 'json'].include?request.format
+ if params[:controller] != 'account'
+ CASClient::Frameworks::Rails::GatewayFilter.filter(self)
+ else
+ CASClient::Frameworks::Rails::Filter.filter(self)
+ end
+ else
+ true
+ end
+ end
+
+ def set_user_id
+ if CAS::CONFIG['enabled']
+ user = User.find_by_login session[:cas_user]
+ if user and session[:user_id] != user.id
+ session[:user_id] = user.id
+ call_hook(:controller_account_success_authentication_after, {:user => user })
+ end
+ end
+ true
+ end
+ end
+ end
+end
+
+Dispatcher.to_prepare do
+ ApplicationController.send(:include, CAS::ApplicationControllerPatch)
+end
@@ -1,46 +0,0 @@
-require 'casclient'
-require 'casclient/frameworks/rails/filter'
-require 'dispatcher'
-require_dependency 'account_controller'
-
-# Patches Redmine's AccountController dinamically. Manages login and logout
-# through CAS.
-module CasAccountControllerPatch
- def self.included(base) # :nodoc:
- base.send(:include, InstanceMethods)
-
- base.class_eval do
- unloadable # Mark as unloadable so it is reloaded in development
-
- alias_method_chain :login, :cas
- alias_method_chain :logout, :cas
- end
- end
-
- module InstanceMethods
- def login_with_cas
- if CAS_CONFIG['enabled']
- if params[:ticket]
- redirect_back_or_default :controller => 'my', :action => 'page'
- else
- CASClient::Frameworks::Rails::Filter::redirect_to_cas_for_authentication(self)
- end
- else
- login_without_cas
- end
- end
-
- def logout_with_cas
- if CAS_CONFIG['enabled']
- self.logged_user = nil
- CASClient::Frameworks::Rails::Filter::logout(self, home_url)
- else
- logout_without_cas
- end
- end
- end
-end
-
-Dispatcher.to_prepare do
- AccountController.send(:include, CasAccountControllerPatch)
-end
@@ -1,47 +0,0 @@
-require 'casclient'
-require 'casclient/frameworks/rails/filter'
-require 'dispatcher'
-require_dependency 'application_controller'
-
-# Patches Redmine's ApplicationController dinamically. Prepends a CAS gatewaying
-# filter.
-module CasApplicationControllerPatch
- def self.included(base) # :nodoc:
- base.send(:include, InstanceMethods)
-
- base.class_eval do
- unloadable # Mark as unloadable so it is reloaded in development
-
- prepend_before_filter :cas_filter, :set_user_id
- end
- end
-
- module InstanceMethods
- def cas_filter
- if CAS_CONFIG['enabled'] and !['atom', 'xml', 'json'].include?request.format
- if params[:controller] != 'account'
- CASClient::Frameworks::Rails::GatewayFilter.filter(self)
- else
- CASClient::Frameworks::Rails::Filter.filter(self)
- end
- else
- true
- end
- end
-
- def set_user_id
- if CAS_CONFIG['enabled']
- user = User.find_by_login session[:cas_user]
- if user and session[:user_id] != user.id
- session[:user_id] = user.id
- call_hook(:controller_account_success_authentication_after, {:user => user })
- end
- end
- true
- end
- end
-end
-
-Dispatcher.to_prepare do
- ApplicationController.send(:include, CasApplicationControllerPatch)
-end

0 comments on commit c70940f

Please sign in to comment.