Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi Tenant, Multi Admin Users in ActiveAdmin

I'm building a multi-tenant app with ActiveAdmin as the main admin interface. I've used the acts_as_tenant gem to accomplish data separate nicely.

I've used the AdminUser model as the user model object for all users.

In order to add other users, the AdminUser is scoped to the tenant as well.

This is throwing off the login, because when ActiveAdmin/Devise tries to authenticate, I assume it is first hitting the find_tenant filter as shown below:

class ApplicationController
  set_current_tenant_through_filter

  before_filter :find_tenant

  def find_tenant
     if admin_user_signed_in?
      set_current_tenant(Company.find(current_admin_user.company_id))
     end
  end

Not sure how to get around this... I want the user to login and then the application takes the company_id from the logged in user and sets the tenant and all data shown on ActiveAdmin is scoped via that tenant (this part works well through the acts_as_tenant gem if I can get past the login).

Thanks

like image 641
RailsTweeter Avatar asked Jul 11 '12 19:07

RailsTweeter


2 Answers

I think that your suspicions are correct, and that the find_tenant method is being called before the authentication, resulting in admin_user_signed_in? to be false. Adjusting it to use an after filter, instead, should do the trick, if this is indeed the case ( from http://guides.rubyonrails.org/action_controller_overview.html#after-filters-and-around-filters ).

class ApplicationController
  set_current_tenant_through_filter

  after_filter :find_tenant

  def find_tenant
    if admin_user_signed_in?
      set_current_tenant(Company.find(current_admin_user.company_id))
    end
  end

Not sure how set_current_tenant_through_filter works into all of this, are you trying to do the same thing two different ways?

like image 99
Brad Werth Avatar answered Nov 09 '22 06:11

Brad Werth


8 years old post but awyway... I set mine up routes.rb like this:

  devise_for :admin_users, ActiveAdmin::Devise.config
  require 'sidekiq/web'
  Sidekiq::Web.set :sessions, false
  authenticate :admin_user do
    mount Sidekiq::Web => '/admin/sidekiq'
  end

In my case my non-admin users have the company_id and I think you want this:

  def find_tenant
    if user_signed_in?
      set_current_tenant(Company.find(current_admin_user.company_id))
    end
  end

In the AA space I did this:

Set Multi Tenant in Active Admin Controllers (required for Searchkick index)

I only set the tenant when updating records in AA. I only do this because of Searchkick index requirements. In your case you may only need to permit the company_id paramater.

like image 36
Dan Tappin Avatar answered Nov 09 '22 06:11

Dan Tappin