Skip to content

A platform for community discussion. Free, open, simple.

FIX: Improve group add members UX for owners without invite permission (#38817)

Natalie Taye881af0a86fb
diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb
index e5768c7e519..d231bea5706 100644
--- a/app/controllers/groups_controller.rb
+++ b/app/controllers/groups_controller.rb
@@ -387,7 +387,9 @@ class GroupsController < ApplicationController
end
end
- guardian.ensure_can_invite_to_forum!([group]) if emails.present?
+ if emails.present? && !guardian.can_invite_to_forum?([group])
+ return render_json_error(I18n.t("groups.errors.cannot_add_emails"))
+ end
if users.empty? && emails.empty?
raise Discourse::InvalidParameters.new(I18n.t("groups.errors.usernames_or_emails_required"))
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 893b37481c3..6af0f330a2d 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -1227,6 +1227,7 @@ en:
add_members:
title: "Add Users to %{group_name}"
description: "Enter a list of users you want to invite to the group or paste in a comma separated list:"
+ description_usernames_only: "Enter a list of usernames you want to add to the group or paste in a comma separated list:"
usernames_placeholder: "usernames"
usernames_or_emails_placeholder: "usernames or emails"
notify_users: "Notify users"
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index e233fc45513..4abe1bfd953 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -651,6 +651,7 @@ en:
one: "Maximum %{count} user can be added at once"
other: "Maximum %{count} users can be added at once"
usernames_or_emails_required: "Usernames or emails must be present"
+ cannot_add_emails: "Only usernames can be used to add members to this group. To invite by email, use the Invite button."
counting_too_many_email_domains:
one: "Maximum %{count} email domain can be counted at once"
other: "Maximum %{count} email domains can be counted at once"
diff --git a/frontend/discourse/app/components/modal/group-add-members.gjs b/frontend/discourse/app/components/modal/group-add-members.gjs
index 68e4675c030..316b7d459c2 100644
--- a/frontend/discourse/app/components/modal/group-add-members.gjs
+++ b/frontend/discourse/app/components/modal/group-add-members.gjs
@@ -10,6 +10,7 @@ import DModal from "discourse/components/d-modal";
import { extractError } from "discourse/lib/ajax-error";
import { emailValid } from "discourse/lib/utilities";
import EmailGroupUserChooser from "discourse/select-kit/components/email-group-user-chooser";
+import UserChooser from "discourse/select-kit/components/user-chooser";
import { and, not, or } from "discourse/truth-helpers";
import { i18n } from "discourse-i18n";
@@ -90,20 +91,32 @@ export default class GroupAddMembers extends Component {
>
<:body>
<form class="form-vertical group-add-members">
- <p>{{i18n "groups.add_members.description"}}</p>
+ <p>{{i18n
+ (if
+ this.currentUser.can_invite_to_forum
+ "groups.add_members.description"
+ "groups.add_members.description_usernames_only"
+ )
+ }}</p>
<div class="input-group">
- <EmailGroupUserChooser
- @value={{this.usernamesAndEmails}}
- @onChange={{this.setUsernamesAndEmails}}
- @options={{hash
- allowEmails=this.currentUser.can_invite_to_forum
- filterPlaceholder=(if
- this.currentUser.can_invite_to_forum
- "groups.add_members.usernames_or_emails_placeholder"
- "groups.add_members.usernames_placeholder"
- )
- }}
- />
+ {{#if this.currentUser.can_invite_to_forum}}
+ <EmailGroupUserChooser
+ @value={{this.usernamesAndEmails}}
+ @onChange={{this.setUsernamesAndEmails}}
+ @options={{hash
+ allowEmails=true
+ filterPlaceholder="groups.add_members.usernames_or_emails_placeholder"
+ }}
+ />
+ {{else}}
+ <UserChooser
+ @value={{this.usernamesAndEmails}}
+ @onChange={{this.setUsernamesAndEmails}}
+ @options={{hash
+ filterPlaceholder="groups.add_members.usernames_placeholder"
+ }}
+ />
+ {{/if}}
</div>
{{#if @model.can_admin_group}}
diff --git a/spec/requests/groups_controller_spec.rb b/spec/requests/groups_controller_spec.rb
index 07779583e18..a7b11fa824e 100644
--- a/spec/requests/groups_controller_spec.rb
+++ b/spec/requests/groups_controller_spec.rb
@@ -1645,12 +1645,29 @@ RSpec.describe GroupsController do
expect(response.status).to eq(200)
end
- it "does not send invites if user cannot invite" do
+ it "returns a clear error when group owner without invite permission submits emails" do
group.add_owner(user)
sign_in(user)
put "/groups/#{group.id}/members.json", params: { emails: "[email protected]" }
- expect(response.status).to eq(403)
+ expect(response.status).to eq(422)
+ expect(response.parsed_body["errors"].first).to include("Only usernames")
+ end
+
+ it "rejects emails even when valid usernames are also submitted by owner without invite permission" do
+ group.add_owner(user)
+ sign_in(user)
+
+ expect {
+ put "/groups/#{group.id}/members.json",
+ params: {
+ usernames: other_user.username,
+ emails: "[email protected]",
+ }
+ }.not_to change { group.users.count }
+
+ expect(response.status).to eq(422)
+ expect(response.parsed_body["errors"].first).to include("Only usernames")
end
context "when is able to add several members to a group" do