Refactor requires login logic, reduce duplicate code

This also corrects the positioning in the chain of the check
and removes misuse of prepend_before_action
This commit is contained in:
Sam 2018-02-01 15:17:59 +11:00
parent ee0d3f15c1
commit 41986cdb2f
24 changed files with 57 additions and 26 deletions

View File

@ -1,7 +1,9 @@
require_dependency 'rate_limiter'
class AboutController < ApplicationController
prepend_before_action :check_xhr, :ensure_logged_in, only: [:live_post_counts]
requires_login only: [:live_post_counts]
skip_before_action :check_xhr, only: [:index]
def index

View File

@ -1,7 +1,7 @@
class Admin::AdminController < ApplicationController
prepend_before_action :check_xhr, :ensure_logged_in
prepend_before_action :check_xhr, :ensure_staff
requires_login
before_action :ensure_staff
def index
render body: nil

View File

@ -48,8 +48,9 @@ class ApplicationController < ActionController::Base
before_action :set_mobile_view
before_action :block_if_readonly_mode
before_action :authorize_mini_profiler
before_action :preload_json
before_action :redirect_to_login_if_required
before_action :block_if_requires_login
before_action :preload_json
before_action :check_xhr
after_action :add_readonly_header
after_action :perform_refresh_session
@ -570,6 +571,28 @@ class ApplicationController < ActionController::Base
raise RenderEmpty.new unless ((request.format && request.format.json?) || request.xhr?)
end
def self.requires_login(arg = {})
@requires_login_arg = arg
end
def self.requires_login_arg
@requires_login_arg
end
def block_if_requires_login
if arg = self.class.requires_login_arg
check =
if except = arg[:except]
!except.include?(action_name.to_sym)
elsif only = arg[:only]
only.include?(action_name.to_sym)
else
true
end
ensure_logged_in if check
end
end
def ensure_logged_in
raise Discourse::NotLoggedIn.new unless current_user.present?
end

View File

@ -2,7 +2,8 @@ require_dependency 'category_serializer'
class CategoriesController < ApplicationController
prepend_before_action :check_xhr, :ensure_logged_in, except: [:index, :categories_and_latest, :show, :redirect, :find_by_slug]
requires_login except: [:index, :categories_and_latest, :show, :redirect, :find_by_slug]
before_action :fetch_category, only: [:show, :update, :destroy]
before_action :initialize_staff_action_logger, only: [:create, :update, :destroy]
skip_before_action :check_xhr, only: [:index, :categories_and_latest, :redirect]

View File

@ -1,5 +1,5 @@
class CategoryHashtagsController < ApplicationController
prepend_before_action :check_xhr, :ensure_logged_in
requires_login
def check
category_slugs = params[:category_slugs]

View File

@ -2,7 +2,7 @@ require_dependency 'html_to_markdown'
class ComposerController < ApplicationController
prepend_before_action :check_xhr, :ensure_logged_in
requires_login
def parse_html
markdown_text = HtmlToMarkdown.new(params[:html]).to_markdown

View File

@ -2,7 +2,7 @@ require_dependency 'composer_messages_finder'
class ComposerMessagesController < ApplicationController
prepend_before_action :check_xhr, :ensure_logged_in
requires_login
def index
finder = ComposerMessagesFinder.new(current_user, params.slice(:composer_action, :topic_id, :post_id))

View File

@ -1,5 +1,6 @@
class DraftController < ApplicationController
prepend_before_action :ensure_logged_in
requires_login
skip_before_action :check_xhr, :preload_json
def show

View File

