diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index 53c07e6d18e..0c976495578 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -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 diff --git a/app/models/invite_redeemer.rb b/app/models/invite_redeemer.rb index 5e7f51fd409..91fb3aefad7 100644 --- a/app/models/invite_redeemer.rb +++ b/app/models/invite_redeemer.rb @@ -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,8 +67,12 @@ 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) end -end \ No newline at end of file +end diff --git a/app/models/user.rb b/app/models/user.rb index aae321eb6ee..77eddf65609 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/lib/current_user.rb b/lib/current_user.rb index d909e5d1fb5..4b204726cc0 100644 --- a/lib/current_user.rb +++ b/lib/current_user.rb @@ -59,9 +59,9 @@ module CurrentUser @current_user.update_ip_address!(request.remote_ip) end - # possible we have an api call, impersonate + # possible we have an api call, impersonate unless @current_user - if api_key = request["api_key"] + if api_key = request["api_key"] if api_username = request["api_username"] if SiteSetting.api_key_valid?(api_key) @current_user = User.where(username_lower: api_username.downcase).first diff --git a/spec/models/invite_spec.rb b/spec/models/invite_spec.rb index 55154b00cfc..727914e287e 100644 --- a/spec/models/invite_spec.rb +++ b/spec/models/invite_spec.rb @@ -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