Merge pull request #4306 from tgxworld/add_discourse_event_trigger_when_user_logs_out

FEATURE: Add event trigger when a user is logged out.
This commit is contained in:
Guo Xiang Tan 2016-07-05 19:50:46 +08:00 committed by GitHub
commit e4a82cdd85
4 changed files with 19 additions and 4 deletions

View file

@ -56,7 +56,7 @@ class Admin::UsersController < Admin::AdminController
@user.save! @user.save!
@user.revoke_api_key @user.revoke_api_key
StaffActionLogger.new(current_user).log_user_suspend(@user, params[:reason]) 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 render nothing: true
end end
@ -73,7 +73,7 @@ class Admin::UsersController < Admin::AdminController
if @user if @user
@user.auth_token = nil @user.auth_token = nil
@user.save! @user.save!
MessageBus.publish "/logout", @user.id, user_ids: [@user.id] @user.logged_out
render json: success_json render json: success_json
else else
render json: {error: I18n.t('admin_js.admin.users.id_not_found')}, status: 404 render json: {error: I18n.t('admin_js.admin.users.id_not_found')}, status: 404

View file

@ -854,6 +854,11 @@ class User < ActiveRecord::Base
User.where(admin: true).where.not(id: id).where.not(id: Discourse::SYSTEM_USER_ID).blank? User.where(admin: true).where.not(id: id).where.not(id: Discourse::SYSTEM_USER_ID).blank?
end end
def logged_out
MessageBus.publish "/logout", self.id, user_ids: [self.id]
DiscourseEvent.trigger(:user_logged_out, self)
end
protected protected
def badge_grant def badge_grant
@ -999,7 +1004,6 @@ class User < ActiveRecord::Base
end end
end end
end end
# == Schema Information # == Schema Information

View file

@ -100,7 +100,7 @@ class Auth::DefaultCurrentUserProvider
cookies["__profilin"] = nil cookies["__profilin"] = nil
end end
MessageBus.publish "/logout", user.id, user_ids: [user.id] user.logged_out
end end
cookies[TOKEN_COOKIE] = nil cookies[TOKEN_COOKIE] = nil
end end

View file

@ -1276,4 +1276,15 @@ describe User do
end 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 end