Skip to content

Commit

Permalink
fixes #3048 - Non Admin user fails to override parameters when creati…
Browse files Browse the repository at this point in the history
…ng new host
  • Loading branch information
Joseph Mitchell Magen authored and Dominic Cleal committed Oct 7, 2013
1 parent 3dd4c0e commit 0690964
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/models/concerns/host_common.rb
Expand Up @@ -33,7 +33,7 @@ def lookup_values_attributes= lookup_values_attributes
mark_for_destruction ? lookup_values.delete(lookup_value) : lookup_value.save!
end
elsif !ActiveRecord::ConnectionAdapters::Column.value_to_boolean attr.delete(:_destroy)
LookupValue.create(attr.merge(:match => lookup_value_match))
LookupValue.create(attr.merge(:match => lookup_value_match, :host_or_hostgroup => self))
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions app/models/lookup_value.rb
Expand Up @@ -7,6 +7,7 @@ class LookupValue < ActiveRecord::Base
before_validation :sanitize_match
before_validation :validate_and_cast_value
validate :validate_list, :validate_regexp
attr_accessor :host_or_hostgroup

serialize :value
attr_name :value
Expand Down Expand Up @@ -62,10 +63,10 @@ def enforce_permissions operation
allowed = case match
when /^fqdn=(.*)/
# check if current fqdn is in our allowed list
Host.my_hosts.where(:name => $1).exists?
Host.my_hosts.where(:name => $1).exists? || self.host_or_hostgroup.try(:new_record?)
when /^hostgroup=(.*)/
# check if current hostgroup is in our allowed list
Hostgroup.my_groups.where(:label => $1).exists?
Hostgroup.my_groups.where(:label => $1).exists? || self.host_or_hostgroup.try(:new_record?)
else
false
end
Expand Down
12 changes: 12 additions & 0 deletions test/unit/host_test.rb
Expand Up @@ -63,6 +63,18 @@ class HostTest < ActiveSupport::TestCase
assert !host.new_record?
end

test "non-admin user should be able to create host with new lookup value" do
User.current = users(:one)
User.current.roles << [roles(:manager)]
assert_difference('LookupValue.count') do
assert Host.create! :name => "abc.host123.com", :mac => "aabbecddeeff", :ip => "2.3.4.3",
:domain => domains(:mydomain), :operatingsystem => operatingsystems(:redhat),
:subnet => subnets(:one), :architecture => architectures(:x86_64), :puppet_proxy => smart_proxies(:puppetmaster),
:environment => environments(:production), :disk => "empty partition",
:lookup_values_attributes => {"new_123456" => {"lookup_key_id" => lookup_keys(:complex).id, "value"=>"some_value", "match" => "fqdn=abc.host123.com"}}
end
end

test "should import facts from json stream" do
h=Host.new(:name => "sinn1636.lan")
h.disk = "!" # workaround for now
Expand Down

0 comments on commit 0690964

Please sign in to comment.