Browse Source

validate when parent is at another user

pull/1/head
Nicolae Claudius 13 years ago
parent
commit
b24dcbf2d1
  1. 13
      app/models/domain.rb
  2. 6
      spec/models/domain_spec.rb

13
app/models/domain.rb

@ -46,8 +46,19 @@ class Domain < ActiveRecord::Base
end
validate :domain_ownership
def domain_ownership # at least one NS is among ours
def domain_ownership
# non-TLD validation
errors[:name] = "cannot be a TLD or a reserved domain" if Tld.include?(name)
# if parent domain is on our system, the user must own it
segments = name.split('.')
if segments.size >= 2
parent = segments[1..-1].join('.')
parent_domain = Domain.find_by_name(parent)
if parent_domain.present? && parent_domain.user_id != user_id
errors[:name] = "issue, the parent domain `#{parent}` is registered to another user"
end
end
end
def slave?; self.type == 'SLAVE' end

6
spec/models/domain_spec.rb

@ -49,9 +49,13 @@ describe Domain do
it "validates ownership" do
domain.name = 'co.uk'
domain.should have(1).errors_on(:name)
domain.name = 'clyfe.ro'
domain.should be_valid
# stub a parent domain on another user account
Domain.stub_chain(:find_by_name, :user_id).and_return(domain.user_id+1)
domain.should have(1).errors_on(:name)
end
end

Loading…
Cancel
Save