effective_roles 2.0.3 → 2.2.0
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/MIT-LICENSE +1 -1
- data/README.md +2 -8
- data/app/controllers/admin/roles_controller.rb +8 -3
- data/app/helpers/effective_roles_helper.rb +104 -0
- data/app/models/concerns/acts_as_role_restricted.rb +4 -7
- data/app/views/effective/roles/_summary_table.html.haml +2 -1
- data/config/effective_roles.rb +2 -28
- data/lib/effective_roles.rb +16 -148
- data/lib/effective_roles/engine.rb +2 -14
- data/lib/effective_roles/version.rb +1 -1
- metadata +20 -8
- data/app/models/effective/access_denied.rb +0 -17
- data/lib/effective_roles/set_current_user.rb +0 -15
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 77423c072ab8659b9e632cebcd9fe939743abac4d0b374f12b3e144a70bcdfe1
|
|
4
|
+
data.tar.gz: 929bbf885819e6303f7a09ffe7c58988f4607403903b3dfdc3c141f175714eee
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a6917b11179747ff215fd926a595594de592b6881a3ca74c7b699fb52a1a0b482259ff2edddd0cd02d2eaebf8d265ebea506128641d32fb7f296567bad2466bf
|
|
7
|
+
data.tar.gz: 90a7d053382163c74cf8df9dee3d03b84239f4f39bd199d1b0ada04f124fd88b8ff4d7222dd8e3fce9f0eb37544c2bf386ab0a0d8c3a38ee9384b6b04b861907
|
data/MIT-LICENSE
CHANGED
data/README.md
CHANGED
|
@@ -8,10 +8,6 @@ Includes a mixin for adding authentication for any model.
|
|
|
8
8
|
|
|
9
9
|
SQL Finders for returning an ActiveRecord::Relation with all permitted records.
|
|
10
10
|
|
|
11
|
-
Handy formtastic and simple_form helpers for assigning roles.
|
|
12
|
-
|
|
13
|
-
Rails 3.2.x and Rails 4
|
|
14
|
-
|
|
15
11
|
|
|
16
12
|
## Getting Started
|
|
17
13
|
|
|
@@ -158,8 +154,8 @@ When using assignable roles, you must assign the acts_as_role_restricted resourc
|
|
|
158
154
|
|
|
159
155
|
You can do this in one of three ways:
|
|
160
156
|
|
|
161
|
-
1. Setting resource.current_user = current_user in your controller directly.
|
|
162
|
-
2. Add `before_action
|
|
157
|
+
1. Setting resource.current_user = current_user in your controller update action directly.
|
|
158
|
+
2. Add `before_action -> { @thing.current_user = current_user }` to your ApplicationController
|
|
163
159
|
3. Using `Effective::CrudController` to do this automatically.
|
|
164
160
|
|
|
165
161
|
This restriction is only applied when running within the rails server. Not on rails console or db:seeds.
|
|
@@ -319,5 +315,3 @@ This model implements the https://github.com/ryanb/cancan/wiki/Role-Based-Author
|
|
|
319
315
|
4. Push to the branch (`git push origin my-new-feature`)
|
|
320
316
|
5. Bonus points for test coverage
|
|
321
317
|
6. Create new Pull Request
|
|
322
|
-
|
|
323
|
-
|
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
module Admin
|
|
2
2
|
class RolesController < ApplicationController
|
|
3
|
-
before_action
|
|
3
|
+
before_action(:authenticate_user!) if defined?(Devise)
|
|
4
|
+
before_action { EffectiveResources.authorize!(self, :admin, :effective_roles) }
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
include Effective::CrudController
|
|
7
|
+
|
|
8
|
+
if (config = EffectiveRoles.layout)
|
|
9
|
+
layout(config.kind_of?(Hash) ? config[:admin] : config)
|
|
10
|
+
end
|
|
6
11
|
|
|
7
12
|
def index
|
|
8
13
|
@page_title = 'Roles'
|
|
9
|
-
EffectiveRoles.authorize!(self, :admin, :effective_roles)
|
|
10
14
|
end
|
|
15
|
+
|
|
11
16
|
end
|
|
12
17
|
end
|
|
@@ -101,4 +101,108 @@ module EffectiveRolesHelper
|
|
|
101
101
|
klass.respond_to?(:name) ? klass.name : klass.to_s
|
|
102
102
|
end
|
|
103
103
|
|
|
104
|
+
# This is used by the effective_roles_summary_table helper method
|
|
105
|
+
def effective_roles_authorization_level(controller, role, resource)
|
|
106
|
+
authorization_method = EffectiveResources.authorization_method
|
|
107
|
+
|
|
108
|
+
raise('expected an authorization method') unless (authorization_method.respond_to?(:call) || authorization_method.kind_of?(Symbol))
|
|
109
|
+
return :unknown unless (controller.current_user rescue nil).respond_to?(:roles=)
|
|
110
|
+
|
|
111
|
+
# Store the current ability (cancan support) and roles
|
|
112
|
+
current_ability = controller.instance_variable_get(:@current_ability)
|
|
113
|
+
current_user = controller.instance_variable_get(:@current_user)
|
|
114
|
+
current_user_roles = controller.current_user.roles
|
|
115
|
+
|
|
116
|
+
# Set up the user, so the check is done with the desired permission level
|
|
117
|
+
controller.instance_variable_set(:@current_ability, nil)
|
|
118
|
+
|
|
119
|
+
level = nil
|
|
120
|
+
|
|
121
|
+
case role
|
|
122
|
+
when :signed_in
|
|
123
|
+
controller.current_user.roles = []
|
|
124
|
+
when :public
|
|
125
|
+
controller.instance_variable_set(:@current_user, nil)
|
|
126
|
+
|
|
127
|
+
if defined?(EffectiveLogging)
|
|
128
|
+
EffectiveLogging.supressed { (controller.request.env['warden'].set_user(false) rescue nil) }
|
|
129
|
+
else
|
|
130
|
+
(controller.request.env['warden'].set_user(false) rescue nil)
|
|
131
|
+
end
|
|
132
|
+
else
|
|
133
|
+
controller.current_user.roles = [role]
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# Find the actual authorization level
|
|
137
|
+
level = effective_roles_item_authorization_level(controller, role, resource, authorization_method)
|
|
138
|
+
|
|
139
|
+
# Restore the existing current_user stuff
|
|
140
|
+
if role == :public
|
|
141
|
+
ActiveRecord::Base.transaction do
|
|
142
|
+
if defined?(EffectiveLogging)
|
|
143
|
+
EffectiveLogging.supressed { (controller.request.env['warden'].set_user(current_user) rescue nil) }
|
|
144
|
+
else
|
|
145
|
+
(controller.request.env['warden'].set_user(current_user) rescue nil)
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
raise ActiveRecord::Rollback
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
controller.instance_variable_set(:@current_ability, current_ability)
|
|
153
|
+
controller.instance_variable_set(:@current_user, current_user)
|
|
154
|
+
controller.current_user.roles = current_user_roles
|
|
155
|
+
|
|
156
|
+
level
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def effective_roles_item_authorization_level(controller, role, resource, auth_method)
|
|
160
|
+
resource = (resource.new() rescue resource) if resource.kind_of?(ActiveRecord::Base)
|
|
161
|
+
|
|
162
|
+
# Custom actions
|
|
163
|
+
if resource.kind_of?(Hash)
|
|
164
|
+
resource.each do |key, value|
|
|
165
|
+
return (controller.instance_exec(controller, key, value, &auth_method) rescue false) ? :yes : :no
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# Check for Manage
|
|
170
|
+
return :manage if (
|
|
171
|
+
(controller.instance_exec(controller, :create, resource, &auth_method) rescue false) &&
|
|
172
|
+
(controller.instance_exec(controller, :update, resource, &auth_method) rescue false) &&
|
|
173
|
+
(controller.instance_exec(controller, :show, resource, &auth_method) rescue false) &&
|
|
174
|
+
(controller.instance_exec(controller, :destroy, resource, &auth_method) rescue false)
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
# Check for Update
|
|
178
|
+
return :update if (controller.instance_exec(controller, :update, resource, &auth_method) rescue false)
|
|
179
|
+
|
|
180
|
+
# Check for Update Own
|
|
181
|
+
if resource.respond_to?('user=')
|
|
182
|
+
resource.user = controller.current_user
|
|
183
|
+
return :update_own if (controller.instance_exec(controller, :update, resource, &auth_method) rescue false)
|
|
184
|
+
resource.user = nil
|
|
185
|
+
elsif resource.respond_to?('user_id=')
|
|
186
|
+
resource.user_id = controller.current_user.id
|
|
187
|
+
return :update_own if (controller.instance_exec(controller, :update, resource, &auth_method) rescue false)
|
|
188
|
+
resource.user_id = nil
|
|
189
|
+
elsif resource.class.name.end_with?('User')
|
|
190
|
+
return :update_own if (controller.instance_exec(controller, :update, controller.current_user, &auth_method) rescue false)
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
# Check for Create
|
|
194
|
+
return :create if (controller.instance_exec(controller, :create, resource, &auth_method) rescue false)
|
|
195
|
+
|
|
196
|
+
# Check for Show
|
|
197
|
+
return :show if (controller.instance_exec(controller, :show, resource, &auth_method) rescue false)
|
|
198
|
+
|
|
199
|
+
# Check for Index
|
|
200
|
+
return :index if (controller.instance_exec(controller, :index, resource, &auth_method) rescue false)
|
|
201
|
+
|
|
202
|
+
# Check for Destroy
|
|
203
|
+
return :destroy if (controller.instance_exec(controller, :destroy, resource, &auth_method) rescue false)
|
|
204
|
+
|
|
205
|
+
:none
|
|
206
|
+
end
|
|
207
|
+
|
|
104
208
|
end
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
module ActsAsRoleRestricted
|
|
15
15
|
extend ActiveSupport::Concern
|
|
16
16
|
|
|
17
|
-
module
|
|
17
|
+
module Base
|
|
18
18
|
def acts_as_role_restricted(multiple: false)
|
|
19
19
|
@acts_as_role_restricted_opts = { multiple: multiple }
|
|
20
20
|
include ::ActsAsRoleRestricted
|
|
@@ -29,13 +29,11 @@ module ActsAsRoleRestricted
|
|
|
29
29
|
|
|
30
30
|
validates :roles_mask, numericality: true, allow_nil: true
|
|
31
31
|
|
|
32
|
-
validate(if: -> { changes.include?(:roles_mask) && EffectiveRoles.assignable_roles_present?(self) }) do
|
|
33
|
-
user = current_user || EffectiveRoles.current_user || (EffectiveLogging.current_user if defined?(EffectiveLogging))
|
|
34
|
-
|
|
32
|
+
validate(if: -> { changes.include?(:roles_mask) && EffectiveRoles.assignable_roles_present?(self) && current_user.present? }) do
|
|
35
33
|
roles_was = EffectiveRoles.roles_for(changes[:roles_mask].first)
|
|
36
34
|
changed = (roles + roles_was) - (roles & roles_was) # XOR
|
|
37
35
|
|
|
38
|
-
assignable = EffectiveRoles.assignable_roles_collection(self,
|
|
36
|
+
assignable = EffectiveRoles.assignable_roles_collection(self, current_user) # Returns all roles when user is blank
|
|
39
37
|
unauthorized = changed - assignable
|
|
40
38
|
|
|
41
39
|
authorized = roles.dup
|
|
@@ -45,7 +43,7 @@ module ActsAsRoleRestricted
|
|
|
45
43
|
Rails.logger.info "\e[31m unassignable roles: #{unauthorized.map { |role| ":#{role}" }.to_sentence}"
|
|
46
44
|
end
|
|
47
45
|
|
|
48
|
-
if unauthorized.present? &&
|
|
46
|
+
if unauthorized.present? && current_user.blank? && defined?(Rails::Server)
|
|
49
47
|
self.errors.add(:roles, 'current_user must be present when assigning roles')
|
|
50
48
|
end
|
|
51
49
|
|
|
@@ -131,4 +129,3 @@ module ActsAsRoleRestricted
|
|
|
131
129
|
end
|
|
132
130
|
|
|
133
131
|
end
|
|
134
|
-
|
|
@@ -13,4 +13,5 @@
|
|
|
13
13
|
%td= effective_roles_authorization_label(klass)
|
|
14
14
|
- roles.each do |role|
|
|
15
15
|
%td.text-center
|
|
16
|
-
=
|
|
16
|
+
- level = effective_roles_authorization_level(controller, role, klass)
|
|
17
|
+
= effective_roles_authorization_badge(level)
|
data/config/effective_roles.rb
CHANGED
|
@@ -67,34 +67,8 @@ EffectiveRoles.setup do |config|
|
|
|
67
67
|
# }
|
|
68
68
|
|
|
69
69
|
# Authorization Method
|
|
70
|
-
#
|
|
71
|
-
# This doesn't have anything to do with the roles themselves.
|
|
72
|
-
# It's only used in two places:
|
|
73
|
-
# - For the effective_roles_summary_table() helper method
|
|
74
|
-
# - The /admin/roles page check
|
|
75
|
-
#
|
|
76
|
-
# It should match the authorization check used by your application
|
|
77
|
-
#
|
|
78
|
-
# This method is called by all controller actions with the appropriate action and resource
|
|
79
|
-
# If the method returns false, an Effective::AccessDenied Error will be raised (see README.md for complete info)
|
|
80
|
-
#
|
|
81
|
-
# Use via Proc (and with CanCan):
|
|
82
|
-
# config.authorization_method = Proc.new { |controller, action, resource| can?(action, resource) }
|
|
83
|
-
#
|
|
84
|
-
# Use via custom method:
|
|
85
|
-
# config.authorization_method = :my_authorization_method
|
|
86
|
-
#
|
|
87
|
-
# And then in your application_controller.rb:
|
|
88
|
-
#
|
|
89
|
-
# def my_authorization_method(action, resource)
|
|
90
|
-
# current_user.is?(:admin)
|
|
91
|
-
# end
|
|
92
|
-
#
|
|
93
|
-
# Or disable the check completely:
|
|
94
|
-
# config.authorization_method = false
|
|
95
|
-
config.authorization_method = Proc.new { |controller, action, resource| authorize!(action, resource) } # CanCanCan
|
|
70
|
+
# This gem serves an /admin/roles endpoint that calls EffectiveResources.authorize!
|
|
96
71
|
|
|
97
72
|
# Layout Settings
|
|
98
|
-
#
|
|
99
|
-
config.layout = 'application'
|
|
73
|
+
# config.layout = 'admin'
|
|
100
74
|
end
|
data/lib/effective_roles.rb
CHANGED
|
@@ -1,59 +1,30 @@
|
|
|
1
|
+
require 'effective_resources'
|
|
1
2
|
require 'effective_roles/engine'
|
|
2
3
|
require 'effective_roles/version'
|
|
3
4
|
|
|
4
5
|
module EffectiveRoles
|
|
5
|
-
mattr_accessor :roles
|
|
6
|
-
mattr_accessor :role_descriptions
|
|
7
|
-
mattr_accessor :assignable_roles
|
|
8
6
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
def self.setup
|
|
13
|
-
yield self
|
|
7
|
+
def self.config_keys
|
|
8
|
+
[:roles, :role_descriptions, :assignable_roles, :layout]
|
|
14
9
|
end
|
|
15
10
|
|
|
11
|
+
include EffectiveGem
|
|
12
|
+
|
|
16
13
|
def self.permitted_params
|
|
17
14
|
{ roles: [] }
|
|
18
15
|
end
|
|
19
16
|
|
|
20
|
-
def self.authorized?(controller, action, resource)
|
|
21
|
-
@_exceptions ||= [Effective::AccessDenied, (CanCan::AccessDenied if defined?(CanCan)), (Pundit::NotAuthorizedError if defined?(Pundit))].compact
|
|
22
|
-
|
|
23
|
-
return !!authorization_method unless authorization_method.respond_to?(:call)
|
|
24
|
-
controller = controller.controller if controller.respond_to?(:controller)
|
|
25
|
-
|
|
26
|
-
begin
|
|
27
|
-
!!(controller || self).instance_exec((controller || self), action, resource, &authorization_method)
|
|
28
|
-
rescue *@_exceptions
|
|
29
|
-
false
|
|
30
|
-
end
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
def self.authorize!(controller, action, resource)
|
|
34
|
-
raise Effective::AccessDenied unless authorized?(controller, action, resource)
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
# This is set by the "set_effective_roles_current_user" before_filter.
|
|
38
|
-
def self.current_user=(user)
|
|
39
|
-
@effective_roles_current_user = user
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
def self.current_user
|
|
43
|
-
@effective_roles_current_user
|
|
44
|
-
end
|
|
45
|
-
|
|
46
17
|
# This method converts whatever is given into its roles
|
|
47
18
|
# Pass an object, Integer, or Symbol to find corresponding role
|
|
48
19
|
def self.roles_for(obj)
|
|
49
20
|
if obj.respond_to?(:is_role_restricted?)
|
|
50
21
|
obj.roles
|
|
51
22
|
elsif obj.kind_of?(Integer)
|
|
52
|
-
roles.reject { |r| (obj & 2**roles.index(r)).zero? }
|
|
23
|
+
roles.reject { |r| (obj & 2 ** config.roles.index(r)).zero? }
|
|
53
24
|
elsif obj.kind_of?(Symbol)
|
|
54
|
-
|
|
25
|
+
Array(roles.find { |role| role == obj })
|
|
55
26
|
elsif obj.kind_of?(String)
|
|
56
|
-
|
|
27
|
+
Array(roles.find { |role| role == obj.to_sym })
|
|
57
28
|
elsif obj.kind_of?(Array)
|
|
58
29
|
obj.map { |obj| roles_for(obj) }.flatten.compact
|
|
59
30
|
elsif obj.nil?
|
|
@@ -65,7 +36,7 @@ module EffectiveRoles
|
|
|
65
36
|
|
|
66
37
|
# EffectiveRoles.roles_mask_for(:admin, :member)
|
|
67
38
|
def self.roles_mask_for(*roles)
|
|
68
|
-
roles_for(roles).map { |r| 2**
|
|
39
|
+
roles_for(roles).map { |r| 2 ** config.roles.index(r) }.sum
|
|
69
40
|
end
|
|
70
41
|
|
|
71
42
|
def self.roles_collection(resource, current_user = nil, only: nil, except: nil, multiple: nil)
|
|
@@ -94,14 +65,12 @@ module EffectiveRoles
|
|
|
94
65
|
def self.assignable_roles_collection(resource, current_user = nil, multiple: nil)
|
|
95
66
|
return roles unless assignable_roles_present?(resource)
|
|
96
67
|
|
|
97
|
-
current_user ||= (EffectiveRoles.current_user || (EffectiveLogging.current_user if defined?(EffectiveLogging)))
|
|
98
|
-
|
|
99
68
|
if current_user && !current_user.respond_to?(:is_role_restricted?)
|
|
100
|
-
raise('expected current_user to respond to is_role_restricted?')
|
|
69
|
+
raise('expected current_user to respond to is_role_restricted?')
|
|
101
70
|
end
|
|
102
71
|
|
|
103
72
|
if !resource.respond_to?(:is_role_restricted?)
|
|
104
|
-
raise('expected current_user to respond to is_role_restricted?')
|
|
73
|
+
raise('expected current_user to respond to is_role_restricted?')
|
|
105
74
|
end
|
|
106
75
|
|
|
107
76
|
assigned_roles = if assignable_roles.kind_of?(Hash)
|
|
@@ -138,13 +107,11 @@ module EffectiveRoles
|
|
|
138
107
|
end
|
|
139
108
|
|
|
140
109
|
def self.assignable_roles_present?(resource)
|
|
141
|
-
return false
|
|
110
|
+
return false unless assignable_roles.present?
|
|
142
111
|
|
|
143
|
-
raise 'EffectiveRoles config.assignable_roles_for must be a Hash
|
|
112
|
+
raise 'EffectiveRoles config.assignable_roles_for must be a Hash or Array' unless [Hash, Array].include?(assignable_roles.class)
|
|
144
113
|
raise('expected resource to respond to is_role_restricted?') unless resource.respond_to?(:is_role_restricted?)
|
|
145
114
|
|
|
146
|
-
return assignable_roles.present? if assignable_roles.kind_of?(Array)
|
|
147
|
-
|
|
148
115
|
if assignable_roles.kind_of?(Array)
|
|
149
116
|
assignable_roles
|
|
150
117
|
elsif assignable_roles.key?(resource.class.to_s)
|
|
@@ -154,113 +121,14 @@ module EffectiveRoles
|
|
|
154
121
|
end.present?
|
|
155
122
|
end
|
|
156
123
|
|
|
157
|
-
# This is used by the effective_roles_summary_table helper method
|
|
158
|
-
def self.authorization_level(controller, role, resource)
|
|
159
|
-
return :unknown unless (authorization_method.respond_to?(:call) || authorization_method.kind_of?(Symbol))
|
|
160
|
-
return :unknown unless (controller.current_user rescue nil).respond_to?(:roles=)
|
|
161
|
-
|
|
162
|
-
# Store the current ability (cancan support) and roles
|
|
163
|
-
current_ability = controller.instance_variable_get(:@current_ability)
|
|
164
|
-
current_user = controller.instance_variable_get(:@current_user)
|
|
165
|
-
current_user_roles = controller.current_user.roles
|
|
166
|
-
|
|
167
|
-
# Set up the user, so the check is done with the desired permission level
|
|
168
|
-
controller.instance_variable_set(:@current_ability, nil)
|
|
169
|
-
|
|
170
|
-
level = nil
|
|
171
|
-
|
|
172
|
-
case role
|
|
173
|
-
when :signed_in
|
|
174
|
-
controller.current_user.roles = []
|
|
175
|
-
when :public
|
|
176
|
-
controller.instance_variable_set(:@current_user, nil)
|
|
177
|
-
|
|
178
|
-
if defined?(EffectiveLogging)
|
|
179
|
-
EffectiveLogging.supressed { (controller.request.env['warden'].set_user(false) rescue nil) }
|
|
180
|
-
else
|
|
181
|
-
(controller.request.env['warden'].set_user(false) rescue nil)
|
|
182
|
-
end
|
|
183
|
-
else
|
|
184
|
-
controller.current_user.roles = [role]
|
|
185
|
-
end
|
|
186
|
-
|
|
187
|
-
# Find the actual authorization level
|
|
188
|
-
level = _authorization_level(controller, role, resource, authorization_method)
|
|
189
|
-
|
|
190
|
-
# Restore the existing current_user stuff
|
|
191
|
-
if role == :public
|
|
192
|
-
ActiveRecord::Base.transaction do
|
|
193
|
-
if defined?(EffectiveLogging)
|
|
194
|
-
EffectiveLogging.supressed { (controller.request.env['warden'].set_user(current_user) rescue nil) }
|
|
195
|
-
else
|
|
196
|
-
(controller.request.env['warden'].set_user(current_user) rescue nil)
|
|
197
|
-
end
|
|
198
|
-
|
|
199
|
-
raise ActiveRecord::Rollback
|
|
200
|
-
end
|
|
201
|
-
end
|
|
202
|
-
|
|
203
|
-
controller.instance_variable_set(:@current_ability, current_ability)
|
|
204
|
-
controller.instance_variable_set(:@current_user, current_user)
|
|
205
|
-
controller.current_user.roles = current_user_roles
|
|
206
|
-
|
|
207
|
-
level
|
|
208
|
-
end
|
|
209
|
-
|
|
210
124
|
private
|
|
211
125
|
|
|
212
126
|
def self.role_description(role, obj = nil)
|
|
213
127
|
raise 'EffectiveRoles config.role_descriptions must be a Hash' unless role_descriptions.kind_of?(Hash)
|
|
214
|
-
(role_descriptions[obj.try(:class).to_s] || {})[role] || role_descriptions[role] || ''
|
|
215
|
-
end
|
|
216
|
-
|
|
217
|
-
def self._authorization_level(controller, role, resource, auth_method)
|
|
218
|
-
resource = (resource.new() rescue resource) if resource.kind_of?(ActiveRecord::Base)
|
|
219
|
-
|
|
220
|
-
# Custom actions
|
|
221
|
-
if resource.kind_of?(Hash)
|
|
222
|
-
resource.each do |key, value|
|
|
223
|
-
return (controller.instance_exec(controller, key, value, &auth_method) rescue false) ? :yes : :no
|
|
224
|
-
end
|
|
225
|
-
end
|
|
226
|
-
|
|
227
|
-
# Check for Manage
|
|
228
|
-
return :manage if (
|
|
229
|
-
(controller.instance_exec(controller, :create, resource, &auth_method) rescue false) &&
|
|
230
|
-
(controller.instance_exec(controller, :update, resource, &auth_method) rescue false) &&
|
|
231
|
-
(controller.instance_exec(controller, :show, resource, &auth_method) rescue false) &&
|
|
232
|
-
(controller.instance_exec(controller, :destroy, resource, &auth_method) rescue false)
|
|
233
|
-
)
|
|
234
|
-
|
|
235
|
-
# Check for Update
|
|
236
|
-
return :update if (controller.instance_exec(controller, :update, resource, &auth_method) rescue false)
|
|
237
|
-
|
|
238
|
-
# Check for Update Own
|
|
239
|
-
if resource.respond_to?('user=')
|
|
240
|
-
resource.user = controller.current_user
|
|
241
|
-
return :update_own if (controller.instance_exec(controller, :update, resource, &auth_method) rescue false)
|
|
242
|
-
resource.user = nil
|
|
243
|
-
elsif resource.respond_to?('user_id=')
|
|
244
|
-
resource.user_id = controller.current_user.id
|
|
245
|
-
return :update_own if (controller.instance_exec(controller, :update, resource, &auth_method) rescue false)
|
|
246
|
-
resource.user_id = nil
|
|
247
|
-
elsif resource.kind_of?(User)
|
|
248
|
-
return :update_own if (controller.instance_exec(controller, :update, controller.current_user, &auth_method) rescue false)
|
|
249
|
-
end
|
|
250
|
-
|
|
251
|
-
# Check for Create
|
|
252
|
-
return :create if (controller.instance_exec(controller, :create, resource, &auth_method) rescue false)
|
|
253
|
-
|
|
254
|
-
# Check for Show
|
|
255
|
-
return :show if (controller.instance_exec(controller, :show, resource, &auth_method) rescue false)
|
|
256
|
-
|
|
257
|
-
# Check for Index
|
|
258
|
-
return :index if (controller.instance_exec(controller, :index, resource, &auth_method) rescue false)
|
|
259
|
-
|
|
260
|
-
# Check for Destroy
|
|
261
|
-
return :destroy if (controller.instance_exec(controller, :destroy, resource, &auth_method) rescue false)
|
|
262
128
|
|
|
263
|
-
|
|
129
|
+
description = role_descriptions.dig(obj.class.to_s, role) if obj.present?
|
|
130
|
+
description ||= role_descriptions[role]
|
|
131
|
+
description || ''
|
|
264
132
|
end
|
|
265
133
|
|
|
266
134
|
end
|
|
@@ -2,27 +2,15 @@ module EffectiveRoles
|
|
|
2
2
|
class Engine < ::Rails::Engine
|
|
3
3
|
engine_name 'effective_roles'
|
|
4
4
|
|
|
5
|
-
config.autoload_paths += Dir["#{config.root}/app/models/concerns", "#{config.root}/lib/"]
|
|
6
|
-
|
|
7
5
|
# Include acts_as_addressable concern and allow any ActiveRecord object to call it
|
|
8
6
|
initializer 'effective_roles.active_record' do |app|
|
|
9
7
|
ActiveSupport.on_load :active_record do
|
|
10
|
-
ActiveRecord::Base.extend(ActsAsRoleRestricted::
|
|
11
|
-
end
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
# Register the log_page_views concern so that it can be called in ActionController or elsewhere
|
|
15
|
-
initializer 'effective_logging.log_changes_action_controller' do |app|
|
|
16
|
-
Rails.application.config.to_prepare do
|
|
17
|
-
ActiveSupport.on_load :action_controller do
|
|
18
|
-
require 'effective_roles/set_current_user'
|
|
19
|
-
ActionController::Base.include(EffectiveRoles::SetCurrentUser::ActionController)
|
|
20
|
-
end
|
|
8
|
+
ActiveRecord::Base.extend(ActsAsRoleRestricted::Base)
|
|
21
9
|
end
|
|
22
10
|
end
|
|
23
11
|
|
|
24
12
|
# Set up our default configuration options.
|
|
25
|
-
initializer "effective_roles.defaults", :
|
|
13
|
+
initializer "effective_roles.defaults", before: :load_config_initializers do |app|
|
|
26
14
|
eval File.read("#{config.root}/config/effective_roles.rb")
|
|
27
15
|
end
|
|
28
16
|
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: effective_roles
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.0
|
|
4
|
+
version: 2.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Code and Effect
|
|
8
|
-
autorequire:
|
|
8
|
+
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2021-06-21 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rails
|
|
@@ -24,6 +24,20 @@ dependencies:
|
|
|
24
24
|
- - ">="
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
26
|
version: 3.2.0
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: effective_resources
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '0'
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
27
41
|
description: Assign multiple roles to any User or other ActiveRecord object. Select
|
|
28
42
|
only the appropriate objects based on intelligent, chainable ActiveRecord::Relation
|
|
29
43
|
finder methods.
|
|
@@ -38,7 +52,6 @@ files:
|
|
|
38
52
|
- app/controllers/admin/roles_controller.rb
|
|
39
53
|
- app/helpers/effective_roles_helper.rb
|
|
40
54
|
- app/models/concerns/acts_as_role_restricted.rb
|
|
41
|
-
- app/models/effective/access_denied.rb
|
|
42
55
|
- app/views/admin/roles/index.html.haml
|
|
43
56
|
- app/views/effective/roles/_summary.html.haml
|
|
44
57
|
- app/views/effective/roles/_summary_table.html.haml
|
|
@@ -46,14 +59,13 @@ files:
|
|
|
46
59
|
- config/routes.rb
|
|
47
60
|
- lib/effective_roles.rb
|
|
48
61
|
- lib/effective_roles/engine.rb
|
|
49
|
-
- lib/effective_roles/set_current_user.rb
|
|
50
62
|
- lib/effective_roles/version.rb
|
|
51
63
|
- lib/generators/effective_roles/install_generator.rb
|
|
52
64
|
homepage: https://github.com/code-and-effect/effective_roles
|
|
53
65
|
licenses:
|
|
54
66
|
- MIT
|
|
55
67
|
metadata: {}
|
|
56
|
-
post_install_message:
|
|
68
|
+
post_install_message:
|
|
57
69
|
rdoc_options: []
|
|
58
70
|
require_paths:
|
|
59
71
|
- lib
|
|
@@ -68,8 +80,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
68
80
|
- !ruby/object:Gem::Version
|
|
69
81
|
version: '0'
|
|
70
82
|
requirements: []
|
|
71
|
-
rubygems_version: 3.
|
|
72
|
-
signing_key:
|
|
83
|
+
rubygems_version: 3.1.2
|
|
84
|
+
signing_key:
|
|
73
85
|
specification_version: 4
|
|
74
86
|
summary: Assign multiple roles to any User or other ActiveRecord object. Select only
|
|
75
87
|
the appropriate objects based on intelligent, chainable ActiveRecord::Relation finder
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
unless defined?(Effective::AccessDenied)
|
|
2
|
-
module Effective
|
|
3
|
-
class AccessDenied < StandardError
|
|
4
|
-
attr_reader :action, :subject
|
|
5
|
-
|
|
6
|
-
def initialize(message = nil, action = nil, subject = nil)
|
|
7
|
-
@message = message
|
|
8
|
-
@action = action
|
|
9
|
-
@subject = subject
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
def to_s
|
|
13
|
-
@message || I18n.t(:'unauthorized.default', :default => 'Access Denied')
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
end
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
module EffectiveRoles
|
|
2
|
-
module SetCurrentUser
|
|
3
|
-
module ActionController
|
|
4
|
-
|
|
5
|
-
# Add me to your ApplicationController
|
|
6
|
-
# before_action :set_effective_roles_current_user
|
|
7
|
-
|
|
8
|
-
def set_effective_roles_current_user
|
|
9
|
-
EffectiveRoles.current_user = current_user
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
|