strongbolt 0.3.9 → 0.3.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 085945fde7504a693c9d0bd3a6a98b04da72911f
4
- data.tar.gz: cde8841b764409146e570aab407bd352e626c373
3
+ metadata.gz: cae9f523b7f4e7e3e8a20d8e1dce761dff86ed09
4
+ data.tar.gz: 5541d8ac7cbf7f88f264c0b516cdbd11a4d297a0
5
5
  SHA512:
6
- metadata.gz: f57f6215dfaf0cd3b095d02fbc5c88073d52514de38767903a451e93c8fc3afd43b2fcee1bdbbcc77f54451683e73dd5a0d517b88122888974e0ef5a919415bd
7
- data.tar.gz: 96236affc3e1b2eed63fe6a9e4ce4cbc1a4a323fd8cc408bca2c46ed08c093e1c861aeef96f8427dd0a1914527989c5a31307d34aa8530ce13b2249e7b137f86
6
+ metadata.gz: 89eecd3ddf5d3c503e154c6405121530d163d40e742bdf3a30fd6a368526377052bfc9223666e70ab03238130e39755ff177cace84790e4a8c5da8343abdc526
7
+ data.tar.gz: 57ad08612046396dc58ed27161ee3525afe978cad2cf800f228e94f28aa14e96923da2525f292a871994c3171da65f057c36c42b5bae8cd2efba4027c38365b1
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- strongbolt (0.3.8)
4
+ strongbolt (0.3.9)
5
5
  awesome_nested_set (~> 3.0.0)
6
6
  grant (~> 3.0)
7
7
  simple_form (~> 3.0)
@@ -40,6 +40,7 @@ GEM
40
40
  activerecord (>= 4.0.0, < 5)
41
41
  builder (3.2.2)
42
42
  concurrent-ruby (1.0.2)
43
+ database_cleaner (1.5.3)
43
44
  diff-lcs (1.2.5)
44
45
  erubis (2.7.0)
45
46
  fabrication (2.15.2)
@@ -119,6 +120,7 @@ PLATFORMS
119
120
 
120
121
  DEPENDENCIES
121
122
  bundler (> 1.7.0)
123
+ database_cleaner
122
124
  fabrication
123
125
  fuubar
124
126
  rails (~> 4.1.0)
@@ -3,7 +3,8 @@ require "strongbolt/generators/migration"
3
3
  module Strongbolt
4
4
  module Generators
5
5
  #
6
- # Creates a migration to fix a has many through with users tenants problem
6
+ # Creates a migration to add an unique index to user_groups_users,
7
+ # so that a user can only exist once in a group.
7
8
  #
8
9
  class FixUniqueGroupMembersGenerator < Rails::Generators::Base
9
10
  include Strongbolt::Generators::Migration
@@ -121,10 +121,10 @@ module Strongbolt
121
121
  @results_cache["#{k}#{attr_k}-#{user_id}"] = true
122
122
  @results_cache["#{k}any-#{user_id}"] = true
123
123
  else
124
- # On the other hand, it doesn't make sense to pre-populate the valid
125
- # IDs for the models with a lot of instances when we probably are never
126
- # going to need to know this. Instead, adding 'owned' is a hint to actually look
127
- # up later if we own a particular geography.
124
+ # On the other hand, it doesn't make sense to pre-populate the valid
125
+ # IDs for the models with a lot of instances when we probably are never
126
+ # going to need to know this. Instead, adding 'owned' is a hint to actually look
127
+ # up later if we own a particular geography.
128
128
  @results_cache["#{k}#{attr_k}-owned"] = true
129
129
  @results_cache["#{k}any-owned"] = true
130
130
  end
@@ -216,6 +216,8 @@ module Strongbolt
216
216
  #
217
217
  # Checks if the instance given fulfills tenant management rules
218
218
  #
219
+ # returns true even if instance has no relationship to any tenant
220
+ #
219
221
  def has_access_to_tenants? instance, tenants = nil
220
222
  # If no tenants list given, we take all
221
223
  tenants ||= Strongbolt.tenants
@@ -224,7 +226,8 @@ module Strongbolt
224
226
 
225
227
  # Go over each tenants and check if we access to at least one of the tenant
226
228
  # models linked to it
227
- tenants.inject(true) do |result, tenant|
229
+ found_any_tenant_relationship = false
230
+ has_access_to_any_tenant = tenants.inject(false) do |result, tenant|
228
231
  begin
