Devise Login with User or Admin Models and Basecamp Style Subdomains

Devise login with user or admin models and Basecamp style subdomains

Try writing your own before filter along the lines of

#application_controller.rb
def authenticate_any!
if admin_signed_in?
true
else
authenticate_user!
end
end

then in the controller where you want both admins and user to be able to have access through authentication use

#myobject_controller.rb
before_filter :authenticate_any!

If you have logged in as an admin then you will pass the before_filter, otherwise you will go through authenticate_user! which is the default behaviour.

Devise with multiple models & multiple login forms

You don't need to have three separate models to build this functionality. What you want to look at is the concept of Roles which are applied to one User model.

There is a Gem which provides this capability called Rolify and can be found at https://github.com/EppO/rolify

This would allow you to specify which users are in which Roles and change them as you see fit, all from one existing model.

Once you have Roles attached to the User model, you can override Devise's registration controllers to detect the Role and render different templates etc. You would do this by:

  1. Running rails generate devise:views to unpack the views from the Devise gem into your project
  2. Create your own Registrations controller:

    # app/controllers/registrations_controller.rb
    class RegistrationsController < Devise::RegistrationsController
    def new
    super
    # Add logic here to detect Role and display different forms
    end

    def create
    super
    end

    def update
    super
    end
    end
  3. Add the correct settings in your routes.rb file to tell Devise to use your new controller:

    # app/config/routes.rb
    devise_for :users, :controllers => {:registrations => "registrations"}

Authenticate two different devise classes in same controller in Rails

Try this:

class IntranetController < ApplicationController
before_action :authenticate_all!
def index
end
def authenticate_all!
if admin_signed_in?
true
else
authenticate_client!
end
end
end

Allowing multiple models to access a controller

Devise offers you helper methods user_signed_in? and admin_signed_in? for the User model and Admin model, respectively.

You can write custom filter in ApplicationController, and apply the filter in the concrete controller on demand.

class ApplicationController < ActionController::Base
...

class AuthorizationException < StandardError
end

rescue_from AuthorizationException do
render text: "Access Denied", status: :unauthorized
end

protected
def authenticate_user_or_admin!
unless user_signed_in? or admin_signed_in?
raise AuthorizationException.new
end
end
end

You can use the filter authenticate_user_or_admin! in your controller now.

Rails 3.2 & Devise: custom authenticate_user! that authenticates Users and Admins

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

authentication of user and admin separately

I believe the way to do this is to override the devise before and after_sign_in_path_for helpers

I think (from what I've read), you can use logic to determine the best way to handle this:

def after_sign_in_path_for(resource)
stored_location_for(resource) ||
if resource.is_a?(Admin)
admin_dashboard_path
else
user_path(resource)
end
end

Devise: Restricting Actions to Administrators

you can easily implement your own before_filter to allow access to only admin users by using the .admin? method associated with your user model. for instance:

before_filter :verify_is_admin

private

def verify_is_admin
(current_user.nil?) ? redirect_to(root_path) : (redirect_to(root_path) unless current_user.admin?)
end

disable devise user registration and move that functionality to an admin method?

Check out the devise Wiki, quite a common question (think this is what you are looking for)

https://github.com/plataformatec/devise/wiki/How-To%3a-Require-admin-to-activate-account-before-sign_in



Related Topics



Leave a reply



Submit