Feature: Allow assign on groups (#31)

Feature: Allow assign on groups (#31)

  • Feature: Allow assign on groups

  • Use type: group_list option

  • Track group changes and update the setting accordingly. Restrict reminders frequency to assign allowed users instead of staff

diff --git a/app/controllers/discourse_assign/assign_controller.rb b/app/controllers/discourse_assign/assign_controller.rb
index 8e141ab..dfae899 100644
--- a/app/controllers/discourse_assign/assign_controller.rb
+++ b/app/controllers/discourse_assign/assign_controller.rb
@@ -1,13 +1,13 @@
 # frozen_string_literal: true
 
 module DiscourseAssign
-  class AssignController < Admin::AdminController
-    before_action :ensure_logged_in
+  class AssignController < ApplicationController
+    requires_login
+    before_action :ensure_logged_in, :ensure_assign_allowed
 
     def suggestions
       users = [current_user]
       users += User
-        .where('admin OR moderator')
         .where('users.id <> ?', current_user.id)
         .joins(<<~SQL
           JOIN(
@@ -18,7 +18,9 @@ module DiscourseAssign
                 HAVING COUNT(*) < #{SiteSetting.max_assigned_topics}
               ) as X ON X.user_id = users.id
         SQL
-        ).order('X.last_assigned DESC')
+        )
+        .assign_allowed
+        .order('X.last_assigned DESC')
         .limit(6)
 
       render json: ActiveModel::ArraySerializer.new(users,
@@ -88,5 +90,9 @@ module DiscourseAssign
         { error: I18n.t('discourse_assign.too_many_assigns', username: user.username, max: max) }
       end
     end
+
+    def ensure_assign_allowed
+      raise Discourse::InvalidAccess.new unless current_user.can_assign?
+    end
   end
 end
diff --git a/assets/javascripts/discourse-assign/connectors/topic-footer-main-buttons-before-create/assign-button.js.es6 b/assets/javascripts/discourse-assign/connectors/topic-footer-main-buttons-before-create/assign-button.js.es6
index ea78d64..e14a552 100644
--- a/assets/javascripts/discourse-assign/connectors/topic-footer-main-buttons-before-create/assign-button.js.es6
+++ b/assets/javascripts/discourse-assign/connectors/topic-footer-main-buttons-before-create/assign-button.js.es6
@@ -3,7 +3,7 @@ import { getOwner } from "discourse-common/lib/get-owner";
 export default {
   shouldRender(args, component) {
     const needsButton =
-      component.currentUser && component.currentUser.get("staff");
+      component.currentUser && component.currentUser.get("can_assign");
     return (
       needsButton &&
       (!component.get("site.mobileView") || args.topic.get("isPrivateMessage"))
diff --git a/assets/javascripts/discourse-assign/connectors/user-activity-bottom/assigned-list.hbs b/assets/javascripts/discourse-assign/connectors/user-activity-bottom/assigned-list.hbs
index a84d53e..545f12b 100644
--- a/assets/javascripts/discourse-assign/connectors/user-activity-bottom/assigned-list.hbs
+++ b/assets/javascripts/discourse-assign/connectors/user-activity-bottom/assigned-list.hbs
@@ -1,4 +1,4 @@
-{{#if currentUser.staff}}
+{{#if currentUser.can_assign}}
   {{#link-to 'userActivity.assigned'}}
     {{d-icon "user-plus"}} {{i18n 'discourse_assign.assigned'}}
   {{/link-to}}
diff --git a/assets/javascripts/discourse-assign/connectors/user-messages-nav/assigned-messages.js.es6 b/assets/javascripts/discourse-assign/connectors/user-messages-nav/assigned-messages.js.es6
index a7ba007..9853ce3 100644
--- a/assets/javascripts/discourse-assign/connectors/user-messages-nav/assigned-messages.js.es6
+++ b/assets/javascripts/discourse-assign/connectors/user-messages-nav/assigned-messages.js.es6
@@ -1,6 +1,6 @@
 export function shouldShowAssigned(args, component) {
   const needsButton =
-    component.currentUser && component.currentUser.get("staff");
+    component.currentUser && component.currentUser.get("can_assign");
   return (
     needsButton &&
     (!component.get("site.mobileView") || args.model.get("isPrivateMessage"))
diff --git a/assets/javascripts/discourse-assign/connectors/user-preferences-notifications/remind-assigns-frequency.js.es6 b/assets/javascripts/discourse-assign/connectors/user-preferences-notifications/remind-assigns-frequency.js.es6
index 3ff2ebb..4db0626 100644
--- a/assets/javascripts/discourse-assign/connectors/user-preferences-notifications/remind-assigns-frequency.js.es6
+++ b/assets/javascripts/discourse-assign/connectors/user-preferences-notifications/remind-assigns-frequency.js.es6
@@ -1,5 +1,5 @@
 export default {
   shouldRender(args, component) {
-    return component.currentUser && component.currentUser.get("staff");
+    return component.currentUser && component.currentUser.get("can_assign");
   }
 };
diff --git a/assets/javascripts/discourse-assign/controllers/assign-user.js.es6 b/assets/javascripts/discourse-assign/controllers/assign-user.js.es6
index da6bcae..13692df 100644
--- a/assets/javascripts/discourse-assign/controllers/assign-user.js.es6
+++ b/assets/javascripts/discourse-assign/controllers/assign-user.js.es6
@@ -3,6 +3,7 @@ import { ajax } from "discourse/lib/ajax";
 import { popupAjaxError } from "discourse/lib/ajax-error";
 
 export default Ember.Controller.extend({
+  taskActions: Ember.inject.service(),
   assignSuggestions: function() {
     ajax("/assign/suggestions").then(users => {
       this.set("assignSuggestions", users);
@@ -23,6 +24,7 @@ export default Ember.Controller.extend({
   actions: {
     assignUser(user) {
       this.set("model.username", user.username);
+      this.set("model.allowedGroups", this.get("taskActions").allowedGroups);
       this.send("assign");
     },
 
diff --git a/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6 b/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6
index 638031c..adf540e 100644
--- a/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6
+++ b/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6
@@ -17,7 +17,7 @@ function modifySelectKit(api) {
   api
     .modifySelectKit("topic-footer-mobile-dropdown")
     .modifyContent((context, existingContent) => {
-      if (context.get("currentUser.staff")) {
+      if (context.get("currentUser.can_assign")) {
         const hasAssignement = context.get("topic.assigned_to_user");
         const button = {
           id: ACTION_ID,
@@ -31,7 +31,7 @@ function modifySelectKit(api) {
       return existingContent;
     })
     .onSelect((context, value) => {
-      if (!context.get("currentUser.staff") || value !== ACTION_ID) {
+      if (!context.get("currentUser.can_assign") || value !== ACTION_ID) {
         return;
       }
 
@@ -157,7 +157,7 @@ function initialize(api) {
   });
 
   api.addUserMenuGlyph(widget => {
-    if (widget.currentUser && widget.currentUser.get("staff")) {
+    if (widget.currentUser && widget.currentUser.get("can_assign")) {
       return {
         label: "discourse_assign.assigned",
         className: "assigned",
diff --git a/assets/javascripts/discourse/services/task-actions.js.es6 b/assets/javascripts/discourse/services/task-actions.js.es6
index 41bf387..ae25601 100644
--- a/assets/javascripts/discourse/services/task-actions.js.es6
+++ b/assets/javascripts/discourse/services/task-actions.js.es6
@@ -1,7 +1,16 @@
 import { ajax } from "discourse/lib/ajax";
 import showModal from "discourse/lib/show-modal";
+import { getOwner } from "discourse-common/lib/get-owner";
 
 export default Ember.Service.extend({
+  init() {
+    this._super(...arguments);
+
+    this.allowedGroups = getOwner(this)
+      .lookup("site-settings:main")
+      .assign_allowed_on_groups.split("|");
+  },
+
   unassign(topicId) {
     return ajax("/assign/unassign", {
       type: "PUT",
@@ -13,7 +22,8 @@ export default Ember.Service.extend({
     return showModal("assign-user", {
       model: {
         topic: topic,
-        username: topic.get("assigned_to_user.username")
+        username: topic.get("assigned_to_user.username"),
+        allowedGroups: this.allowedGroups
       }
     });
   }
diff --git a/assets/javascripts/discourse/templates/modal/assign-user.hbs b/assets/javascripts/discourse/templates/modal/assign-user.hbs
index 149cf1c..b2ad776 100644
--- a/assets/javascripts/discourse/templates/modal/assign-user.hbs

[... diff too long, it was truncated ...]

GitHub sha: f312ece4

1 Like