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
master
Matt Jankowski 8 years ago committed by Eugen
parent 1be6aa0c7f
commit dbe9f33fdc
  1. 93
      app/controllers/admin/accounts_controller.rb
  2. 9
      app/controllers/admin/base_controller.rb
  3. 42
      app/controllers/admin/domain_blocks_controller.rb
  4. 12
      app/controllers/admin/pubsubhubbub_controller.rb
  5. 81
      app/controllers/admin/reports_controller.rb
  6. 46
      app/controllers/admin/settings_controller.rb
  7. 14
      spec/controllers/admin/reports_controller_spec.rb
  8. 14
      spec/controllers/admin/settings_controller_spec.rb

@ -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 = Account.alphabetic.paginate(page: params[:page], per_page: 40) @accounts = @accounts.local if params[:local].present?
@accounts = @accounts.remote if params[:remote].present?
@accounts = @accounts.local if params[:local].present? @accounts = @accounts.where(domain: params[:by_domain]) if params[:by_domain].present?
@accounts = @accounts.remote if params[:remote].present? @accounts = @accounts.silenced if params[:silenced].present?
@accounts = @accounts.where(domain: params[:by_domain]) if params[:by_domain].present? @accounts = @accounts.recent if params[:recent].present?
@accounts = @accounts.silenced if params[:silenced].present? @accounts = @accounts.suspended if params[:suspended].present?
@accounts = @accounts.recent if params[:recent].present? end
@accounts = @accounts.suspended if params[:suspended].present?
end def show; end
def show; end def suspend
Admin::SuspensionWorker.perform_async(@account.id)
def suspend redirect_to admin_accounts_path
Admin::SuspensionWorker.perform_async(@account.id) end
redirect_to admin_accounts_path
end def unsuspend
@account.update(suspended: false)
def unsuspend redirect_to admin_accounts_path
@account.update(suspended: false) end
redirect_to admin_accounts_path
end def silence
@account.update(silenced: true)
def silence redirect_to admin_accounts_path
@account.update(silenced: true) end
redirect_to admin_accounts_path
end def unsilence
@account.update(silenced: false)
def unsilence redirect_to admin_accounts_path
@account.update(silenced: false) end
redirect_to admin_accounts_path
end private
private def set_account
@account = Account.find(params[:id])
def set_account end
@account = Account.find(params[:id])
end def account_params
params.require(:account).permit(:silenced, :suspended)
def account_params end
params.require(:account).permit(:silenced, :suspended)
end end
end end

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

@ -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
layout 'admin' @blocks = DomainBlock.paginate(page: params[:page], per_page: 40)
end
def index
@blocks = DomainBlock.paginate(page: params[:page], per_page: 40)
end
def new def new
@domain_block = DomainBlock.new @domain_block = DomainBlock.new
end end
def create def create
@domain_block = DomainBlock.new(resource_params) @domain_block = DomainBlock.new(resource_params)
if @domain_block.save if @domain_block.save
DomainBlockWorker.perform_async(@domain_block.id) DomainBlockWorker.perform_async(@domain_block.id)
redirect_to admin_domain_blocks_path, notice: 'Domain block is now being processed' redirect_to admin_domain_blocks_path, notice: 'Domain block is now being processed'
else else
render action: :new render action: :new
end
end end
end
private private
def resource_params def resource_params
params.require(:domain_block).permit(:domain, :severity) params.require(:domain_block).permit(:domain, :severity)
end
end end
end end

@ -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

@ -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)
def index @reports = params[:action_taken].present? ? @reports.resolved : @reports.unresolved
@reports = Report.includes(:account, :target_account).order('id desc').paginate(page: params[:page], per_page: 40) end
@reports = params[:action_taken].present? ? @reports.resolved : @reports.unresolved
end def show
@statuses = Status.where(id: @report.status_ids)
def show end
@statuses = Status.where(id: @report.status_ids)
end def resolve
@report.update(action_taken: true, action_taken_by_account_id: current_account.id)
def resolve redirect_to admin_report_path(@report)
@report.update(action_taken: true, action_taken_by_account_id: current_account.id) end
redirect_to admin_report_path(@report)
end def suspend
Admin::SuspensionWorker.perform_async(@report.target_account.id)
def suspend Report.unresolved.where(target_account: @report.target_account).update_all(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 silence
@report.target_account.update(silenced: true)
def silence Report.unresolved.where(target_account: @report.target_account).update_all(action_taken: true, action_taken_by_account_id: current_account.id)
@report.target_account.update(silenced: true) 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 remove
RemovalWorker.perform_async(params[:status_id])
def remove redirect_to admin_report_path(@report)
RemovalWorker.perform_async(params[:status_id]) end
redirect_to admin_report_path(@report)
end private
private def set_report
@report = Report.find(params[:id])
def set_report end
@report = Report.find(params[:id])
end end
end end

@ -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
end
def index def update
@settings = Setting.all_as_records @setting = Setting.where(var: params[:id]).first_or_initialize(var: params[:id])
end value = settings_params[:value]
def update # Special cases
@setting = Setting.where(var: params[:id]).first_or_initialize(var: params[:id]) value = value == 'true' if @setting.var == 'open_registrations'
value = settings_params[:value]
# Special cases if @setting.value != value
value = value == 'true' if @setting.var == 'open_registrations' @setting.value = value
@setting.save
end
if @setting.value != value respond_to do |format|
@setting.value = value format.html { redirect_to admin_settings_path }
@setting.save format.json { respond_with_bip(@setting) }
end
end end
respond_to do |format| private
format.html { redirect_to admin_settings_path }
format.json { respond_with_bip(@setting) }
end
end
private
def settings_params def settings_params
params.require(:setting).permit(:value) params.require(:setting).permit(:value)
end
end end
end end

@ -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

@ -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
Loading…
Cancel
Save