Sha256: bea034ac87d8aaf4d9f41316cd54b3485a54509d2a820b472d915f5fca08f8f5

Contents?: true

Size: 891 Bytes

Versions: 28

Compression:

Stored size: 891 Bytes

Contents

module TkhAuthenticationActionControllerExtension
  def self.included(base)
    base.send(:include, InstanceMethods) 
  end

  module InstanceMethods
    def current_user
      @current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token]
    end

    def authenticate
      if current_user.nil?
        session[:target_page] = request.url
        redirect_to login_url, alert: t('authentication.warning.login_needed')
      end
    end
    
    def authenticate_with_admin
      unless administrator?
        session[:target_page] = request.url if session[:target_page].nil?
        redirect_to safe_root_url, alert: t('authentication.warning.restricted_access')
      end
    end
    
    def administrator?
      current_user && current_user.admin?
    end
    
    private
    
    def safe_root_url
      defined?(root_url) ? root_url : '/'
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
tkh_authentication-0.9.15 lib/tkh_authentication/tkh_authentication_action_controller_extension.rb
tkh_authentication-0.9.14 lib/tkh_authentication/tkh_authentication_action_controller_extension.rb
tkh_authentication-0.9.13 lib/tkh_authentication/tkh_authentication_action_controller_extension.rb
tkh_authentication-0.9.12 lib/tkh_authentication/tkh_authentication_action_controller_extension.rb
tkh_authentication-0.9.11 lib/tkh_authentication/tkh_authentication_action_controller_extension.rb
tkh_authentication-0.9.10 lib/tkh_authentication/tkh_authentication_action_controller_extension.rb
tkh_authentication-0.9.9 lib/tkh_authentication/tkh_authentication_action_controller_extension.rb
tkh_authentication-0.9.8 lib/tkh_authentication/tkh_authentication_action_controller_extension.rb
tkh_authentication-0.9.7 lib/tkh_authentication/tkh_authentication_action_controller_extension.rb
tkh_authentication-0.9.6 lib/tkh_authentication/tkh_authentication_action_controller_extension.rb
tkh_authentication-0.9.5 lib/tkh_authentication/tkh_authentication_action_controller_extension.rb
tkh_authentication-0.9.4 lib/tkh_authentication/tkh_authentication_action_controller_extension.rb
tkh_authentication-0.9.3 lib/tkh_authentication/tkh_authentication_action_controller_extension.rb
tkh_authentication-0.1.11 lib/tkh_authentication/tkh_authentication_action_controller_extension.rb
tkh_authentication-0.1.10 lib/tkh_authentication/tkh_authentication_action_controller_extension.rb
tkh_authentication-0.1.9 lib/tkh_authentication/tkh_authentication_action_controller_extension.rb
tkh_authentication-0.1.8 lib/tkh_authentication/tkh_authentication_action_controller_extension.rb
tkh_authentication-0.9.2 lib/tkh_authentication/tkh_authentication_action_controller_extension.rb
tkh_authentication-0.9.1 lib/tkh_authentication/tkh_authentication_action_controller_extension.rb
tkh_authentication-0.9 lib/tkh_authentication/tkh_authentication_action_controller_extension.rb