forked from theforeman/foreman
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #14013 - add a setting for default owner of new hosts
- Loading branch information
Showing
13 changed files
with
241 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
module Hostext | ||
module Ownership | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
OWNER_TYPES = %w(User Usergroup) | ||
belongs_to :owner, :polymorphic => true | ||
|
||
before_validation :set_default_user | ||
validates :owner_type, :inclusion => | ||
{:in => OWNER_TYPES, | ||
:allow_blank => true, | ||
:message => (_("Owner type needs to be one of the following: %s") % OWNER_TYPES.join(', '))} | ||
validate :validate_owner | ||
validate :owner_taxonomies_match, :if => Proc.new { |host| host.owner.is_a?(User) } | ||
end | ||
|
||
# method to return the correct owner list for host edit owner select dropbox | ||
def is_owned_by | ||
owner.id_and_type if owner | ||
end | ||
|
||
# virtual attributes which sets the owner based on the user selection | ||
# supports a simple user, or a usergroup | ||
# selection parameter is expected to be an ActiveRecord id_and_type method (see Foreman's AR extentions). | ||
def is_owned_by=(selection) | ||
owner = OwnerClassifier.new(selection).user_or_usergroup | ||
self.owner = owner | ||
end | ||
|
||
def owner_suggestion | ||
owner_id_and_type = Setting[:host_owner] | ||
owner = OwnerClassifier.new(owner_id_and_type).user_or_usergroup | ||
self.owner || owner || User.current | ||
end | ||
|
||
private | ||
|
||
def owner_taxonomies_match | ||
return true if self.owner.admin? | ||
|
||
if Organization.organizations_enabled && self.organization_id && !self.owner.my_organizations.where(:id => self.organization_id).exists? | ||
errors.add :is_owned_by, _("does not belong into host's organization") | ||
end | ||
if Location.locations_enabled && self.location_id && !self.owner.my_locations.where(:id => self.location_id).exists? | ||
errors.add :is_owned_by, _("does not belong into host's location") | ||
end | ||
end | ||
|
||
def set_default_user | ||
return if self.owner_type.present? && (!OWNER_TYPES.include?(self.owner_type) || self.owner.nil?) | ||
self.owner = owner_suggestion | ||
end | ||
|
||
def validate_owner | ||
return true if self.owner_type.nil? && self.owner.nil? | ||
|
||
add_owner_error if self.owner_type.present? && self.owner.nil? | ||
end | ||
|
||
def add_owner_error | ||
if self.owner_id.present? | ||
errors.add(:owner, (_('There is no owner with id %d and type %s') % [self.owner_id, self.owner_type])) | ||
else | ||
errors.add(:owner, _('If owner type is specified, owner must be specified too.')) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class OwnerClassifier | ||
def initialize(id_and_type) | ||
@id_and_type = id_and_type | ||
end | ||
|
||
def user_or_usergroup | ||
return nil unless @id_and_type =~ /^\d+-(Users|Usergroups)$/ | ||
@id_and_type.include?('Users') ? User.find_by_id(@id_and_type.to_i) : Usergroup.find_by_id(@id_and_type.to_i) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.