strongbolt 0.3.12 → 0.3.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +56 -0
- data/.rubocop_todo.yml +91 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +18 -2
- data/Rakefile +1 -1
- data/app/controllers/strongbolt/capabilities_controller.rb +36 -45
- data/app/controllers/strongbolt/roles_controller.rb +39 -47
- data/app/controllers/strongbolt/security_controller.rb +2 -3
- data/app/controllers/strongbolt/user_groups_controller.rb +48 -54
- data/app/controllers/strongbolt/user_groups_users_controller.rb +2 -4
- data/app/controllers/strongbolt_controller.rb +1 -1
- data/circle.yml +13 -0
- data/lib/generators/strongbolt/fix_generator.rb +5 -6
- data/lib/generators/strongbolt/fix_unique_group_members_generator.rb +2 -3
- data/lib/generators/strongbolt/indexes_generator.rb +3 -4
- data/lib/generators/strongbolt/install_generator.rb +8 -9
- data/lib/generators/strongbolt/templates/fix_unique_group_members.rb +1 -1
- data/lib/generators/strongbolt/templates/indexes.rb +1 -1
- data/lib/generators/strongbolt/templates/migration.rb +11 -12
- data/lib/generators/strongbolt/templates/strongbolt.rb +1 -1
- data/lib/generators/strongbolt/views_generator.rb +4 -4
- data/lib/strongbolt.rb +51 -54
- data/lib/strongbolt/base.rb +1 -1
- data/lib/strongbolt/bolted.rb +12 -13
- data/lib/strongbolt/bolted_controller.rb +46 -57
- data/lib/strongbolt/capabilities_role.rb +5 -5
- data/lib/strongbolt/capability.rb +32 -31
- data/lib/strongbolt/configuration.rb +18 -19
- data/lib/strongbolt/controllers/url_helpers.rb +5 -5
- data/lib/strongbolt/engine.rb +9 -9
- data/lib/strongbolt/errors.rb +4 -4
- data/lib/strongbolt/generators/migration.rb +4 -6
- data/lib/strongbolt/helpers.rb +5 -7
- data/lib/strongbolt/rails/routes.rb +4 -4
- data/lib/strongbolt/role.rb +11 -12
- data/lib/strongbolt/roles_user_group.rb +5 -5
- data/lib/strongbolt/rspec.rb +2 -2
- data/lib/strongbolt/rspec/user.rb +13 -15
- data/lib/strongbolt/tenantable.rb +78 -80
- data/lib/strongbolt/user_abilities.rb +44 -54
- data/lib/strongbolt/user_group.rb +8 -10
- data/lib/strongbolt/user_groups_user.rb +6 -6
- data/lib/strongbolt/version.rb +1 -1
- data/lib/tasks/strongbolt_tasks.rake +4 -4
- data/spec/controllers/strongbolt/capabilities_controller_spec.rb +28 -45
- data/spec/controllers/strongbolt/roles_controller_spec.rb +39 -72
- data/spec/controllers/strongbolt/user_groups_controller_spec.rb +34 -65
- data/spec/controllers/strongbolt/user_groups_users_controller_spec.rb +11 -19
- data/spec/controllers/without_authorization_controller_spec.rb +5 -5
- data/spec/dummy/app/controllers/posts_controller.rb +2 -2
- data/spec/dummy/app/controllers/test_controller.rb +1 -1
- data/spec/dummy/app/controllers/without_authorization_controller.rb +1 -1
- data/spec/dummy/bin/rails +1 -1
- data/spec/dummy/config.ru +1 -1
- data/spec/dummy/config/application.rb +4 -5
- data/spec/dummy/config/initializers/cookies_serializer.rb +1 -1
- data/spec/dummy/config/initializers/strongbolt.rb +2 -2
- data/spec/dummy/config/routes.rb +1 -3
- data/spec/dummy/db/migrate/20150630212236_create_strongbolt_tables.rb +9 -10
- data/spec/dummy/db/migrate/20150630212251_create_strongbolt_tables_indexes.rb +2 -2
- data/spec/dummy/db/migrate/20160531110509_fix_unique_group_members.rb +1 -1
- data/spec/fabricators/capability_fabricator.rb +4 -4
- data/spec/fabricators/role_fabricator.rb +3 -3
- data/spec/fabricators/user_fabricator.rb +2 -2
- data/spec/fabricators/user_group_fabricator.rb +3 -3
- data/spec/fixtures/application.rb +6 -3
- data/spec/fixtures/controllers.rb +1 -1
- data/spec/spec_helper.rb +7 -8
- data/spec/strongbolt/bolted_controller_spec.rb +110 -208
- data/spec/strongbolt/bolted_spec.rb +26 -40
- data/spec/strongbolt/capability_spec.rb +72 -86
- data/spec/strongbolt/configuration_spec.rb +33 -46
- data/spec/strongbolt/controllers/url_helpers_spec.rb +7 -9
- data/spec/strongbolt/helpers_spec.rb +14 -16
- data/spec/strongbolt/role_spec.rb +32 -35
- data/spec/strongbolt/tenantable_spec.rb +88 -86
- data/spec/strongbolt/user_abilities_multiple_tenants_spec.rb +29 -34
- data/spec/strongbolt/user_abilities_spec.rb +142 -188
- data/spec/strongbolt/user_group_spec.rb +14 -14
- data/spec/strongbolt/users_tenant_spec.rb +10 -12
- data/spec/strongbolt_spec.rb +53 -73
- data/spec/support/controller_macros.rb +1 -3
- data/spec/support/db_setup.rb +31 -25
- data/spec/support/helpers.rb +12 -12
- data/spec/support/transactional_specs.rb +1 -3
- data/strongbolt.gemspec +14 -12
- metadata +20 -3
@@ -1,76 +1,70 @@
|
|
1
1
|
module Strongbolt
|
2
2
|
class UserGroupsController < ::StrongboltController
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
def index
|
4
|
+
@user_groups = UserGroup.all
|
5
|
+
end
|
6
6
|
|
7
|
-
|
7
|
+
def show
|
8
8
|
@user_group = UserGroup.find params[:id]
|
9
9
|
# We select the users not yet in the user group
|
10
10
|
@users = Strongbolt.user_class_constant
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
.joins("LEFT JOIN strongbolt_user_groups_users sugu ON sugu.user_id = #{Strongbolt.user_class_constant.table_name}.id")
|
12
|
+
.joins('LEFT JOIN strongbolt_user_groups sug ON sug.id = sugu.user_group_id')
|
13
|
+
.where('sug.id IS NULL OR sug.id != ?', @user_group.id)
|
14
|
+
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
@user_group = UserGroup.create! user_group_params
|
16
|
+
def create
|
17
|
+
@user_group = UserGroup.create! user_group_params
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
end
|
19
|
+
flash[:success] = 'Use group was successfully created!'
|
20
|
+
redirect_to user_group_path(@user_group)
|
21
|
+
rescue ActiveRecord::RecordInvalid
|
22
|
+
flash[:danger] = 'User Group could not be created, please review the errors below'
|
23
|
+
redirect_to new_user_group_path
|
24
|
+
rescue ActionController::ParameterMissing => e
|
25
|
+
flash[:danger] = "User Group could not be created: ERROR #{e}"
|
26
|
+
redirect_to new_user_group_path
|
27
|
+
end
|
30
28
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
@user_group.update_attributes! user_group_params
|
35
|
-
|
36
|
-
flash[:success] = "User group was successfully updated!"
|
37
|
-
redirect_to user_group_path params[:id]
|
38
|
-
rescue ActiveRecord::RecordInvalid => e
|
39
|
-
flash[:danger] = "User Group could not be modified, please review the errors below"
|
40
|
-
redirect_to edit_user_group_path(params[:id])
|
41
|
-
rescue ActionController::ParameterMissing => e
|
42
|
-
flash[:danger] = "User Group could not be updated: ERROR #{e}"
|
43
|
-
redirect_to edit_user_group_path(params[:id])
|
44
|
-
end
|
45
|
-
end
|
29
|
+
def update
|
30
|
+
@user_group = UserGroup.find params[:id]
|
31
|
+
@user_group.update_attributes! user_group_params
|
46
32
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
33
|
+
flash[:success] = 'User group was successfully updated!'
|
34
|
+
redirect_to user_group_path params[:id]
|
35
|
+
rescue ActiveRecord::RecordInvalid
|
36
|
+
flash[:danger] = 'User Group could not be modified, please review the errors below'
|
37
|
+
redirect_to edit_user_group_path(params[:id])
|
38
|
+
rescue ActionController::ParameterMissing => e
|
39
|
+
flash[:danger] = "User Group could not be updated: ERROR #{e}"
|
40
|
+
redirect_to edit_user_group_path(params[:id])
|
41
|
+
end
|
51
42
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
rescue ActiveRecord::DeleteRestrictionError
|
56
|
-
flash[:danger] = "User group #{@user_group.name} cannot be deleted because #{@user_group.users.size} users belong to it"
|
43
|
+
def destroy
|
44
|
+
@user_group = UserGroup.find params[:id]
|
45
|
+
@user_group.destroy!
|
57
46
|
|
58
|
-
|
59
|
-
end
|
60
|
-
end
|
47
|
+
flash[:success] = "User group #{@user_group.name} successfully deleted"
|
61
48
|
|
62
|
-
|
49
|
+
redirect_to user_groups_path
|
50
|
+
rescue ActiveRecord::DeleteRestrictionError
|
51
|
+
flash[:danger] = "User group #{@user_group.name} cannot be deleted because #{@user_group.users.size} users belong to it"
|
52
|
+
|
53
|
+
redirect_to user_group_path(@user_group)
|
54
|
+
end
|
55
|
+
|
56
|
+
def edit
|
63
57
|
@user_group = UserGroup.find params[:id]
|
64
|
-
|
58
|
+
end
|
65
59
|
|
66
|
-
|
60
|
+
def new
|
67
61
|
@user_group = UserGroup.new
|
68
|
-
|
62
|
+
end
|
69
63
|
|
70
64
|
private
|
71
65
|
|
72
66
|
def user_group_params
|
73
|
-
params.require(:user_group).permit(:name, :
|
67
|
+
params.require(:user_group).permit(:name, role_ids: [])
|
74
68
|
end
|
75
69
|
end
|
76
|
-
end
|
70
|
+
end
|
@@ -1,7 +1,6 @@
|
|
1
1
|
module Strongbolt
|
2
2
|
class UserGroupsUsersController < ::StrongboltController
|
3
|
-
|
4
|
-
self.model_for_authorization = "Strongbolt::UserGroup"
|
3
|
+
self.model_for_authorization = 'Strongbolt::UserGroup'
|
5
4
|
|
6
5
|
def create
|
7
6
|
@user_group = UserGroup.find(params[:user_group_id])
|
@@ -24,7 +23,7 @@ module Strongbolt
|
|
24
23
|
redirect_to request.referrer || user_group_path(@user_group)
|
25
24
|
end
|
26
25
|
|
27
|
-
rescue_from ActiveRecord::RecordNotFound do |
|
26
|
+
rescue_from ActiveRecord::RecordNotFound do |_e|
|
28
27
|
if @user_group.nil?
|
29
28
|
flash[:danger] = "User Group ##{params[:user_group_id]} does not exist"
|
30
29
|
redirect_to user_groups_path
|
@@ -33,6 +32,5 @@ module Strongbolt
|
|
33
32
|
redirect_to user_group_path(@user_group)
|
34
33
|
end
|
35
34
|
end
|
36
|
-
|
37
35
|
end
|
38
36
|
end
|
@@ -1,2 +1,2 @@
|
|
1
1
|
class StrongboltController < Strongbolt.parent_controller.constantize
|
2
|
-
end
|
2
|
+
end
|
data/circle.yml
ADDED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'strongbolt/generators/migration'
|
2
2
|
|
3
3
|
module Strongbolt
|
4
4
|
module Generators
|
@@ -11,13 +11,12 @@ module Strongbolt
|
|
11
11
|
source_root File.expand_path('../templates', __FILE__)
|
12
12
|
|
13
13
|
def copy_fix
|
14
|
-
|
15
|
-
|
14
|
+
if Strongbolt::UsersTenant.primary_key.nil?
|
15
|
+
copy_migration 'fix', 'fix_strongbolt_users_tenants_id'
|
16
16
|
else
|
17
|
-
|
17
|
+
puts 'Strongbolt::UsersTenant already has a primary key, no need to use the fix'
|
18
18
|
end
|
19
19
|
end
|
20
|
-
|
21
20
|
end
|
22
21
|
end
|
23
|
-
end
|
22
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'strongbolt/generators/migration'
|
2
2
|
|
3
3
|
module Strongbolt
|
4
4
|
module Generators
|
@@ -12,9 +12,8 @@ module Strongbolt
|
|
12
12
|
source_root File.expand_path('../templates', __FILE__)
|
13
13
|
|
14
14
|
def copy_fix
|
15
|
-
copy_migration
|
15
|
+
copy_migration 'fix_unique_group_members', 'fix_unique_group_members'
|
16
16
|
end
|
17
|
-
|
18
17
|
end
|
19
18
|
end
|
20
19
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'strongbolt/generators/migration'
|
2
2
|
|
3
3
|
module Strongbolt
|
4
4
|
module Generators
|
@@ -11,9 +11,8 @@ module Strongbolt
|
|
11
11
|
source_root File.expand_path('../templates', __FILE__)
|
12
12
|
|
13
13
|
def copy_fix
|
14
|
-
copy_migration
|
14
|
+
copy_migration 'indexes', 'create_strongbolt_tables_indexes'
|
15
15
|
end
|
16
|
-
|
17
16
|
end
|
18
17
|
end
|
19
|
-
end
|
18
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'strongbolt/generators/migration'
|
2
2
|
|
3
3
|
module Strongbolt
|
4
4
|
module Generators
|
@@ -8,22 +8,21 @@ module Strongbolt
|
|
8
8
|
source_root File.expand_path('../templates', __FILE__)
|
9
9
|
|
10
10
|
def copy_migrations
|
11
|
-
copy_migration
|
11
|
+
copy_migration 'migration', 'create_strongbolt_tables'
|
12
12
|
end
|
13
13
|
|
14
14
|
def copy_initializer
|
15
15
|
# Laods all the application models
|
16
16
|
Rails.application.eager_load!
|
17
17
|
# Copy the file
|
18
|
-
copy_file
|
18
|
+
copy_file 'strongbolt.rb', 'config/initializers/strongbolt.rb'
|
19
19
|
# Fill in the list of models of the application
|
20
|
-
gsub_file
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
20
|
+
gsub_file 'config/initializers/strongbolt.rb', '%MODELS%',
|
21
|
+
ActiveRecord::Base.descendants
|
22
|
+
.reject { |m| m.name =~ /^Strongbolt::/ }
|
23
|
+
.map { |m| "'#{m.name}'" }
|
24
|
+
.join(', ')
|
25
25
|
end
|
26
|
-
|
27
26
|
end
|
28
27
|
end
|
29
28
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
class FixUniqueGroupMembers < ActiveRecord::Migration[4.2]
|
2
2
|
def change
|
3
|
-
add_index :strongbolt_user_groups_users, [
|
3
|
+
add_index :strongbolt_user_groups_users, %i[user_group_id user_id], unique: true, name: :index_strongbolt_user_groups_users_unique
|
4
4
|
end
|
5
5
|
end
|
@@ -16,6 +16,6 @@ class CreateStrongboltTablesIndexes < ActiveRecord::Migration[4.2]
|
|
16
16
|
add_index :strongbolt_users_tenants, :user_id
|
17
17
|
add_index :strongbolt_users_tenants, :tenant_id
|
18
18
|
add_index :strongbolt_users_tenants, :type
|
19
|
-
add_index :strongbolt_users_tenants, [
|
19
|
+
add_index :strongbolt_users_tenants, %i[tenant_id type]
|
20
20
|
end
|
21
21
|
end
|
@@ -1,18 +1,18 @@
|
|
1
1
|
class CreateStrongboltTables < ActiveRecord::Migration[4.2]
|
2
2
|
def change
|
3
|
-
create_table :strongbolt_capabilities, :
|
3
|
+
create_table :strongbolt_capabilities, force: true do |t|
|
4
4
|
t.string :name
|
5
5
|
t.string :description
|
6
6
|
t.string :model
|
7
7
|
t.string :action
|
8
8
|
t.string :attr
|
9
|
-
t.boolean :require_ownership, :
|
10
|
-
t.boolean :require_tenant_access, :
|
9
|
+
t.boolean :require_ownership, default: false, null: false
|
10
|
+
t.boolean :require_tenant_access, default: true, null: false
|
11
11
|
|
12
12
|
t.timestamps
|
13
13
|
end
|
14
14
|
|
15
|
-
create_table :strongbolt_roles, :
|
15
|
+
create_table :strongbolt_roles, force: true do |t|
|
16
16
|
t.string :name
|
17
17
|
t.integer :parent_id
|
18
18
|
t.integer :lft
|
@@ -22,29 +22,29 @@ class CreateStrongboltTables < ActiveRecord::Migration[4.2]
|
|
22
22
|
t.timestamps
|
23
23
|
end
|
24
24
|
|
25
|
-
create_table :strongbolt_user_groups, :
|
25
|
+
create_table :strongbolt_user_groups, force: true do |t|
|
26
26
|
t.string :name
|
27
27
|
t.text :description
|
28
28
|
|
29
29
|
t.timestamps
|
30
30
|
end
|
31
31
|
|
32
|
-
create_table :strongbolt_user_groups_users, :
|
32
|
+
create_table :strongbolt_user_groups_users, force: true do |t|
|
33
33
|
t.integer :user_group_id
|
34
34
|
t.integer :user_id
|
35
35
|
end
|
36
36
|
|
37
|
-
create_table :strongbolt_roles_user_groups, :
|
37
|
+
create_table :strongbolt_roles_user_groups, force: true do |t|
|
38
38
|
t.integer :user_group_id
|
39
39
|
t.integer :role_id
|
40
40
|
end
|
41
41
|
|
42
|
-
create_table :strongbolt_capabilities_roles, :
|
42
|
+
create_table :strongbolt_capabilities_roles, force: true do |t|
|
43
43
|
t.integer :role_id
|
44
44
|
t.integer :capability_id
|
45
45
|
end
|
46
46
|
|
47
|
-
create_table :strongbolt_users_tenants, :
|
47
|
+
create_table :strongbolt_users_tenants, force: true do |t|
|
48
48
|
t.integer :user_id
|
49
49
|
t.integer :tenant_id
|
50
50
|
t.string :type
|
@@ -67,9 +67,8 @@ class CreateStrongboltTables < ActiveRecord::Migration[4.2]
|
|
67
67
|
add_index :strongbolt_users_tenants, :user_id
|
68
68
|
add_index :strongbolt_users_tenants, :tenant_id
|
69
69
|
add_index :strongbolt_users_tenants, :type
|
70
|
-
add_index :strongbolt_users_tenants, [
|
70
|
+
add_index :strongbolt_users_tenants, %i[tenant_id type]
|
71
71
|
|
72
|
-
add_index :strongbolt_user_groups_users, [
|
72
|
+
add_index :strongbolt_user_groups_users, %i[user_group_id user_id], unique: true, name: :index_strongbolt_user_groups_users_unique
|
73
73
|
end
|
74
74
|
end
|
75
|
-
|
@@ -3,14 +3,14 @@ require 'rails/generators/base'
|
|
3
3
|
module Strongbolt
|
4
4
|
module Generators
|
5
5
|
class ViewsGenerator < Rails::Generators::Base
|
6
|
-
desc
|
6
|
+
desc 'Copies Strongbolt views to your application.'
|
7
7
|
|
8
8
|
argument :scope, required: false, default: nil,
|
9
|
-
desc:
|
9
|
+
desc: 'The scope to copy views to'
|
10
10
|
|
11
11
|
public_task :copy_views
|
12
12
|
|
13
|
-
source_root File.expand_path(
|
13
|
+
source_root File.expand_path('../../../../app/views', __FILE__)
|
14
14
|
|
15
15
|
def copy_views
|
16
16
|
directory :strongbolt, target_path
|
@@ -23,4 +23,4 @@ module Strongbolt
|
|
23
23
|
end
|
24
24
|
end
|
25
25
|
end
|
26
|
-
end
|
26
|
+
end
|
data/lib/strongbolt.rb
CHANGED
@@ -1,33 +1,33 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require 'active_record'
|
2
|
+
require 'awesome_nested_set'
|
3
|
+
require 'simple_form'
|
4
4
|
|
5
|
-
require
|
6
|
-
require
|
5
|
+
require 'grant/grantable'
|
6
|
+
require 'grant/status'
|
7
7
|
require 'grant/user'
|
8
8
|
|
9
|
-
require
|
10
|
-
require
|
11
|
-
require
|
12
|
-
require
|
13
|
-
require
|
14
|
-
require
|
15
|
-
require
|
16
|
-
require
|
17
|
-
require
|
18
|
-
require
|
19
|
-
require
|
20
|
-
require
|
21
|
-
require
|
22
|
-
require
|
23
|
-
require
|
9
|
+
require 'strongbolt/version'
|
10
|
+
require 'strongbolt/errors'
|
11
|
+
require 'strongbolt/configuration'
|
12
|
+
require 'strongbolt/tenantable'
|
13
|
+
require 'strongbolt/bolted'
|
14
|
+
require 'strongbolt/bolted_controller'
|
15
|
+
require 'strongbolt/user_abilities'
|
16
|
+
require 'strongbolt/base'
|
17
|
+
require 'strongbolt/capability'
|
18
|
+
require 'strongbolt/user_groups_user'
|
19
|
+
require 'strongbolt/roles_user_group'
|
20
|
+
require 'strongbolt/capabilities_role'
|
21
|
+
require 'strongbolt/role'
|
22
|
+
require 'strongbolt/user_group'
|
23
|
+
require 'strongbolt/users_tenant'
|
24
24
|
|
25
25
|
#
|
26
26
|
# Raise an error if version of AR not compatible (4.1.0 and 4.1.1)
|
27
27
|
#
|
28
28
|
ar_version = ActiveRecord.version.version
|
29
|
-
if ar_version >=
|
30
|
-
raise StandardError,
|
29
|
+
if ar_version >= '4.1.0' && ar_version <= '4.1.1'
|
30
|
+
raise StandardError, 'You cannot use Strongbolt with ActiveRecord versions 4.1.0 and 4.1.1. Please upgrade to >= 4.1.2'
|
31
31
|
end
|
32
32
|
|
33
33
|
#
|
@@ -38,7 +38,7 @@ ActiveRecord::Base.send :include, Strongbolt::Bolted
|
|
38
38
|
#
|
39
39
|
# Default behavior, when method current_user defined on controller
|
40
40
|
#
|
41
|
-
if defined?(ActionController)
|
41
|
+
if defined?(ActionController) && defined?(ActionController::Base)
|
42
42
|
|
43
43
|
ActionController::Base.send :include, Strongbolt::BoltedController
|
44
44
|
|
@@ -49,7 +49,6 @@ end
|
|
49
49
|
#
|
50
50
|
require 'strongbolt/engine' if defined?(Rails::Engine)
|
51
51
|
|
52
|
-
|
53
52
|
#
|
54
53
|
# Main module
|
55
54
|
#
|
@@ -62,20 +61,20 @@ module Strongbolt
|
|
62
61
|
|
63
62
|
# Delegates to the configuration the access denied
|
64
63
|
def_delegators Configuration, :access_denied, :logger, :tenants, :user_class, :user_class_constant,
|
65
|
-
|
64
|
+
:default_capabilities
|
66
65
|
module_function :access_denied, :logger, :tenants, :user_class, :user_class_constant,
|
67
|
-
|
66
|
+
:default_capabilities
|
68
67
|
|
69
68
|
# Delegates switching thread behavior
|
70
69
|
def_delegators Grant::Status, :switch_to_multithread,
|
71
|
-
|
70
|
+
:switch_to_monothread
|
72
71
|
module_function :switch_to_multithread, :switch_to_monothread
|
73
72
|
|
74
73
|
#
|
75
74
|
# Tje parent controller to all strongbolt controllers
|
76
75
|
#
|
77
76
|
mattr_accessor :parent_controller
|
78
|
-
@@parent_controller =
|
77
|
+
@@parent_controller = 'ApplicationController'
|
79
78
|
|
80
79
|
#
|
81
80
|
# Current User
|
@@ -86,13 +85,11 @@ module Strongbolt
|
|
86
85
|
|
87
86
|
# We keep an hash so we don't have each time to test
|
88
87
|
# if the module is included in the list
|
89
|
-
def self.current_user=
|
88
|
+
def self.current_user=(user)
|
90
89
|
# If user is an instance of something and different from what we have
|
91
90
|
if user.present?
|
92
91
|
# Raise error if wrong user class
|
93
|
-
unless valid_user? user
|
94
|
-
raise Strongbolt::WrongUserClass
|
95
|
-
end
|
92
|
+
raise Strongbolt::WrongUserClass unless valid_user? user
|
96
93
|
|
97
94
|
# If the user class doesn't have included the module yet
|
98
95
|
unless user.class.included_modules.include? Strongbolt::UserAbilities
|
@@ -107,7 +104,7 @@ module Strongbolt
|
|
107
104
|
#
|
108
105
|
# Setting up Strongbolt
|
109
106
|
#
|
110
|
-
def self.setup
|
107
|
+
def self.setup(&block)
|
111
108
|
# Configuration by user
|
112
109
|
block.call Configuration
|
113
110
|
|
@@ -120,36 +117,36 @@ module Strongbolt
|
|
120
117
|
logger.warn "User class #{Configuration.user_class} wasn't found"
|
121
118
|
end
|
122
119
|
rescue => e
|
123
|
-
error =
|
124
|
-
[ERROR] Strongbolt could not initialized successfully.
|
125
|
-
|
126
|
-
|
127
|
-
|
120
|
+
error = <<~CONTENT
|
121
|
+
[ERROR] Strongbolt could not initialized successfully.
|
122
|
+
This can happen when running migrations, and in this situation, you can ignore this message.
|
123
|
+
If it happens in test, make sure you've run `rake db:test:prepare` so that test database is ready.
|
124
|
+
Otherwise, please review the error below to check what happened:
|
128
125
|
|
129
|
-
Error message:
|
130
|
-
|
126
|
+
Error message:
|
127
|
+
#{e.message}
|
131
128
|
|
132
|
-
|
129
|
+
#{e.backtrace.join("\n")}
|
133
130
|
CONTENT
|
134
131
|
logger.fatal error
|
135
132
|
# Display in the console when error test env
|
136
133
|
puts error if defined?(Rails) && Rails.env.test?
|
137
134
|
# If not being done in a rake task, this should propagate the error
|
138
|
-
raise e unless $
|
135
|
+
raise e unless $PROGRAM_NAME =~ /rake$/ # && ARGV.join(" ").include?("db:")
|
139
136
|
end
|
140
137
|
|
141
138
|
#
|
142
139
|
# Perform the block without grant
|
143
140
|
#
|
144
|
-
def self.without_authorization
|
145
|
-
Grant::Status.without_grant
|
141
|
+
def self.without_authorization(&block)
|
142
|
+
Grant::Status.without_grant(&block)
|
146
143
|
end
|
147
144
|
|
148
145
|
#
|
149
146
|
# Perform the block with grant
|
150
147
|
#
|
151
|
-
def self.with_authorization
|
152
|
-
Grant::Status.with_grant
|
148
|
+
def self.with_authorization(&block)
|
149
|
+
Grant::Status.with_grant(&block)
|
153
150
|
end
|
154
151
|
|
155
152
|
#
|
@@ -166,8 +163,9 @@ Error message:
|
|
166
163
|
def self.enabled?
|
167
164
|
Grant::Status.grant_enabled?
|
168
165
|
end
|
166
|
+
|
169
167
|
def self.disabled?
|
170
|
-
!
|
168
|
+
!enabled?
|
171
169
|
end
|
172
170
|
|
173
171
|
#
|
@@ -175,7 +173,7 @@ Error message:
|
|
175
173
|
# It checks whether the class or the base_class (in case of STI) of the instance class
|
176
174
|
# has been configured as the user model
|
177
175
|
#
|
178
|
-
def self.valid_user?
|
176
|
+
def self.valid_user?(user)
|
179
177
|
user.class.name == Strongbolt::Configuration.user_class ||
|
180
178
|
user.class.base_class.name == Strongbolt::Configuration.user_class
|
181
179
|
end
|
@@ -193,7 +191,7 @@ Error message:
|
|
193
191
|
end
|
194
192
|
|
195
193
|
# Not to use directly, only used in tests
|
196
|
-
def self.tenants=
|
194
|
+
def self.tenants=(tenants)
|
197
195
|
@@tenants = tenants
|
198
196
|
end
|
199
197
|
private_class_method :tenants=
|
@@ -204,13 +202,11 @@ end
|
|
204
202
|
# should not have any authorization check perform
|
205
203
|
#
|
206
204
|
class Object
|
207
|
-
def self.perform_without_authorization
|
208
|
-
method_names.each {|name| setup_without_authorization name}
|
205
|
+
def self.perform_without_authorization(*method_names)
|
206
|
+
method_names.each { |name| setup_without_authorization name }
|
209
207
|
end
|
210
208
|
|
211
|
-
|
212
|
-
|
213
|
-
def self.setup_without_authorization method_name
|
209
|
+
def self.setup_without_authorization(method_name)
|
214
210
|
aliased_name = "_with_autorization_#{method_name}"
|
215
211
|
alias_method aliased_name, method_name
|
216
212
|
define_method method_name do |*args, &block|
|
@@ -219,4 +215,5 @@ class Object
|
|
219
215
|
end
|
220
216
|
end
|
221
217
|
end
|
218
|
+
private_class_method :setup_without_authorization
|
222
219
|
end
|