active_for_authentication? not working

1.6k Views Asked by At

I am using active_for_authentication? to allow only active users to login through devise. But it's not working.

My controller:

class DashboardUsersController < ApplicationController
def active_for_authentication?
  super && self.deactivated_ind
end

def inactive_message
  "Sorry, this account has been deactivated."
end

My model:

class DashboardUser < ActiveRecord::Base

  devise :database_authenticatable,:rememberable, :trackable, :validatable, :timeoutable

  attr_accessor :login

  def self.find_first_by_auth_conditions(warden_conditions)
    conditions = warden_conditions.dup
    if login = conditions.delete(:login)
      where(conditions).where(["username = :value OR lower(email) = lower(:value)", { :value => login }]).first
    else
      where(conditions).first
    end
  end
end

My user table:

t.string   "deactivated_ind",      limit: 1
t.date     "deactivated_date"

By using deactivated_ind Y / N i check user is active / inactive. Now i want allow devise to log in only those user have N in deactivated_ind colomn

2

There are 2 best solutions below

0
On

Part of the problem, I'm guessing is that you're using a string for "deactivated_ind" instead of a boolean. A string of "N" and a string of "Y" will both be true when you try to use them as boolean values, since neither is nil.

1
On
  1. active_for_authentication? needs to be a method on a model, not on a controller

  2. (not a problem in your case, but was mine) active_for_authentication? must be a public method on a model