From dfaa140ed3b2d221d03d298040a94392fd6673b0 Mon Sep 17 00:00:00 2001 From: Brian Austin <13002992+brianjaustin@users.noreply.github.com> Date: Fri, 29 Nov 2024 00:19:26 +0100 Subject: [PATCH] AO3-6697 Limit roles that can edit known issues (#4891) * AO3-6697 Limit roles that can edit known issues * Tests without weird workarounds * Test consolidation and formatting cleanup * Make things translatable * Add missing tests * Hide posts button in admin nav when unauthorised * Fix issue from merge --- app/controllers/known_issues_controller.rb | 19 +- app/policies/known_issue_policy.rb | 16 ++ app/views/admin/_admin_nav.html.erb | 26 +-- app/views/admin/_header.html.erb | 4 +- app/views/known_issues/index.html.erb | 2 +- config/locales/views/en.yml | 6 + features/admins/admin_post_issues.feature | 39 +++- features/step_definitions/admin_steps.rb | 10 +- .../known_issues_controller_spec.rb | 187 ++++++++++++++++++ 9 files changed, 269 insertions(+), 40 deletions(-) create mode 100644 app/policies/known_issue_policy.rb create mode 100644 spec/controllers/known_issues_controller_spec.rb diff --git a/app/controllers/known_issues_controller.rb b/app/controllers/known_issues_controller.rb index 4f6bbe6cd30..793daff088e 100644 --- a/app/controllers/known_issues_controller.rb +++ b/app/controllers/known_issues_controller.rb @@ -1,5 +1,4 @@ class KnownIssuesController < ApplicationController - before_action :admin_only, except: [:index] # GET /known_issues @@ -9,25 +8,24 @@ def index # GET /known_issues/1 def show - @known_issue = KnownIssue.find(params[:id]) + @known_issue = authorize KnownIssue.find(params[:id]) end # GET /known_issues/new def new - @known_issue = KnownIssue.new + @known_issue = authorize KnownIssue.new end # GET /known_issues/1/edit def edit - @known_issue = KnownIssue.find(params[:id]) + @known_issue = authorize KnownIssue.find(params[:id]) end # POST /known_issues def create - @known_issue = KnownIssue.new(known_issue_params) - + @known_issue = authorize KnownIssue.new(known_issue_params) if @known_issue.save - flash[:notice] = 'Known issue was successfully created.' + flash[:notice] = "Known issue was successfully created." redirect_to(@known_issue) else render action: "new" @@ -36,10 +34,9 @@ def create # PUT /known_issues/1 def update - @known_issue = KnownIssue.find(params[:id]) - + @known_issue = authorize KnownIssue.find(params[:id]) if @known_issue.update(known_issue_params) - flash[:notice] = 'Known issue was successfully updated.' + flash[:notice] = "Known issue was successfully updated." redirect_to(@known_issue) else render action: "edit" @@ -48,7 +45,7 @@ def update # DELETE /known_issues/1 def destroy - @known_issue = KnownIssue.find(params[:id]) + @known_issue = authorize KnownIssue.find(params[:id]) @known_issue.destroy redirect_to(known_issues_path) end diff --git a/app/policies/known_issue_policy.rb b/app/policies/known_issue_policy.rb new file mode 100644 index 00000000000..a74de07e65b --- /dev/null +++ b/app/policies/known_issue_policy.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +class KnownIssuePolicy < ApplicationPolicy + MANAGE_ROLES = %w[superadmin support].freeze + + def admin_index? + user_has_roles?(MANAGE_ROLES) + end + + alias destroy? admin_index? + alias edit? admin_index? + alias create? admin_index? + alias new? admin_index? + alias show? admin_index? + alias update? admin_index? +end diff --git a/app/views/admin/_admin_nav.html.erb b/app/views/admin/_admin_nav.html.erb index 24f6abc0738..e705ea49bb8 100644 --- a/app/views/admin/_admin_nav.html.erb +++ b/app/views/admin/_admin_nav.html.erb @@ -1,12 +1,13 @@ -

<%= ts("Admin Navigation") %>

+

<%= t(".landmark") %>