Skip to content

Commit

Permalink
Fixes #28769 - add more loader macros
Browse files Browse the repository at this point in the history
There are multiple objects that has Jail defined but can't be loaded in
a sane way. This patch adds load_* macros for all such objects. It also
extracts loaders from base macros to separate file. Load macros are
defined dynamically to avoid repetition of definition and to unify
capabilities of all such macros.
  • Loading branch information
ares committed Jan 15, 2020
1 parent 0f4cd82 commit 89aea4f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 29 deletions.
4 changes: 4 additions & 0 deletions app/models/taxonomies/location.rb
Expand Up @@ -22,6 +22,10 @@ class Location < Taxonomy
user.admin? ? all : where(id: user.location_and_child_ids)
}

class Jail < ::Safemode::Jail
allow :id, :name, :title, :created_at, :updated_at, :description
end

def dup
new = super
new.organizations = organizations
Expand Down
4 changes: 4 additions & 0 deletions app/models/taxonomies/organization.rb
Expand Up @@ -22,6 +22,10 @@ class Organization < Taxonomy
user.admin? ? all : where(id: user.organization_and_child_ids)
}

class Jail < ::Safemode::Jail
allow :id, :name, :title, :created_at, :updated_at, :description
end

def dup
new = super
new.locations = locations
Expand Down
7 changes: 5 additions & 2 deletions lib/foreman/renderer/configuration.rb
Expand Up @@ -89,18 +89,21 @@ class Configuration
:update_ip_from_built_request,
]

DEFAULT_ALLOWED_LOADERS = Foreman::Renderer::Scope::Macros::Loaders::LOADERS.map(&:first)

def initialize
@allowed_variables = DEFAULT_ALLOWED_VARIABLES
@allowed_global_settings = DEFAULT_ALLOWED_GLOBAL_SETTINGS
@allowed_generic_helpers = DEFAULT_ALLOWED_GENERIC_HELPERS
@allowed_host_helpers = DEFAULT_ALLOWED_HOST_HELPERS
@allowed_loaders = DEFAULT_ALLOWED_LOADERS
end

attr_accessor :allowed_variables, :allowed_global_settings,
:allowed_generic_helpers, :allowed_host_helpers
:allowed_generic_helpers, :allowed_host_helpers, :allowed_loaders

def allowed_helpers
allowed_generic_helpers + allowed_host_helpers
allowed_generic_helpers + allowed_host_helpers + allowed_loaders
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/foreman/renderer/scope/base.rb
Expand Up @@ -4,6 +4,7 @@ module Scope
class Base
include Foreman::Renderer::Scope::Variables
include Foreman::Renderer::Scope::Macros::Base
include Foreman::Renderer::Scope::Macros::Loaders
include Foreman::Renderer::Scope::Macros::TemplateLogging
include Foreman::Renderer::Scope::Macros::SnippetRendering

Expand Down
27 changes: 0 additions & 27 deletions lib/foreman/renderer/scope/macros/base.rb
Expand Up @@ -90,10 +90,6 @@ def default_template_url(template, hostgroup)
:host => host, :port => port)
end

def load_hosts(search: '', includes: nil, preload: nil)
load_resource(klass: Host, search: search, permission: 'view_hosts', includes: includes, preload: preload)
end

def all_host_statuses
@all_host_statuses ||= HostStatus.status_registry.to_a.sort_by(&:status_name)
end
Expand All @@ -108,10 +104,6 @@ def host_status(host, name)
host.get_status(klass)
end

def load_users(search: '', includes: nil, preload: nil)
load_resource(klass: User, search: search, permission: :view_users, includes: includes, preload: preload)
end

def user_auth_source_name(user)
user.auth_source&.name
end
Expand Down Expand Up @@ -161,25 +153,6 @@ def host_virtual(host)
def validate_subnet(subnet)
raise WrongSubnetError.new(object_name: subnet.to_s, object_class: subnet.class.to_s) unless subnet.is_a?(Subnet)
end

# returns a batched relation, use either
# .each { |batch| batch.each { |record| record.name }}
# or
# .each_record { |record| record.name }
def load_resource(klass:, search:, permission:, batch: 1_000, includes: nil, limit: nil, select: nil, joins: nil, where: nil, preload: nil)
limit ||= 10 if preview?

base = klass
base = base.search_for(search)
base = base.preload(preload) unless preload.nil?
base = base.includes(includes) unless includes.nil?
base = base.joins(joins) unless joins.nil?
base = base.authorized(permission) unless permission.nil?
base = base.limit(limit) unless limit.nil?
base = base.where(where) unless where.nil?
base = base.select(select) unless select.nil?
base.in_batches(of: batch)
end
end
end
end
Expand Down
52 changes: 52 additions & 0 deletions lib/foreman/renderer/scope/macros/loaders.rb
@@ -0,0 +1,52 @@
module Foreman
module Renderer
module Scope
module Macros
module Loaders
include Foreman::Renderer::Errors

LOADERS = [
[ :load_organizations, Organization, :view_organizations ],
[ :load_locations, Location, :view_locations ],
[ :load_hosts, Host, :view_hosts ],
[ :load_operaitng_systems, Operatingsystem, :view_operatingsystems ],
[ :load_subnets, Subnet, :view_subnets ],
[ :load_smart_proxies, SmartProxy , :view_smart_proxies ],
[ :load_user_groups, Usergroup, :view_usergroups ],
[ :load_host_groups, Hostgroup, :view_hostgroups ],
[ :load_domains, Domain, :view_domains ],
[ :load_realms, Realm, :view_realms ],
[ :load_users, User, :view_users ]
]

LOADERS.each do |name, model, permission|
define_method name do |search: '', includes: nil, preload: nil, joins: nil, select: nil, batch: 1_000, limit: nil|
load_resource(klass: model, search: search, permission: permission, includes: includes, preload: preload, joins: joins, select: select, batch: batch, limit: limit)
end
end

private

# returns a batched relation, use either
# .each { |batch| batch.each { |record| record.name }}
# or
# .each_record { |record| record.name }
def load_resource(klass:, search:, permission:, batch: 1_000, includes: nil, limit: nil, select: nil, joins: nil, where: nil, preload: nil)
limit ||= 10 if preview?

base = klass
base = base.search_for(search)
base = base.preload(preload) unless preload.nil?
base = base.includes(includes) unless includes.nil?
base = base.joins(joins) unless joins.nil?
base = base.authorized(permission) unless permission.nil?
base = base.limit(limit) unless limit.nil?
base = base.where(where) unless where.nil?
base = base.select(select) unless select.nil?
base.in_batches(of: batch)
end
end
end
end
end
end

0 comments on commit 89aea4f

Please sign in to comment.