Module: QAuthRubyClient::SessionsHelper

Defined in:
app/helpers/q_auth_ruby_client/sessions_helper.rb

Instance Method Summary collapse

Instance Method Details

#current_userObject

This method is widely used to create the @current_user object from the session This method will return @current_user if it already exists which will save queries when called multiple times



37
38
39
40
41
42
# File 'app/helpers/q_auth_ruby_client/sessions_helper.rb', line 37

def current_user
  session[:qarc] = "true"
  return @current_user if @current_user
  # Check if the user exists with the auth token present in session
  @current_user = QAuthRubyClient::User.where("q_auth_uid = ?", session[:id]).first
end

#default_redirect_url_after_sign_inObject

Returns the default URL to which the system should redirect the user after successful authentication



5
6
7
# File 'app/helpers/q_auth_ruby_client/sessions_helper.rb', line 5

def 
  QAuthRubyClient.configuration.
end

#default_redirect_url_after_sign_outObject

Returns the default URL to which the system should redirect after the user successfully logout



10
11
12
# File 'app/helpers/q_auth_ruby_client/sessions_helper.rb', line 10

def default_redirect_url_after_sign_out
  QAuthRubyClient.configuration.default_redirect_url_after_sign_out
end

#redirect_to_sign_in_pageObject



18
19
20
21
22
23
24
25
26
# File 'app/helpers/q_auth_ruby_client/sessions_helper.rb', line 18

def 
  respond_to do |format|
    format.html {
      redirect_to q_auth_ruby_client.
    }
    format.json { render json: {heading: @heading, alert: @alert} }
    format.js { render(:partial => 'sessions/redirect.js.erb', :handlers => [:erb], :formats => [:js]) }
  end
end

#redirect_url_after_sign_inObject



14
15
16
# File 'app/helpers/q_auth_ruby_client/sessions_helper.rb', line 14

def 
  params[:redirect_back_url] || 
end

#require_adminObject

This method is usually used as a before filter from admin controllers to ensure that the logged in user is an admin



63
64
65
66
67
68
69
# File 'app/helpers/q_auth_ruby_client/sessions_helper.rb', line 63

def require_admin
  unless @current_user.is_admin?
    set_notification_messages(I18n.t("authentication.permission_denied_heading"), I18n.t("authentication.permission_denied_message"), :error)
    
    return
  end
end

#require_super_adminObject

This method is usually used as a before filter from admin controllers to ensure that the logged in user is a super admin



72
73
74
75
76
77
# File 'app/helpers/q_auth_ruby_client/sessions_helper.rb', line 72

def require_super_admin
  unless @current_user.is_super_admin?
    set_notification_messages(I18n.t("authentication.permission_denied_heading"), I18n.t("authentication.permission_denied_message"), :error)
    
  end
end

#require_userObject

This method is usually used as a before filter to secure some of the actions which requires the user to be signed in.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/helpers/q_auth_ruby_client/sessions_helper.rb', line 45

def require_user
  current_user
  if @current_user
    if @current_user.token_expired?
      @current_user = nil
      session.delete(:id)
      set_notification_messages(I18n.t("authentication.session_expired_heading"), I18n.t("authentication.session_expired_message"), :error)
      
      return
    end
  else
    set_notification_messages(I18n.t("authentication.permission_denied_heading"), I18n.t("authentication.permission_denied_message"), :error)
    
    return
  end
end

#update_user_profile_data_and_auth_tokenObject



28
29
30
31
32
33
# File 'app/helpers/q_auth_ruby_client/sessions_helper.rb', line 28

def 
  # Store the user object and Redirect to the Q-Auth sign in page with required params
  params_hsh = {client_app: QAuthRubyClient.configuration.q_app_name, redirect_back_url: create_session_url}
  url = add_query_params(QAuthRubyClient.configuration.q_auth_url, params_hsh)
  redirect_to url
end