Skip to content

Commit

Permalink
Cache admin endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
cavis committed Nov 14, 2023
1 parent d39bced commit 26b2fab
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 21 deletions.
10 changes: 1 addition & 9 deletions app/controllers/api/auth/episodes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
class Api::Auth::EpisodesController < Api::EpisodesController
include ApiAdminToken
include ApiAuthenticated
include ApiUpdatedSince

skip_before_action :authenticate_user!, if: :api_admin_token?

api_versions :v1
represent_with Api::Auth::EpisodeRepresenter
filter_resources_by :podcast_id
Expand All @@ -31,11 +28,6 @@ def sorted(res)
end

def resources_base
@resources_base ||=
if api_admin_token?
super
else
super.merge(authorization.token_auth_episodes)
end
authorization.token_auth_episodes
end
end
7 changes: 6 additions & 1 deletion app/controllers/api/auth/feeds_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ class Api::Auth::FeedsController < Api::BaseController
filter_resources_by :podcast_id

after_action :publish, only: [:create, :update, :destroy]
allow_params :index, [:format, :api_version, :podcast_id, :page, :per]

def publish
resource.podcast.publish! if resource&.podcast
end

def included(relation)
relation.includes(:podcast, :feed_images, :itunes_images, :feed_tokens)
end

def resources_base
@feeds ||= super.merge(authorization.token_auth_feeds)
authorization.token_auth_feeds
end
end
2 changes: 1 addition & 1 deletion app/controllers/api/auth/podcasts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ def visible?
end

def resources_base
@podcasts ||= super.merge(authorization.token_auth_podcasts)
authorization.token_auth_podcasts
end
end
5 changes: 2 additions & 3 deletions app/controllers/api/authorizations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ def resource
authorization
end

# this is the only auth#show endpoint not cached, since it's specific to a user
def cache_show?
false
def show_cache_path
authorization.cache_key
end
end
7 changes: 6 additions & 1 deletion app/controllers/api/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

class Api::BaseController < ApplicationController
include HalApi::Controller
include ApiAdminToken

# these API endpoints use PRX JWTs, not session based auth
skip_before_action :verify_authenticity_token
Expand Down Expand Up @@ -33,7 +34,11 @@ def pundit_user
end

def authorization
Authorization.new(prx_auth_token) if prx_auth_token
if prx_auth_token
Authorization.new(prx_auth_token)
elsif api_admin_token?
Authorization.new(nil, true)
end
end

allow_params :show, [:api_version, :format, :zoom]
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/api/feeds_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class Api::FeedsController < Api::BaseController
include ApiAdminToken
include ApiAuthenticated

# only allows admin tokens, not regular token users
skip_before_action :authenticate_user!
before_action :api_admin_token!

def index
Expand Down
15 changes: 10 additions & 5 deletions app/controllers/concerns/api_authenticated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ module ApiAuthenticated
before_action :authenticate_user!
end

def authenticated?
api_admin_token? || prx_auth_token
end

def authenticate_user!
user_not_authorized unless prx_auth_token
user_not_authorized unless authenticated?
end

def cache_show?
true
# don't bother calculating cache keys if user will be 401'd anyways
def index_cache_path
super if authenticated?
end

def cache_index?
false
def show_cache_path
super if authenticated?
end
end

0 comments on commit 26b2fab

Please sign in to comment.