Use CanCanCan for changeset comments

This introduces different deny_access handlers for web and api requests, since we want to avoid sending redirects as API responses. See #2064 for discussion.
This commit is contained in:
Andy Allan 2018-11-14 15:45:30 +01:00
parent b29c173ac7
commit 8f70fb2114
6 changed files with 89 additions and 3 deletions

View file

@ -477,7 +477,15 @@ class ApplicationController < ActionController::Base
end
end
def deny_access(_exception)
def deny_access(exception)
if @api_deny_access_handling
api_deny_access(exception)
else
web_deny_access(exception)
end
end
def web_deny_access(_exception)
if current_token
set_locale
report_error t("oauth.permissions.missing"), :forbidden
@ -497,6 +505,26 @@ class ApplicationController < ActionController::Base
end
end
def api_deny_access(_exception)
if current_token
set_locale
report_error t("oauth.permissions.missing"), :forbidden
elsif current_user
head :forbidden
else
realm = "Web Password"
errormessage = "Couldn't authenticate you"
response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\""
render :plain => errormessage, :status => :unauthorized
end
end
attr_accessor :api_access_handling
def api_deny_access_handler
@api_deny_access_handling = true
end
private
# extract authorisation credentials from headers, returns user = nil if none

View file

@ -3,8 +3,10 @@ class ChangesetCommentsController < ApplicationController
before_action :authorize_web, :only => [:index]
before_action :set_locale, :only => [:index]
before_action :authorize, :only => [:create, :destroy, :restore]
before_action :require_moderator, :only => [:destroy, :restore]
before_action :require_allow_write_api, :only => [:create, :destroy, :restore]
before_action :api_deny_access_handler, :only => [:create, :destroy, :restore]
authorize_resource
before_action :require_public_data, :only => [:create]
before_action :check_api_writable, :only => [:create, :destroy, :restore]
before_action :check_api_readable, :except => [:create, :index]