Class: Api::V2::BaseController

Inherits:
BaseController show all
Includes:
Api::Version2, Foreman::Controller::Authorize
Defined in:
app/controllers/api/v2/base_controller.rb

Direct Known Subclasses

ArchitecturesController, AuditsController, AuthSourceExternalsController, AuthSourceInternalsController, AuthSourceLdapsController, AuthSourcesController, AutosignController, BookmarksController, CommonParametersController, ComputeAttributesController, ComputeProfilesController, ComputeResourcesController, ConfigGroupsController, ConfigReportsController, DashboardController, DomainsController, EnvironmentsController, ExternalUsergroupsController, FactValuesController, FiltersController, HomeController, HostClassesController, HostgroupClassesController, HostgroupsController, HostsController, HttpProxiesController, ImagesController, InterfacesController, LocationsController, MailNotificationsController, MediaController, ModelsController, OperatingsystemsController, OrganizationsController, OsDefaultTemplatesController, OverrideValuesController, ParametersController, PermissionsController, PersonalAccessTokensController, PingController, PluginsController, ProvisioningTemplatesController, PtablesController, PuppetHostsController, PuppetclassesController, RealmsController, ReportTemplatesController, RolesController, SettingsController, SmartClassParametersController, SmartProxiesController, SshKeysController, StatisticsController, SubnetsController, TablePreferencesController, TasksController, TemplateCombinationsController, TemplateInputsController, TemplateKindsController, TrendsController, UsergroupsController, UsersController, NotificationRecipientsController, UserMenusController

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Api::Version2

#api_version

Methods inherited from BaseController

#api_request?, #controller_permission, #get_resource, parameter_filter_context, #parent_scope, #resource_class_join, #resource_scope, #resource_scope_for_index

Methods included from Foreman::Controller::BruteforceProtection

#bruteforce_attempt?

Methods included from ApplicationShared

#current_permission, #find_default_taxonomy, #find_session_taxonomy, #set_current_taxonomy, #set_current_user, #set_taxonomy, #store_default_taxonomy, #store_taxonomy

Methods included from FindCommon

#controller_permission, #find_resource, #resource_class, #resource_class_for, #resource_finder, #resource_name, #resource_scope, #scope_for

Methods included from Foreman::ThreadSession::Cleaner

#clear_thread

Methods included from Foreman::Controller::Timezone

#set_timezone

Methods included from Foreman::Controller::TopbarSweeper

#set_topbar_sweeper_controller

Methods included from Foreman::Controller::Session

#backup_session_content, #expire_session, #ignore_api_request?, #session_expiry, #set_activity_time, #update_activity_time

Methods included from Foreman::Controller::Authentication

#authenticate, #authorized, #available_sso, #check_user_enabled, #is_admin?, #path_to_authenticate, #require_login

Methods included from Foreman::Controller::MigrationChecker

needs_migration?

Class Method Details

.skip_before_action(*names) ⇒ Object


147
148
149
150
151
152
153
154
155
156
157
# File 'app/controllers/api/v2/base_controller.rb', line 147

def self.skip_before_action(*names)
  names = names.map do |n|
    if n == :check_content_type
      Foreman::Deprecation.deprecation_warning('2.3', '#check_content_type is renamed to #check_media_type')
      :check_media_type
    else
      n
    end
  end
  super(*names)
end

Instance Method Details

#append_array_of_ids(hash_params) ⇒ Object

For the purpose of ADDING/REMOVING associations in CHILD node on POST/PUT payload This method adds a Rails magic method (association_ids) based on the CHILD node ARRAY of OBJECTS Example: PUT api/operatingsystems/24 {

 "operatingsystem": {
   "id": 24,
   "name": "CentOs",
   "architectures": [
       {
           "name": "i386",
           "id": 2
       },
       {
           "name": "x86_64",
           "id": 1
       }
    ]
   }
}

Rails magic method (ex. architecture_ids) is added to params hash based on CHILD node (architectures)

 "operatingsystem": {
   "id": 24,
   "name": "CentOs",
   "architecture_ids": [1,2]
  }

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/controllers/api/v2/base_controller.rb', line 124

