Skip to content

Commit 300bffc

Browse files
committed
Allow admins to reset an user's password through the admin panel
1 parent e3b6daf commit 300bffc

3 files changed

Lines changed: 60 additions & 1 deletion

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!--
2+
insert_before "fieldset#admin_user_edit_api_key"
3+
original "904c52ff702412d1dc8d55ff44d87d7f581f6675"
4+
-->
5+
6+
<% if @user != try_spree_current_user %>
7+
<fieldset class="no-border-bottom" data-hook="admin_user_reset_password">
8+
<legend><%= t(:'spree.forgot_password') %></legend>
9+
10+
<%= form_for [:admin, @user], as: :spree_user, url: admin_reset_password_path, method: :post do |f| %>
11+
<%= f.hidden_field :email, value: @user.email %>
12+
13+
<% if can?(:update, @user) %>
14+
<div class="align-center">
15+
<%= f.submit Spree.user_class.human_attribute_name(:reset_password), class: "button primary" %>
16+
</div>
17+
<% end %>
18+
<% end %>
19+
</fieldset>
20+
<% end %>

lib/controllers/backend/spree/admin/user_passwords_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def create
2525
set_flash_message(:notice, :send_instructions) if is_navigational_format?
2626

2727
if resource.errors.empty?
28-
respond_with resource, location: spree.admin_login_path
28+
respond_with resource, location: admin_user_path(resource)
2929
else
3030
respond_with_navigational(resource) { render :new }
3131
end

spec/features/admin/password_reset_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,43 @@
3434
def fill_in_email
3535
fill_in 'Email', with: 'foobar@example.com'
3636
end
37+
38+
context 'password management' do
39+
let!(:admin) do
40+
create(:admin_user,
41+
email: 'admin@example.com',
42+
password: 'secret',
43+
password_confirmation: 'secret'
44+
)
45+
end
46+
47+
let!(:user) do
48+
create(:user,
49+
email: 'user@example.com',
50+
password: 'test123',
51+
password_confirmation: 'test123'
52+
)
53+
end
54+
55+
before do
56+
visit spree.admin_login_path
57+
fill_in 'Email', with: admin.email
58+
fill_in 'Password', with: admin.password
59+
click_button 'Login'
60+
visit spree.admin_users_path
61+
end
62+
63+
context 'if currently logged-in admin' do
64+
context "clicks on an user's page" do
65+
it 'can reset its password' do
66+
within("#spree_user_#{user.id}") do
67+
click_link user.email
68+
end
69+
70+
click_button 'Reset password'
71+
expect(page).to have_content 'You will receive an email with instructions on how to reset your password in a few minutes.'
72+
end
73+
end
74+
end
75+
end
3776
end

0 commit comments

Comments
 (0)