@ -1,6 +1,6 @@
class GroupsController < ApplicationController
prepend_before_action :check_xhr, :ensure_logged_in, only: [
requires_login only: [
:set_notifications,
:mentionable,
:messageable,

View File

@ -1,7 +1,7 @@
require_dependency 'inline_oneboxer'
class InlineOneboxController < ApplicationController
prepend_before_action :check_xhr, :ensure_logged_in
requires_login
def show
oneboxes = InlineOneboxer.new(params[:urls] || []).process

View File

@ -2,7 +2,7 @@ require_dependency 'rate_limiter'
class InvitesController < ApplicationController
prepend_before_action :check_xhr, :ensure_logged_in, only: [
requires_login only: [
:destroy, :create, :create_invite_link, :rescind_all_invites,
:resend_invite, :resend_all_invites, :upload_csv
]

View File

@ -2,7 +2,7 @@ require_dependency 'notification_serializer'
class NotificationsController < ApplicationController
prepend_before_action :check_xhr, :ensure_logged_in
requires_login
def index
user =

View File

@ -1,7 +1,7 @@
require_dependency 'oneboxer'
class OneboxController < ApplicationController
prepend_before_action :check_xhr, :ensure_logged_in
requires_login
def show
unless params[:refresh] == 'true'

View File

@ -1,7 +1,8 @@
require_dependency 'discourse'
class PostActionsController < ApplicationController
prepend_before_action :check_xhr, :ensure_logged_in
requires_login
before_action :fetch_post_from_params
before_action :fetch_post_action_type_id_from_params

View File

@ -8,7 +8,7 @@ require_dependency 'post_locker'
class PostsController < ApplicationController
prepend_before_action :check_xhr, :ensure_logged_in, except: [
requires_login except: [
:show,
:replies,
:by_number,

View File

@ -3,9 +3,9 @@ require_dependency 'wizard/builder'
require_dependency 'wizard/step_updater'
class StepsController < ApplicationController
requires_login
before_action :ensure_wizard_enabled
prepend_before_action :check_xhr, :ensure_logged_in
before_action :ensure_admin
def update

View File

@ -1,5 +1,6 @@
class TagGroupsController < ApplicationController
prepend_before_action :check_xhr, :ensure_logged_in, except: [:index, :show]
requires_login except: [:index, :show]
skip_before_action :check_xhr, only: [:index, :show]
before_action :fetch_tag_group, only: [:show, :update, :destroy]

View File

@ -7,7 +7,7 @@ class TagsController < ::ApplicationController
before_action :ensure_tags_enabled
prepend_before_action :check_xhr, :ensure_logged_in, except: [
requires_login except: [
:index,
:show,
:tag_feed,

View File

@ -6,7 +6,7 @@ require_dependency 'discourse_event'
require_dependency 'rate_limiter'
class TopicsController < ApplicationController
prepend_before_action :check_xhr, :ensure_logged_in, only: [
requires_login only: [
:timings,
:destroy_timings,
:update,

View File

@ -2,7 +2,8 @@ require "mini_mime"
require_dependency 'upload_creator'
class UploadsController < ApplicationController
prepend_before_action :check_xhr, :ensure_logged_in, except: [:show]
requires_login except: [:show]
skip_before_action :preload_json, :check_xhr, :redirect_to_login_if_required, only: [:show]
def create

View File

@ -2,7 +2,7 @@ class UserApiKeysController < ApplicationController
layout 'no_ember'
prepend_before_action :check_xhr, :ensure_logged_in, only: [:create, :revoke, :undo_revoke]
requires_login only: [:create, :revoke, :undo_revoke]
skip_before_action :redirect_to_login_if_required, only: [:new]
skip_before_action :check_xhr, :preload_json

View File

@ -9,7 +9,7 @@ class UsersController < ApplicationController
skip_before_action :authorize_mini_profiler, only: [:avatar]
prepend_before_action :check_xhr, :ensure_logged_in, only: [
requires_login only: [
:username, :update, :user_preferences_redirect, :upload_user_image,
:pick_avatar, :destroy_user_image, :destroy, :check_emails, :topic_tracking_state,
:preferences

View File

@ -4,7 +4,7 @@ require_dependency 'email_updater'
class UsersEmailController < ApplicationController
prepend_before_action :check_xhr, :ensure_logged_in, only: [:index, :update]
requires_login only: [:index, :update]
skip_before_action :check_xhr, only: [:confirm]
skip_before_action :redirect_to_login_if_required, only: [:confirm]

View File

@ -2,8 +2,9 @@ require_dependency 'wizard'
require_dependency 'wizard/builder'
class WizardController < ApplicationController
prepend_before_action :check_xhr, :ensure_admin, except: [:qunit]
prepend_before_action :check_xhr, :ensure_logged_in, except: [:qunit]
requires_login except: [:qunit]
before_action :ensure_admin, except: [:qunit]
before_action :ensure_wizard_enabled, only: [:index]
skip_before_action :check_xhr, :preload_json