77 lines
2.6 KiB
Ruby
77 lines
2.6 KiB
Ruby
class OverlaysController < ApplicationController
|
|
before_action :set_overlay, only: [ :destroy ]
|
|
before_action :set_server_template, only: [ :create, :destroy ]
|
|
|
|
def index
|
|
@system_overlays = Overlay.system_overlays
|
|
@custom_overlays = current_user.overlays.custom_overlays.order(:name)
|
|
end
|
|
|
|
def new
|
|
@overlay = current_user.overlays.build
|
|
end
|
|
|
|
def create
|
|
if @server_template.present?
|
|
# Attach existing overlay to a template
|
|
@overlay = Overlay.find(params[:overlay_id])
|
|
|
|
position = @server_template.template_overlays.maximum(:position).to_i + 1
|
|
@template_overlay = @server_template.template_overlays.build(overlay_id: @overlay.id, position: position)
|
|
|
|
if @template_overlay.save
|
|
Activity.log(current_user, "added_overlay", "ServerTemplate", @server_template.id, { overlay: @overlay.name })
|
|
redirect_to @server_template, notice: "Overlay added successfully!"
|
|
else
|
|
redirect_to @server_template, alert: "Failed to add overlay"
|
|
end
|
|
else
|
|
# Create a new custom overlay for the current user
|
|
@overlay = current_user.overlays.build(overlay_params.merge(overlay_type: "custom"))
|
|
|
|
if @overlay.save
|
|
Activity.log(current_user, "created_overlay", "Overlay", @overlay.id, { name: @overlay.name, slug: @overlay.slug })
|
|
redirect_to overlays_path, notice: "Overlay created"
|
|
else
|
|
render :new, status: :unprocessable_entity
|
|
end
|
|
end
|
|
end
|
|
|
|
def destroy
|
|
if @server_template.present?
|
|
authorize_user_for_template_overlay!
|
|
@overlay.template_overlays.where(server_template_id: @server_template.id).destroy_all
|
|
Activity.log(current_user, "removed_overlay", "ServerTemplate", @server_template.id, { overlay: @overlay.name })
|
|
redirect_to @server_template, notice: "Overlay removed successfully!"
|
|
else
|
|
authorize_owner!
|
|
name = @overlay.name
|
|
@overlay.destroy
|
|
redirect_to overlays_path, notice: "Overlay '#{name}' deleted"
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def set_overlay
|
|
@overlay = Overlay.find(params[:id]) if params[:id].present?
|
|
end
|
|
|
|
def set_server_template
|
|
return unless params[:server_template_id].present?
|
|
@server_template = current_user.server_templates.find(params[:server_template_id])
|
|
end
|
|
|
|
def overlay_params
|
|
params.require(:overlay).permit(:name, :slug)
|
|
end
|
|
|
|
def authorize_user_for_template_overlay!
|
|
redirect_to dashboard_path, alert: "Not authorized" unless @overlay.user_id.nil? || @overlay.user_id == current_user.id
|
|
end
|
|
|
|
def authorize_owner!
|
|
redirect_to overlays_path, alert: "Not authorized" unless @overlay.user_id == current_user.id
|
|
end
|
|
end
|