Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise: ask password only for certain situations

I'm developing an app using Rails and Devise for user's authentication. I wonder if there is a way to ask the password only for some changes.

For instance, I want the password to be asked when:

  • Deleting the account
  • Changing the password

And I want the user to be free to edit other fields without any password, such as:

  • Name
  • Username
  • Avatar

So, I'd like a way to swap between this two methods. How can I solve this?

EDIT:

In Devise's documentation I found this and it works fine, it only allows changing password and email if I enter the password:

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

Now, what could i put in #HERE to also require the password when I'm deleting the account?

like image 474
Fran Barros Avatar asked Apr 07 '13 20:04

Fran Barros


1 Answers

due to question edit:

def destroy
  if current_user.valid_password?(params[:user][:password])
    current_user.destroy
  else 
    render "destroy" # or what ever
  end
end
like image 169
ted Avatar answered Oct 16 '22 08:10

ted