effective_roles 1.3.7 → 1.4.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/README.md +0 -3
- data/app/controllers/admin/roles_controller.rb +2 -2
- data/app/helpers/effective_roles_helper.rb +5 -5
- data/app/models/effective/access_denied.rb +17 -0
- data/app/views/admin/roles/index.html.haml +6 -1
- data/app/views/effective/roles/_summary_table.html.haml +5 -1
- data/{lib/generators/templates → config}/effective_roles.rb +24 -6
- data/lib/effective_roles.rb +22 -9
- data/lib/effective_roles/engine.rb +1 -1
- data/lib/effective_roles/version.rb +1 -1
- data/lib/generators/effective_roles/install_generator.rb +3 -7
- metadata +4 -4
- data/lib/generators/templates/README +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a5e585f998b2061367570bc40dbff8085f877a6
|
4
|
+
data.tar.gz: a0fab03af238f58c876de0f477d14692391ea8ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: df1780092055c2617a963a0949999fe988a5aa296f83f6e6ac1ee121d181c7924ac773f8dcc608b1fad274a1181f9b8b00f5b218e1c134cdd4529af656c14807
|
7
|
+
data.tar.gz: 50572e287aae3987ddb2142a7044993f5d87edf80e37ad753145f75210bb81f18d91eb6c5477678dc86735589a52737e69c728291f36900389219f66f7d1b9db
|
data/README.md
CHANGED
@@ -298,9 +298,6 @@ All roles are get/set through the roles and roles= methods.
|
|
298
298
|
|
299
299
|
MIT License. Copyright [Code and Effect Inc.](http://www.codeandeffect.com/)
|
300
300
|
|
301
|
-
Code and Effect is the product arm of [AgileStyle](http://www.agilestyle.com/), an Edmonton-based shop that specializes in building custom web applications with Ruby on Rails.
|
302
|
-
|
303
|
-
|
304
301
|
## Credits
|
305
302
|
|
306
303
|
This model implements the https://github.com/ryanb/cancan/wiki/Role-Based-Authorization multi role based authorization based on the roles_mask field
|
@@ -1,13 +1,13 @@
|
|
1
1
|
module Admin
|
2
2
|
class RolesController < ApplicationController
|
3
|
-
|
3
|
+
respond_to?(:before_action) ? before_action(:authenticate_user!) : before_filter(:authenticate_user!) # Devise
|
4
4
|
|
5
5
|
layout (EffectiveRoles.layout.kind_of?(Hash) ? EffectiveRoles.layout[:admin_roles] : EffectiveRoles.layout)
|
6
6
|
|
7
7
|
def index
|
8
8
|
@page_title = 'Roles'
|
9
9
|
|
10
|
-
|
10
|
+
EffectiveRoles.authorized?(self, :admin, :effective_roles)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
end
|
@@ -20,7 +20,7 @@ module EffectiveRolesHelper
|
|
20
20
|
raise 'Expected argument to be a Hash' unless opts.kind_of?(Hash)
|
21
21
|
|
22
22
|
roles = Array(opts[:roles]).presence
|
23
|
-
roles ||=
|
23
|
+
roles ||= [:public, :signed_in] + EffectiveRoles.roles
|
24
24
|
|
25
25
|
if opts[:only].present?
|
26
26
|
klasses = Array(opts[:only])
|
@@ -68,7 +68,7 @@ module EffectiveRolesHelper
|
|
68
68
|
def effective_roles_authorization_badge(level)
|
69
69
|
case level
|
70
70
|
when :manage
|
71
|
-
content_tag(:span, 'Full', class: 'label label-
|
71
|
+
content_tag(:span, 'Full', class: 'label label-primary')
|
72
72
|
when :update
|
73
73
|
content_tag(:span, 'Edit', class: 'label label-success')
|
74
74
|
when :update_own
|
@@ -84,13 +84,13 @@ module EffectiveRolesHelper
|
|
84
84
|
when :none
|
85
85
|
content_tag(:span, 'No Access', class: 'label label-danger')
|
86
86
|
when :yes
|
87
|
-
content_tag(:span, 'Yes', class: 'label label-
|
87
|
+
content_tag(:span, 'Yes', class: 'label label-primary')
|
88
88
|
when :no
|
89
89
|
content_tag(:span, 'No', class: 'label label-danger')
|
90
90
|
when :unknown
|
91
91
|
content_tag(:span, 'Unknown', class: 'label')
|
92
92
|
else
|
93
|
-
content_tag(:span, level.to_s.titleize, class: 'label label-
|
93
|
+
content_tag(:span, level.to_s.titleize, class: 'label label-info')
|
94
94
|
end
|
95
95
|
end
|
96
96
|
|
@@ -101,7 +101,7 @@ module EffectiveRolesHelper
|
|
101
101
|
klass = klass.keys.first if klass.kind_of?(Hash)
|
102
102
|
label = (klass.respond_to?(:name) ? klass.name : klass.to_s)
|
103
103
|
|
104
|
-
['Effective::Datatables::'
|
104
|
+
['Effective::Datatables::'].each do |replace|
|
105
105
|
label = label.sub(replace, '')
|
106
106
|
end
|
107
107
|
|
@@ -0,0 +1,17 @@
|
|
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,3 +1,8 @@
|
|
1
|
-
%
|
1
|
+
%h1.effective-heading= @page_title
|
2
|
+
|
3
|
+
- if defined?(CanCanCan)
|
4
|
+
%p The following roles are computed based on the ability.rb file.
|
5
|
+
- else
|
6
|
+
%p The following roles are computed based on the configured authorization_method.
|
2
7
|
|
3
8
|
= effective_roles_summary_table
|
@@ -80,17 +80,35 @@ EffectiveRoles.setup do |config|
|
|
80
80
|
# }
|
81
81
|
|
82
82
|
|
83
|
-
#
|
84
|
-
#
|
85
|
-
#
|
83
|
+
# Authorization Method
|
84
|
+
#
|
85
|
+
# This doesn't have anything to do with the roles themselves.
|
86
|
+
# It's only used in two places:
|
87
|
+
# - For the effective_roles_summary_table() helper method
|
88
|
+
# - The /admin/roles page check
|
86
89
|
#
|
87
90
|
# It should match the authorization check used by your application
|
88
91
|
#
|
89
|
-
#
|
90
|
-
|
92
|
+
# This method is called by all controller actions with the appropriate action and resource
|
93
|
+
# If the method returns false, an Effective::AccessDenied Error will be raised (see README.md for complete info)
|
94
|
+
#
|
95
|
+
# Use via Proc (and with CanCan):
|
96
|
+
# config.authorization_method = Proc.new { |controller, action, resource| can?(action, resource) }
|
97
|
+
#
|
98
|
+
# Use via custom method:
|
99
|
+
# config.authorization_method = :my_authorization_method
|
100
|
+
#
|
101
|
+
# And then in your application_controller.rb:
|
102
|
+
#
|
103
|
+
# def my_authorization_method(action, resource)
|
104
|
+
# current_user.is?(:admin)
|
105
|
+
# end
|
106
|
+
#
|
107
|
+
# Or disable the check completely:
|
108
|
+
# config.authorization_method = false
|
109
|
+
config.authorization_method = Proc.new { |controller, action, resource| authorize!(action, resource) } # CanCanCan
|
91
110
|
|
92
111
|
# Layout Settings
|
93
112
|
# Configure the Layout per controller, or all at once
|
94
113
|
config.layout = 'application'
|
95
|
-
|
96
114
|
end
|
data/lib/effective_roles.rb
CHANGED
@@ -10,12 +10,19 @@ module EffectiveRoles
|
|
10
10
|
mattr_accessor :assignable_roles
|
11
11
|
mattr_accessor :disabled_roles
|
12
12
|
|
13
|
-
mattr_accessor :
|
13
|
+
mattr_accessor :authorization_method
|
14
14
|
|
15
15
|
def self.setup
|
16
16
|
yield self
|
17
17
|
end
|
18
18
|
|
19
|
+
def self.authorized?(controller, action, resource)
|
20
|
+
if authorization_method.respond_to?(:call) || authorization_method.kind_of?(Symbol)
|
21
|
+
raise Effective::AccessDenied.new() unless (controller || self).instance_exec(controller, action, resource, &authorization_method)
|
22
|
+
end
|
23
|
+
true
|
24
|
+
end
|
25
|
+
|
19
26
|
# This method converts whatever is given into its roles
|
20
27
|
# Pass an object, Integer, or Symbol to find corresponding role
|
21
28
|
def self.roles_for(obj)
|
@@ -61,7 +68,7 @@ module EffectiveRoles
|
|
61
68
|
|
62
69
|
# This is used by the effective_roles_summary_table helper method
|
63
70
|
def self.authorization_level(controller, role, resource)
|
64
|
-
return :unknown unless (
|
71
|
+
return :unknown unless (authorization_method.respond_to?(:call) || authorization_method.kind_of?(Symbol))
|
65
72
|
return :unknown unless (controller.current_user rescue nil).respond_to?(:roles=)
|
66
73
|
|
67
74
|
# Store the current ability (cancan support) and roles
|
@@ -72,10 +79,12 @@ module EffectiveRoles
|
|
72
79
|
# Set up the user, so the check is done with the desired permission level
|
73
80
|
controller.instance_variable_set(:@current_ability, nil)
|
74
81
|
|
82
|
+
level = nil
|
83
|
+
|
75
84
|
case role
|
76
85
|
when :signed_in
|
77
86
|
controller.current_user.roles = []
|
78
|
-
when :
|
87
|
+
when :public
|
79
88
|
controller.instance_variable_set(:@current_user, nil)
|
80
89
|
|
81
90
|
if defined?(EffectiveLogging) && EffectiveLogging.respond_to?(:supressed?)
|
@@ -88,14 +97,18 @@ module EffectiveRoles
|
|
88
97
|
end
|
89
98
|
|
90
99
|
# Find the actual authorization level
|
91
|
-
level = _authorization_level(controller, role, resource,
|
100
|
+
level = _authorization_level(controller, role, resource, authorization_method)
|
92
101
|
|
93
102
|
# Restore the existing current_user stuff
|
94
|
-
if role == :
|
95
|
-
|
96
|
-
EffectiveLogging
|
97
|
-
|
98
|
-
|
103
|
+
if role == :public
|
104
|
+
ActiveRecord::Base.transaction do
|
105
|
+
if defined?(EffectiveLogging) && EffectiveLogging.respond_to?(:supressed?)
|
106
|
+
EffectiveLogging.supressed { (controller.request.env['warden'].set_user(current_user) rescue nil) }
|
107
|
+
else
|
108
|
+
(controller.request.env['warden'].set_user(current_user) rescue nil)
|
109
|
+
end
|
110
|
+
|
111
|
+
raise ActiveRecord::Rollback
|
99
112
|
end
|
100
113
|
end
|
101
114
|
|
@@ -20,7 +20,7 @@ module EffectiveRoles
|
|
20
20
|
|
21
21
|
# Set up our default configuration options.
|
22
22
|
initializer "effective_roles.defaults", :before => :load_config_initializers do |app|
|
23
|
-
eval File.read("#{config.root}/
|
23
|
+
eval File.read("#{config.root}/config/effective_roles.rb")
|
24
24
|
end
|
25
25
|
|
26
26
|
end
|
@@ -1,16 +1,12 @@
|
|
1
1
|
module EffectiveRoles
|
2
2
|
module Generators
|
3
3
|
class InstallGenerator < Rails::Generators::Base
|
4
|
-
desc
|
4
|
+
desc 'Creates an EffectiveRoles initializer in your application.'
|
5
5
|
|
6
|
-
source_root File.expand_path(
|
6
|
+
source_root File.expand_path('../../templates', __FILE__)
|
7
7
|
|
8
8
|
def copy_initializer
|
9
|
-
template
|
10
|
-
end
|
11
|
-
|
12
|
-
def show_readme
|
13
|
-
readme "README" if behavior == :invoke
|
9
|
+
template ('../' * 3) + 'config/effective_roles.rb', 'config/initializers/effective_roles.rb'
|
14
10
|
end
|
15
11
|
end
|
16
12
|
end
|
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: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Code and Effect
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-12-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -165,16 +165,16 @@ files:
|
|
165
165
|
- app/controllers/admin/roles_controller.rb
|
166
166
|
- app/helpers/effective_roles_helper.rb
|
167
167
|
- app/models/concerns/acts_as_role_restricted.rb
|
168
|
+
- app/models/effective/access_denied.rb
|
168
169
|
- app/views/admin/roles/index.html.haml
|
169
170
|
- app/views/effective/roles/_roles_fields.html.haml
|
170
171
|
- app/views/effective/roles/_summary_table.html.haml
|
172
|
+
- config/effective_roles.rb
|
171
173
|
- config/routes.rb
|
172
174
|
- lib/effective_roles.rb
|
173
175
|
- lib/effective_roles/engine.rb
|
174
176
|
- lib/effective_roles/version.rb
|
175
177
|
- lib/generators/effective_roles/install_generator.rb
|
176
|
-
- lib/generators/templates/README
|
177
|
-
- lib/generators/templates/effective_roles.rb
|
178
178
|
- lib/tasks/effective_roles_tasks.rake
|
179
179
|
- spec/dummy/README.rdoc
|
180
180
|
- spec/dummy/Rakefile
|
@@ -1 +0,0 @@
|
|
1
|
-
Thanks for using EffectiveRoles
|