From d8b89042b46f94b767a800ce74c9fe0e00a6bf85 Mon Sep 17 00:00:00 2001 From: Nicolae Claudius Date: Fri, 14 Oct 2011 13:02:11 -0700 Subject: [PATCH] validate domain ownership --- app/models/domain.rb | 9 +++++++++ db/migrate/20111014194447_index_domains_user_id.rb | 5 +++++ 2 files changed, 14 insertions(+) create mode 100644 db/migrate/20111014194447_index_domains_user_id.rb diff --git a/app/models/domain.rb b/app/models/domain.rb index c517994..a1c8089 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -34,6 +34,15 @@ class Domain < ActiveRecord::Base :minimum => 2, :maximum => 10, :message => "must have be at least 2, at most 10"} validates_associated :records validates :user_id, :presence => true + validate do # domain ownership + segments = name.split('.') + if segments.size > 2 + parent = segments[1..-1].join('.') + unless Domain.exists?(:name => parent, :user_id => user_id) + errors.add :name, "issue, must create the domain named `#{parent}` first" + end + end + end def slave?; self.type == 'SLAVE' end diff --git a/db/migrate/20111014194447_index_domains_user_id.rb b/db/migrate/20111014194447_index_domains_user_id.rb new file mode 100644 index 0000000..7c8ca11 --- /dev/null +++ b/db/migrate/20111014194447_index_domains_user_id.rb @@ -0,0 +1,5 @@ +class IndexDomainsUserId < ActiveRecord::Migration + def change + add_index :domains, :user_id + end +end