dawarich/app/controllers/application_controller.rb

32 lines
698 B
Ruby
Raw Normal View History

# frozen_string_literal: true
class ApplicationController < ActionController::Base
2023-02-03 09:42:56 -05:00
include Pundit::Authorization
2024-07-04 16:20:12 -04:00
before_action :unread_notifications
protected
2024-07-04 16:20:12 -04:00
def unread_notifications
return [] unless current_user
@unread_notifications ||= Notification.where(user: current_user).unread
end
2024-07-09 14:28:59 -04:00
def authenticate_first_user!
return if current_user == User.first
redirect_to root_path, notice: 'You are not authorized to perform this action.', status: :unauthorized
end
def authenticate_api_key
return head :unauthorized unless current_api_user
true
end
def current_api_user
@current_api_user ||= User.find_by(api_key: params[:api_key])
end
end