UX: better error message if moderator is not allowed to invite to group

UX: better error message if moderator is not allowed to invite to group

UX: do not show invite to group option if mod is not owner of any group

diff --git a/app/assets/javascripts/discourse/app/components/invite-link-panel.js b/app/assets/javascripts/discourse/app/components/invite-link-panel.js
index 6856d2d..b0d2393 100644
--- a/app/assets/javascripts/discourse/app/components/invite-link-panel.js
+++ b/app/assets/javascripts/discourse/app/components/invite-link-panel.js
@@ -1,7 +1,7 @@
 import I18n from "I18n";
 import Component from "@ember/component";
 import Group from "discourse/models/group";
-import { readOnly } from "@ember/object/computed";
+import { alias, readOnly } from "@ember/object/computed";
 import { action } from "@ember/object";
 import discourseComputed from "discourse-common/utils/decorators";
 import Invite from "discourse/models/invite";
@@ -17,6 +17,8 @@ export default Component.extend({
   groupIds: null,
   allGroups: null,
 
+  isAdmin: alias("currentUser.admin"),
+
   init() {
     this._super(...arguments);
 
@@ -43,6 +45,13 @@ export default Component.extend({
 
   errorMessage: I18n.t("user.invited.invite_link.error"),
 
+  @discourseComputed("isAdmin", "inviteModel.group_users")
+  showGroups(isAdmin, groupUsers) {
+    return (
+      isAdmin || (groupUsers && groupUsers.some(groupUser => groupUser.owner))
+    );
+  },
+
   reset() {
     this.setProperties({
       maxRedemptionAllowed: 5,
diff --git a/app/assets/javascripts/discourse/app/templates/components/invite-link-panel.hbs b/app/assets/javascripts/discourse/app/templates/components/invite-link-panel.hbs
index 4edd213..270df19 100644
--- a/app/assets/javascripts/discourse/app/templates/components/invite-link-panel.hbs
+++ b/app/assets/javascripts/discourse/app/templates/components/invite-link-panel.hbs
@@ -9,16 +9,18 @@
     {{generated-invite-link link=inviteModel.inviteLink}}
   {{else}}
 
-    <div class="group-access-control">
-      <label class="instructions">
-        {{i18n "topic.automatically_add_to_groups"}}
-      </label>
-      {{group-chooser
-        content=allGroups
-        value=groupIds
-        labelProperty="name"
-        onChange=(action (mut groupIds))}}
-    </div>
+    {{#if showGroups}}
+      <div class="group-access-control">
+        <label class="instructions">
+          {{i18n "topic.automatically_add_to_groups"}}
+        </label>
+        {{group-chooser
+          content=allGroups
+          value=groupIds
+          labelProperty="name"
+          onChange=(action (mut groupIds))}}
+      </div>
+    {{/if}}
 
     <div class="max-redemptions-allowed">
       <label class="instructions">
diff --git a/app/controllers/invites_controller.rb b/app/controllers/invites_controller.rb
index 6e26d45..6c5e2c4 100644
--- a/app/controllers/invites_controller.rb
+++ b/app/controllers/invites_controller.rb
@@ -122,7 +122,9 @@ class InvitesController < ApplicationController
       group_ids: params[:group_ids],
       group_names: params[:group_names]
     )
-    guardian.ensure_can_invite_to_forum!(groups)
+    if !guardian.can_invite_to_forum?(groups)
+      raise StandardError.new I18n.t("invite.cant_invite_to_group")
+    end
     group_ids = groups.map(&:id)
 
     if is_single_invite
@@ -142,25 +144,23 @@ class InvitesController < ApplicationController
       end
     end
 
-    begin
-      invite_link = if is_single_invite
-        Invite.generate_single_use_invite_link(params[:email], current_user, topic, group_ids)
-      else
-        Invite.generate_multiple_use_invite_link(
-          invited_by: current_user,
-          max_redemptions_allowed: params[:max_redemptions_allowed],
-          expires_at: params[:expires_at],
-          group_ids: group_ids
-        )
-      end
-      if invite_link.present?
-        render_json_dump(invite_link)
-      else
-        render json: failed_json, status: 422
-      end
-    rescue => e
-      render json: { errors: [e.message] }, status: 422
+    invite_link = if is_single_invite
+      Invite.generate_single_use_invite_link(params[:email], current_user, topic, group_ids)
+    else
+      Invite.generate_multiple_use_invite_link(
+        invited_by: current_user,
+        max_redemptions_allowed: params[:max_redemptions_allowed],
+        expires_at: params[:expires_at],
+        group_ids: group_ids
+      )
+    end
+    if invite_link.present?
+      render_json_dump(invite_link)
+    else
+      render json: failed_json, status: 422
     end
+  rescue => e
+    render json: { errors: [e.message] }, status: 422
   end
 
   def destroy
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index e685a5e..ad766f8 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -229,6 +229,7 @@ en:
     error_message: "There was an error accepting invite. Please contact the site's administrator."
     user_exists: "There's no need to invite <b>%{email}</b>, they <a href='%{base_path}/u/%{username}/summary'>already have an account!</a>"
     confirm_email: "<p>You’re almost done! We sent an activation mail to your email address. Please follow the instructions in the mail to activate your account.</p><p>If it doesn’t arrive, check your spam folder.</p>"
+    cant_invite_to_group: "You are not allowed to invite users to specified group(s). Make sure you are owner of the group(s) you are trying to invite to."
 
   bulk_invite:
     file_should_be_csv: "The uploaded file should be of csv format."

GitHub sha: 7532f246

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/invite-links-to-groups-show-in-moderator-menu-but-doesnt-work/157533/5