views:

93

answers:

0

Hi,

I have some models such as:

class User < ActiveRecord::Base
  acts_as_authentic
end

class Admin < User
end

class Superadmin < Admin
end

And some controllers such as:

class UserSessionsController < ApplicationController
  def new
    @user_session = UserSession.new
  end

  def create
    @user_session = UserSession.new(params[:user_session])
    if @user_session.save
      flash[:notice] = t('user_sessions.flash.create.notice')
      redirect_to root_path
    else
      render :action => :new
    end
  end

  def destroy
    current_user_session.destroy
    flash[:notice] = t('user_sessions.flash.destroy.notice')
    redirect_to new_session_path
  end
end

class Admin::AdminSessionsController < ApplicationController
  layout 'admin'

  def new
    @user_session = UserSession.new
  end

  def create
    @user_session = UserSession.new(params[:user_session])
    if @user_session.save
      flash[:notice] = t('user_sessions.flash.create.notice')
      redirect_to admin_dashboard_path
    else
      render :action => :new
    end
  end

  def destroy
    current_user_session.destroy
    flash[:notice] = t('user_sessions.flash.destroy.notice')
    redirect_to new_admin_session_path
  end
end

class Superadmin::SuperadminSessionsController < ApplicationController
  layout 'superadmin'

  def new
    @user_session = UserSession.new
  end

  def create
    @user_session = UserSession.new(params[:user_session])
    if @user_session.save
      flash[:notice] = t('user_sessions.flash.create.notice')
      redirect_to superadmin_dashboard_path
    else
      render :action => :new
    end
  end

  def destroy
    current_user_session.destroy
    flash[:notice] = t('user_sessions.flash.destroy.notice')
    redirect_to new_superadmin_session_path
  end
end

I would like to allow users to login, but only with the good controller and model. For instance, if an admin want to log in, its controller should be Admin::AdminSessionsController (handled Admin model).

But I don't want that, a user connect itself using another interface like superadmin (and the opposite).

I suppose that I need to add a condition in each controller just before @user_session.save. Maybe using @user_session.save... But I don't see exactly how.

I just know that, it's should be possible to know the type of current_user thanks to: current_user.type (as documented at http://api.rubyonrails.org/classes/ActiveRecord/Base.html)

According to you, how can we do so?

Many thanks.