Skip to content

Commit

Permalink
added example Rails app
Browse files Browse the repository at this point in the history
  • Loading branch information
zuk committed Nov 20, 2008
1 parent 8df4357 commit 83a0bb3
Show file tree
Hide file tree
Showing 23 changed files with 1,296 additions and 0 deletions.
16 changes: 16 additions & 0 deletions examples/rails/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
This is a skeleton Rails application hooked up for CAS authentication.

To try this out:

1. If you have an existing CAS server, modify the CAS client settings in
config/environment.rb to point to your server. If you do not yet
have a CAS server, install rubycas-server, and configure it to run on
http://localhost:7777 (or modify environment.rb to your likings).

2. Run `ruby script/server`

3. Point your web browser to http://localhost:3000

4. Have a look at the source code in app/controllers/simple_example_controller.rb
and app/controllers/advanced_example_controller.rb. The
corresponding views under app/views might also be worth looking at.
31 changes: 31 additions & 0 deletions examples/rails/app/controllers/advanced_example_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# A more advanced example.
# For basic usage see the SimpleExampleController.
class AdvancedExampleController < ApplicationController
# This will allow the user to view the index page without authentication
# but will process CAS authentication data if the user already
# has an SSO session open.
before_filter CASClient::Frameworks::Rails::GatewayFilter, :only => :index

# This requires the user to be authenticated for viewing allother pages.
before_filter CASClient::Frameworks::Rails::Filter, :except => :index

def index
@username = session[:cas_user]

@login_url = CASClient::Frameworks::Rails::Filter.login_url(self)
end

def my_account
@username = session[:cas_user]

# Additional user attributes are available if your
# CAS server is configured to provide them.
# See http://code.google.com/p/rubycas-server/wiki/HowToSendExtraUserAttributes
@extra_attributes = session[:cas_extra_attributes]
end

def logout
CASClient::Frameworks::Rails::Filter.logout(self)
end

end
2 changes: 2 additions & 0 deletions examples/rails/app/controllers/application.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ApplicationController < ActionController::Base
end
16 changes: 16 additions & 0 deletions examples/rails/app/controllers/simple_example_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This is the most basic, bare-bones example.
# For advanced usage see the AdvancedExampleController.
class SimpleExampleController < ApplicationController
# This will force CAS authentication before the user
# is allowed to access any action in this controller.
before_filter CASClient::Frameworks::Rails::Filter

def index
@username = session[:cas_user]
end

def logout
CASClient::Frameworks::Rails::Filter.logout(self)
end

end
13 changes: 13 additions & 0 deletions examples/rails/app/views/advanced_example/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h1>AdvancedExample#index</h1>

<% if @username %>
<p>Hello, <%= @username %>! You are authenticated.</p>
<% else %>
<p>You are not yet authenticated. <%= link_to("Login", @login_url) %>
<% end %>

<p>&raquo; <%= link_to("Go To My Account", :action => 'my_account') %></p>

<% if @username %>
<p>[ <%= link_to("Logout", :action => 'logout') %> ]</p>
<% end %>
11 changes: 11 additions & 0 deletions examples/rails/app/views/advanced_example/my_account.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h1>AdvancedExample#my_account</h1>
<p><%= @username %>'s Account page</p>

<p>
<strong>Extra Attributes</strong>:<br />
<% unless @extra_attributes.blank? %>
<%= debug(@extra_attributes) %>
<% end %>
</p>

<p>[ <%= link_to("Logout", :action => 'logout') %> ]</p>
6 changes: 6 additions & 0 deletions examples/rails/app/views/simple_example/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>SimpleExample#index</h1>
<p>Hello, <%= @username %>!</p>

<p>&raquo; <%= link_to("Go To AdvancedExample", :controller => 'advanced_example') %></p>

<p>[ <%= link_to("Logout", :action => 'logout') %> ]</p>
109 changes: 109 additions & 0 deletions examples/rails/config/boot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Don't change this file!
# Configure your app in config/environment.rb and config/environments/*.rb

RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)

module Rails
class << self
def boot!
unless booted?
preinitialize
pick_boot.run
end
end

def booted?
defined? Rails::Initializer
end

def pick_boot
(vendor_rails? ? VendorBoot : GemBoot).new
end

def vendor_rails?
File.exist?("#{RAILS_ROOT}/vendor/rails")
end

def preinitialize
load(preinitializer_path) if File.exist?(preinitializer_path)
end

def preinitializer_path
"#{RAILS_ROOT}/config/preinitializer.rb"
end
end

class Boot
def run
load_initializer
Rails::Initializer.run(:set_load_path)
end
end

class VendorBoot < Boot
def load_initializer
require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
Rails::Initializer.run(:install_gem_spec_stubs)
end
end

class GemBoot < Boot
def load_initializer
self.class.load_rubygems
load_rails_gem
require 'initializer'
end

def load_rails_gem
if version = self.class.gem_version
gem 'rails', version
else
gem 'rails'
end
rescue Gem::LoadError => load_error
$stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
exit 1
end

class << self
def rubygems_version
Gem::RubyGemsVersion if defined? Gem::RubyGemsVersion
end

def gem_version
if defined? RAILS_GEM_VERSION
RAILS_GEM_VERSION
elsif ENV.include?('RAILS_GEM_VERSION')
ENV['RAILS_GEM_VERSION']
else
parse_gem_version(read_environment_rb)
end
end

