Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/controllers/alchemy/admin/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,18 @@ def load_locked_pages
end

# Returns the current site for admin controllers.
# Raises CanCan::AccessDenied if the user is not allowed to access the site.
#
def current_alchemy_site
@current_alchemy_site ||= begin
site_id = params[:site_id] || session[:alchemy_site_id]
site = Site.find_by(id: site_id) || super

authorize! :access, site if site
rescue CanCan::AccessDenied
site = Site.accessible_by(current_alchemy_user).first
raise
ensure
session[:alchemy_site_id] = site&.id
site
end
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/alchemy/admin/base_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def alchemy_admin_js_translations(locale = ::I18n.locale)

# Used for site selector in Alchemy cockpit.
def sites_for_select
Alchemy::Site.all.map do |site|
@_sites_for_select ||= Alchemy::Site.accessible_by(current_alchemy_user).map do |site|
[site.name, site.id]
end
end
Expand Down
5 changes: 5 additions & 0 deletions app/models/alchemy/permissions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ def alchemy_author_rules
can :edit_content, Alchemy::Page, Alchemy::Page.all do |page|
page.editable_by?(@user)
end

can :switch, Alchemy::Site
can(:access, Alchemy::Site) do |site|
site.accessible_by?(@user)
end
end
end

Expand Down
19 changes: 19 additions & 0 deletions app/models/alchemy/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ def default_language
languages.find_by(default: true)
end

# Returns true if the given user has access to this site.
# A site is accessible by all users if no roles are specified.
#
def accessible_by?(user)
return false if user.nil?

(accessible_by & user.alchemy_roles).any? || accessible_by.empty?
end

# Returns an array of role names that are allowed to access this site.
#
def accessible_by
definition.fetch("accessible_by", [])
end

class << self
def find_for_host(host)
# These are split up into two separate queries in order to run the
Expand All @@ -66,6 +81,10 @@ def find_in_aliases(host)
site.aliases.split.include?(host) if site.aliases.present?
end
end

def accessible_by(user)
all.select { |site| site.accessible_by?(user) }
end
end
end
end
2 changes: 1 addition & 1 deletion app/views/alchemy/admin/partials/_site_select.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%- if multi_site? -%>
<%- if can?(:switch, Alchemy::Site) && sites_for_select.size > 1 -%>
<div class="toolbar_button">
<sl-tooltip content="<%= Alchemy.t("Current site") %>">
<%= select_tag 'change_site',
Expand Down
Loading