2025-11-21 15:27:53 +00:00
|
|
|
module BreakEscape
|
|
|
|
|
class ApplicationController < ActionController::Base
|
2025-11-21 15:27:54 +00:00
|
|
|
protect_from_forgery with: :exception
|
|
|
|
|
|
|
|
|
|
# Include Pundit if available
|
|
|
|
|
include Pundit::Authorization if defined?(Pundit)
|
|
|
|
|
|
|
|
|
|
# Helper method to get current player (polymorphic)
|
|
|
|
|
def current_player
|
2025-11-21 15:27:54 +00:00
|
|
|
if BreakEscape.standalone_mode? || !respond_to?(:current_user, true)
|
|
|
|
|
# Standalone mode or no current_user available - get/create demo user
|
2025-11-21 15:27:54 +00:00
|
|
|
@current_player ||= DemoUser.first_or_create!(handle: 'demo_player')
|
|
|
|
|
else
|
|
|
|
|
# Mounted mode - use Hacktivity's current_user
|
|
|
|
|
current_user
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
helper_method :current_player
|
|
|
|
|
|
2025-11-21 15:27:54 +00:00
|
|
|
# Tell Pundit to use current_player as the user for authorization
|
|
|
|
|
def pundit_user
|
|
|
|
|
current_player
|
|
|
|
|
end
|
|
|
|
|
|
2025-11-21 15:27:54 +00:00
|
|
|
# Handle authorization errors
|
|
|
|
|
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def user_not_authorized
|
|
|
|
|
flash[:alert] = "You are not authorized to perform this action."
|
|
|
|
|
redirect_to(request.referrer || root_path)
|
|
|
|
|
end
|
2025-11-21 15:27:53 +00:00
|
|
|
end
|
|
|
|
|
end
|