dawarich/app/controllers/application_controller.rb

83 lines
1.9 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class ApplicationController < ActionController::Base
2023-02-03 09:42:56 -05:00
include Pundit::Authorization
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
before_action :unread_notifications, :set_self_hosted_status, :store_client_header
2024-07-04 16:20:12 -04:00
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-16 16:26:16 -04:00
def authenticate_admin!
2024-11-08 11:56:14 -05:00
return if current_user&.admin?
2024-07-09 14:28:59 -04:00
user_not_authorized
2024-07-09 14:28:59 -04:00
end
2025-01-15 15:52:59 -05:00
def authenticate_self_hosted!
return if DawarichSettings.self_hosted?
user_not_authorized
2025-01-15 15:52:59 -05:00
end
def authenticate_active_user!
return if current_user&.active_until&.future?
redirect_to root_path, notice: 'Your account is not active.', status: :see_other
end
2025-04-04 16:16:52 -04:00
def authenticate_non_self_hosted!
return unless DawarichSettings.self_hosted?
user_not_authorized
2025-04-04 16:16:52 -04:00
end
def after_sign_in_path_for(resource)
client_type = request.headers['X-Dawarich-Client'] || session[:dawarich_client]
case client_type
when 'ios'
payload = { api_key: resource.api_key, exp: 5.minutes.from_now.to_i }
token = Subscription::EncodeJwtToken.new(
payload, ENV['AUTH_JWT_SECRET_KEY']
).call
ios_success_path(token:)
else
super
end
end
2025-10-04 17:19:00 -04:00
def ensure_family_feature_enabled!
return if DawarichSettings.family_feature_enabled?
render json: { error: 'Family feature is not enabled' }, status: :forbidden
end
private
def set_self_hosted_status
@self_hosted = DawarichSettings.self_hosted?
end
def store_client_header
return unless request.headers['X-Dawarich-Client']
session[:dawarich_client] = request.headers['X-Dawarich-Client']
end
def user_not_authorized
2025-10-22 14:39:02 -04:00
redirect_back fallback_location: root_path,
alert: 'You are not authorized to perform this action.',
status: :see_other
end
end