Module: Softwear::Library::ControllerAuthentication

Extended by:
ActiveSupport::Concern
Defined in:
lib/softwear/library/controller_authentication.rb

Defined Under Namespace

Classes: NotSignedInError

Instance Method Summary collapse

Instance Method Details

#auth_server_down(error) ⇒ Object

Action called when a NotSignedInError is raised.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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!Object

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



77
78
79
80
81
82
83
84
85
86
87
88
# 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_userObject



90
91
92
93
94
95
96
97
98
# 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_pathObject



116
117
118
# 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) ⇒ Object



125
126
127
128
# 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_urlObject

– url uelpers –



106
107
108
109
110
111
112
113
114
# 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_classObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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_inObject

Action called when a NotSignedInError is raised.



40
41
42
43
44
45
46
47
# 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) ⇒ Object



120
121
122
123
# 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?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/softwear/library/controller_authentication.rb', line 100

def user_signed_in?
  !!current_user
end

#users_pathObject



130
131
132
# File 'lib/softwear/library/controller_authentication.rb', line 130

def users_path
  softwear_hub_url + "/users"
end