Server side support for inviting as a moderator via the wizard

This commit is contained in:
Robin Ward 2016-09-20 13:12:00 -04:00
parent 42f6e52dc6
commit b0ee7930e8
7 changed files with 64 additions and 12 deletions

View file

@ -18,6 +18,7 @@ body.stop-scrolling {
.sweet-alert { .sweet-alert {
background-color: white; background-color: white;
font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif;
width: 478px; width: 478px;
padding: 17px; padding: 17px;
border-radius: 5px; border-radius: 5px;

View file

@ -73,19 +73,35 @@ class Invite < ActiveRecord::Base
end end
def self.invite_by_email(email, invited_by, topic=nil, group_ids=nil, custom_message=nil) def self.invite_by_email(email, invited_by, topic=nil, group_ids=nil, custom_message=nil)
create_invite_by_email(email, invited_by, topic, group_ids, true, custom_message) create_invite_by_email(email, invited_by, {
topic: topic,
group_ids: group_ids,
custom_message: custom_message,
send_email: true
})
end end
# generate invite link # generate invite link
def self.generate_invite_link(email, invited_by, topic=nil, group_ids=nil) def self.generate_invite_link(email, invited_by, topic=nil, group_ids=nil)
invite = create_invite_by_email(email, invited_by, topic, group_ids, false) invite = create_invite_by_email(email, invited_by, {
topic: topic,
group_ids: group_ids,
send_email: false
})
return "#{Discourse.base_url}/invites/#{invite.invite_key}" if invite return "#{Discourse.base_url}/invites/#{invite.invite_key}" if invite
end end
# Create an invite for a user, supplying an optional topic # Create an invite for a user, supplying an optional topic
# #
# Return the previously existing invite if already exists. Returns nil if the invite can't be created. # Return the previously existing invite if already exists. Returns nil if the invite can't be created.
def self.create_invite_by_email(email, invited_by, topic=nil, group_ids=nil, send_email=true, custom_message=nil) def self.create_invite_by_email(email, invited_by, opts=nil)
opts ||= {}
topic = opts[:topic]
group_ids = opts[:group_ids]
send_email = opts[:send_email] || true
custom_message = opts[:custom_message]
lower_email = Email.downcase(email) lower_email = Email.downcase(email)
user = User.find_by(email: lower_email) user = User.find_by(email: lower_email)
@ -105,7 +121,9 @@ class Invite < ActiveRecord::Base
end end
if !invite if !invite
invite = Invite.create!(invited_by: invited_by, email: lower_email) create_args = { invited_by: invited_by, email: lower_email }
create_args[:moderator] = true if opts[:moderator]
invite = Invite.create!(create_args)
end end
if topic && !invite.topic_invites.pluck(:topic_id).include?(topic.id) if topic && !invite.topic_invites.pluck(:topic_id).include?(topic.id)

View file

@ -30,6 +30,8 @@ InviteRedeemer = Struct.new(:invite, :username, :name) do
available_name = name || available_username available_name = name || available_username
user = User.new(email: invite.email, username: available_username, name: available_name, active: true, trust_level: SiteSetting.default_invitee_trust_level) user = User.new(email: invite.email, username: available_username, name: available_name, active: true, trust_level: SiteSetting.default_invitee_trust_level)
user.moderator = true if invite.moderator? && invite.invited_by.staff?
user.save! user.save!
user user

View file

@ -0,0 +1,5 @@
class AddModeratorToInvites < ActiveRecord::Migration
def change
add_column :invites, :moderator, :boolean, default: false, null: false
end
end

View file

@ -155,7 +155,9 @@ class Wizard
users = JSON.parse(updater.fields[:invite_list]) users = JSON.parse(updater.fields[:invite_list])
users.each do |u| users.each do |u|
Invite.create_invite_by_email(u['email'], @wizard.user) args = {}
args[:moderator] = true if u['role'] == 'moderator'
Invite.create_invite_by_email(u['email'], @wizard.user, args)
end end
end end
end end

View file

@ -203,8 +203,13 @@ describe Wizard::StepUpdater do
expect(updater).to be_success expect(updater).to be_success
expect(wizard.completed_steps?('invites')).to eq(true) expect(wizard.completed_steps?('invites')).to eq(true)
expect(Invite.where(email: 'regular@example.com')).to be_present reg_invite = Invite.where(email: 'regular@example.com').first
expect(Invite.where(email: 'moderator@example.com')).to be_present expect(reg_invite).to be_present
expect(reg_invite.moderator?).to eq(false)
mod_invite = Invite.where(email: 'moderator@example.com').first
expect(mod_invite).to be_present
expect(mod_invite.moderator?).to eq(true)
end end
end end

View file

@ -222,7 +222,7 @@ describe Invite do
end end
it 'does not enqueue an email if the user has already set password' do it 'does not enqueue an email if the user has already set password' do
user = Fabricate(:user, email: invite.email, password_hash: "7af7805c9ee3697ed1a83d5e3cb5a3a431d140933a87fdcdc5a42aeef9337f81") Fabricate(:user, email: invite.email, password_hash: "7af7805c9ee3697ed1a83d5e3cb5a3a431d140933a87fdcdc5a42aeef9337f81")
Jobs.expects(:enqueue).with(:invite_password_instructions_email, has_key(:username)).never Jobs.expects(:enqueue).with(:invite_password_instructions_email, has_key(:username)).never
invite.redeem invite.redeem
end end
@ -234,6 +234,25 @@ describe Invite do
end end
context "as a moderator" do
it "will give the user a moderator flag" do
invite.invited_by = Fabricate(:admin)
invite.moderator = true
invite.save
user = invite.redeem
expect(user).to be_moderator
end
it "will not give the user a moderator flag if the inviter is not staff" do
invite.moderator = true
invite.save
user = invite.redeem
expect(user).not_to be_moderator
end
end
context "when inviting to groups" do context "when inviting to groups" do
it "add the user to the correct groups" do it "add the user to the correct groups" do
group = Fabricate(:group) group = Fabricate(:group)
@ -465,13 +484,13 @@ describe Invite do
let(:user) { Fabricate(:user, email: invite.email) } let(:user) { Fabricate(:user, email: invite.email) }
it 'redeems the invite from email' do it 'redeems the invite from email' do
result = Invite.redeem_from_email(user.email) Invite.redeem_from_email(user.email)
invite.reload invite.reload
expect(invite).to be_redeemed expect(invite).to be_redeemed
end end
it 'does not redeem the invite if email does not match' do it 'does not redeem the invite if email does not match' do
result = Invite.redeem_from_email('test24@example.com') Invite.redeem_from_email('test24@example.com')
invite.reload invite.reload
expect(invite).not_to be_redeemed expect(invite).not_to be_redeemed
end end
@ -484,13 +503,13 @@ describe Invite do
let(:user) { Fabricate(:user, email: invite.email) } let(:user) { Fabricate(:user, email: invite.email) }
it 'redeems the invite from token' do it 'redeems the invite from token' do
result = Invite.redeem_from_token(invite.invite_key, user.email) Invite.redeem_from_token(invite.invite_key, user.email)
invite.reload invite.reload
expect(invite).to be_redeemed expect(invite).to be_redeemed
end end
it 'does not redeem the invite if token does not match' do it 'does not redeem the invite if token does not match' do
result = Invite.redeem_from_token("bae0071f995bb4b6f756e80b383778b5", user.email) Invite.redeem_from_token("bae0071f995bb4b6f756e80b383778b5", user.email)
invite.reload invite.reload
expect(invite).not_to be_redeemed expect(invite).not_to be_redeemed
end end