forked from radiant/radiant-multi-site-extension
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add scoped multi-site updates as described at http://blog.zerosum.org…
…/2008/4/3/radiant-super-multi * scopes user level access to individual sites (admins and developers still have access to all sites) * regular users cannot see, edit, or access other user sites that they do not own * optionally scopes layouts to sites * snippets usable everywhere, but display and editing of snippets limited to admins/developers updated for radiant 0.6.7
- Loading branch information
Showing
20 changed files
with
478 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<p> | ||
<label for="layout_site_id">Site </label> | ||
<%= select "layout", "site_id", [['<N/A>', nil]] + Site.find(:all).map { |s| [s.name, s.id] } %> | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<tr> | ||
<th class="label"><label class="optional" for="user_site_id">Site</label></td> | ||
<td class="field" style="text-align: left"><%= select "user", "site_id", [['<N/A>', nil]] + Site.find(:all).map { |s| [s.name, s.id] } %></td> | ||
<td class="help">Optional. Please specify a site for this user.</td> | ||
</tr> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class CreateDefaultSite < ActiveRecord::Migration | ||
def self.up | ||
Site.create(:name => 'Default', :domain => '', :base_domain => 'default', :homepage => Page.find(:first, :conditions => "parent_id IS NULL")) | ||
end | ||
|
||
def self.down | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class AddSiteIdToUsers < ActiveRecord::Migration | ||
def self.up | ||
add_column :users, :site_id, :integer | ||
end | ||
|
||
def self.down | ||
remove_column :users, :site_id | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class AddSiteIdToLayouts < ActiveRecord::Migration | ||
def self.up | ||
add_column :layouts, :site_id, :integer | ||
end | ||
|
||
def self.down | ||
remove_column :layouts, :site_id | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module MultiSite::LayoutExtensions | ||
def self.included(base) | ||
base.belongs_to :site | ||
base.extend ClassMethods | ||
end | ||
|
||
module ClassMethods | ||
def scoped_to_site(site_id, &block) | ||
with_scope(:find => { :conditions => ["site_id = ? OR site_id IS NULL", site_id] }, &block) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,113 @@ | ||
module MultiSite::PageControllerExtensions | ||
def self.included(base) | ||
base.class_eval { | ||
before_filter :set_site, :only => [:remove, :new, :edit] | ||
around_filter :scope_layouts_to_site, :only => [:new, :edit] | ||
|
||
alias_method_chain :index, :root | ||
alias_method_chain :clear_model_cache, :site | ||
alias_method_chain :continue_url, :site | ||
%w{remove}.each do |m| | ||
alias_method_chain m.to_sym, :back | ||
%w{edit new remove continue_url clear_model_cache}.each do |m| | ||
alias_method_chain m.to_sym, :site | ||
end | ||
} | ||
end | ||
|
||
def index_with_root | ||
if params[:root] # If a root page is specified | ||
@homepage = Page.find(params[:root]) | ||
@site = @homepage.root.site | ||
elsif @site = Site.find(:first, :order => "position ASC") # If there is a site defined | ||
if @site.homepage | ||
cookies.delete('expanded_rows') | ||
if user_developer? # or admin | ||
if params[:root] # If a root page is specified (should this ever be required for non-developers?) | ||
@homepage = Page.find(params[:root]) | ||
@site = @homepage.root.site | ||
elsif (@site = Site.find(:first, :order => "position ASC")) && @site.homepage # If there is a site defined | ||
@homepage = @site.homepage | ||
else | ||
index_without_root | ||
end | ||
else # Just do the default | ||
index_without_root | ||
elsif (@site = current_user.site) && @site.homepage | ||
@homepage = @site.homepage | ||
else | ||
access_denied | ||
end | ||
end | ||
|
||
def remove_with_back | ||
@page = Page.find(params[:id]) | ||
if request.post? | ||
announce_pages_removed(@page.children.count + 1) | ||
@page.destroy | ||
return_url = session[:came_from] | ||
session[:came_from] = nil | ||
if return_url && return_url != page_index_url(:root => @page) | ||
redirect_to return_url | ||
|
||
def remove_with_site | ||
if user_authorized? | ||
if request.post? | ||
announce_pages_removed(@page.children.count + 1) | ||
@page.destroy | ||
return_url = session[:came_from] | ||
session[:came_from] = nil | ||
if return_url && return_url != page_index_url(:root => @page) | ||
redirect_to return_url | ||
else | ||
redirect_to page_index_url(:page => @page.parent) | ||
end | ||
else | ||
redirect_to page_index_url(:page => @page.parent) | ||
session[:came_from] = request.env["HTTP_REFERER"] | ||
end | ||
else | ||
session[:came_from] = request.env["HTTP_REFERER"] | ||
access_denied | ||
end | ||
end | ||
|
||
def clear_model_cache_with_site | ||
Page.current_site ||= @site || @page.root.site | ||
clear_model_cache_without_site | ||
end | ||
|
||
def continue_url_with_site(options={}) | ||
options[:redirect_to] || (params[:continue] ? page_edit_url(:id => model.id) : page_index_url(:root => model.root.id)) | ||
|
||
def new_with_site | ||
if user_authorized? | ||
if request.get? | ||
@page = Page.new_with_defaults(config) | ||
else | ||
@page = Page.new | ||
end | ||
|
||
@page.slug = params[:slug] | ||
@page.breadcrumb = params[:breadcrumb] | ||
@page.parent = Page.find_by_id(params[:parent_id]) | ||
render :action => :edit if handle_new_or_edit_post | ||
else | ||
access_denied | ||
end | ||
end | ||
|
||
def edit_with_site | ||
if user_authorized? | ||
@old_page_url = @page.url | ||
handle_new_or_edit_post | ||
else | ||
access_denied | ||
end | ||
end | ||
end | ||
|
||
protected | ||
|
||
def continue_url_with_site(options = {}) | ||
options[:redirect_to] || (params[:continue] ? model_edit_url(:id => model.id) : model_index_url(:root => model.root.id)) | ||
end | ||
|
||
def access_denied | ||
flash[:error] = 'Access denied.' | ||
redirect_to login_url | ||
end | ||
|
||
def user_developer? | ||
current_user and (current_user.developer? or current_user.admin?) | ||
end | ||
|
||
def user_authorized? | ||
user_developer? || (!current_user.nil? && current_user.owner?(@site)) | ||
end | ||
|
||
def set_site | ||
id = params[:id] || params[:root] || params[:parent_id] | ||
@page = Page.find(id) | ||
@site = @page.root.site | ||
end | ||
|
||
def scope_layouts_to_site | ||
Layout.scoped_to_site(@site.id) do | ||
yield | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module MultiSite::SnippetControllerExtensions | ||
def self.included(base) | ||
base.class_eval { | ||
only_allow_access_to :index, :new, :edit, :remove, | ||
:when => [:developer, :admin], | ||
:denied_url => { :controller => 'page', :action => 'index' }, | ||
:denied_message => 'You must have developer privileges to perform this action.' | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module MultiSite::UserExtensions | ||
def self.included(base) | ||
base.belongs_to :site | ||
end | ||
|
||
def owner?(multi_site) | ||
site == multi_site | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.