automatically approve invited users on forum where moderators must approve (keep in mind only moderators can invite)

speed up specs a touch
allow invite controller to accept an email in absence of user (cleans up API)
This commit is contained in:
Sam 2013-07-11 11:21:39 +10:00
parent 7792b7da48
commit 1aef6de4b0
5 changed files with 42 additions and 24 deletions

View File

@ -190,12 +190,20 @@ class TopicsController < ApplicationController
end
def invite
params.require(:user)
username_or_email = params[:user]
if username_or_email
# provides a level of protection for hashes
params.require(:user)
else
params.require(:email)
username_or_email = params[:email]
end
topic = Topic.where(id: params[:topic_id]).first
guardian.ensure_can_invite_to!(topic)
if topic.invite(current_user, params[:user])
user = User.find_by_username_or_email(params[:user])
if topic.invite(current_user, username_or_email)
user = User.find_by_username_or_email(username_or_email)
if user
render_json_dump BasicUserSerializer.new(user, scope: guardian, root: 'user')
else

View File

@ -18,6 +18,7 @@ InviteRedeemer = Struct.new(:invite) do
add_to_private_topics_if_invited
add_user_to_invited_topics
send_welcome_message
approve_account_if_needed
notify_invitee
end
@ -66,6 +67,10 @@ InviteRedeemer = Struct.new(:invite) do
end
end
def approve_account_if_needed
invited_user.approve(invite.invited_by_id, send_email=false)
end
def notify_invitee
invite.invited_by.notifications.create(notification_type: Notification.types[:invitee_accepted],
data: {display_username: invited_user.username}.to_json)

View File

@ -182,7 +182,13 @@ class User < ActiveRecord::Base
# Approve this user
def approve(approved_by, send_mail=true)
self.approved = true
self.approved_by = approved_by
if Fixnum === approved_by
self.approved_by_id = approved_by
else
self.approved_by = approved_by
end
self.approved_at = Time.now
send_approval_email if save and send_mail

View File

@ -172,19 +172,22 @@ describe Invite do
end
context 'inviting when must_approve_users? is enabled' do
it 'correctly acitvates accounts' do
SiteSetting.stubs(:must_approve_users).returns(true)
user = invite.redeem
user.approved?.should == true
end
end
context 'simple invite' do
let!(:user) { invite.redeem }
it 'returns a user record' do
it 'works correctly' do
user.is_a?(User).should be_true
end
it 'wants us to send a welcome message' do
user.send_welcome_message.should be_true
end
it 'has the default_invitee_trust_level' do
user.trust_level.should == SiteSetting.default_invitee_trust_level
end
@ -193,28 +196,24 @@ describe Invite do
invite.reload
end
it 'no longer in the pending list for that user' do
it 'works correctly' do
# no longer in the pending list for that user
InvitedList.new(invite.invited_by).pending.should be_blank
end
it 'is redeeemed in the invite list for the creator' do
# is redeeemed in the invite list for the creator
InvitedList.new(invite.invited_by).redeemed.should == [invite]
end
it 'has set the user_id attribute' do
# has set the user_id attribute
invite.user.should == user
end
it 'returns true for redeemed' do
# returns true for redeemed
invite.should be_redeemed
end
context 'again' do
it 'will not redeem twice' do
invite.redeem.should == user
end
it "doesn't want us to send a welcome message" do
invite.redeem.send_welcome_message.should be_false
end