gpt4 book ai didi

ruby-on-rails - 设计:仅在某些情况下询问密码

转载 作者:行者123 更新时间:2023-12-04 15:35:09 24 4
gpt4 key购买 nike

我正在开发一个使用 Rails 和 Devise 进行用户身份验证的应用程序。我想知道是否有办法仅在某些更改时询问密码。

例如,我希望在以下情况下询问密码:

  • 删除帐号
  • 修改密码

  • 我希望用户可以在没有任何密码的情况下自由编辑其他字段,例如:
  • 姓名
  • 用户名
  • 头像

  • 所以,我想要一种在这两种方法之间交换的方法。我该如何解决这个问题?

    编辑:

    在 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删除帐户时还需要密码吗?

    最佳答案

    由于问题编辑:

    def destroy
    if current_user.valid_password?(params[:user][:password])
    current_user.destroy
    else
    render "destroy" # or what ever
    end
    end

    关于ruby-on-rails - 设计:仅在某些情况下询问密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15867177/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com