21474

Devise before_authenticate?

Question:

I use Devise for authentication and I have an admin role who can manage users.

Apart from this the admin can also lock a user preventing him from logging in the future. I have created a Boolean field inside 'User' model called 'is_locked?'. When the admin locks a user this boolean field is set to true. Based on this info I can know if a user is locked or not.

Now when user tries to log in, before setting up his session, I have to check this logic. I'm clueless about where to add this logic. Or this any custom methods or events which devise provides, so that I can add it there.

Answer1:

I've found that you can stack before_filters in the controller, so if you wanted to check for an authenticate_user!, you could also use a before_filter (after authenticate_user!) to check for a locked user. If the user model has a boolean attribute locked, you can simply write a private method in your controller (or helper) like this:

#top of controller before_filter authenticate_user! before_filter user_active! #bottom of controller private def user_active! unless current_user.locked? return true end redirect_to root_url, :notice => "Your account is locked." return false end

This will give you the page you want if you're an unlocked user, and redirect you to the root page with an error message if the user is locked.

Answer2:

You can add an active? method on the User model:

def active? super && !self.is_locked? end

Recommend

  • How to make a shared resource thread-safe when using dependency injection?
  • Playing a monetized YouTube song inside of a Google Chrome Extension. Do I have any options?
  • iPhone - UIWebView multiple locks
  • How concerned should we be about thread safety with JSF managed beans?
  • mysql table locked after php crashes
  • How can I stop my python script when another python script is running?
  • New Firebase failed: First argument must be a valid firebase URL and the path can't contain “.”
  • JSON encode and decode on PHP
  • Building Qt project for C++11 standard
  • Basic many-to-many left join query
  • How can I extract results of aggregate queries in slick?
  • OOP Javascript - Is “get property” method necessary?
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • Use of this Javascript
  • C++ Partial template specialization - design simplification
  • How to install a .deb file on a jailbroken iphone programmatically?
  • How to rebase a series of branches?
  • PHP - How to update data to MySQL when click a radio button
  • swift auto completion not working in Xcode6-Beta
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Upload files with Ajax and Jquery
  • How to get next/previous record number?
  • Cannot Parse HTML Data Using Android / JSOUP
  • Python: how to group similar lists together in a list of lists?
  • AngularJs get employee from factory
  • Proper way to use connect-multiparty with express.js?
  • C# - Getting references of reference
  • How to set the response of a form post action to a iframe source?
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • Linking SubReports Without LinkChild/LinkMaster
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • reshape alternating columns in less time and using less memory
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?