ユーザーの認証に Rails と Devise を使用してアプリを開発しています。一部の変更のみパスワードを尋ねる方法があるのだろうか。
たとえば、次の場合にパスワードを尋ねられるようにします。
- アカウントの削除
- パスワードの変更
また、ユーザーがパスワードなしで他のフィールドを自由に編集できるようにしたい:
- 名前
- ユーザー名
- アバター
したがって、この 2 つの方法を切り替える方法が必要です。どうすればこれを解決できますか?
編集:
Devise のドキュメントでこれを見つけましたが、問題なく動作します。パスワードを入力した場合にのみ、パスワードと電子メールを変更できます。
def update
@user = User.find(current_user.id)
successfully_updated = if needs_password?(@user, params)
@user.update_with_password(params[:user])
else
# remove the virtual current_password attribute update_without_password
# doesn't know how to ignore it
params[:user].delete(:current_password)
@user.update_without_password(params[:user])
end
if successfully_updated
set_flash_message :notice, :updated
# Sign in the user bypassing validation in case his password changed
sign_in @user, :bypass => true
redirect_to after_update_path_for(@user)
else
render "edit"
end
end
private
# check if we need password to update user data
# ie if password or email was changed
# extend this as needed
def needs_password?(user, params)
user.email != params[:user][:email] ||
!params[:user][:password].blank?
#HERE
end
#HERE
では、アカウントを削除するときにパスワードも要求するには、何を入力すればよいでしょうか?