3

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

Raj
  • 950
  • 1
  • 9
  • 33

2 Answers2

4
  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

Mladen Jablanović
  • 43,461
  • 10
  • 90
  • 113
  • 2
    I got tripped up since I usually make all Devise callback method overrides I write `private`, but `active_for_authentication` (and it's companion `inactive_message` method), would seem to _need_ to be `public`. – Paul Fioravanti May 24 '16 at 00:08
2

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.

Bryce
  • 2,802
  • 1
  • 21
  • 46