From f571abfaafa0cc7b705fe8588700c86a39651c90 Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Tue, 29 Jul 2014 23:27:08 +0530 Subject: [PATCH] FEATURE: allow staff to send multiple invites to same email --- app/controllers/invites_controller.rb | 5 +++++ app/models/invite.rb | 2 +- ...emove_unique_constraint_from_invites_index.rb | 11 +++++++++++ lib/guardian.rb | 4 ++++ spec/controllers/invites_controller_spec.rb | 16 ++++++++++++++++ 5 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20140729092525_remove_unique_constraint_from_invites_index.rb diff --git a/app/controllers/invites_controller.rb b/app/controllers/invites_controller.rb index 8664f86c8..32ce8f8f8 100644 --- a/app/controllers/invites_controller.rb +++ b/app/controllers/invites_controller.rb @@ -35,6 +35,11 @@ class InvitesController < ApplicationController guardian.ensure_can_invite_to_forum!(group_ids) + invite_exists = Invite.where(email: params[:email], invited_by_id: current_user.id).first + if invite_exists + guardian.ensure_can_send_multiple_invites!(current_user) + end + if Invite.invite_by_email(params[:email], current_user, topic=nil, group_ids) render json: success_json else diff --git a/app/models/invite.rb b/app/models/invite.rb index 61e6759d4..dc1db2b1d 100644 --- a/app/models/invite.rb +++ b/app/models/invite.rb @@ -205,6 +205,6 @@ end # # Indexes # -# index_invites_on_email_and_invited_by_id (email,invited_by_id) UNIQUE +# index_invites_on_email_and_invited_by_id (email,invited_by_id) # index_invites_on_invite_key (invite_key) UNIQUE # diff --git a/db/migrate/20140729092525_remove_unique_constraint_from_invites_index.rb b/db/migrate/20140729092525_remove_unique_constraint_from_invites_index.rb new file mode 100644 index 000000000..6ec759f65 --- /dev/null +++ b/db/migrate/20140729092525_remove_unique_constraint_from_invites_index.rb @@ -0,0 +1,11 @@ +class RemoveUniqueConstraintFromInvitesIndex < ActiveRecord::Migration + def up + remove_index :invites, [:email, :invited_by_id] + add_index :invites, [:email, :invited_by_id], unique: false + end + + def down + remove_index :invites, [:email, :invited_by_id] + add_index :invites, [:email, :invited_by_id], unique: true + end +end diff --git a/lib/guardian.rb b/lib/guardian.rb index 2f2365566..838e6325d 100644 --- a/lib/guardian.rb +++ b/lib/guardian.rb @@ -214,6 +214,10 @@ class Guardian user.admin? end + def can_send_multiple_invites?(user) + user.staff? + end + def can_see_private_messages?(user_id) is_admin? || (authenticated? && @user.id == user_id) end diff --git a/spec/controllers/invites_controller_spec.rb b/spec/controllers/invites_controller_spec.rb index dce252b64..db7578459 100644 --- a/spec/controllers/invites_controller_spec.rb +++ b/spec/controllers/invites_controller_spec.rb @@ -53,6 +53,14 @@ describe InvitesController do response.should_not be_success end + it "fails for normal user if invite email already exists" do + user = log_in(:elder) + invite = Invite.invite_by_email("invite@example.com", user) + invite.reload + post :create, email: invite.email + response.should_not be_success + end + it "allows admins to invite to groups" do group = Fabricate(:group) log_in(:admin) @@ -60,6 +68,14 @@ describe InvitesController do response.should be_success Invite.find_by(email: email).invited_groups.count.should == 1 end + + it "allows admin to send multiple invites to same email" do + user = log_in(:admin) + invite = Invite.invite_by_email("invite@example.com", user) + invite.reload + post :create, email: invite.email + response.should be_success + end end end