From b24dcbf2d1e00cebc68ee1764a209966ae439070 Mon Sep 17 00:00:00 2001 From: Nicolae Claudius Date: Thu, 27 Oct 2011 10:45:25 -0700 Subject: [PATCH] validate when parent is at another user --- app/models/domain.rb | 13 ++++++++++++- spec/models/domain_spec.rb | 6 +++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/models/domain.rb b/app/models/domain.rb index a00c523..48efa38 100644 --- a/app/models/domain.rb +++ b/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 diff --git a/spec/models/domain_spec.rb b/spec/models/domain_spec.rb index 0ed4aeb..13e40b5 100644 --- a/spec/models/domain_spec.rb +++ b/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