Skip to content

Commit 2d653a6

Browse files
authored
Merge pull request #1746 from codidact/0valt/dev-impersonate
Allow dev mode to impersonate users directly
2 parents 1f130d5 + 5d1c724 commit 2d653a6

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

app/controllers/admin_controller.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class AdminController < ApplicationController
44
before_action :verify_global_admin, only: [:admin_email, :send_admin_email, :new_site, :create_site, :setup,
55
:setup_save, :hellban, :all_email, :send_all_email]
66
before_action :verify_developer, only: [:change_users, :impersonate]
7+
before_action :set_user, only: [:change_users, :hellban, :impersonate]
78

89
def index; end
910

@@ -182,20 +183,19 @@ def setup_save
182183
end
183184

184185
def hellban
185-
@user = User.find params[:id]
186186
@user.block("user manually blocked by admin ##{current_user.id}")
187187
flash[:success] = t 'admin.user_fed_stat'
188188
redirect_back fallback_location: admin_path
189189
end
190190

191191
def impersonate
192-
@user = User.find params[:id]
192+
if Rails.env.development?
193+
change_users
194+
end
193195
end
194196

195197
def change_users
196-
@user = User.find params[:id]
197-
198-
unless params[:comment].present?
198+
unless params[:comment].present? || Rails.env.development?
199199
flash[:danger] = 'Please explain why you are impersonating this user.'
200200
render :impersonate
201201
return
@@ -246,4 +246,10 @@ def do_email_query
246246
end
247247
render :email_query
248248
end
249+
250+
private
251+
252+
def set_user
253+
@user = User.find(params[:id])
254+
end
249255
end

app/views/admin/impersonate.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@
2828
<%= submit_tag 'Impersonate', class: 'button is-danger h-m-b-2' %>
2929
</div>
3030
</div>
31-
<% end %>
31+
<% end %>

test/controllers/admin_controller_test.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,35 @@ class AdminControllerTest < ActionController::TestCase
165165
assert_response(:success)
166166
assert_not_nil assigns(:logs)
167167
end
168+
169+
test 'hellban should correctly block the user' do
170+
sign_in users(:global_admin)
171+
172+
user = users(:standard_user)
173+
try_hellban_user(user)
174+
user.reload
175+
176+
assert_response(:found)
177+
assert BlockedItem.where(item_type: 'email', value: user.email).any?
178+
end
179+
180+
test 'impersonate should bypass reason in dev env' do
181+
sign_in users(:developer)
182+
183+
Rails.env.stub(:development?, true) do
184+
try_impersonate_user(users(:standard_user))
185+
assert_response(:found)
186+
assert_redirected_to root_path
187+
end
188+
end
189+
190+
private
191+
192+
def try_hellban_user(user)
193+
post :hellban, params: { id: user.id }
194+
end
195+
196+
def try_impersonate_user(user)
197+
get :impersonate, params: { id: user.id }
198+
end
168199
end

test/fixtures/community_users.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ sample_global_admin:
6161
is_moderator: false
6262
reputation: 1
6363

64+
sample_developer:
65+
user: developer
66+
community: sample
67+
is_admin: true
68+
is_moderator: false
69+
reputation: 1
70+
6471
sample_staff:
6572
user: staff
6673
community: sample

0 commit comments

Comments
 (0)