229
232
  if instance.class == tenant
230
233
  tenant_ids = [instance.id]
@@ -245,8 +248,11 @@ module Strongbolt
245
248
  rescue ActiveModel::MissingAttributeError
246
249
  tenant_ids = []
247
250
  end
248
- result && (tenant_ids.size == 0 || (@tenants_cache[tenant.name] & tenant_ids).present?)
251
+ found_any_tenant_relationship = true unless tenant_ids.empty?
252
+ has_access_to_current_tenant = (tenant_ids.size > 0 && (@tenants_cache[tenant.name] & tenant_ids).present?)
253
+ result || has_access_to_current_tenant
249
254
  end
255
+ has_access_to_any_tenant || !found_any_tenant_relationship
250
256
  end
251
257
 
252
258
  #
@@ -1,3 +1,3 @@
1
1
  module Strongbolt
2
- VERSION = "0.3.9"
2
+ VERSION = "0.3.10"
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -36,6 +36,7 @@ require 'shoulda/matchers'
36
36
 
37
37
  require 'rspec/rails'
38
38
  require 'fabrication'
39
+ require 'database_cleaner'
39
40
 
40
41
  # Requires supporting files with custom matchers and macros, etc,
41
42
  # in ./support/ and its subdirectories.
@@ -74,6 +75,14 @@ RSpec.configure do |config|
74
75
  config.before(:suite) do
75
76
  TestsMigrations.new.migrate :up
76
77
  User.send :include, Strongbolt::UserAbilities
78
+ DatabaseCleaner.clean_with :truncation
79
+ DatabaseCleaner.strategy = :transaction
80
+ end
81
+
82
+ config.around(:each) do |spec|
83
+ DatabaseCleaner.start
84
+ spec.run
85
+ DatabaseCleaner.clean
77
86
  end
78
87
 
79
88
  config.after(:suite) do
@@ -0,0 +1,93 @@
1
+ require "spec_helper"
2
+
3
+ describe Strongbolt::UserAbilities do
4
+
5
+ before(:all) do
6
+ define_model "TenantA" do
7
+ self.table_name = "tenant_a"
8
+
9
+ has_many :model_with_tenant
10
+ end
11
+ define_model "TenantB" do
12
+ self.table_name = "tenant_b"
13
+
14
+ has_many :model_with_tenant
15
+ end
16
+
17
+ define_model "ModelWithTenant" do
18
+ self.table_name = "model_with_tenants"
19
+
20
+ belongs_to :tenant_a
21
+ belongs_to :tenant_b
22
+ end
23
+
24
+ Strongbolt::Configuration.add_tenant TenantA
25
+ Strongbolt::Configuration.add_tenant TenantB
26
+ end
27
+ after(:all) do
28
+ undefine_model TenantA
29
+ undefine_model TenantB
30
+ Strongbolt::Configuration.tenants = []
31
+ end
32
+
33
+
34
+ #
35
+ # Creates some fixtures for the tests here
36
+ #
37
+ def create_fixtures
38
+ @user1 = User.create!
39
+ @user2 = User.create!
40
+ @user3 = User.create!
41
+
42
+ @tenant_a = TenantA.create!
43
+ @tenant_b = TenantB.create!
44
+ @user1.add_tenant @tenant_a
45
+ @user1.add_tenant @tenant_b
46
+ @user2.add_tenant @tenant_a
47
+ @user3.add_tenant @tenant_b
48
+
49
+ @tenanted_model1 = ModelWithTenant.create! tenant_a: @tenant_a, tenant_b: @tenant_b
50
+ @tenanted_model2 = ModelWithTenant.create! tenant_a: @tenant_a
51
+ @tenanted_model3 = ModelWithTenant.create! tenant_b: @tenant_b
52
+
53
+ @group = Strongbolt::UserGroup.create! name: "Normal"
54
+ @group.users << @user1
55
+ @group.users << @user2
56
+ @group.users << @user3
57
+
58
+ @role = @group.roles.create! name: "Normal"
59
+ @role.capabilities.create! model: "User", action: "update", require_ownership: true
60
+ @role.capabilities.create! model: "ModelWithTenant", action: "find", require_tenant_access: true
61
+ end
62
+
63
+
64
+
65
+ #
66
+ # Has access to tenants?
67
+ #
68
+ describe "has_access_to_tenants?" do
69
+ before { create_fixtures }
70
+
71
+ it "should be true when model is tenant" do
72
+ expect(@user1.send :has_access_to_tenants?, @tenant_a).to eq true
73
+ expect(@user1.send :has_access_to_tenants?, @tenant_b).to eq true
74
+ expect(@user2.send :has_access_to_tenants?, @tenant_a).to eq true
75
+ expect(@user2.send :has_access_to_tenants?, @tenant_b).to eq false
76
+ expect(@user3.send :has_access_to_tenants?, @tenant_a).to eq false
77
+ expect(@user3.send :has_access_to_tenants?, @tenant_b).to eq true
78
+ end
79
+
80
+ it "should be true when model is first child" do
81
+ expect(@user1.send :has_access_to_tenants?, @tenanted_model1).to eq true
82
+ expect(@user1.send :has_access_to_tenants?, @tenanted_model2).to eq true
83
+ expect(@user1.send :has_access_to_tenants?, @tenanted_model3).to eq true
84
+ expect(@user2.send :has_access_to_tenants?, @tenanted_model1).to eq true
85
+ expect(@user2.send :has_access_to_tenants?, @tenanted_model2).to eq true
86
+ expect(@user2.send :has_access_to_tenants?, @tenanted_model3).to eq false
87
+ expect(@user3.send :has_access_to_tenants?, @tenanted_model1).to eq true
88
+ expect(@user3.send :has_access_to_tenants?, @tenanted_model2).to eq false
89
+ expect(@user3.send :has_access_to_tenants?, @tenanted_model3).to eq true
90
+ end
91
+ end
92
+
93
+ end
@@ -55,6 +55,26 @@ class TestsMigrations < ActiveRecord::Migration
55
55
  t.integer :child_id
