Skip to content

Commit 77825be

Browse files
committed
refactor forbidden
1 parent f74fac9 commit 77825be

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

app/controllers/application_controller.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
require 'errors/forbidden_path_error'
2+
13
class ApplicationController < ActionController::API
24
include CanCan::ControllerAdditions
35

46
rescue_from CanCan::AccessDenied, with: :not_authorized
57
rescue_from ActiveRecord::RecordNotFound, :with => :not_found
8+
rescue_from ForbiddenPathError, :with => :forbidden
69

710
before_filter :fetch_team
811

@@ -24,6 +27,10 @@ def current_team_membership
2427
TeamMembership.find_by!(user: current_user, team: current_team) if current_team.present?
2528
end
2629

30+
def restrict_to_team_subdomain
31+
raise ForbiddenPathError, "Resource not allowed" unless has_team_subdomain?
32+
end
33+
2734
private
2835

2936
def has_team_subdomain?

app/controllers/pages_controller.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
class PagesController < ApplicationController
77
acts_as_token_authentication_handler_for User, fallback_to_devise: false
88

9+
before_filter :restrict_to_team_subdomain
10+
911
def index
1012
authorize! :read, Page
1113

@@ -29,16 +31,12 @@ def create
2931
end
3032

3133
def update
32-
if has_team_subdomain?
33-
authorize! :update, current_page
34-
page = TeamPlaybook::Scenario::UpdatePage.new.call(page: current_page, page_params: page_params)
35-
if page.valid?
36-
render json: page, status: 200
37-
else
38-
render json: {error: page.errors.full_messages.to_sentence}, status: :unprocessable_entity
39-
end
34+
authorize! :update, current_page
35+
page = TeamPlaybook::Scenario::UpdatePage.new.call(page: current_page, page_params: page_params)
36+
if page.valid?
37+
render json: page, status: 200
4038
else
41-
forbidden
39+
render json: {error: page.errors.full_messages.to_sentence}, status: :unprocessable_entity
4240
end
4341
end
4442

lib/errors/forbidden_path_error.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class ForbiddenPathError < StandardError
2+
end

0 commit comments

Comments
 (0)