Module: RackCASRails::ActionControllerBaseAdditions

Defined in:
lib/rack-cas-rails/action_controller_base_additions.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/rack-cas-rails/action_controller_base_additions.rb', line 60

def self.included(base)
  # Expose newly added instance methods as helpers
  base.helper_method :authenticate!
  base.helper_method :authenticated?
  base.helper_method :login_url
  base.helper_method :logout_url
end

Instance Method Details

#authenticate!Object

When invoked, will force authenticate. Most likely to be invoked as a before_action.



9
10
11
12
13
14
15
16
# File 'lib/rack-cas-rails/action_controller_base_additions.rb', line 9

def authenticate!
  return if authenticated?
  if File.exist?("public/401.html")
    render(:file => "public/401.html", :status => :unauthorized)
  else
    render(:plain => "Unauthorized!", :status => :unauthorized)
  end
end

#authenticated?Bool

Determines whether the current request belongs to a session that is authenticated or not.

Returns:

  • (Bool)

    True if current request belongs to an authenticated session, false otherwise.



22
23
24
# File 'lib/rack-cas-rails/action_controller_base_additions.rb', line 22

def authenticated?
  request.session["cas"] && request.session["cas"]["user"]
end

#login_url(service_url = request.url) ⇒ String

Renders the CAS login URL with re-direct back to some URL.

Parameters:

  • service_url (String) (defaults to: request.url)

    Optional url to redirect to after authentication.

Returns:

  • (String)

    The CAS login URL.



31
32
33
34
35
36
# File 'lib/rack-cas-rails/action_controller_base_additions.rb', line 31

def (service_url=request.url)
  url = URI(Rails.application.cas_server_url)
  url.path = "/login"
  url.query = "service=#{service_url || request.url}"
  url.to_s
end

#logout_url(service_url = request.url) ⇒ String

Note:

This helper depends on your application having a root route, so that the root_url helper is defined. Otherwise, it degrades to current request URL’s scheme + host + port + “/logout”, which may not be what you want, especially in sub-URI hosting situations.

Renders the CAS logout URL with re-direct back to some URL (e.g. the root URL). The logout path is “/logout”, which is actually undocumented. I had to find out by looking into the source code of the rack-cas gem.

Parameters:

  • service_url (String) (defaults to: request.url)

    Optional url to redirect to after authentication.

Returns:

  • (String)

    The CAS logout URL.



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rack-cas-rails/action_controller_base_additions.rb', line 48

def logout_url(service_url=request.url)
  if self.respond_to?(:root_url)
    url = URI(root_url)
    url.path += "logout"
  else
    url = URI(request.url)
    url.path = "/logout"
  end
  url.query = "service=#{service_url || request.url}"
  url.to_s
end