Class: Kadmin::ApplicationController

Inherits:
ActionController::Base
  • Object
show all
Includes:
Concerns::AuthorizedUser
Defined in:
app/controllers/kadmin/application_controller.rb

Direct Known Subclasses

AuthController, DashController

Instance Method Summary collapse

Methods included from Concerns::AuthorizedUser

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

Instance Method Details

#handle_error(error, options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'app/controllers/kadmin/application_controller.rb', line 42

def handle_error(error, options = {})
  options = {
    title: error.try(:title) || error.class.name,
    message: error.message,
    status: :internal_server_error,
    error: error
  }.merge(options)
  render 'kadmin/error', status: options[:status], locals: options
end

#handle_unexpected_error(error) ⇒ Object



38
39
40
# File 'app/controllers/kadmin/application_controller.rb', line 38

def handle_unexpected_error(error)
  handle_error(error, title: I18n.t('kadmin.errors.unexpected'), message: I18n.t('kadmin.errors.unexpected_message'))
end

#not_found(error) ⇒ Object



34
35
36
# File 'app/controllers/kadmin/application_controller.rb', line 34

def not_found(error)
  handle_error(error, title: I18n.t('kadmin.errors.not_found'), status: :not_found)
end

#organizationObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/kadmin/application_controller.rb', line 69

def organization
  return @organization if authorized_user.blank?
  return @organization if @organization.present?

  if session[Kadmin::AuthController::SESSION_ORG_OVERWRITE_KEY] && authorized_user.admin?
    @organization = Kadmin::Organization.find_by!(name: session[AuthController::SESSION_ORG_OVERWRITE_KEY])
  else
    @organization = Kadmin::Organization.find_by!(name: authorized_user.organization)
  end

  return @organization
rescue ActiveRecord::RecordNotFound
  render plain: "Forbidden - organization #{authorized_user.organization} not found in DB", status: :forbidden
end

#params_missing(error) ⇒ Object



30
31
32
# File 'app/controllers/kadmin/application_controller.rb', line 30

def params_missing(error)
  handle_error(error, title: I18n.t('kadmin.errors.params_missing'), status: :bad_request)
end

#scoped_all(organization_scoped_ar) ⇒ Object

returns all organization_scoped_ar object(s) that are of the user’s organization. admin user gets all. you can chain scopes, e.g. scoped_all(Segments.my_scope) is valid organization_scoped_ar is an ActiveRecord that has organization_scope(Organization) scope defined



65
66
67
# File 'app/controllers/kadmin/application_controller.rb', line 65

def scoped_all(organization_scoped_ar)
  organization_scoped_ar.organization_scope(organization).all
end

#scoped_find_by!(organization_scoped_ar, id) ⇒ Object

returns organization_scoped_ar object(s) by id (or array of ids) or throw RecordNotFound in case id(s) does not exist or is not visible in scope

organization_scoped_ar is an ActiveRecord that has organization_scope(Organization) scope defined



58
59
60
# File 'app/controllers/kadmin/application_controller.rb', line 58

def scoped_find_by!(organization_scoped_ar, id)
  return organization_scoped_ar.organization_scope(@organization).find(id)
end