Perform a check and then call super class when user creates new session

Hey all,

I’m using devise plugin for rails. When the user creates a new session,
in addition to devise authenticating the user, I want to check if the
user is enabled by checking the value of the enabled (a boolean) field
in the database. If the value is 0, I want to alert user that they
haven’t been enabled, otherwise devise can do it’s thing.

I have this:

class SessionsController < Devise::RegistrationsController

def new

def create
@user = User.authenticate(params[:user][:email])
if !@user.enabled
raise “You are not enabled”

def update

I have this in User model:

class User < ActiveRecord::Base
devise :invitable, :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

def self.authenticate(email)
u = find(:first, :conditions=>[“email = ?”, email])
return nil if u.nil?
return u

scope :enabled, :conditions => {:enabled => true}

And the routes:

devise_for :users, :controllers => {
:registrations => “registrations”,
:sessions => “sessions” }

Unfortunately right now I get a nil object error for instance variable
@user, presumably because the params hash is not structured correctly. I
look at html form and notice the name attribute for email says
users[email] which is an array. So that’s why I try to do it:
[:user][:email] but it doesn’t work. Or if you know a better way of
doing all this, I’d greatly appreciate the knowledge.

Thanks for response.

While I almost got the behavior I want, there’s a problem:

def create

@user = User.authenticate(params[:user])
if !@user.enabled
render “shared_navigation/confirmation”
elsif #just using else instead of elsif
gives same unwanted behavior

When I click the create button and if the user is enabled, it reloads
the same page and I have to click create button twice. This occurs when
that super keyword is called which calls the super class. Anyone know
why devise reload the same page?

Thanks for response.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs