Thursday, September 19, 2024 11:56:20 PM
> settings

Customize


Authenticate

> dashboard_controller.rb
# frozen_string_literal: true

class DashboardController < ApplicationController
  before_action :authenticate_user!
  before_action :load_communities, unless: -> { controller_name == "dashboard" && action_name == "index" }
  before_action :check_for_access, if: -> { controller_name != "dashboard" }

  def index
  end

  def player_dashboard
    redirect_to edit_user_path(current_user.discord_id)
  end

  protected

  def current_context
    current_community || current_user
  end

  def current_community
    @community
  end

  def load_communities
    @communities = current_user.admin_communities
  end

  def check_for_access
    return if (params[:user_id] || params[:id]) == current_user.discord_id

    @community = Community.find_by(public_id: params[:community_id] || params[:id])
    return if current_community&.modifiable_by?(current_user)

    respond_to do |format|
      format.html { redirect_to root_path, alert: "You are not allowed to view that page" }
      format.json { render json: {}, status: :unauthorized }
    end
  end

  def redirect_if_player_mode
    return redirect_to communities_path if current_community.nil?
    return unless current_community.player_mode_enabled?

    redirect_to edit_community_path(current_community.public_id),
      alert: "Player mode is enabled on this community. You cannot access this page with it enabled."
  end

  def redirect_if_not_player_mode
    return redirect_to communities_path if current_community.nil?
    return if current_community.player_mode_enabled?

    redirect_to edit_community_path(current_community.public_id),
      alert: "Player mode is not enabled on this community. You cannot access this page with it disabled."
  end

  def check_failed!(opts = {})
    respond_to do |format|
      format.html do
        flash[:error] = opts[:message]
        redirect_to opts[:redirect_to] || root_path
      end

      format.json { render json: {message: opts[:message]}, status: opts[:status] || :bad_request }
    end

    Rails.logger.error { opts[:message] }

    true
  end
end
All opinions represented herein are my own
- © 2024 itsthedevman
- build 3c15a1b