Skip to content

Commit

Permalink
Allow admins to reset an user's password through the admin panel
Browse files Browse the repository at this point in the history
  • Loading branch information
aitbw committed Apr 9, 2019
1 parent cda788b commit 1efba33
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!--
insert_before "fieldset#admin_user_edit_api_key"
original "904c52ff702412d1dc8d55ff44d87d7f581f6675"
-->

<% if @user != try_spree_current_user %>
<fieldset class="no-border-bottom" data-hook="admin_user_reset_password">
<legend><%= I18n.t(:'spree.forgot_password') %></legend>

<%= form_for [:admin, @user], as: :spree_user, url: admin_reset_password_path, method: :post do |f| %>
<%= f.hidden_field :email, value: @user.email %>

<% if can?(:update, @user) %>
<div class="align-center">
<%= f.submit Spree.user_class.human_attribute_name(:reset_password), class: "button primary" %>
</div>
<% end %>
<% end %>
</fieldset>
<% end %>
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def create

if resource.errors.empty?
set_flash_message(:notice, :send_instructions) if is_navigational_format?
respond_with resource, location: spree.admin_login_path
respond_with resource, location: admin_user_path(resource)
else
respond_with_navigational(resource) { render :new }
end
Expand Down
39 changes: 39 additions & 0 deletions spec/features/admin/password_reset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,43 @@
click_button 'Reset my password'
expect(page).to have_text "Email can't be blank"
end

context 'password management' do
let!(:admin) do
create(:admin_user,
email: '[email protected]',
password: 'secret',
password_confirmation: 'secret'
)
end

let!(:user) do
create(:user,
email: '[email protected]',
password: 'test123',
password_confirmation: 'test123'
)
end

before do
visit spree.admin_login_path
fill_in 'Email', with: admin.email
fill_in 'Password', with: admin.password
click_button 'Login'
visit spree.admin_users_path
end

context 'if currently logged-in admin' do
context "clicks on an user's page" do
it 'can reset its password' do
within("#spree_user_#{user.id}") do
click_link user.email
end

click_button 'Reset password'
expect(page).to have_content 'You will receive an email with instructions on how to reset your password in a few minutes.'
end
end
end
end
end

0 comments on commit 1efba33

Please sign in to comment.