A platform for community discussion. Free, open, simple.
FEATURE: Impersonation countdown (#39871)
Ted Johansson66c55f60b4f1
diff --git a/app/controllers/admin/impersonate_controller.rb b/app/controllers/admin/impersonate_controller.rbindex 5c386eec688..400ccaf8d72 100644--- a/app/controllers/admin/impersonate_controller.rb+++ b/app/controllers/admin/impersonate_controller.rb@@ -21,11 +21,11 @@ class Admin::ImpersonateController < Admin::AdminControllerenddef destroy- raise Discourse::InvalidAccess unless current_user.is_impersonating-- impersonated_user = current_user- stop_impersonating_user- StaffActionLogger.new(current_user).log_stop_impersonation(impersonated_user)+ if current_user.is_impersonating+ impersonated_user = current_user+ stop_impersonating_user+ StaffActionLogger.new(current_user).log_stop_impersonation(impersonated_user)+ endrender body: nilenddiff --git a/app/models/user.rb b/app/models/user.rbindex 4e5c939a04b..fa925ff0db1 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -254,8 +254,9 @@ class User < ActiveRecord::Base# Information if user was authenticated with OAuthattr_accessor :authenticated_with_oauth- # Flag used when admin is impersonating a user+ # Attributes used when admin is impersonating a userattr_accessor :is_impersonating+ attr_accessor :impersonation_expires_atscope :with_email,->(email) { joins(:user_emails).where("lower(user_emails.email) IN (?)", email) }diff --git a/app/models/user_auth_token.rb b/app/models/user_auth_token.rbindex bda31ddcc07..3c5285a8a01 100644--- a/app/models/user_auth_token.rb+++ b/app/models/user_auth_token.rb@@ -43,7 +43,10 @@ class UserAuthToken < ActiveRecord::Basereturn if !guardian.can_impersonate?(puppet)- puppet.tap { |u| u.is_impersonating = true }+ puppet.tap do |user|+ user.is_impersonating = true+ user.impersonation_expires_at = impersonation_expires_at+ endenddef self.log(info)diff --git a/app/serializers/current_user_serializer.rb b/app/serializers/current_user_serializer.rbindex 8317161ba41..5c90b0c59bd 100644--- a/app/serializers/current_user_serializer.rb+++ b/app/serializers/current_user_serializer.rb@@ -84,6 +84,7 @@ class CurrentUserSerializer < BasicUserSerializer:effective_locale,:can_see_ip,:is_impersonating,+ :impersonation_expires_at,:can_change_post_owner,:show_site_owner_onboarding@@ -109,6 +110,10 @@ class CurrentUserSerializer < BasicUserSerializer!!object.is_impersonatingend+ def impersonation_expires_at+ object.impersonation_expires_at+ end+def include_can_change_post_owner?return true if admin?return true if SiteSetting.moderators_change_post_ownership && moderator?diff --git a/config/locales/client.en.yml b/config/locales/client.en.ymlindex 2c6e95cf55b..1e41d5994a5 100644--- a/config/locales/client.en.yml+++ b/config/locales/client.en.yml@@ -362,6 +362,9 @@ en:impersonation:notice: "You are impersonating %{username}"+ time_left:+ one: "(%{count} minute left)"+ other: "(%{count} minutes left)"stop: "Stop impersonating"themes:diff --git a/frontend/discourse/app/components/impersonation-notice.gjs b/frontend/discourse/app/components/impersonation-notice.gjsindex 8555b301e6e..4ce67892be9 100644--- a/frontend/discourse/app/components/impersonation-notice.gjs+++ b/frontend/discourse/app/components/impersonation-notice.gjs@@ -12,11 +12,36 @@ export default class ImpersonationNotice extends Component {@service currentUser;@tracked stopping = false;+ @tracked timeLeft = 0;++ constructor() {+ super(...arguments);+ this.timeLeft = this.calculateTimeLeft();+ this.startCountdown();+ }get impersonatedUserName() {return this.currentUser.username;}+ startCountdown() {+ this.countdown = setInterval(() => {+ this.timeLeft = this.calculateTimeLeft();+ if (this.timeLeft <= 0) {+ this.stopImpersonating();+ }+ }, 1000);+ }++ calculateTimeLeft() {+ return (+ moment(this.currentUser.impersonation_expires_at).diff(+ moment(),+ "minutes"+ ) + 1+ );+ }+@actionasync stopImpersonating() {try {@@ -24,6 +49,7 @@ export default class ImpersonationNotice extends Component {await ajax("/admin/impersonate", {type: "DELETE",});+ clearInterval(this.countdown);DiscourseURL.redirectTo("/");} catch (err) {popupAjaxError(err);@@ -33,10 +59,14 @@ export default class ImpersonationNotice extends Component {<template><div class="impersonation-notice">- <div>{{i18n- "impersonation.notice"- username=this.impersonatedUserName- }}</div>+ <div>+ <span>+ {{i18n "impersonation.notice" username=this.impersonatedUserName}}+ </span>+ <span>+ {{i18n "impersonation.time_left" count=this.timeLeft}}+ </span>+ </div><DButton@action={{this.stopImpersonating}}@isLoading={{this.stopping}}diff --git a/lib/impersonator_constraint.rb b/lib/impersonator_constraint.rbindex c862bb17eb9..2c69f3b11f2 100644--- a/lib/impersonator_constraint.rb+++ b/lib/impersonator_constraint.rb@@ -1,9 +1,19 @@# frozen_string_literal: true+# This constraint accounts for admins who are impersonating other users+# and are therefore currently not identified as admins in their session.class ImpersonatorConstraintdef matches?(request)current_user = CurrentUser.lookup_from_env(request.env)- !!current_user&.is_impersonating++ # An admin who's not impersonating anyone. Cool.+ return true if current_user&.admin?++ # An admin who's impersonating someone. Also cool.+ # We know this because only admins can impersonate others.+ return true if current_user&.is_impersonating++ falserescue Discourse::InvalidAccess, Discourse::ReadOnlyfalseenddiff --git a/spec/models/user_auth_token_spec.rb b/spec/models/user_auth_token_spec.rbindex 1102bccf76d..a538ff77d2c 100644--- a/spec/models/user_auth_token_spec.rb+++ b/spec/models/user_auth_token_spec.rb@@ -18,11 +18,14 @@ RSpec.describe UserAuthToken doexpect(token.user).to eq(user)expect(token.user.is_impersonating).to be_falsey+ expect(token.user.impersonation_expires_at).to be_nilendendcontext "when impersonating another user" doit "returns the user being impersonated if not expired" do+ freeze_time+token =UserAuthToken.generate!(user_id: admin.id,@@ -34,6 +37,7 @@ RSpec.describe UserAuthToken doexpect(token.user).to eq(user)expect(token.user.is_impersonating).to eq(true)+ expect(token.user.impersonation_expires_at).to eq_time(15.minutes.from_now)endit "returns the user associated with the session if expired" do@@ -48,6 +52,7 @@ RSpec.describe UserAuthToken doexpect(token.user).to eq(admin)expect(token.user.is_impersonating).to be_falsey+ expect(token.user.impersonation_expires_at).to be_nilendit "returns the user associated with the session if can no longer impersonate" do@@ -63,6 +68,7 @@ RSpec.describe UserAuthToken doexpect(token.user).to eq(admin)expect(token.user.is_impersonating).to be_falsey+ expect(token.user.impersonation_expires_at).to be_nilendendenddiff --git a/spec/requests/admin/impersonate_controller_spec.rb b/spec/requests/admin/impersonate_controller_spec.rbindex e3c94758893..265c4fc830f 100644--- a/spec/requests/admin/impersonate_controller_spec.rb+++ b/spec/requests/admin/impersonate_controller_spec.rb@@ -130,10 +130,10 @@ RSpec.describe Admin::ImpersonateController doexpect(session[:current_user_id]).to eq(admin.id)end- it "does not pass routing constraint when current user is not impersonating" do+ it "does nothing and returns success if not impersonating" dodelete "/admin/impersonate.json"- expect(response.status).to eq(404)+ expect(response.status).to eq(200)endit "stops impersonating" do