Skip to content

Commit

Permalink
Merge pull request #90 from restarone/bootstrapApplicationInstallFlow
Browse files Browse the repository at this point in the history
Bootstrap application install flow (customize root domain)
  • Loading branch information
donrestarone authored May 20, 2021
2 parents f24f355 + 350c665 commit bfc3ff8
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 60 deletions.
2 changes: 1 addition & 1 deletion app/controllers/admin/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def ensure_user_logged_in
end

def ensure_superuser
unless current_user.global_admin
unless current_user.global_admin && Apartment::Tenant.current == 'public'
flash.alert = 'you do not have permissions to access this'
redirect_to root_path
end
Expand Down
8 changes: 3 additions & 5 deletions app/controllers/subdomains/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ class Subdomains::BaseController < ApplicationController
layout "subdomains"

def ensure_user_belongs_to_subdomain
Apartment::Tenant.switch request.subdomain do
unless User.find_by(id: current_user.id)
flash.alert = 'You arent not authorized to visit that page'
redirect_to root_url
end
unless User.find_by(id: current_user.id)
flash.alert = 'You arent not authorized to visit that page'
redirect_to root_url
end
end
end
64 changes: 37 additions & 27 deletions app/models/subdomain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ def storage_used
end
end

def self.unsafe_bootstrap_www_subdomain
Apartment::Tenant.switch('public') do
Subdomain.bootstrap_via_comfy('public', 'www')
end
end

private

def drop_tenant
Expand All @@ -66,36 +72,40 @@ def downcase_subdomain_name
def create_cms_site
hostname = self.hostname
Apartment::Tenant.switch(self.name) do
site = Comfy::Cms::Site.create!(
identifier: self.name,
# this is only for local testing
hostname: hostname,
)
layout = site.layouts.create(
label: 'default',
identifier: 'default',
content: "{{cms:wysiwyg content}}",
app_layout: 'website'
)
page = layout.pages.create(
site_id: site.id,
label: 'root',
)
Comfy::Cms::Fragment.create!(
identifier: 'content',
record: page,
tag: 'wysiwyg',
content: "
<div>
<h1>Hello from #{self.name}</h1>
To access the admin panel for your website,
<a href='http://#{hostname}/admin' target='_blank'>click here</a>
</div>
"
)
Subdomain.bootstrap_via_comfy(self.name, hostname)
end
end

def self.bootstrap_via_comfy(name, hostname)

site = Comfy::Cms::Site.create!(
identifier: name,
hostname: hostname,
)
layout = site.layouts.create(
label: 'default',
identifier: 'default',
content: "{{cms:wysiwyg content}}",
app_layout: 'website'
)
page = layout.pages.create(
site_id: site.id,
label: 'root',
)
Comfy::Cms::Fragment.create!(
identifier: 'content',
record: page,
tag: 'wysiwyg',
content: "
<div>
<h1>Hello from #{name}</h1>
To access the admin panel for your website,
<a href='http://#{hostname}/admin' target='_blank'>click here</a>
</div>
"
)
end

def create_tenant
Apartment::Tenant.create(self.name)
end
Expand Down
14 changes: 10 additions & 4 deletions config/initializers/apartment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,18 @@
# Setup a custom Tenant switching middleware. The Proc should return the name of the Tenant that
# you want to switch to.
# Rails.application.config.middleware.use Apartment::Elevators::Generic, lambda { |request|
# request.host.split('.').first
# "www"
# }

# Rails.application.config.middleware.use Apartment::Elevators::Domain
Rails.application.config.middleware.use Apartment::Elevators::Subdomain
Apartment::Elevators::Subdomain.excluded_subdomains = ['www']
Rails.application.config.middleware.use Apartment::Elevators::Generic,
Proc.new { |request|
hostname = request.host.split('.')[0]
Apartment.tenant_names.include?(hostname) ? hostname : 'public'
}
# Rails.application.config.middleware.use Apartment::Elevators::Subdomain

# plug in exclusions model here
Apartment::Elevators::Subdomain.excluded_subdomains = []
# Rails.application.config.middleware.use Apartment::Elevators::FirstSubdomain
# Rails.application.config.middleware.use Apartment::Elevators::Host

30 changes: 16 additions & 14 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require 'sidekiq/web'
class SubdomainConstraint
def self.matches?(request)
subdomains = ['www', 'admin', 'help', 'info', 'contact']
request.subdomain.present? && !subdomains.include?(request.subdomain)
# plug in exclusions model here
subdomains = []
!subdomains.include?(request.subdomain)
end
end

Expand All @@ -29,17 +30,12 @@ def self.matches?(request)
end
end
end
resources :users, controller: 'comfy/admin/users', as: :admin_users, except: [:create, :show] do
collection do
post 'invite'
end
end

resources :users, controller: 'comfy/admin/users', as: :admin_users, except: [:create, :show] do
collection do
post 'invite'
end
comfy_route :cms_admin, path: "/admin"
comfy_route :blog, path: "blog"
comfy_route :blog_admin, path: "admin"
mount SimpleDiscussion::Engine => "/forum"
# cms comes last because its a catch all
comfy_route :cms, path: "/"
end

# system admin panel login
Expand All @@ -49,7 +45,7 @@ def self.matches?(request)
delete 'global_login', to: 'users/sessions#destroy'
end
# system admin panel authentication (ensure public schema as well)
get 'admin', to: 'admin/subdomain_requests#index'
get 'sysadmin', to: 'admin/subdomain_requests#index'
namespace :admin do
mount Sidekiq::Web => '/sidekiq'
resources :subdomain_requests, except: [:new, :create] do
Expand All @@ -61,7 +57,13 @@ def self.matches?(request)
resources :subdomains
end


comfy_route :cms_admin, path: "/admin"
comfy_route :blog, path: "blog"
comfy_route :blog_admin, path: "admin"
mount SimpleDiscussion::Engine => "/forum"
# cms comes last because its a catch all
comfy_route :cms, path: "/"

root to: 'content#index'


Expand Down
9 changes: 1 addition & 8 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)

Apartment::Tenant.switch('public') do
User.create!(email: '[email protected]', password: '123456', password_confirmation: '123456', global_admin: true, confirmed_at: Time.now)
Subdomain.unsafe_bootstrap_www_subdomain
end
6 changes: 5 additions & 1 deletion test/controllers/content_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
class ContentControllerTest < ActionDispatch::IntegrationTest
setup do
@user = users(:public)
@restarone_subdomain = Subdomain.find_by(name: 'restarone').name
Apartment::Tenant.switch @restarone_subdomain do
@restarone_user = User.find_by(email: '[email protected]')
end
end

test "should get index" do
get root_url
assert_response :success
get root_url(subdomain: @user.subdomain)
get root_url(subdomain: @restarone_user.subdomain)
assert_response :success
end
end

0 comments on commit bfc3ff8

Please sign in to comment.