Skip to content

Commit 69c74f8

Browse files
Merge pull request #146 from aitbw/aitbw/admins-password-reset
Allow admins to reset an user's password through the admin panel
2 parents 178c0f9 + e79ff3e commit 69c74f8

6 files changed

Lines changed: 66 additions & 12 deletions

File tree

Gemfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,4 @@ group :development, :test do
2525
gem 'pry-rails', '~> 0.3.9'
2626
end
2727

28-
gem 'deface', '~> 1.3', require: false
29-
3028
gemspec
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

lib/solidus_auth_devise.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,4 @@
44
require "solidus_support"
55
require "spree/auth/devise"
66
require "spree/authentication_helpers"
7-
8-
if SolidusSupport.solidus_gem_version < Gem::Version.new('2.5.x')
9-
begin
10-
require "deface"
11-
rescue LoadError
12-
warn "deface is required to run solidus_auth_devise with solidus versions < 2.5. Please add deface to your Gemfile"
13-
raise
14-
end
15-
end
7+
require "deface"

solidus_auth_devise.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Gem::Specification.new do |s|
3333
s.add_dependency "devise-encryptable", "0.2.0"
3434
s.add_dependency "solidus_core", solidus_version
3535
s.add_dependency "solidus_support", ">= 0.1.3"
36+
s.add_dependency "deface", "~> 1.0"
3637

3738
s.add_development_dependency "capybara", "~> 2.14"
3839
s.add_development_dependency "capybara-screenshot"

spec/features/admin/password_reset_spec.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,47 @@
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(
72+
'If an account with that email address exists,
73+
you will receive an email with instructions about
74+
how to reset your password in a few minutes.'
75+
)
76+
end
77+
end
78+
end
79+
end
3780
end

0 commit comments

Comments
 (0)