A platform for community discussion. Free, open, simple.
FIX: show sidebar "New category" option for moderators (#37624)
Régis Hanolca8fcb032f87
diff --git a/app/serializers/current_user_serializer.rb b/app/serializers/current_user_serializer.rbindex 12f491921a0..87fa81f69d1 100644--- a/app/serializers/current_user_serializer.rb+++ b/app/serializers/current_user_serializer.rb@@ -53,6 +53,7 @@ class CurrentUserSerializer < BasicUserSerializer:primary_group_id,:flair_group_id,:can_create_topic,+ :can_create_category,:can_create_group,:link_posting_access,:external_id,@@ -127,8 +128,16 @@ class CurrentUserSerializer < BasicUserSerializerscope.can_create_topic?(nil)end+ def can_create_category+ true+ end++ def include_can_create_category?+ scope.can_create_category?+ end+def can_create_group- scope.can_create_group?+ trueenddef include_can_create_group?@@ -189,7 +198,7 @@ class CurrentUserSerializer < BasicUserSerializerenddef can_invite_to_forum- scope.can_invite_to_forum?+ trueenddef include_can_invite_to_forum?@@ -204,14 +213,14 @@ class CurrentUserSerializer < BasicUserSerializer!object.has_password?end- def include_can_delete_account?- scope.can_delete_user?(object)- end-def can_delete_accounttrueend+ def include_can_delete_account?+ scope.can_delete_user?(object)+ end+def custom_fieldsfields = nilif SiteSetting.public_user_custom_fields.present?diff --git a/frontend/discourse/admin/routes/new-category.js b/frontend/discourse/admin/routes/new-category.jsindex a302227d755..7364947283b 100644--- a/frontend/discourse/admin/routes/new-category.js+++ b/frontend/discourse/admin/routes/new-category.js@@ -21,17 +21,8 @@ export default class NewCategory extends DiscourseRoute {templateName = "edit-category.tabs";beforeModel() {- if (!this.currentUser) {- this.router.replaceWith("/404");- return;- }- if (!this.currentUser.admin) {- if (- !this.currentUser.moderator ||- this.siteSettings.moderators_manage_categories === false- ) {- this.router.replaceWith("/404");- }+ if (!this.currentUser?.can_create_category) {+ return this.router.replaceWith("/404");}}diff --git a/frontend/discourse/app/components/sidebar/user/categories-section.gjs b/frontend/discourse/app/components/sidebar/user/categories-section.gjsindex d756fc1917a..21776abcca1 100644--- a/frontend/discourse/app/components/sidebar/user/categories-section.gjs+++ b/frontend/discourse/app/components/sidebar/user/categories-section.gjs@@ -71,7 +71,7 @@ export default class SidebarUserCategoriesSection extends CommonCategoriesSectioget headerActions() {const actions = [];- if (this.currentUser.admin) {+ if (this.currentUser.can_create_category) {actions.push({id: "new-category",action: () => this.router.transitionTo("newCategory"),diff --git a/frontend/discourse/tests/acceptance/category-new-test.js b/frontend/discourse/tests/acceptance/category-new-test.jsindex 548ad53e213..222823dadd4 100644--- a/frontend/discourse/tests/acceptance/category-new-test.js+++ b/frontend/discourse/tests/acceptance/category-new-test.js@@ -6,26 +6,29 @@ import { cloneJSON } from "discourse/lib/object";import DiscourseURL from "discourse/lib/url";import { fixturesByUrl } from "discourse/tests/helpers/create-pretender";import formKit from "discourse/tests/helpers/form-kit-helper";-import { acceptance } from "discourse/tests/helpers/qunit-helpers";+import {+ acceptance,+ updateCurrentUser,+} from "discourse/tests/helpers/qunit-helpers";import selectKit from "discourse/tests/helpers/select-kit-helper";import { i18n } from "discourse-i18n";acceptance("New category access for moderators", function (needs) {needs.user({ moderator: true, admin: false, trust_level: 1 });- test("Authorizes access based on site setting", async function (assert) {- this.siteSettings.moderators_manage_categories = false;+ test("Prevents access when moderator cannot create categories", async function (assert) {await visit("/new-category");-assert.strictEqual(currentURL(), "/404");+ });- this.siteSettings.moderators_manage_categories = true;+ test("Authorizes access when moderator can create categories", async function (assert) {+ updateCurrentUser({ can_create_category: true });await visit("/new-category");assert.strictEqual(currentURL(),"/new-category",- "it allows access to new category when site setting is enabled"+ "it allows access to new category");});});@@ -38,7 +41,7 @@ acceptance("New category access for non authorized users", function () {});acceptance("Category New", function (needs) {- needs.user();+ needs.user({ can_create_category: true });test("Creating a new category", async function (assert) {await visit("/new-category");@@ -119,7 +122,7 @@ acceptance("Category New", function (needs) {});acceptance("Category text color", function (needs) {- needs.user();+ needs.user({ can_create_category: true });needs.pretender((server, helper) => {const category = cloneJSON(fixturesByUrl["/c/11/show.json"]).category;@@ -155,7 +158,7 @@ acceptance("Category text color", function (needs) {});acceptance("New category preview", function (needs) {- needs.user({ admin: true });+ needs.user({ admin: true, can_create_category: true });test("Category badge color appears and updates", async function (assert) {await visit("/new-category");diff --git a/frontend/discourse/tests/acceptance/sidebar-user-categories-section-test.js b/frontend/discourse/tests/acceptance/sidebar-user-categories-section-test.jsindex 4986eb1e5a5..41ab7658773 100644--- a/frontend/discourse/tests/acceptance/sidebar-user-categories-section-test.js+++ b/frontend/discourse/tests/acceptance/sidebar-user-categories-section-test.js@@ -1023,8 +1023,8 @@ acceptance("Sidebar - Logged on user - Categories Section", function (needs) {);});- test("categories section header shows dropdown with new and edit actions for admin user", async function (assert) {- updateCurrentUser({ admin: true });+ test("categories section header shows dropdown with new and edit actions for user who can create categories", async function (assert) {+ updateCurrentUser({ admin: true, can_create_category: true });await visit("/");@@ -1044,7 +1044,7 @@ acceptance("Sidebar - Logged on user - Categories Section", function (needs) {assert.strictEqual(currentURL(), "/new-category");});- test("categories section header shows single edit button for non-admin user", async function (assert) {+ test("categories section header shows single edit button for user who cannot create categories", async function (assert) {await visit("/");const categoriesSection =@@ -1366,7 +1366,7 @@ acceptance(acceptance("Sidebar - Admin - Categories Section - Header Dropdown Mode",function (needs) {- needs.user({ admin: true });+ needs.user({ admin: true, can_create_category: true });needs.settings({ navigation_menu: "header dropdown" });test("edit action shows 'Edit nav categories' text", async function (assert) {diff --git a/spec/serializers/current_user_serializer_spec.rb b/spec/serializers/current_user_serializer_spec.rbindex 0dc68d59cea..e933fbaf1cd 100644--- a/spec/serializers/current_user_serializer_spec.rb+++ b/spec/serializers/current_user_serializer_spec.rb@@ -331,6 +331,44 @@ RSpec.describe CurrentUserSerializer doendend+ describe "#can_create_category" do+ let(:payload) { serializer.as_json }++ context "when user is an admin" do+ let(:user) { Fabricate(:admin) }++ it "returns true" do+ expect(payload[:can_create_category]).to eq(true)+ end+ end++ context "when user is a moderator and moderators_manage_categories is enabled" do+ let(:user) { Fabricate(:moderator) }++ before { SiteSetting.moderators_manage_categories = true }++ it "returns true" do+ expect(payload[:can_create_category]).to eq(true)+ end+ end++ context "when user is a moderator and moderators_manage_categories is disabled" do+ let(:user) { Fabricate(:moderator) }++ before { SiteSetting.moderators_manage_categories = false }++ it "is not included" do+ expect(payload).not_to have_key(:can_create_category)+ end+ end++ context "when user is a regular user" do+ it "is not included" do+ expect(payload).not_to have_key(:can_create_category)+ end+ end+ end+describe "#can_see_ip" dolet(:payload) { serializer.as_json }