effective_roles 1.3.7 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d8341f843f44965055d3383dfe315f189492c335
4
- data.tar.gz: ad687a36c11392260ae5fcc29797e9b78de69604
3
+ metadata.gz: 8a5e585f998b2061367570bc40dbff8085f877a6
4
+ data.tar.gz: a0fab03af238f58c876de0f477d14692391ea8ec
5
5
  SHA512:
6
- metadata.gz: 27879df95d52bde6c48ab2625c3aeceeb7979d59aa5af59efe0633166532427b535fbb9999c47c903089a4609a3a8c1b24266f20524d0c496ec1773ac8407663
7
- data.tar.gz: 5d3688519ff88bd21223cfe026e4628761cd4d886a84da7158c95bb005b4fc63f000d536c5b382266104b43e6d3ddc709f518c00fc51fbf37c1ea5b3b2154402
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
- before_filter :authenticate_user! # This is devise, ensure we're logged in.
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
- EffectiveOrders.authorized?(self, :admin, :effective_roles)
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 ||= EffectiveRoles.roles + [:signed_in, :signed_out]
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-success')
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-success')
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-success')
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::', 'Effective::'].each do |replace|
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
- %h2= @page_title
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
@@ -2,7 +2,11 @@
2
2
  %thead
3
3
  %th
4
4
  - roles.each do |role|
5
- %th.text-center= role
5
+ %th.text-center
6
+ - if [:public, :signed_in].include?(role)
7
+ = role.to_s.titleize
8
+ - else
9
+ = ":#{role}"
6
10
  %tbody
7
11
  - klasses.each do |klass|
8
12
  %tr
@@ -80,17 +80,35 @@ EffectiveRoles.setup do |config|
80
80
  # }
81
81
 
82
82
 
83
- # config.authorization_method_for_summary_table
84
- # This has absolutely no affect on the any logic involving roles
85
- # It's purely for the effective_roles_summary_table() helper method
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
- # Use CanCan: can?(action, resource)
90
- config.authorization_method_for_summary_table = Proc.new { |controller, action, resource| true }
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
@@ -10,12 +10,19 @@ module EffectiveRoles
10
10
  mattr_accessor :assignable_roles
11
11
  mattr_accessor :disabled_roles
12
12
 
13
- mattr_accessor :authorization_method_for_summary_table
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 (authorization_method_for_summary_table.respond_to?(:call) || authorization_method_for_summary_table.kind_of?(Symbol))
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 :signed_out
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, authorization_method_for_summary_table)
100
+ level = _authorization_level(controller, role, resource, authorization_method)
92
101
 
93
102
  # Restore the existing current_user stuff
94
- if role == :signed_out
95
- if defined?(EffectiveLogging) && EffectiveLogging.respond_to?(:supressed?)
96
- EffectiveLogging.supressed { (controller.request.env['warden'].set_user(current_user) rescue nil) }
97
- else
98
- (controller.request.env['warden'].set_user(current_user) rescue nil)
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}/lib/generators/templates/effective_roles.rb")
23
+ eval File.read("#{config.root}/config/effective_roles.rb")
24
24
  end
25
25
 
26
26
  end
@@ -1,3 +1,3 @@
1
1
  module EffectiveRoles
2
- VERSION = '1.3.7'.freeze
2
+ VERSION = '1.4.0'.freeze
3
3
  end
@@ -1,16 +1,12 @@
1
1
  module EffectiveRoles
2
2
  module Generators
3
3
  class InstallGenerator < Rails::Generators::Base
4
- desc "Creates an EffectiveRoles initializer in your application."
4
+ desc 'Creates an EffectiveRoles initializer in your application.'
5
5
 
6
- source_root File.expand_path("../../templates", __FILE__)
6
+ source_root File.expand_path('../../templates', __FILE__)
7
7
 
8
8
  def copy_initializer
9
- template "effective_roles.rb", "config/initializers/effective_roles.rb"
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.3.7
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-03-08 00:00:00.000000000 Z
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