|
|
|
@ -2,13 +2,22 @@ class Admin::AdminController < ApplicationController |
|
|
|
|
|
|
|
layout 'admin' |
|
|
|
|
|
|
|
# The admin has two roles: Admin, who gets everything, and Guest (the `user` |
|
|
|
# role), who gets a read-only slice -- the analytics dashboard and the |
|
|
|
# published leaderboard. Listed by controller name. |
|
|
|
GUEST_SECTIONS = %w[analytics leaderboard].freeze |
|
|
|
|
|
|
|
# The nav sections in the order they appear in the sidebar. |
|
|
|
ADMIN_SECTIONS = %i[analytics leaderboard nodes assets].freeze |
|
|
|
|
|
|
|
before_action :authenticate_user! |
|
|
|
before_action :only_admin! |
|
|
|
before_action :only_permitted_section! |
|
|
|
before_action :set_locale_to_default |
|
|
|
|
|
|
|
helper_method :current_user |
|
|
|
helper_method :user_signed_in? |
|
|
|
helper_method :form_locale, :available_locales |
|
|
|
helper_method :admin?, :permitted_admin_sections |
|
|
|
|
|
|
|
# DELETE admin/cache/clear |
|
|
|
def clear_cache |
|
|
|
@ -50,10 +59,41 @@ private |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
def admin? |
|
|
|
current_user&.admin_role? |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
# What a Guest is allowed to open. Anything else bounces them to analytics |
|
|
|
# rather than out of the admin entirely, so every link in the nav they see |
|
|
|
# actually works and a stray URL doesn't log them out. |
|
|
|
def only_permitted_section! |
|
|
|
return if admin? |
|
|
|
return if GUEST_SECTIONS.include?(controller_name) |
|
|
|
|
|
|
|
bounce_guest! |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
# For the handful of actions inside a Guest section that still change |
|
|
|
# something -- clearing a cache, say. Guests may look, not touch, so this |
|
|
|
# cannot lean on the section check: the section itself is permitted. |
|
|
|
def only_admin! |
|
|
|
unless current_user&.admin_role? |
|
|
|
redirect_to root_path |
|
|
|
end |
|
|
|
return if admin? |
|
|
|
|
|
|
|
bounce_guest! |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
def bounce_guest! |
|
|
|
redirect_to admin_analytics_path(locale: params[:locale] || I18n.default_locale) |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
def permitted_admin_sections |
|
|
|
return ADMIN_SECTIONS if admin? |
|
|
|
|
|
|
|
ADMIN_SECTIONS.select { |section| GUEST_SECTIONS.include?(section.to_s) } |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|