users: allow site owner to reset passwords of other users.
This commit is contained in:
@@ -9,7 +9,7 @@ class PasswordsController < ApplicationController
|
|||||||
def update
|
def update
|
||||||
@user = authorize User.find(params[:user_id]), policy_class: PasswordPolicy
|
@user = authorize User.find(params[:user_id]), policy_class: PasswordPolicy
|
||||||
|
|
||||||
if @user.authenticate_password(params[:user][:old_password]) || @user.authenticate_login_key(params[:user][:signed_user_id])
|
if @user.authenticate_password(params[:user][:old_password]) || @user.authenticate_login_key(params[:user][:signed_user_id]) || CurrentUser.user.is_owner?
|
||||||
@user.update(password: params[:user][:password], password_confirmation: params[:user][:password_confirmation])
|
@user.update(password: params[:user][:password], password_confirmation: params[:user][:password_confirmation])
|
||||||
else
|
else
|
||||||
@user.errors.add(:base, "Incorrect password")
|
@user.errors.add(:base, "Incorrect password")
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
class PasswordPolicy < ApplicationPolicy
|
class PasswordPolicy < ApplicationPolicy
|
||||||
def update?
|
def update?
|
||||||
record.id == user.id || user.is_admin?
|
record.id == user.id || user.is_owner?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -31,6 +31,24 @@ class PasswordsControllerTest < ActionDispatch::IntegrationTest
|
|||||||
assert_equal(@user, @user.authenticate_password("abcde"))
|
assert_equal(@user, @user.authenticate_password("abcde"))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
should "allow the site owner to change the password of other users" do
|
||||||
|
@owner = create(:owner_user)
|
||||||
|
put_auth user_password_path(@user), @owner, params: { user: { password: "abcde", password_confirmation: "abcde" } }
|
||||||
|
|
||||||
|
assert_redirected_to @user
|
||||||
|
assert_equal(false, @user.reload.authenticate_password("12345"))
|
||||||
|
assert_equal(@user, @user.authenticate_password("abcde"))
|
||||||
|
end
|
||||||
|
|
||||||
|
should "not allow non-owners to change the password of other users" do
|
||||||
|
@admin = create(:admin_user)
|
||||||
|
put_auth user_password_path(@user), @admin, params: { user: { old_password: "12345", password: "abcde", password_confirmation: "abcde" } }
|
||||||
|
|
||||||
|
assert_response 403
|
||||||
|
assert_equal(@user, @user.reload.authenticate_password("12345"))
|
||||||
|
assert_equal(false, @user.authenticate_password("abcde"))
|
||||||
|
end
|
||||||
|
|
||||||
should "not update the password when given an invalid old password" do
|
should "not update the password when given an invalid old password" do
|
||||||
put_auth user_password_path(@user), @user, params: { user: { old_password: "3qoirjqe", password: "abcde", password_confirmation: "abcde" } }
|
put_auth user_password_path(@user), @user, params: { user: { old_password: "3qoirjqe", password: "abcde", password_confirmation: "abcde" } }
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user