forked from mastodon/mastodon
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic admin UI for emergency rules
- Loading branch information
1 parent
84f8cb9
commit f44b28a
Showing
16 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module Admin | ||
class EmergencyRulesController < BaseController | ||
before_action :set_rule, except: [:index] | ||
|
||
def index | ||
authorize [:emergency, :rule], :index? | ||
|
||
@rules = Emergency::Rule.all.to_a | ||
end | ||
|
||
def deactivate | ||
authorize @rule, :deactivate? | ||
# TODO: log? | ||
@rule.deactivate! | ||
|
||
redirect_to admin_emergency_rules_path, notice: I18n.t('admin.emergency_rules.deactivated_msg') | ||
end | ||
|
||
private | ||
|
||
def set_rule | ||
@rule = Emergency::Rule.find(params[:id]) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
class Emergency::RulePolicy < ApplicationPolicy | ||
def create? | ||
false # TODO | ||
end | ||
|
||
def index? | ||
role.can?(:manage_reports, :view_audit_log, :manage_users, :manage_invites, :manage_taxonomies, :manage_federation, :manage_blocks) | ||
end | ||
|
||
def deactivate? | ||
role.can?(:manage_reports, :view_audit_log, :manage_users, :manage_invites, :manage_taxonomies, :manage_federation, :manage_blocks) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
= emergency_rule_notice | ||
|
||
- content_for :page_title do | ||
= t('admin.dashboard.title') | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
.filters-list__item | ||
.filters-list__item__title | ||
= emergency_rule.name | ||
|
||
- if emergency_rule.active? | ||
.expiration | ||
- if can?(:deactivate, emergency_rule) | ||
= link_to t('admin.emergency_rules.index.deactivate'), deactivate_admin_emergency_rule_path(emergency_rule.id), method: :post, class: 'button button-tertiary button--destructive' | ||
- else | ||
= t('admin.emergency_rules.active') | ||
|
||
.filters-list__item__permissions | ||
%ul.permissions-list | ||
= render partial: 'trigger', collection: emergency_rule.triggers | ||
= render partial: 'rate_limit_action', collection: emergency_rule.rate_limit_actions | ||
= render partial: 'setting_override_action', collection: emergency_rule.setting_override_actions | ||
|
||
.announcements-list__item__action-bar | ||
.announcements-list__item__meta | ||
-# TODO: history | ||
- if emergency_rule.duration.present? | ||
= t('admin.emergency_rules.expires_after_duration', duration: distance_of_time_in_words(Time.at(0).utc, Time.at(emergency_rule.duration).utc)) | ||
- else | ||
= t('admin.emergency_rules.no_expiration') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
%li.permissions-list__item | ||
.permissions-list__item__icon | ||
= fa_icon('gavel') | ||
.permissions-list__item__text | ||
.permissions-list__item__text__title | ||
= rate_limit_action.new_users_only? ? t('admin.emergency_rules.rate_limit_actions.new_users_title') : t('admin.emergency_rules.rate_limit_actions.title') | ||
.permissions-list__item__text__type | ||
= rate_limit_action.new_users_only? ? t('admin.emergency_rules.rate_limit_actions.new_users_description') : t('admin.emergency_rules.rate_limit_actions.description') |
8 changes: 8 additions & 0 deletions
8
app/views/admin/emergency_rules/_setting_override_action.html.haml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
%li.permissions-list__item | ||
.permissions-list__item__icon | ||
= fa_icon('gavel') | ||
.permissions-list__item__text | ||
.permissions-list__item__text__title | ||
= t("admin.emergency_rules.setting_override_actions.#{setting_override_action.setting}.title_#{setting_override_action.value}") | ||
.permissions-list__item__text__type | ||
= t('admin.emergency_rules.setting_override_actions.description') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
%li.permissions-list__item | ||
.permissions-list__item__icon | ||
= fa_icon('cogs') | ||
.permissions-list__item__text | ||
.permissions-list__item__text__title | ||
= t("admin.emergency_rules.triggers.title.#{trigger.event}") | ||
.permissions-list__item__text__type | ||
- period = t("admin.emergency_rules.triggers.periods.#{trigger.duration_bucket}") | ||
= t('admin.emergency_rules.triggers.description', threshold: trigger.threshold, period: period) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
- content_for :page_title do | ||
= t('admin.emergency_rules.index.title') | ||
|
||
- content_for :heading_actions do | ||
= link_to t('admin.emergency_rules.index.add'), new_admin_emergency_rule_path, class: 'button' if can?(:create, [:emergency, :rule]) | ||
|
||
%p.lead= t('admin.emergency_rules.index.preamble') | ||
|
||
- if @rules.empty? | ||
.muted-hint.center-text= t 'admin.emergency_rules.index.empty' | ||
- else | ||
.applications-list | ||
= render partial: 'emergency_rule', collection: @rules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Admin::EmergencyRulesController do | ||
before do | ||
Fabricate('Emergency::Trigger') | ||
Fabricate('Emergency::SettingOverrideAction') | ||
Fabricate('Emergency::RateLimitAction') | ||
end | ||
|
||
describe 'the index route' do | ||
context 'when not logged in' do | ||
it 'returns HTTP forbidden' do | ||
get '/admin/emergency_rules' | ||
|
||
expect(response).to have_http_status(403) | ||
end | ||
end | ||
|
||
context 'when logged in as a regular user' do | ||
before do | ||
sign_in Fabricate(:user), scope: :user | ||
end | ||
|
||
it 'returns HTTP forbidden' do | ||
get '/admin/emergency_rules' | ||
|
||
expect(response).to have_http_status(403) | ||
end | ||
end | ||
|
||
context 'when logged in as a moderator' do | ||
let(:user) { Fabricate(:user, role: UserRole.find_by!(name: 'Moderator')) } | ||
|
||
before do | ||
sign_in user, scope: :user | ||
end | ||
|
||
it 'returns HTTP success' do | ||
get '/admin/emergency_rules' | ||
|
||
expect(response).to have_http_status(200) | ||
end | ||
end | ||
end | ||
|
||
describe 'the deactivation route' do | ||
let(:rule) { Fabricate('Emergency::Rule') } | ||
|
||
before do | ||
rule.trigger!(1.day.ago) | ||
end | ||
|
||
context 'when not logged in' do | ||
it 'returns HTTP forbidden' do | ||
post "/admin/emergency_rules/#{rule.id}/deactivate" | ||
|
||
expect(response).to have_http_status(403) | ||
end | ||
end | ||
|
||
context 'when logged in as a regular user' do | ||
before do | ||
sign_in Fabricate(:user), scope: :user | ||
end | ||
|
||
it 'returns HTTP forbidden' do | ||
post "/admin/emergency_rules/#{rule.id}/deactivate" | ||
|
||
expect(response).to have_http_status(403) | ||
end | ||
end | ||
|
||
context 'when logged in as a moderator' do | ||
let(:user) { Fabricate(:user, role: UserRole.find_by!(name: 'Moderator')) } | ||
|
||
before do | ||
sign_in user, scope: :user | ||
end | ||
|
||
it 'redirects' do | ||
post "/admin/emergency_rules/#{rule.id}/deactivate" | ||
|
||
expect(response).to redirect_to '/admin/emergency_rules' | ||
end | ||
|
||
it 'deactivates the rule' do | ||
expect { post "/admin/emergency_rules/#{rule.id}/deactivate" }.to change { rule.reload.active? }.from(true).to(false) | ||
end | ||
end | ||
end | ||
end |