56
56
  end
57
57
 
58
+ create_table :tenant_a, :force => true do |t|
59
+ t.string :name
60
+
61
+ t.timestamps
62
+ end
63
+
64
+ create_table :tenant_b, :force => true do |t|
65
+ t.string :name
66
+
67
+ t.timestamps
68
+ end
69
+
70
+ create_table :model_with_tenants, :force => true do |t|
71
+ t.string :name
72
+ t.integer :tenant_a_id
73
+ t.integer :tenant_b_id
74
+
75
+ t.timestamps
76
+ end
77
+
58
78
  create_table :strongbolt_capabilities, :force => true do |t|
59
79
  t.string :name
60
80
  t.string :description
data/strongbolt.gemspec CHANGED
@@ -30,4 +30,5 @@ Gem::Specification.new do |spec|
30
30
  spec.add_development_dependency 'fuubar'
31
31
  spec.add_development_dependency 'rspec-rails', '~> 3.1.0'
32
32
  spec.add_development_dependency 'fabrication'
33
+ spec.add_development_dependency 'database_cleaner'
33
34
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strongbolt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.9
4
+ version: 0.3.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Césaré-Herriau
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-05-31 00:00:00.000000000 Z
12
+ date: 2016-06-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: awesome_nested_set
@@ -165,6 +165,20 @@ dependencies:
165
165
  - - ">="
166
166
  - !ruby/object:Gem::Version
167
167
  version: '0'
168
+ - !ruby/object:Gem::Dependency
169
+ name: database_cleaner
170
+ requirement: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ type: :development
176
+ prerelease: false
177
+ version_requirements: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
168
182
  description: Use model-level authorization with a very granular roles and permissions
169
183
  definition.
170
184
  email:
@@ -311,6 +325,7 @@ files:
311
325
  - spec/strongbolt/helpers_spec.rb
312
326
  - spec/strongbolt/role_spec.rb
313
327
  - spec/strongbolt/tenantable_spec.rb
328
+ - spec/strongbolt/user_abilities_multiple_tenants_spec.rb
314
329
  - spec/strongbolt/user_abilities_spec.rb
315
330
  - spec/strongbolt/user_group_spec.rb
316
331
  - spec/strongbolt/users_tenant_spec.rb
@@ -415,6 +430,7 @@ test_files:
415
430
  - spec/strongbolt/helpers_spec.rb
416
431
  - spec/strongbolt/role_spec.rb
417
432
  - spec/strongbolt/tenantable_spec.rb
433
+ - spec/strongbolt/user_abilities_multiple_tenants_spec.rb
418
434
  - spec/strongbolt/user_abilities_spec.rb
419
435
  - spec/strongbolt/user_group_spec.rb
420
436
  - spec/strongbolt/users_tenant_spec.rb