Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9107555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:36:02+00:00 2026-06-17T02:36:02+00:00

I am using devise with two separate models for User and Admin. I wanted

  • 0

I am using devise with two separate models for User and Admin. I wanted to replace authenticate_user! with my own function, auth_user! such that an Admin’s permissions is a superset of the User’s permissions. I also wrote a function, actions_permitted, that makes it easier to call skip_before_filter. My added code in ApplicationController.rb is below. For example, I use it in a controller by writing: actions_permitted :public => [:show], user: [:new, :create].

However, the code is not functioning as expected: some actions aren’t properly authenticated, and others require an admin to also sign in as a user when an admin should always have user abilities. After some googling, I suspect that the issue may be that when the inherited models call actions_permitted, it happens on the ApplicationController level rather than in a particular model. I also found that many on Stackoverflow recommended CanCan, although I’d prefer to stick with the simple syntax of actions_permitted if you can help me get it working!

# app/controllers/application_controller.rb
#
# call with :user and :public defined as either :all or an array
# of symbols that represent methods. Admins can do everything that users
# can (by definition of auth_user!).
def self.actions_permitted(hash)
  # first process exceptions to user authentication
  if hash[:public] == :all
    # skip all filters and return
    skip_before_filter :auth_user!
    skip_before_filter :authenticate_admin!
    return
  elsif hash[:public].kind_of?(Array)
    # skip user authentication for methods in :public array
    skip_before_filter :auth_user!, only: hash[:public]
  end

  # then process exceptions to admin authentication
  if hash[:user] == :all
    # users can do everything, so skip all admin authenticatoin
    skip_before_filter :authenticate_admin!

  elsif hash[:user].kind_of?(Array)
    if hash[:public].kind_of?(Array)
      # Join the two arrays and skip admin authentication as not to filter
      # actions allowed by the public or by users
      skip_before_filter :authenticate_admin!, only: (hash[:user] | hash[:public])
    else
      # otherwise, simply skip admin authentication for actions allowed by users
      skip_before_filter :authenticate_admin!, only: hash[:user]
    end

  elsif hash[:public].kind_of?(Array)
    # skip admin authentication for actions allowed by the public
    skip_before_filter :authenticate_admin!, only: hash[:public]
  end

end

# checks if user OR admin is authenticated.
def auth_user!(opts = {})
  # return (authenticate_user! || authenticate_admin!)
  return (env['warden'].authenticated?(:user) ||
          env['warden'].authenticated?(:admin))
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T02:36:04+00:00Added an answer on June 17, 2026 at 2:36 am

    Turns out the problem was in auth_user!. For anyone that wants to use this code in the future, here is the correction:

    def auth_user!(opts = {})
      if admin_signed_in?
        authenticate_admin!
      else
        authenticate_user!
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My Rails 3.1 app has two user models: User and Admin. I'm using Devise.
I am using Devise in a rail app. I have two models : User
In a Rails 3.2 app I have two user models set up using Devise:
I'm using Devise for authentication, but I'm using two user models, Buyer and Seller.
I am using rails3 and gem devise and i have two roles admin and
So I am using Devise for user authentication in my rails app. I have
I'm using Devise and Active Admin, and so far there are playing nice together.
I'm trying to make a two-step confirmation like heroku using Devise. My routes: devise_for
I am using devise in my rails application and I have two different resource
I am trying to set-up a two step signup process using Devise in Rails

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.