Skip to content

Commit d888908

Browse files
authored
Merge pull request #141 from mgriffin/pretend
Use the pretender gem to impersonate users
2 parents 4ea452d + e1167ee commit d888908

File tree

11 files changed

+104
-5
lines changed

11 files changed

+104
-5
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ gem "image_processing", "~> 1.2"
5151

5252
### For authentication
5353
gem "devise"
54+
gem "pretender"
5455
### For pagination
5556
gem "pagy"
5657
gem "typhoeus"

Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ GEM
303303
mime-types (>= 3.0)
304304
pp (0.6.3)
305305
prettyprint
306+
pretender (0.6.0)
307+
actionpack (>= 7.1)
306308
prettyprint (0.2.0)
307309
prism (1.6.0)
308310
psych (5.2.6)
@@ -529,6 +531,7 @@ DEPENDENCIES
529531
lograge
530532
mocha
531533
pagy
534+
pretender
532535
puma (>= 5.0)
533536
pundit (~> 2.5)
534537
rails (~> 8.0)

app/assets/images/circle-slash.svg

Lines changed: 1 addition & 0 deletions
Loading

app/assets/stylesheets/sporty.css

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ nav.admin-toolbar {
157157
color: hsl(0, 0%, 100%, 0.7);
158158
padding: 5px 10px;
159159
justify-content: normal;
160+
margin: 0 -12px;
160161

161162
svg {
162163
padding-right: 3px;
@@ -168,6 +169,24 @@ nav.admin-toolbar {
168169
padding: 0 10px;
169170
}
170171
}
172+
div.impersonate-bar {
173+
background-color: var(--bs-danger-bg-subtle);
174+
color: var(--bs-danger-text-emphasis);
175+
border-bottom: 1px solid var(--bs-danger-border-subtle);
176+
padding: 5px 10px;
177+
justify-content: normal;
178+
margin: 0 -12px;
179+
180+
svg {
181+
padding-right: 3px;
182+
fill: var(--bs-danger-text-emphasis);
183+
}
184+
185+
a {
186+
color: var(--bs-danger-text-emphasis);
187+
padding: 0 10px;
188+
}
189+
}
171190

172191
.card-title {
173192
a {
@@ -182,3 +201,44 @@ nav.admin-toolbar {
182201
margin-bottom: 1.25rem;
183202
color: #727272;
184203
}
204+
@keyframes appear-then-fade {
205+
0%, 100% {
206+
opacity:0
207+
}
208+
5%, 60% {
209+
opacity:1
210+
}
211+
}
212+
.flash {
213+
position:fixed;
214+
top: 11rem;
215+
left: 50%;
216+
transform: translateX(-50%);
217+
218+
display: flex;
219+
flex-direction: column;
220+
align-items: center;
221+
gap: var(--space-s);
222+
223+
max-width: 100%;
224+
width: max-content;
225+
padding: 0 var(--space-m);
226+
}
227+
.flash__message {
228+
font-size: var(--font-size-s);
229+
color: var(--bs-primary-text-emphasis);
230+
padding: var(--space-xs) var(--space-m);
231+
background-color: var(--bs-primary-bg-subtle);
232+
animation: appear-then-fade 4s both;
233+
border-radius: 5px;
234+
}
235+
.flash__alert {
236+
color: var(--bs-danger-text-emphasis);
237+
background-color: var(--bs-danger-bg-subtle);
238+
border: 1px solid var(--bs-danger-border-subtle);
239+
}
240+
.flash__notice {
241+
color: var(--bs-success-text-emphasis);
242+
background-color: var(--bs-success-bg-subtle);
243+
border: 1px solid var(--bs-success-border-subtle);
244+
}

app/controllers/admin/users_controller.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Admin
44
class UsersController < ApplicationController
55
include Pagy::Backend
66

7-
before_action :admin_only!
7+
before_action :admin_only!, except: :stop_impersonating
88

99
def index
1010
@pagy, @users = pagy(User.order(created_at: :desc))
@@ -24,6 +24,17 @@ def update
2424
end
2525
end
2626

27+
def impersonate
28+
user = User.find(params[:id])
29+
impersonate_user(user)
30+
redirect_to root_path
31+
end
32+
33+
def stop_impersonating
34+
stop_impersonating_user
35+
redirect_to root_path
36+
end
37+
2738
private
2839

2940
def user_params

app/controllers/application_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
class ApplicationController < ActionController::Base
44
include Pundit::Authorization
55

6+
impersonates :user
7+
68
before_action :configure_permitted_parameters, if: :devise_controller?
79

810
layout "sporty"

app/views/admin/users/index.html.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<th>Username</th>
66
<th>Name</th>
77
<th>Admin?</th>
8+
<th></th>
89
</tr>
910
<%- @users.each do |user| %>
1011
<tr>
@@ -21,6 +22,9 @@
2122
<%- end %>
2223
<%- end %>
2324
</td>
25+
<td>
26+
<%= button_to "Impersonate", impersonate_admin_user_path(user), data: {turbo: false}, class: "btn btn-link" %>
27+
</td>
2428
</tr>
2529
<%- end %>
2630
</table>

app/views/layouts/sporty.html.erb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
<body>
1717
<div class="container">
1818
<%= render AdminToolbarComponent.new(user: current_user) %>
19+
<% if current_user != true_user %>
20+
<div class="impersonate-bar" aria-label="Impersonation toolbar">
21+
<%= inline_svg_tag "circle-slash.svg" %>
22+
<%= link_to "Stop impersonating #{current_user.name}", stop_impersonating_admin_users_path, data: {turbo_method: :post} %>
23+
</div>
24+
<% end %>
1925
<header class="row">
2026
<%= link_to root_path, class: "logo col" do %>
2127
<%= inline_svg_tag "shield.svg" %>
@@ -39,6 +45,7 @@
3945
</div>
4046
</nav>
4147
<section class="main row py-3">
48+
<%= render FlashComponent.new(flash: flash) %>
4249
<%= yield %>
4350
</section>
4451
<footer id="site-footer" class="row">

app/views/layouts/thredded.html.erb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
<body>
1919
<div class="container">
2020
<%= render AdminToolbarComponent.new(user: current_user) %>
21+
<% if current_user != true_user %>
22+
<div class="impersonate-bar" aria-label="Impersonation toolbar">
23+
<%= inline_svg_tag "circle-slash.svg" %>
24+
<%= link_to "Stop impersonating #{current_user.name}", main_app.stop_impersonating_admin_users_path, data: {turbo_method: :post} %>
25+
</div>
26+
<% end %>
2127
<header class="row">
2228
<%= link_to main_app.root_path, class: "logo col" do %>
2329
<%= inline_svg_tag "shield.svg" %>

config/locales/en.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ en:
4444
sessions:
4545
create:
4646
alert: "Username or password was incorrect"
47-
comments:
48-
create:
49-
notice: "Your comment was successfully posted"
47+
posts:
48+
comments:
49+
create:
50+
notice: "Your comment was successfully posted"
5051
person_relationships:
5152
create:
5253
already_done: "already linked"

0 commit comments

Comments
 (0)