Permalink
Browse files

Adds initial super admin support

  • Loading branch information...
1 parent d4327ec commit f1713b8a7dc364352fb8c0086ec3e56661ed9897 @mscottford mscottford committed Jan 10, 2013
View
@@ -0,0 +1 @@
+1.9.3-p327
@@ -6,4 +6,12 @@
end
validates :email, :uniqueness => { :scope => :tenant_id, :allow_blank => true, :if => :email_changed? }
+
+ self.default_scopes = []
+ default_scope lambda {
+ if Spree::Tenant.table_exists? && column_names.include?('tenant_id') && column_names.include?('super_admin')
+ where( "#{table_name}.tenant_id = ? or #{table_name}.super_admin = ?", Spree::Tenant.current_tenant_id, true )
+ end
+ }
+
end
@@ -0,0 +1,5 @@
+class AddSuperAdminToSpreeUsers < ActiveRecord::Migration
+ def change
+ add_column :spree_users, :super_admin, :boolean
+ end
+end
@@ -0,0 +1,84 @@
+require 'spec_helper'
+
+describe 'normal admin users' do
+ let!(:apples_tenant) { FactoryGirl.create(:tenant, :shortname => 'apples', :domain => 'apples.com', name: "Apple") }
+ let!(:oranges_tenant) { FactoryGirl.create(:tenant, :shortname => 'oranges', :domain => 'oranges.com', name: "Orange") }
+
+ let(:apples_admin) {
+ Spree::User.create!(email: 'apples-admin@example.com', password: 'spree123').tap do |u|
+ u.tenant = apples_tenant
+ u.save!
+ end
+ }
+
+ before {
+ Spree::Tenant.set_current_tenant apples_tenant
+ Spree::Role.find_or_create_by_name(:user)
+ Spree::Role.find_or_create_by_name(:admin)
+ }
+
+ it 'can log into its assigned tenant' do
+ visit 'http://apples.example.com/admin'
+
+ fill_in 'Email', :with => apples_admin.email
+ fill_in 'Password', :with => apples_admin.password
+ click_button 'Login'
+
+ page.should_not have_content('Invalid email or password')
+ page.should have_content("Logged In As: #{apples_admin.email}")
+ end
+
+ it 'cannot log into a different tenant' do
+ visit 'http://oranges.example.com/admin'
+
+ fill_in 'Email', :with => apples_admin.email
+ fill_in 'Password', :with => apples_admin.password
+ click_button 'Login'
+
+ page.should have_content('Invalid email or password')
+ page.should_not have_content("Logged In As: #{apples_admin.email}")
+ end
+
+ it 'cannot change the super admin status of another user'
+
+ it 'can create admin users' do
+ visit 'http://apples.example.com/admin/users/new'
+
+ fill_in 'Email', :with => apples_admin.email
+ fill_in 'Password', :with => apples_admin.password
+ click_button 'Login'
+
+ fill_in 'Email', :with => 'user@example.com'
+ fill_in 'Password', :with => 'spree123'
+ fill_in 'Password Confirmation', :with => 'spree123'
+ check 'user_spree_role_admin'
+ click_button 'Create'
+
+ page.should have_content('Listing Users')
+ page.should have_content('user@example.com')
+
+ Spree::User.find_by_email('user@example.com').should have_spree_role(:admin)
+ end
+
+ it 'can create customer users' do
+ visit 'http://apples.example.com/admin/users/new'
+
+ fill_in 'Email', :with => apples_admin.email
+ fill_in 'Password', :with => apples_admin.password
+ click_button 'Login'
+
+ fill_in 'Email', :with => 'user@example.com'
+ fill_in 'Password', :with => 'spree123'
+ fill_in 'Password Confirmation', :with => 'spree123'
+
+ check 'user_spree_role_user'
+ click_button 'Create'
+
+ page.should have_content('Listing Users')
+ page.should have_content('user@example.com')
+
+ user = Spree::User.find_by_email('user@example.com')
+ user.should have_spree_role(:user)
+ user.should_not have_spree_role(:admin)
+ end
+end
@@ -0,0 +1,92 @@
+require 'spec_helper'
+
+describe 'super admin users' do
+ let!(:apples_tenant) { FactoryGirl.create(:tenant, :shortname => 'apples', :domain => 'apples.com', name: "Apple") }
+ let!(:oranges_tenant) { FactoryGirl.create(:tenant, :shortname => 'oranges', :domain => 'oranges.com', name: "Orange") }
+
+ let(:super_admin) {
+ Spree::User.create!(email: 'super-admin@example.com', password: 'spree123').tap do |u|
+ u.tenant = apples_tenant
+ u.super_admin = true
+ u.spree_roles << Spree::Role.find_by_name(:admin)
+ u.save!
+ end
+ }
+
+ before {
+ Spree::Tenant.set_current_tenant apples_tenant
+ Spree::Role.find_or_create_by_name(:user)
+ Spree::Role.find_or_create_by_name(:admin)
+ }
+
+
+ it 'can log into the tenant backend it belongs to' do
+ visit 'http://apples.example.com/admin'
+
+ fill_in 'Email', :with => super_admin.email
+ fill_in 'Password', :with => super_admin.password
+ click_button 'Login'
+
+ page.should_not have_content('Invalid email or password')
+ page.should have_content("Logged In As: #{super_admin.email}")
+ end
+
+ it 'can log into the tenant backend it does not belong to' do
+ visit 'http://oranges.example.com/admin'
+
+ fill_in 'Email', :with => super_admin.email
+ fill_in 'Password', :with => super_admin.password
+ click_button 'Login'
+
+ page.should_not have_content('Invalid email or password')
+ page.should have_content("Logged In As: #{super_admin.email}")
+ end
+
+ it 'can create super admin users'
+
+ it 'can create admin users' do
+ visit 'http://apples.example.com/admin/users/new'
+
+ fill_in 'Email', :with => super_admin.email
+ fill_in 'Password', :with => super_admin.password
+ click_button 'Login'
+
+ fill_in 'Email', :with => 'user@example.com'
+ fill_in 'Password', :with => 'spree123'
+ fill_in 'Password Confirmation', :with => 'spree123'
+ check 'user_spree_role_admin'
+ click_button 'Create'
+
+ page.should have_content('Listing Users')
+ page.should have_content('user@example.com')
+
+ user = Spree::User.find_by_email('user@example.com')
+ user.should have_spree_role(:admin)
+ user.should_not be_super_admin
+ end
+
+ it 'can create customer users' do
+ visit 'http://apples.example.com/admin/users/new'
+
+ fill_in 'Email', :with => super_admin.email
+ fill_in 'Password', :with => super_admin.password
+ click_button 'Login'
+
+ fill_in 'Email', :with => 'user@example.com'
+ fill_in 'Password', :with => 'spree123'
+ fill_in 'Password Confirmation', :with => 'spree123'
+
+ check 'user_spree_role_user'
+ click_button 'Create'
+
+ page.should have_content('Listing Users')
+ page.should have_content('user@example.com')
+
+ user = Spree::User.find_by_email('user@example.com')
+ user.should have_spree_role(:user)
+ user.should_not have_spree_role(:admin)
+ user.should_not be_super_admin
+ end
+
+ it 'show up the user lists of all tenant backends'
+end
@@ -23,4 +23,7 @@
Spree::User.create!(email: 'test@example.com', password: 'spree123')
}.to raise_error('Validation failed: Email has already been taken')
end
+
+ it 'creates first user as a super admin'
+ it 'creates second user as neither super nor admin'
end

0 comments on commit f1713b8

Please sign in to comment.