docbrown/app/controllers/api/v0/api_controller.rb
Jacob Herrington a5b2d109d5
Rename banned and comment_banned roles (#12270)
* Rename banned and comment_banned roles

* Add data update script to rename roles containing 'ban'

* Add named error for Suspended users

* Update unidiomatic method names

* Rename misc banned to suspended

* Apply suggestions from code review

Co-authored-by: Michael Kohl <me@citizen428.net>

* Add unit tests for suspended methods

This commit also adds TODO comments for removing banned and
comment_banned from the codebase after data update scripts have
successfully run on all of our Forems.

Co-authored-by: Michael Kohl <me@citizen428.net>
2021-04-06 10:12:14 -05:00

83 lines
2.4 KiB
Ruby

module Api
module V0
class ApiController < ApplicationController
protect_from_forgery with: :exception, prepend: true
include ValidRequest
respond_to :json
rescue_from ActionController::ParameterMissing do |exc|
error_unprocessable_entity(exc.message)
end
rescue_from ActiveRecord::RecordInvalid do |exc|
error_unprocessable_entity(exc.message)
end
rescue_from ActiveRecord::RecordNotFound, with: :error_not_found
rescue_from Pundit::NotAuthorizedError, with: :error_unauthorized
protected
def error_unprocessable_entity(message)
render json: { error: message, status: 422 }, status: :unprocessable_entity
end
def error_unauthorized
render json: { error: "unauthorized", status: 401 }, status: :unauthorized
end
def error_not_found
render json: { error: "not found", status: 404 }, status: :not_found
end
def authenticate!
@user = authenticated_user
return error_unauthorized unless @user && !@user.suspended?
end
def authorize_super_admin
error_unauthorized unless @user.has_role?(:super_admin)
end
# Checks if the user is authenticated, sets @user to nil otherwise
def authenticate_with_api_key_or_current_user
@user = authenticate_with_api_key || current_user
end
# Checks if the user is authenticated, if so sets the variable @user
# Returns HTTP 401 Unauthorized otherwise
def authenticate_with_api_key_or_current_user!
@user = authenticate_with_api_key || current_user
error_unauthorized unless @user
end
private
def authenticate_with_api_key
api_key = request.headers["api-key"]
return unless api_key
api_secret = ApiSecret.includes(:user).find_by(secret: api_key)
return unless api_secret
# guard against timing attacks
# see <https://www.slideshare.net/NickMalcolm/timing-attacks-and-ruby-on-rails>
secure_secret = ActiveSupport::SecurityUtils.secure_compare(api_secret.secret, api_key)
return api_secret.user if secure_secret
end
def authenticated_user
if doorkeeper_token
User.find(doorkeeper_token.resource_owner_id)
elsif request.headers["api-key"]
authenticate_with_api_key
elsif current_user
current_user
end
end
end
end
end