Files
danbooru/app/controllers/user_upgrades_controller.rb
evazion c82e05d828 users: add stricter checks for user promotions.
New rules for user promotions:

* Moderators can no longer promote other users to moderator level. Only
  Admins can promote users to Mod level. Mods can only promote up to Builder level.
* Admins can no longer promote other users to Admin level. Only Owners
  can promote users to Admin. Admins can only promote up to Mod level.
* Admins can no longer demote themselves or other admins.

These rules are being changed to account for the new Owner user level.

Also change it so that when a user upgrades their account, the promotion
is done by DanbooruBot. This means that the inviter and the mod action
will show DanbooruBot as the promoter instead of the user themselves.
2020-12-13 21:21:08 -06:00

65 lines
1.4 KiB
Ruby

class UserUpgradesController < ApplicationController
helper_method :user
skip_before_action :verify_authenticity_token, only: [:create]
def create
if params[:stripeToken]
create_stripe
end
end
def new
end
def show
authorize User, :upgrade?
end
def user
if params[:user_id]
User.find(params[:user_id])
else
CurrentUser.user
end
end
private
def create_stripe
@user = user
if params[:desc] == "Upgrade to Gold"
level = User::Levels::GOLD
cost = UserUpgrade.gold_price
elsif params[:desc] == "Upgrade to Platinum"
level = User::Levels::PLATINUM
cost = UserUpgrade.platinum_price
elsif params[:desc] == "Upgrade Gold to Platinum" && @user.level == User::Levels::GOLD
level = User::Levels::PLATINUM
cost = UserUpgrade.upgrade_price
else
raise "Invalid desc"
end
begin
charge = Stripe::Charge.create(
:amount => cost,
:currency => "usd",
:card => params[:stripeToken],
:description => params[:desc]
)
@user.promote_to!(level, User.system, is_upgrade: true)
flash[:success] = true
rescue Stripe::CardError => e
DanbooruLogger.log(e)
flash[:error] = e.message
end
if @user == CurrentUser.user
redirect_to user_upgrade_path
else
redirect_to user_upgrade_path(user_id: params[:user_id])
end
end
end