def load_rubygems
require 'rubygems'
min_version = '1.1.1'
unless rubygems_version >= min_version
$stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
exit 1
end

rescue LoadError
$stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
exit 1
end

def parse_gem_version(text)
$1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
end

private
def read_environment_rb
File.read("#{RAILS_ROOT}/config/environment.rb")
end
end
end
end

# All that for this:
Rails.boot!
39 changes: 39 additions & 0 deletions examples/rails/config/environment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
RAILS_GEM_VERSION = '2.1.2' unless defined? RAILS_GEM_VERSION

require File.join(File.dirname(__FILE__), 'boot')

Rails::Initializer.run do |config|
config.time_zone = 'UTC'
config.action_controller.session = {
:session_key => '_rails_session',
:secret => 'e2f5641ab4a3627096a2b6ca8c62cefe53f572906ad6a5fb1c949d183a0'
}
config.frameworks -= [:active_record]
end


# Basic CAS client configuration

require 'casclient'
require 'casclient/frameworks/rails/filter'

CASClient::Frameworks::Rails::Filter.configure(
:cas_base_url => "https://mzukowski.urbacon.net:6543/cas"
)


# More complicated configuration

#cas_logger = CASClient::Logger.new(RAILS_ROOT+'/log/cas.log')
#cas_logger.level = Logger::DEBUG
#
#CASClient::Frameworks::Rails::Filter.configure(
# :cas_base_url => "https://localhost:7778/",
# :login_url => "https://localhost:7778/login",
# :logout_url => "https://localhost:7778/logout",
# :validate_url => "https://localhost:7778/proxyValidate",
# :session_username_key => :cas_user,
# :session_extra_attributes_key => :cas_extra_attributes
# :logger => cas_logger,
# :authenticate_on_every_request => true
#)
17 changes: 17 additions & 0 deletions examples/rails/config/environments/development.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Settings specified here will take precedence over those in config/environment.rb

# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the webserver when you make code changes.
config.cache_classes = false

# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true

# Show full error reports and disable caching
config.action_controller.consider_all_requests_local = true
config.action_view.debug_rjs = true
config.action_controller.perform_caching = false

# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false
22 changes: 22 additions & 0 deletions examples/rails/config/environments/production.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Settings specified here will take precedence over those in config/environment.rb

# The production environment is meant for finished, "live" apps.
# Code is not reloaded between requests
config.cache_classes = true

# Use a different logger for distributed setups
# config.logger = SyslogLogger.new

# Full error reports are disabled and caching is turned on
config.action_controller.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.action_view.cache_template_loading = true

# Use a different cache store in production
# config.cache_store = :mem_cache_store

# Enable serving of images, stylesheets, and javascripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"

# Disable delivery errors, bad email addresses will be ignored
# config.action_mailer.raise_delivery_errors = false
22 changes: 22 additions & 0 deletions examples/rails/config/environments/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Settings specified here will take precedence over those in config/environment.rb

# The test environment is used exclusively to run your application's
# test suite. You never need to work with it otherwise. Remember that
# your test database is "scratch space" for the test suite and is wiped
# and recreated between test runs. Don't rely on the data there!
config.cache_classes = true

# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true

# Show full error reports and disable caching
config.action_controller.consider_all_requests_local = true
config.action_controller.perform_caching = false

# Disable request forgery protection in test environment
config.action_controller.allow_forgery_protection = false

# Tell Action Mailer not to deliver emails to the real world.
# The :test delivery method accumulates sent emails in the
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test
10 changes: 10 additions & 0 deletions examples/rails/config/initializers/inflections.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Be sure to restart your server when you modify this file.

# Add new inflection rules using the following format
# (all these examples are active by default):
# ActiveSupport::Inflector.inflections do |inflect|
# inflect.plural /^(ox)$/i, '\1en'
# inflect.singular /^(ox)en/i, '\1'
# inflect.irregular 'person', 'people'
# inflect.uncountable %w( fish sheep )
# end
5 changes: 5 additions & 0 deletions examples/rails/config/initializers/mime_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Be sure to restart your server when you modify this file.

# Add new mime types for use in respond_to blocks:
# Mime::Type.register "text/richtext", :rtf
# Mime::Type.register_alias "text/html", :iphone
17 changes: 17 additions & 0 deletions examples/rails/config/initializers/new_rails_defaults.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# These settings change the behavior of Rails 2 apps and will be defaults
# for Rails 3. You can remove this initializer when Rails 3 is released.

if defined?(ActiveRecord)
# Include Active Record class name as root for JSON serialized output.
ActiveRecord::Base.include_root_in_json = true

# Store the full class name (including module namespace) in STI type column.
ActiveRecord::Base.store_full_sti_class = true
end

# Use ISO 8601 format for JSON serialized times and dates.
ActiveSupport.use_standard_json_time_format = true

# Don't escape HTML entities in JSON, leave that for the #json_escape helper.
# if you're including raw json in an HTML page.
ActiveSupport.escape_html_entities_in_json = false
4 changes: 4 additions & 0 deletions examples/rails/config/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ActionController::Routing::Routes.draw do |map|
map.root :controller => "simple_example"
map.connect ':controller/:action/:id'
end
Loading

0 comments on commit 83a0bb3

Please sign in to comment.