module Softwear::Library::ControllerAuthentication

Public Instance Methods

auth_server_down(error) click to toggle source

Action called when a NotSignedInError is raised.

# File lib/softwear/library/controller_authentication.rb, line 52
def auth_server_down(error)
  respond_to do |format|
    format.html do
      render inline: \
        "<div class='panel panel-danger'>"\
          "<div class='panel-heading'>"\
            "<h3 class='panel-title'>#{error.message}</h3>"\
          "</div>"\
          "<div class='panel-body'>"\
            "Not all site functions will work until the problem is resolved. "\
            "<a href='javascript' onclick='history.go(-1);return false;' class='btn btn-default'>Go back.</a>"\
          "</div>"\
        "</div>"
    end

    format.js do
      render inline: "alert(\"#{error.message.gsub('"', '\"')}\");"
    end
  end
end
authenticate_user!() click to toggle source

Drop this into a before_filter to require a user be signed in on every request - just like in Devise.

# File lib/softwear/library/controller_authentication.rb, line 77
def authenticate_user!
  if user_token.blank?
    raise NotSignedInError, "No token"
  end

  if user = user_class.auth(user_token)
    @current_user = user
  else
    self.user_token = nil
    raise NotSignedInError, "Invalid token"
  end
end
current_user() click to toggle source
# File lib/softwear/library/controller_authentication.rb, line 90
def current_user
  return @current_user if @current_user

  if @current_user.nil? && !user_token.blank?
    @current_user = user_class.auth(user_token)
  else
    nil
  end
end
destroy_user_session_path() click to toggle source
# File lib/softwear/library/controller_authentication.rb, line 116
def destroy_user_session_path
  softwear_hub_url + "/users/sign_out"
end
edit_user_path(user) click to toggle source
# File lib/softwear/library/controller_authentication.rb, line 125
def edit_user_path(user)
  user_id = user.is_a?(user_class) ? user.id : user
  softwear_hub_url + "/users/#{user_id}/edit"
end
softwear_hub_url() click to toggle source

– url uelpers –

# File lib/softwear/library/controller_authentication.rb, line 106
def softwear_hub_url
  if Rails.env.production?
    Figaro.env.softwear_hub_url || (raise "Please set softwear_hub_url in application.yml")
  elsif Rails.env.test?
    'http://hub.example.com'
  else
    Figaro.env.softwear_hub_url || 'http://localhost:2995'
  end
end
user_class() click to toggle source
# File lib/softwear/library/controller_authentication.rb, line 22
def user_class
  if Softwear::Auth::Model.descendants.size > 1
    raise "More than one descendent of Softwear::Auth::Model is not supported."
  elsif Softwear::Auth::Model.descendants.size == 0
    # Assume there is a "User" model
    begin
      User.inspect
      return User if User.ancestors.include?(Softwear::Auth::Model)
    rescue NameError => _
    end
    raise "Please define a user model that extends Softwear::Auth::Model."
  end
  Softwear::Auth::Model.descendants.first
end
user_not_signed_in() click to toggle source

Action called when a NotSignedInError is raised.

# File lib/softwear/library/controller_authentication.rb, line 40
def user_not_signed_in
  if Softwear::Auth::Model::STUBBED
    self.user_token = "dummy-token"
    authenticate_user!
  else
    redirect_to softwear_hub_url + "/users/sign_in?#{{return_to: request.original_url}.to_param}"
  end
end
user_path(user) click to toggle source
# File lib/softwear/library/controller_authentication.rb, line 120
def user_path(user)
  user_id = user.is_a?(user_class) ? user.id : user
  softwear_hub_url + "/users/#{user_id}"
end
user_signed_in?() click to toggle source
# File lib/softwear/library/controller_authentication.rb, line 100
def user_signed_in?
  !!current_user
end
users_path() click to toggle source
# File lib/softwear/library/controller_authentication.rb, line 130
def users_path
  softwear_hub_url + "/users"
end

Private Instance Methods

user_token() click to toggle source
# File lib/softwear/library/controller_authentication.rb, line 136
def user_token
  session[:user_token]
end
user_token=(new_token) click to toggle source
# File lib/softwear/library/controller_authentication.rb, line 140
def user_token=(new_token)
  session[:user_token] = new_token
end