Skip to content

Commit

Permalink
Added an execute method to domain for cross domain database access.
Browse files Browse the repository at this point in the history
Added tests for the domains and systems controllers.
  • Loading branch information
Doug Youch committed Jun 22, 2010
1 parent ac85a0c commit d92d906
Show file tree
Hide file tree
Showing 6 changed files with 432 additions and 30 deletions.
62 changes: 32 additions & 30 deletions app/controllers/manage/domains_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@ def validate_domain

layout "manage"

# need to include
include ActiveTable::Controller
active_table :domains_table,
# need to include
include ActiveTable::Controller
active_table :domains_table,
Domain,
[ ActiveTable::StringHeader.new('domains.name',:label => 'Domain Name'),
ActiveTable::StaticHeader.new('Client'),
ActiveTable::StaticHeader.new('Type'),
ActiveTable::StaticHeader.new('Status'),
ActiveTable::StaticHeader.new('Goto'),
ActiveTable::StaticHeader.new('Delete')

[ hdr(:string, 'domains.name', :label => 'Domain Name'),
hdr(:static, 'Client'),
hdr(:static, 'Type'),
hdr(:static, 'Status'),
hdr(:static, 'Goto'),
hdr(:static, 'Delete')
]

def domains_table(display=true)
Expand All @@ -62,6 +61,10 @@ def edit
flash[:notice] = 'Domain is currently initializing and cannot be edited'
redirect_to :action => 'index'
return
when 'working':
flash[:notice] = 'Domain is currently initializing and cannot be edited'
redirect_to :action => 'index'
return
when 'setup':
redirect_to :action => 'setup', :path => @domain.id
return
Expand All @@ -78,7 +81,7 @@ def edit
:max_file_storage => params[:domain][:max_file_storage])

if @domain.email_enabled
DomainEmail.setup_domain_emails
@domain.execute { DomainEmail.setup_domain_emails }
end
flash.now[:notice] = 'Updated Domain Options'
end
Expand All @@ -91,7 +94,7 @@ def edit
end

def update_module
if self.system_admin? && request.post?
if self.system_admin? && request.post? && @domain.status == 'initialized'
mod = params[:mod]
entry = @domain.domain_modules.find_by_name(mod) || @domain.domain_modules.build(:name => mod)
entry.access = params[:access] == 'available' ? 'available' : 'unavailable'
Expand All @@ -104,14 +107,6 @@ def update_module
def add
cms_page_info [ ['System',url_for(:controller => '/manage/system')], ['Domains',url_for(:controller => '/manage/domains')], 'Add Domain'],'system'

if self.client.can_add_database?
@domain = Domain.new
else
flash[:notice] = 'Database Limit Reached'
redirect_to :action => 'index'
return
end

if request.post? && params[:domain]
if params[:commit]
@domain = Domain.new(params[:domain])
Expand Down Expand Up @@ -140,6 +135,10 @@ def setup
flash[:notice] = 'Domain is currently initializing and cannot be edited'
redirect_to :action => 'index'
return
when 'working':
flash[:notice] = 'Domain is currently initializing and cannot be edited'
redirect_to :action => 'index'
return
when 'initialized':
redirect_to :action => 'edit', :path => @domain.id
return
Expand All @@ -150,15 +149,19 @@ def setup
if request.post? && params[:domain]
if @domain.domain_type == 'domain'
if params[:domain][:database] == 'create'
@domain.attributes = params[:domain].slice(:www_prefix,:active)
@domain.max_file_storage = params[:domain][:max_file_storage].blank? ? DomainDatabase::DEFAULT_MAX_FILE_STORAGE : params[:domain][:max_file_storage].to_i

@domain.status = 'initializing'
if @domain.save
DomainModel.run_worker('Domain',@domain.id,'initialize_database')
flash[:notice] = 'Initializing the %s Domain' / @domain.name
redirect_to :action => 'index'
return
if self.client.can_add_database?
@domain.attributes = params[:domain].slice(:www_prefix,:active)
@domain.max_file_storage = params[:domain][:max_file_storage].blank? ? DomainDatabase::DEFAULT_MAX_FILE_STORAGE : params[:domain][:max_file_storage].to_i

@domain.status = 'initializing'
if @domain.save
DomainModel.run_worker('Domain',@domain.id,'initialize_database')
flash[:notice] = 'Initializing the %s Domain' / @domain.name
redirect_to :action => 'index'
return
end
else
flash[:notice] = 'Database Limit Reached'
end
else
@copy_domain = @domain.client.domains.find_by_id(params[:domain][:database])
Expand Down Expand Up @@ -201,7 +204,6 @@ def destroy
redirect_to :action => "index"
return
end

end

flash[:notice] = 'Could not delete domain'
Expand Down
10 changes: 10 additions & 0 deletions app/models/domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def after_save #:nodoc:
self.domain_database.save
elsif @max_file_storage
self.create_domain_database :client_id => self.client_id, :name => self.database, :max_file_storage => @max_file_storage
@max_file_storage = nil
self.save
else
DataCache.set_domain_info(self.name,nil)
end
Expand Down Expand Up @@ -180,4 +182,12 @@ def self.each(env='production', ids=nil)
yield dmn
end
end

def execute(environment='production')
active_domain_id = DomainModel.active_domain_id
active_domain = DomainModel.active_domain
DomainModel.activate_domain(self.get_info, environment) unless active_domain_id == self.id
yield
DomainModel.activate_domain(active_domain, environment) unless active_domain_id == self.id || active_domain_id.nil?
end
end
19 changes: 19 additions & 0 deletions spec/controllers/manage/clients_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@
response.should redirect_to(:controller => '/manage/access', :action => 'denied')
end

it "should redirect if not a system admin" do
@user = @client.client_users.create :username => 'my_system_admin', :client_admin => 0, :system_admin => 0
@myself = @user.end_user
controller.should_receive('myself').at_least(:once).and_return(@myself)
get 'index'
response.should redirect_to(:controller => '/manage/access', :action => 'denied')
end

it "should redirect if not a system admin" do
mock_user
get 'index'
response.should redirect_to(:controller => '/manage/access', :action => 'denied')
end

it "should redirect if not a system admin" do
get 'index'
response.should redirect_to(:controller => '/manage/access', :action => 'denied')
end

describe "System Administrators" do
before(:each) do
@user = @client.client_users.create :username => 'my_system_admin', :client_admin => 1, :system_admin => 1
Expand Down
Loading

0 comments on commit d92d906

Please sign in to comment.