effective_roles 2.0.2 → 2.1.1
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 +6 -9
- data/app/views/effective/roles/_summary_table.html.haml +2 -1
- data/config/effective_roles.rb +4 -29
- data/lib/effective_roles.rb +44 -157
- 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: 2839a8a58a284f7b6f9f7b12a3d5a0eff69b9b60c1f99ef68d364acab5257355
|
4
|
+
data.tar.gz: 69856eb8d9e3296a26386d28560c36c238cbfc7d407168f2614f13f48fc9b9eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e7c8c3befff4c7f7c51290f39f8f14a70ce50cca7448be66869875bca53422d23b73ef5b558b58ebc046b66f398d356d2fa3125bc595b9ecad737eb16a06d41d
|
7
|
+
data.tar.gz: c504136f2a6593dd272ce0208d72f26fb77a491e846c91b24ed5806a60aa4ffbea942ecb09e19e463166bbe0835e21071b7ac9493aecda527a479dc26c68c054
|
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
|
@@ -30,16 +30,10 @@ module ActsAsRoleRestricted
|
|
30
30
|
validates :roles_mask, numericality: true, allow_nil: true
|
31
31
|
|
32
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
|
-
|
35
|
-
if user.blank? && defined?(Rails::Server)
|
36
|
-
self.errors.add(:roles, 'current_user must be present when assigning roles')
|
37
|
-
end
|
38
|
-
|
39
33
|
roles_was = EffectiveRoles.roles_for(changes[:roles_mask].first)
|
40
34
|
changed = (roles + roles_was) - (roles & roles_was) # XOR
|
41
35
|
|
42
|
-
assignable = EffectiveRoles.assignable_roles_collection(self,
|
36
|
+
assignable = EffectiveRoles.assignable_roles_collection(self, current_user) # Returns all roles when user is blank
|
43
37
|
unauthorized = changed - assignable
|
44
38
|
|
45
39
|
authorized = roles.dup
|
@@ -49,6 +43,10 @@ module ActsAsRoleRestricted
|
|
49
43
|
Rails.logger.info "\e[31m unassignable roles: #{unauthorized.map { |role| ":#{role}" }.to_sentence}"
|
50
44
|
end
|
51
45
|
|
46
|
+
if unauthorized.present? && current_user.blank? && defined?(Rails::Server)
|
47
|
+
self.errors.add(:roles, 'current_user must be present when assigning roles')
|
48
|
+
end
|
49
|
+
|
52
50
|
self.roles_mask = EffectiveRoles.roles_mask_for(authorized)
|
53
51
|
end
|
54
52
|
|
@@ -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
@@ -62,38 +62,13 @@ EffectiveRoles.setup do |config|
|
|
62
62
|
# config.assignable_roles = {
|
63
63
|
# :superadmin => [:superadmin, :admin, :member], # Superadmins may assign any resource any role
|
64
64
|
# :admin => [:admin, :member], # Admins may only assign the :admin or :member role
|
65
|
-
# :member => []
|
65
|
+
# :member => [], # Members may not assign any roles
|
66
|
+
# :new_record => [:member] # Member may be assigned to a new_record without a current_user
|
66
67
|
# }
|
67
68
|
|
68
69
|
# Authorization Method
|
69
|
-
#
|
70
|
-
# This doesn't have anything to do with the roles themselves.
|
71
|
-
# It's only used in two places:
|
72
|
-
# - For the effective_roles_summary_table() helper method
|
73
|
-
# - The /admin/roles page check
|
74
|
-
#
|
75
|
-
# It should match the authorization check used by your application
|
76
|
-
#
|
77
|
-
# This method is called by all controller actions with the appropriate action and resource
|
78
|
-
# If the method returns false, an Effective::AccessDenied Error will be raised (see README.md for complete info)
|
79
|
-
#
|
80
|
-
# Use via Proc (and with CanCan):
|
81
|
-
# config.authorization_method = Proc.new { |controller, action, resource| can?(action, resource) }
|
82
|
-
#
|
83
|
-
# Use via custom method:
|
84
|
-
# config.authorization_method = :my_authorization_method
|
85
|
-
#
|
86
|
-
# And then in your application_controller.rb:
|
87
|
-
#
|
88
|
-
# def my_authorization_method(action, resource)
|
89
|
-
# current_user.is?(:admin)
|
90
|
-
# end
|
91
|
-
#
|
92
|
-
# Or disable the check completely:
|
93
|
-
# config.authorization_method = false
|
94
|
-
config.authorization_method = Proc.new { |controller, action, resource| authorize!(action, resource) } # CanCanCan
|
70
|
+
# This gem serves an /admin/roles endpoint that calls EffectiveResources.authorize!
|
95
71
|
|
96
72
|
# Layout Settings
|
97
|
-
#
|
98
|
-
config.layout = 'application'
|
73
|
+
# config.layout = 'admin'
|
99
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,38 +65,53 @@ 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
|
-
|
104
|
-
|
105
|
-
elsif current_user.present?
|
106
|
-
current_roles = assignable_roles[resource.class.to_s] || assignable_roles || {}
|
107
|
-
current_user.roles.map { |role| current_roles[role] }.flatten.compact.uniq
|
108
|
-
else
|
109
|
-
current_roles = assignable_roles[resource.class.to_s] || assignable_roles || {}
|
110
|
-
current_roles.values.flatten.uniq
|
72
|
+
if !resource.respond_to?(:is_role_restricted?)
|
73
|
+
raise('expected current_user to respond to is_role_restricted?')
|
111
74
|
end
|
112
75
|
|
76
|
+
assigned_roles = if assignable_roles.kind_of?(Hash)
|
77
|
+
assignable = (assignable_roles[resource.class.to_s] || assignable_roles || {})
|
78
|
+
assigned = [] # our return value
|
79
|
+
|
80
|
+
if current_user.blank?
|
81
|
+
assigned = assignable.values.flatten
|
82
|
+
end
|
83
|
+
|
84
|
+
if current_user.present?
|
85
|
+
assigned = current_user.roles.map { |role| assignable[role] }.flatten.compact
|
86
|
+
end
|
87
|
+
|
88
|
+
if assignable[:new_record] && resource.new_record?
|
89
|
+
assigned += Array(assignable[:new_record])
|
90
|
+
end
|
91
|
+
|
92
|
+
if assignable[:persisted] && resource.persisted?
|
93
|
+
assigned += Array(assignable[:persisted])
|
94
|
+
end
|
95
|
+
|
96
|
+
assigned
|
97
|
+
elsif assignable_roles.kind_of?(Array)
|
98
|
+
assignable_roles
|
99
|
+
end.uniq
|
100
|
+
|
113
101
|
# Check boxes
|
114
102
|
multiple = resource.acts_as_role_restricted_options[:multiple] if multiple.nil?
|
115
|
-
return
|
103
|
+
return assigned_roles if multiple
|
116
104
|
|
117
105
|
# Radios
|
118
|
-
(resource.roles -
|
106
|
+
(resource.roles - assigned_roles).present? ? [] : assigned_roles
|
119
107
|
end
|
120
108
|
|
121
109
|
def self.assignable_roles_present?(resource)
|
122
|
-
return false
|
110
|
+
return false unless assignable_roles.present?
|
123
111
|
|
124
|
-
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)
|
125
113
|
raise('expected resource to respond to is_role_restricted?') unless resource.respond_to?(:is_role_restricted?)
|
126
114
|
|
127
|
-
return assignable_roles.present? if assignable_roles.kind_of?(Array)
|
128
|
-
|
129
115
|
if assignable_roles.kind_of?(Array)
|
130
116
|
assignable_roles
|
131
117
|
elsif assignable_roles.key?(resource.class.to_s)
|
@@ -135,113 +121,14 @@ module EffectiveRoles
|
|
135
121
|
end.present?
|
136
122
|
end
|
137
123
|
|
138
|
-
# This is used by the effective_roles_summary_table helper method
|
139
|
-
def self.authorization_level(controller, role, resource)
|
140
|
-
return :unknown unless (authorization_method.respond_to?(:call) || authorization_method.kind_of?(Symbol))
|
141
|
-
return :unknown unless (controller.current_user rescue nil).respond_to?(:roles=)
|
142
|
-
|
143
|
-
# Store the current ability (cancan support) and roles
|
144
|
-
current_ability = controller.instance_variable_get(:@current_ability)
|
145
|
-
current_user = controller.instance_variable_get(:@current_user)
|
146
|
-
current_user_roles = controller.current_user.roles
|
147
|
-
|
148
|
-
# Set up the user, so the check is done with the desired permission level
|
149
|
-
controller.instance_variable_set(:@current_ability, nil)
|
150
|
-
|
151
|
-
level = nil
|
152
|
-
|
153
|
-
case role
|
154
|
-
when :signed_in
|
155
|
-
controller.current_user.roles = []
|
156
|
-
when :public
|
157
|
-
controller.instance_variable_set(:@current_user, nil)
|
158
|
-
|
159
|
-
if defined?(EffectiveLogging)
|
160
|
-
EffectiveLogging.supressed { (controller.request.env['warden'].set_user(false) rescue nil) }
|
161
|
-
else
|
162
|
-
(controller.request.env['warden'].set_user(false) rescue nil)
|
163
|
-
end
|
164
|
-
else
|
165
|
-
controller.current_user.roles = [role]
|
166
|
-
end
|
167
|
-
|
168
|
-
# Find the actual authorization level
|
169
|
-
level = _authorization_level(controller, role, resource, authorization_method)
|
170
|
-
|
171
|
-
# Restore the existing current_user stuff
|
172
|
-
if role == :public
|
173
|
-
ActiveRecord::Base.transaction do
|
174
|
-
if defined?(EffectiveLogging)
|
175
|
-
EffectiveLogging.supressed { (controller.request.env['warden'].set_user(current_user) rescue nil) }
|
176
|
-
else
|
177
|
-
(controller.request.env['warden'].set_user(current_user) rescue nil)
|
178
|
-
end
|
179
|
-
|
180
|
-
raise ActiveRecord::Rollback
|
181
|
-
end
|
182
|
-
end
|
183
|
-
|
184
|
-
controller.instance_variable_set(:@current_ability, current_ability)
|
185
|
-
controller.instance_variable_set(:@current_user, current_user)
|
186
|
-
controller.current_user.roles = current_user_roles
|
187
|
-
|
188
|
-
level
|
189
|
-
end
|
190
|
-
|
191
124
|
private
|
192
125
|
|
193
126
|
def self.role_description(role, obj = nil)
|
194
127
|
raise 'EffectiveRoles config.role_descriptions must be a Hash' unless role_descriptions.kind_of?(Hash)
|
195
|
-
(role_descriptions[obj.try(:class).to_s] || {})[role] || role_descriptions[role] || ''
|
196
|
-
end
|
197
|
-
|
198
|
-
def self._authorization_level(controller, role, resource, auth_method)
|
199
|
-
resource = (resource.new() rescue resource) if resource.kind_of?(ActiveRecord::Base)
|
200
|
-
|
201
|
-
# Custom actions
|
202
|
-
if resource.kind_of?(Hash)
|
203
|
-
resource.each do |key, value|
|
204
|
-
return (controller.instance_exec(controller, key, value, &auth_method) rescue false) ? :yes : :no
|
205
|
-
end
|
206
|
-
end
|
207
|
-
|
208
|
-
# Check for Manage
|
209
|
-
return :manage if (
|
210
|
-
(controller.instance_exec(controller, :create, resource, &auth_method) rescue false) &&
|
211
|
-
(controller.instance_exec(controller, :update, resource, &auth_method) rescue false) &&
|
212
|
-
(controller.instance_exec(controller, :show, resource, &auth_method) rescue false) &&
|
213
|
-
(controller.instance_exec(controller, :destroy, resource, &auth_method) rescue false)
|
214
|
-
)
|
215
|
-
|
216
|
-
# Check for Update
|
217
|
-
return :update if (controller.instance_exec(controller, :update, resource, &auth_method) rescue false)
|
218
|
-
|
219
|
-
# Check for Update Own
|
220
|
-
if resource.respond_to?('user=')
|
221
|
-
resource.user = controller.current_user
|
222
|
-
return :update_own if (controller.instance_exec(controller, :update, resource, &auth_method) rescue false)
|
223
|
-
resource.user = nil
|
224
|
-
elsif resource.respond_to?('user_id=')
|
225
|
-
resource.user_id = controller.current_user.id
|
226
|
-
return :update_own if (controller.instance_exec(controller, :update, resource, &auth_method) rescue false)
|
227
|
-
resource.user_id = nil
|
228
|
-
elsif resource.kind_of?(User)
|
229
|
-
return :update_own if (controller.instance_exec(controller, :update, controller.current_user, &auth_method) rescue false)
|
230
|
-
end
|
231
|
-
|
232
|
-
# Check for Create
|
233
|
-
return :create if (controller.instance_exec(controller, :create, resource, &auth_method) rescue false)
|
234
|
-
|
235
|
-
# Check for Show
|
236
|
-
return :show if (controller.instance_exec(controller, :show, resource, &auth_method) rescue false)
|
237
|
-
|
238
|
-
# Check for Index
|
239
|
-
return :index if (controller.instance_exec(controller, :index, resource, &auth_method) rescue false)
|
240
|
-
|
241
|
-
# Check for Destroy
|
242
|
-
return :destroy if (controller.instance_exec(controller, :destroy, resource, &auth_method) rescue false)
|
243
128
|
|
244
|
-
|
129
|
+
description = role_descriptions.dig(obj.class.to_s, role) if obj.present?
|
130
|
+
description ||= role_descriptions[role]
|
131
|
+
description || ''
|
245
132
|
end
|
246
133
|
|
247
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.
|
4
|
+
version: 2.1.1
|
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-02-18 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
|
-
|