I'm using Devise to handle authentication in a Rails app, and I'm using permanent_records to soft delete users. The default scope for my User model is the undeleted users. If a user deletes (deactivates) his account, I want him to be able to reactivate his account by logging in, similar to the way Facebook does it. Problem is, since Devise doesn't know to look for deleted users, no account is found. I thought about overriding the sessions#create method

 def create
    self.resource = warden.authenticate!(auth_options)
    set_flash_message(:notice, :signed_in) if is_navigational_format?
    sign_in(resource_name, resource)
    respond_with resource, :location => after_sign_in_path_for(resource)
  end

But since this is handled by Warden, it seems I'm out of luck. I'm afraid that if I start digging too deep I'm going to start breaking things.

Any ideas?

Thanks!

有帮助吗?

解决方案

You need:

  1. Overwrite find_for_authentication method in User model to allow finding for any users https://github.com/plataformatec/devise/blob/master/lib/devise/models/authenticatable.rb#L229

  2. Redefine after_database_authentication method in your model to remove deleted flag here https://github.com/plataformatec/devise/blob/master/lib/devise/models/database_authenticatable.rb#L98

That is all, I believe. No need to touch controller actions.

其他提示

this works with the paranoia gem:

class << self
  def find_for_authentication(conditions)
    User.unscoped do
      user = super(conditions)
      user.restore!(recursive: true) if user.deleted?
      user
    end
  end
end
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top