def append_array_of_ids(hash_params)
  model_name = controller_name.singularize
  hash_params&.dup&.each do |k, v|
    if v.is_a?(Array)
      association_name_ids = "#{k.singularize}_ids"
      association_name_names = "#{k.singularize}_names"
      if resource_class.instance_methods.map(&:to_s).include?(association_name_ids) && v.any? && v.all? { |a| a.key?("id") }
        params[model_name][association_name_ids] = v.map { |a| a["id"] }
        params[model_name].delete(k)
      elsif resource_class.instance_methods.map(&:to_s).include?(association_name_names) && v.any? && v.all? { |a| a.key?("name") }
        params[model_name][association_name_names] = v.map { |a| a["name"] }
        params[model_name].delete(k)
      end
    end
  end
end

#check_content_typeObject


159
160
161
162
# File 'app/controllers/api/v2/base_controller.rb', line 159

def check_content_type
  Foreman::Deprecation.deprecation_warning('2.3', '#check_content_type is renamed to #check_media_type')
  check_media_type
end

#check_media_typeObject


164
165
166
167
168
# File 'app/controllers/api/v2/base_controller.rb', line 164

def check_media_type
  if (request.post? || request.put?) && request.media_type != "application/json"
    render_error(:unsupported_media_type, :status => :unsupported_media_type)
  end
end

#metadata_byObject


83
84
85
# File 'app/controllers/api/v2/base_controller.rb', line 83

def 
  @by ||= (params[:order].present? && (order_array = params[:order].split(' ')).any?) ? order_array[0] : nil
end

#metadata_orderObject


79
80
81
# File 'app/controllers/api/v2/base_controller.rb', line 79

def 
  @order ||=  (params[:order].present? && (order_array = params[:order].split(' ')).any?) ? (order_array[1] || 'ASC') : nil
end

#metadata_pageObject


87
88
89
# File 'app/controllers/api/v2/base_controller.rb', line 87

def 
  @page ||= params[:page].present? ? params[:page].to_i : 1
end

#metadata_per_pageObject


91
92
93
94
# File 'app/controllers/api/v2/base_controller.rb', line 91

def 
  @per_page ||= Setting[:entries_per_page] if params[:per_page].empty?
  @per_page ||= params[:per_page] == 'all' ?  : params[:per_page].to_i
end

#metadata_searchObject


75
76
77
# File 'app/controllers/api/v2/base_controller.rb', line 75

def 
  @search ||= params[:search]
end

#metadata_subtotalObject


67
68
69
70
71
72
73
# File 'app/controllers/api/v2/base_controller.rb', line 67

def 
  if params[:search].present?
    @subtotal ||= instance_variable_get("@#{controller_name}").try(:count).to_i
  else
    @subtotal ||= 
  end
end

#metadata_totalObject


63
64
65
# File 'app/controllers/api/v2/base_controller.rb', line 63

def 
  @total ||= resource_scope.try(:size).to_i
end

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


170
171
172
173
174
# File 'app/controllers/api/v2/base_controller.rb', line 170

def render_error(error, options = { })
  options = set_error_details(error, options)
  render options.merge(:template => "api/v2/errors/#{error}",
                       :layout   => 'api/v2/layouts/error_layout')
end

#root_node_nameObject


53
54
55
56
57
58
59
60
61
# File 'app/controllers/api/v2/base_controller.rb', line 53

def root_node_name
  @root_node_name ||= if Rabl.configuration.use_controller_name_as_json_root
                        controller_name.split('/').last
                      elsif params['root_name'].present?
                        params['root_name']
                      else
                        Rabl.configuration.json_root_default_name
                      end
end

#setup_has_many_paramsObject


141
142
143
144
145
# File 'app/controllers/api/v2/base_controller.rb', line 141

def setup_has_many_params
  model_name = controller_name.singularize
  append_array_of_ids(params[model_name]) # wrapped params
  append_array_of_ids(params)             # unwrapped params
end