Admin base controller (#1465)

* Add Admin::BaseController to wrap admin area

Extracts the setting of the `admin` layout and verifying that users are admins
to a common base class for the admin/ controllers.

* Add basic coverage for admin/reports and admin/settings controllers
This commit is contained in:
Matt Jankowski 2017-04-10 15:27:03 -04:00 committed by Eugen
parent 1be6aa0c7f
commit dbe9f33fdc
8 changed files with 154 additions and 125 deletions

View file

@ -1,51 +1,50 @@
# frozen_string_literal: true # frozen_string_literal: true
class Admin::AccountsController < ApplicationController module Admin
before_action :require_admin! class AccountsController < BaseController
before_action :set_account, except: :index before_action :set_account, except: :index
layout 'admin' def index
@accounts = Account.alphabetic.paginate(page: params[:page], per_page: 40)
def index @accounts = @accounts.local if params[:local].present?
@accounts = Account.alphabetic.paginate(page: params[:page], per_page: 40) @accounts = @accounts.remote if params[:remote].present?
@accounts = @accounts.where(domain: params[:by_domain]) if params[:by_domain].present?
@accounts = @accounts.silenced if params[:silenced].present?
@accounts = @accounts.recent if params[:recent].present?
@accounts = @accounts.suspended if params[:suspended].present?
end
@accounts = @accounts.local if params[:local].present? def show; end
@accounts = @accounts.remote if params[:remote].present?
@accounts = @accounts.where(domain: params[:by_domain]) if params[:by_domain].present?
@accounts = @accounts.silenced if params[:silenced].present?
@accounts = @accounts.recent if params[:recent].present?
@accounts = @accounts.suspended if params[:suspended].present?
end
def show; end def suspend
Admin::SuspensionWorker.perform_async(@account.id)
redirect_to admin_accounts_path
end
def suspend def unsuspend
Admin::SuspensionWorker.perform_async(@account.id) @account.update(suspended: false)
redirect_to admin_accounts_path redirect_to admin_accounts_path
end end
def unsuspend def silence
@account.update(suspended: false) @account.update(silenced: true)
redirect_to admin_accounts_path redirect_to admin_accounts_path
end end
def silence def unsilence
@account.update(silenced: true) @account.update(silenced: false)
redirect_to admin_accounts_path redirect_to admin_accounts_path
end end
def unsilence private
@account.update(silenced: false)
redirect_to admin_accounts_path
end
private def set_account
@account = Account.find(params[:id])
end
def set_account def account_params
@account = Account.find(params[:id]) params.require(:account).permit(:silenced, :suspended)
end end
def account_params
params.require(:account).permit(:silenced, :suspended)
end end
end end

View file

@ -0,0 +1,9 @@
# frozen_string_literal: true
module Admin
class BaseController < ApplicationController
before_action :require_admin!
layout 'admin'
end
end

View file

@ -1,32 +1,30 @@
# frozen_string_literal: true # frozen_string_literal: true
class Admin::DomainBlocksController < ApplicationController module Admin
before_action :require_admin! class DomainBlocksController < BaseController
def index
@blocks = DomainBlock.paginate(page: params[:page], per_page: 40)
end
layout 'admin' def new
@domain_block = DomainBlock.new
end
def index def create
@blocks = DomainBlock.paginate(page: params[:page], per_page: 40) @domain_block = DomainBlock.new(resource_params)
end
def new if @domain_block.save
@domain_block = DomainBlock.new DomainBlockWorker.perform_async(@domain_block.id)
end redirect_to admin_domain_blocks_path, notice: 'Domain block is now being processed'
else
render action: :new
end
end
def create private
@domain_block = DomainBlock.new(resource_params)
if @domain_block.save def resource_params
DomainBlockWorker.perform_async(@domain_block.id) params.require(:domain_block).permit(:domain, :severity)
redirect_to admin_domain_blocks_path, notice: 'Domain block is now being processed'
else
render action: :new
end end
end end
private
def resource_params
params.require(:domain_block).permit(:domain, :severity)
end
end end

View file

@ -1,11 +1,9 @@
# frozen_string_literal: true # frozen_string_literal: true
class Admin::PubsubhubbubController < ApplicationController module Admin
before_action :require_admin! class PubsubhubbubController < BaseController
def index
layout 'admin' @subscriptions = Subscription.order('id desc').includes(:account).paginate(page: params[:page], per_page: 40)
end
def index
@subscriptions = Subscription.order('id desc').includes(:account).paginate(page: params[:page], per_page: 40)
end end
end end

View file

@ -1,45 +1,44 @@
# frozen_string_literal: true # frozen_string_literal: true
class Admin::ReportsController < ApplicationController module Admin
before_action :require_admin! class ReportsController < BaseController
before_action :set_report, except: [:index] before_action :set_report, except: [:index]
layout 'admin' def index
@reports = Report.includes(:account, :target_account).order('id desc').paginate(page: params[:page], per_page: 40)
@reports = params[:action_taken].present? ? @reports.resolved : @reports.unresolved
end
def index def show
@reports = Report.includes(:account, :target_account).order('id desc').paginate(page: params[:page], per_page: 40) @statuses = Status.where(id: @report.status_ids)
@reports = params[:action_taken].present? ? @reports.resolved : @reports.unresolved end
end
def show def resolve
@statuses = Status.where(id: @report.status_ids) @report.update(action_taken: true, action_taken_by_account_id: current_account.id)
end redirect_to admin_report_path(@report)
end
def resolve def suspend
@report.update(action_taken: true, action_taken_by_account_id: current_account.id) Admin::SuspensionWorker.perform_async(@report.target_account.id)
redirect_to admin_report_path(@report) Report.unresolved.where(target_account: @report.target_account).update_all(action_taken: true, action_taken_by_account_id: current_account.id)
end redirect_to admin_report_path(@report)
end
def suspend def silence
Admin::SuspensionWorker.perform_async(@report.target_account.id) @report.target_account.update(silenced: true)
Report.unresolved.where(target_account: @report.target_account).update_all(action_taken: true, action_taken_by_account_id: current_account.id) Report.unresolved.where(target_account: @report.target_account).update_all(action_taken: true, action_taken_by_account_id: current_account.id)
redirect_to admin_report_path(@report) redirect_to admin_report_path(@report)
end end
def silence def remove
@report.target_account.update(silenced: true) RemovalWorker.perform_async(params[:status_id])
Report.unresolved.where(target_account: @report.target_account).update_all(action_taken: true, action_taken_by_account_id: current_account.id) redirect_to admin_report_path(@report)
redirect_to admin_report_path(@report) end
end
def remove private
RemovalWorker.perform_async(params[:status_id])
redirect_to admin_report_path(@report)
end
private def set_report
@report = Report.find(params[:id])
def set_report end
@report = Report.find(params[:id])
end end
end end

View file

@ -1,35 +1,33 @@
# frozen_string_literal: true # frozen_string_literal: true
class Admin::SettingsController < ApplicationController module Admin
before_action :require_admin! class SettingsController < BaseController
def index
layout 'admin' @settings = Setting.all_as_records
def index
@settings = Setting.all_as_records
end
def update
@setting = Setting.where(var: params[:id]).first_or_initialize(var: params[:id])
value = settings_params[:value]
# Special cases
value = value == 'true' if @setting.var == 'open_registrations'
if @setting.value != value
@setting.value = value
@setting.save
end end
respond_to do |format| def update
format.html { redirect_to admin_settings_path } @setting = Setting.where(var: params[:id]).first_or_initialize(var: params[:id])
format.json { respond_with_bip(@setting) } value = settings_params[:value]
# Special cases
value = value == 'true' if @setting.var == 'open_registrations'
if @setting.value != value
@setting.value = value
@setting.save
end
respond_to do |format|
format.html { redirect_to admin_settings_path }
format.json { respond_with_bip(@setting) }
end
end end
end
private private
def settings_params def settings_params
params.require(:setting).permit(:value) params.require(:setting).permit(:value)
end
end end
end end

View file

@ -0,0 +1,14 @@
require 'rails_helper'
RSpec.describe Admin::ReportsController, type: :controller do
describe 'GET #index' do
before do
sign_in Fabricate(:user, admin: true), scope: :user
end
it 'returns http success' do
get :index
expect(response).to have_http_status(:success)
end
end
end

View file

@ -0,0 +1,14 @@
require 'rails_helper'
RSpec.describe Admin::SettingsController, type: :controller do
describe 'GET #index' do
before do
sign_in Fabricate(:user, admin: true), scope: :user
end
it 'returns http success' do
get :index
expect(response).to have_http_status(:success)
end
end
end