Multitenant Auth with Spree2.3 on Rails 4.1.4 and Apartment

151 views
Skip to first unread message

Jonathon Batson

unread,
Jul 16, 2014, 6:34:26 AM7/16/14
to spree...@googlegroups.com
Hey spree'ers ...

I am new to this wonderful project, big kudos to all involved, it rocks.
Currently I am working on integrating spree with an existing multitenant app using devise and apartment
gem 'apartment'
gem 'devise', '3.2.4'
gem 'spree', github: 'spree/spree', branch: '2-3-stable'

Spree integration into my multitenant app seemed to go smoothly until I could not login to admin interface under a subdomain.

After hours of hitting walls I finally realized that apartment has this restriction on has_and_belongs_to_many relationships where
you cannot exclude the join table from a tenants schema, for example, 'spree_roles_users', and hence in a subdomain the user roles come from the tenants
schema, which is empty, ie not an admin under this subdomain, (or any subdomain)

I have logged this as a potential issue with apartment gem on git hub, but would like to know if anyone else has hit this or has ideas about getting
around it.  Spree has lots of habtm relationship so it would be great to find a solution.

Thanks

./spree/option_value.rb: has_and_belongs_to_many :variants, join_table: 'spree_option_values_variants', class_name: "Spree::Variant"
./spree/prototype.rb: has_and_belongs_to_many :properties, join_table: :spree_properties_prototypes
./spree/prototype.rb: has_and_belongs_to_many :option_types, join_table: :spree_option_types_prototypes
./spree/zone.rb: has_and_belongs_to_many :shipping_methods, :join_table => 'spree_shipping_methods_zones'
./spree/product.rb: has_and_belongs_to_many :promotion_rules, join_table: :spree_products_promotion_rules
./spree/role.rb: has_and_belongs_to_many :users, join_table: 'spree_roles_users', class_name: Spree.user_class.to_s
./spree/promotion.rb: has_and_belongs_to_many :orders, join_table: 'spree_orders_promotions'
./spree/shipping_method.rb: has_and_belongs_to_many :zones, :join_table => 'spree_shipping_methods_zones',
./spree/variant.rb: has_and_belongs_to_many :option_values, join_table: :spree_option_values_variants
./spree/order.rb: has_and_belongs_to_many :promotions, join_table: 'spree_orders_promotions'
./spree/option_type.rb: has_and_belongs_to_many :prototypes, join_table: 'spree_option_types_prototypes'
./spree/property.rb: has_and_belongs_to_many :prototypes, join_table: 'spree_properties_prototypes'
./spree/promotion/rules/product.rb: has_and_belongs_to_many :products, class_name: '::Spree::Product', join_table: 'spree_products_promotion_rules', foreign_key: 'promotion_rule_id'
./spree/promotion/rules/user.rb: has_and_belongs_to_many :users, class_name: "::#{Spree.user_class.to_s}"


Jonathon Batson

unread,
Jul 23, 2014, 2:19:00 AM7/23/14
to spree...@googlegroups.com
So if anyone hits the same issue, I could not solve it and proceeded to remove the Apartment gem altogether and implement my own
solution for multi-tenancy based on the approach that the db.connection.schema_path is along the lines of ('tenant1', 'public')
and then removing all table from tenant that are not public. 

For example this would mean dropping (tenants, users, user_roles, roles) from all tenant schemas so that when they are not found in the tenant1 schema, 
the default search path of 'public' would hold these tables and data.
Reply all
Reply to author
Forward
0 new messages