Class: Kadmin::AuthController

Inherits:
ApplicationController show all
Defined in:
app/controllers/kadmin/auth_controller.rb

Constant Summary collapse

SESSION_KEY =
'kadmin.user'.freeze
SESSION_ORG_OVERWRITE_KEY =
'kadmin.org_overwrite'.freeze

Endpoints collapse

Helpers collapse

Methods inherited from ApplicationController

#handle_error, #handle_unexpected_error, #not_found, #organization, #params_missing, #scoped_all, #scoped_find_by!

Methods included from Concerns::AuthorizedUser

#authorize, #authorized?, #authorized_user, #current_user, #logged_in?

Instance Method Details

#change_organizationObject

POST /change_organization



65
66
67
68
69
70
# File 'app/controllers/kadmin/auth_controller.rb', line 65

def change_organization
  if authorized_user&.admin?
    session[SESSION_ORG_OVERWRITE_KEY] = Kadmin::Organization.find(params[:organization_id]).name
  end
  redirect_to :dash
end

#failureObject

GET /auth/failure



52
53
54
55
# File 'app/controllers/kadmin/auth_controller.rb', line 52

def failure
  flash.alert = params[:message]
  redirect_to (origin: request.env['omniauth.origin'])
end

#loginObject

GET /auth/login



11
12
13
14
15
16
17
# File 'app/controllers/kadmin/auth_controller.rb', line 11

def 
  if logged_in? && authorized?
    redirect_to dash_path
  else
    render 'kadmin/auth/login'
  end
end

#logoutObject

GET /auth/logout DELETE /auth/logout



21
22
23
24
# File 'app/controllers/kadmin/auth_controller.rb', line 21

def logout
  session.delete(SESSION_KEY)
  redirect_to 
end


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/controllers/kadmin/auth_controller.rb', line 88

def omniauth_provider_link
  auth_prefix = auth_path
  provider_link = "#{auth_prefix}/#{Kadmin::Auth.omniauth_provider}"
  origin = params[:origin]

  # if the referer is a auth route, then we risk ending in an endless loop
  if origin.blank?
    referer = request.referer
    if referer.blank?
      origin = Kadmin.config.mount_path
    else
      uri = URI(referer)
      origin = referer unless uri&.path&.start_with?(auth_prefix)
    end
  end

  provider_link = "#{provider_link}?origin=#{CGI.escape(origin)}" if origin.present?
  return provider_link
end

#saveObject

GET /auth/:provider/callback POST /auth/:provider/callback



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/kadmin/auth_controller.rb', line 28

def save
  auth_hash = request.env['omniauth.auth']

  if auth_hash.blank?
    Kadmin.logger.error('No authorization hash provided')
    flash.alert = I18n.t('kadmin.auth.error')
    redirect_to (origin: request.env['omniauth.origin'])
    return
  end

  email = auth_hash.dig('info', 'email')
  if Kadmin::Auth.users.exists?(email)
    session[SESSION_KEY] = email
    redirect_url = request.env['omniauth.origin']
    redirect_url = Kadmin.config.mount_path unless valid_redirect_url?(redirect_url)
  else
    flash.alert = I18n.t('kadmin.auth.unauthorized_message')
    redirect_url = (origin: request.env['omniauth.origin'])
  end

  redirect_to redirect_url
end

#unauthorizedObject



57
58
59
60
61
62
# File 'app/controllers/kadmin/auth_controller.rb', line 57

def unauthorized
  render 'kadmin/error', format: ['html'], locals: {
    title: I18n.t('kadmin.auth.unauthorized'),
    message: I18n.t('kadmin.auth.unauthorized_message')
  }
end