Friday, September 20, 2024 12:06:44 AM
> settings

Customize


Authenticate

> users_controller.rb
# frozen_string_literal: true

class UsersController < DashboardController
  # De-register these actions so they can be re-registered
  skip_before_action :authenticate_user!
  skip_before_action :check_for_access
  skip_before_action :load_communities

  before_action :authenticate_user!, except: :register
  before_action :check_for_access, except: [:register, :authorize_steam]

  def authorize_steam
    render "layouts/oauth_login", locals: {url: user_steam_omniauth_authorize_path}
  end

  def edit
    communities_cache = communities
    servers_by_community = Community.servers_by_community

    render action_name, locals: {
      already_registered: params[:already_registered],
      steam_info: current_user.steam_data,
      default: {
        communities: communities_cache,
        servers: servers_by_community,
        default_community: current_user.id_defaults.community&.clientize,
        default_server: current_user.id_defaults.server&.clientize
      },
      alias: {
        communities: communities_cache,
        servers: servers_by_community,
        aliases: current_user.id_aliases.clientize,
        user_id: current_user.discord_id
      }
    }
  end

  def transfer_account
    if session[:transfer_steam_account].blank?
      flash[:alert] = "You are not authorized to view that page"
      return redirect_to root_path
    end

    steam_uid = session[:transfer_steam_account]

    # Remove it immediately
    session.delete(:transfer_steam_account)

    # Get the other users with this steam uid
    users = User.where(steam_uid: steam_uid)
    users.update_all(steam_uid: "")

    # Transfer this uid to the new user and refresh their data
    current_user.update(steam_uid: steam_uid)

    flash[:success] = {
      title: "Welcome #{current_user.steam_data.username}!",
      message: "You are now registered with ESM<br/>We've sent you a message via Discord to help you get started",
      hide_after: 8000
    }

    ESM.send_message(
      channel_id: current_user.discord_id,
      message: {
        author: {
          name: current_user.steam_data.username,
          icon_url: current_user.steam_data.avatar
        },
        title: "Successfully Registered!",
        description: "You have been registered with Exile Server Manager. This allows you to use ESM on any server running ESM that you join. You don't even have to be in their Discord!\n**Below is some important information to get you started.**",
        color: ESM::COLORS::GREEN,
        fields: [{
          name: "Getting Started",
          value: "First time using ESM or need a refresher? Come read the [Getting Started](https://www.esmbot.com/wiki) article to help get you acquainted"
        }, {
          name: "Commands",
          value: "Need to feel powerful? Check out my [commands](https://www.esmbot.com/wiki/commands) and come back to show off your new found knowledge!"
        }]
      }
    )

    redirect_to edit_user_path(current_user.discord_id)
  end

  def cancel_transfer
    session.delete(:transfer_steam_account)
    render json: {}
  end

  def destroy
    return redirect_to root_path if !current_user

    if current_user.destroy
      sign_out(current_user)
      flash[:success] = "Your account has been deleted"
    else
      flash[:alert] = {
        title: "Well... This is awkward",
        message: "We failed to delete your account, please join our Discord and notify a developer so we can take care of it for you.",
        hide_after: 8000
      }
    end

    redirect_to root_path
  end

  def register
    session[:registering] = true
    render "layouts/oauth_login", locals: {url: user_discord_omniauth_authorize_path}
  end

  def deregister
    return redirect_to root_path if !current_user

    current_user.deregister!

    flash[:success] = "You've been deregistered.<br/>You can reregister via the Sign into Steam button"
    redirect_to edit_user_path(current_user.discord_id)
  end

  private

  def check_for_access
    return redirect_to edit_user_path(current_user.discord_id) if current_user.discord_id != params[:id]
  end

  def communities
    Community.select(:community_id, :community_name).order(:community_id).map do |community|
      {
        id: community.community_id,
        name: "[#{community.community_id}] #{community.community_name}",
      }
    end
  end
end
All opinions represented herein are my own
- © 2024 itsthedevman
- build 3c15a1b