diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 4f02e19ab..855e3ab63 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -56,7 +56,7 @@ class Admin::UsersController < Admin::AdminController @user.save! @user.revoke_api_key StaffActionLogger.new(current_user).log_user_suspend(@user, params[:reason]) - MessageBus.publish "/logout", @user.id, user_ids: [@user.id] + @user.logged_out render nothing: true end @@ -73,7 +73,7 @@ class Admin::UsersController < Admin::AdminController if @user @user.auth_token = nil @user.save! - MessageBus.publish "/logout", @user.id, user_ids: [@user.id] + @user.logged_out render json: success_json else render json: {error: I18n.t('admin_js.admin.users.id_not_found')}, status: 404 diff --git a/app/models/user.rb b/app/models/user.rb index cbc9fe393..bf89597d1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -854,6 +854,11 @@ class User < ActiveRecord::Base User.where(admin: true).where.not(id: id).where.not(id: Discourse::SYSTEM_USER_ID).blank? end + def logged_out + MessageBus.publish "/logout", self.id, user_ids: [self.id] + DiscourseEvent.trigger(:user_logged_out, self) + end + protected def badge_grant @@ -999,7 +1004,6 @@ class User < ActiveRecord::Base end end - end # == Schema Information diff --git a/lib/auth/default_current_user_provider.rb b/lib/auth/default_current_user_provider.rb index 315a19b89..0c084e84a 100644 --- a/lib/auth/default_current_user_provider.rb +++ b/lib/auth/default_current_user_provider.rb @@ -100,7 +100,7 @@ class Auth::DefaultCurrentUserProvider cookies["__profilin"] = nil end - MessageBus.publish "/logout", user.id, user_ids: [user.id] + user.logged_out end cookies[TOKEN_COOKIE] = nil end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 71deb205c..665a9839d 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1276,4 +1276,15 @@ describe User do end + describe "#logged_out" do + let(:user) { Fabricate(:user) } + + it 'should publish the right message' do + message = MessageBus.track_publish { user.logged_out }.first + + expect(message.channel).to eq('/logout') + expect(message.data).to eq(user.id) + end + end + end