From 9dd5639f90e7a256863a2b1fc199390def8bdb14 Mon Sep 17 00:00:00 2001
From: Eugen Rochko <eugen@zeonfederated.com>
Date: Sun, 19 Aug 2018 00:58:53 +0200
Subject: [PATCH] Add admin function to deactivate all invites (#8279)

Fix #8261
---
 app/controllers/admin/invites_controller.rb |  6 +++++
 app/policies/invite_policy.rb               |  4 +++
 app/views/admin/invites/index.html.haml     | 28 +++++++++++++--------
 config/locales/en.yml                       |  1 +
 config/routes.rb                            |  7 +++++-
 5 files changed, 34 insertions(+), 12 deletions(-)

diff --git a/app/controllers/admin/invites_controller.rb b/app/controllers/admin/invites_controller.rb
index faccaa7c89..44a8eec77b 100644
--- a/app/controllers/admin/invites_controller.rb
+++ b/app/controllers/admin/invites_controller.rb
@@ -30,6 +30,12 @@ module Admin
       redirect_to admin_invites_path
     end
 
+    def deactivate_all
+      authorize :invite, :deactivate_all?
+      Invite.available.in_batches.update_all(expires_at: Time.now.utc)
+      redirect_to admin_invites_path
+    end
+
     private
 
     def resource_params
diff --git a/app/policies/invite_policy.rb b/app/policies/invite_policy.rb
index a2a65f934d..14236f78b8 100644
--- a/app/policies/invite_policy.rb
+++ b/app/policies/invite_policy.rb
@@ -9,6 +9,10 @@ class InvitePolicy < ApplicationPolicy
     min_required_role?
   end
 
+  def deactivate_all?
+    admin?
+  end
+
   def destroy?
     owner? || (Setting.min_invite_role == 'admin' ? admin? : staff?)
   end
diff --git a/app/views/admin/invites/index.html.haml b/app/views/admin/invites/index.html.haml
index 944a604714..42159e9f36 100644
--- a/app/views/admin/invites/index.html.haml
+++ b/app/views/admin/invites/index.html.haml
@@ -9,22 +9,28 @@
       %li= filter_link_to t('admin.invites.filter.available'), available: 1, expired: nil
       %li= filter_link_to t('admin.invites.filter.expired'), available: nil, expired: 1
 
+%hr.spacer/
+
 - if policy(:invite).create?
   %p= t('invites.prompt')
 
   = render 'invites/form'
 
-  %hr/
+  %hr.spacer/
 
-%table.table
-  %thead
-    %tr
-      %th
-      %th= t('invites.table.uses')
-      %th= t('invites.table.expires_at')
-      %th
-      %th
-  %tbody
-    = render @invites
+.table-wrapper
+  %table.table
+    %thead
+      %tr
+        %th
+        %th= t('invites.table.uses')
+        %th= t('invites.table.expires_at')
+        %th
+        %th
+    %tbody
+      = render @invites
 
 = paginate @invites
+
+- if policy(:invite).deactivate_all?
+  = link_to t('admin.invites.deactivate_all'), deactivate_all_admin_invites_path, method: :post, data: { confirm: t('admin.accounts.are_you_sure') }, class: 'button'
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 66ab8f10a1..65fc7b78e0 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -281,6 +281,7 @@ en:
       search: Search
       title: Known instances
     invites:
+      deactivate_all: Deactivate all
       filter:
         all: All
         available: Available
diff --git a/config/routes.rb b/config/routes.rb
index fd3b5fe4ba..a8716aae59 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -134,7 +134,12 @@ Rails.application.routes.draw do
     resources :email_domain_blocks, only: [:index, :new, :create, :destroy]
     resources :action_logs, only: [:index]
     resource :settings, only: [:edit, :update]
-    resources :invites, only: [:index, :create, :destroy]
+
+    resources :invites, only: [:index, :create, :destroy] do
+      collection do
+        post :deactivate_all
+      end
+    end
 
     resources :relays, only: [:index, :new, :create, :destroy] do
       member do