effective_roles 2.1.0 → 2.1.1

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
  SHA256:
3
- metadata.gz: 7f392991013eeab38fcc7b6092c1014078fc2a356833bd5033b9a268987dc512
4
- data.tar.gz: 38bb573be4e391b9cfd6141713bd89d29914e46148705588f1859642a5269bed
3
+ metadata.gz: 2839a8a58a284f7b6f9f7b12a3d5a0eff69b9b60c1f99ef68d364acab5257355
4
+ data.tar.gz: 69856eb8d9e3296a26386d28560c36c238cbfc7d407168f2614f13f48fc9b9eb
5
5
  SHA512:
6
- metadata.gz: ef6d332b9c6d93d8e28b61e856c93ec20e8c731b2fd02678fd1dcd8e52c2bcd12e1f5b68c891d82a265054065fe5694cc2bd59baa72ddaf2bb4553af28a93739
7
- data.tar.gz: e7f10cde194b45115445a09bd99d0da254c81dd611c97cb9f1c25e8cd33181941449aaa20eb45b5b24d4bea0af8397720cf1156865ef0bf217b1798bf103225a
6
+ metadata.gz: e7c8c3befff4c7f7c51290f39f8f14a70ce50cca7448be66869875bca53422d23b73ef5b558b58ebc046b66f398d356d2fa3125bc595b9ecad737eb16a06d41d
7
+ data.tar.gz: c504136f2a6593dd272ce0208d72f26fb77a491e846c91b24ed5806a60aa4ffbea942ecb09e19e463166bbe0835e21071b7ac9493aecda527a479dc26c68c054
@@ -5,7 +5,7 @@ module Admin
5
5
 
6
6
  include Effective::CrudController
7
7
 
8
- if (config = EffectiveRoles.config.layout)
8
+ if (config = EffectiveRoles.layout)
9
9
  layout(config.kind_of?(Hash) ? config[:admin] : config)
10
10
  end
11
11
 
@@ -2,7 +2,7 @@ module EffectiveRolesHelper
2
2
  def effective_roles_summary(obj, options = {}) # User or a Post, any acts_as_roleable
3
3
  raise 'expected an acts_as_roleable object' unless obj.respond_to?(:roles)
4
4
 
5
- descriptions = EffectiveRoles.config.role_descriptions[obj.class.name] || EffectiveRoles.config.role_descriptions || {}
5
+ descriptions = EffectiveRoles.role_descriptions[obj.class.name] || EffectiveRoles.role_descriptions || {}
6
6
  opts = { obj: obj, roles: obj.roles, descriptions: descriptions }.merge(options)
7
7
 
8
8
  render partial: 'effective/roles/summary', locals: opts
@@ -17,7 +17,7 @@ module EffectiveRolesHelper
17
17
  raise 'Expected argument to be a Hash' unless opts.kind_of?(Hash)
18
18
 
19
19
  roles = Array(opts[:roles]).presence
20
- roles ||= [:public, :signed_in] + EffectiveRoles.config.roles
20
+ roles ||= [:public, :signed_in] + EffectiveRoles.roles
21
21
 
22
22
  if opts[:only].present?
23
23
  klasses = Array(opts[:only])
@@ -103,7 +103,7 @@ module EffectiveRolesHelper
103
103
 
104
104
  # This is used by the effective_roles_summary_table helper method
105
105
  def effective_roles_authorization_level(controller, role, resource)
106
- authorization_method = EffectiveRoles.config.authorization_method
106
+ authorization_method = EffectiveResources.authorization_method
107
107
 
108
108
  raise('expected an authorization method') unless (authorization_method.respond_to?(:call) || authorization_method.kind_of?(Symbol))
109
109
  return :unknown unless (controller.current_user rescue nil).respond_to?(:roles=)
@@ -71,18 +71,18 @@ module ActsAsRoleRestricted
71
71
  def with_role_sql(*roles)
72
72
  roles = roles.flatten.compact
73
73
  roles = roles.first.roles if roles.length == 1 && roles.first.respond_to?(:roles)
74
- roles = (roles.map { |role| role.to_sym } & EffectiveRoles.config.roles)
74
+ roles = (roles.map { |role| role.to_sym } & EffectiveRoles.roles)
75
75
 
76
- roles.map { |role| "(#{self.table_name}.roles_mask & %d > 0)" % 2**EffectiveRoles.config.roles.index(role) }.join(' OR ')
76
+ roles.map { |role| "(#{self.table_name}.roles_mask & %d > 0)" % 2**EffectiveRoles.roles.index(role) }.join(' OR ')
77
77
  end
78
78
 
79
79
  def without_role(*roles)
80
80
  roles = roles.flatten.compact
81
81
  roles = roles.first.roles if roles.length == 1 && roles.first.respond_to?(:roles)
82
- roles = (roles.map { |role| role.to_sym } & EffectiveRoles.config.roles)
82
+ roles = (roles.map { |role| role.to_sym } & EffectiveRoles.roles)
83
83
 
84
84
  where(
85
- roles.map { |role| "NOT(#{self.table_name}.roles_mask & %d > 0)" % 2**EffectiveRoles.config.roles.index(role) }.join(' AND ')
85
+ roles.map { |role| "NOT(#{self.table_name}.roles_mask & %d > 0)" % 2**EffectiveRoles.roles.index(role) }.join(' AND ')
86
86
  ).or(where(roles_mask: nil))
87
87
  end
88
88
  end
@@ -3,25 +3,12 @@ require 'effective_roles/engine'
3
3
  require 'effective_roles/version'
4
4
 
5
5
  module EffectiveRoles
6
- # mattr_accessor :roles
7
- # mattr_accessor :role_descriptions
8
- # mattr_accessor :assignable_roles
9
- # mattr_accessor :layout
10
6
 
11
- def self.config(namespace = nil)
12
- @config ||= ActiveSupport::OrderedOptions.new
13
- namespace ||= Tenant.current if defined?(Tenant)
14
-
15
- if namespace
16
- @config[namespace] ||= ActiveSupport::OrderedOptions.new
17
- else
18
- @config
19
- end
7
+ def self.config_keys
8
+ [:roles, :role_descriptions, :assignable_roles, :layout]
20
9
  end
21
10
 
22
- def self.setup(namespace = nil, &block)
23
- yield(config(namespace))
24
- end
11
+ include EffectiveGem
25
12
 
26
13
  def self.permitted_params
27
14
  { roles: [] }
@@ -33,11 +20,11 @@ module EffectiveRoles
33
20
  if obj.respond_to?(:is_role_restricted?)
34
21
  obj.roles
35
22
  elsif obj.kind_of?(Integer)
36
- config.roles.reject { |r| (obj & 2 ** config.roles.index(r)).zero? }
23
+ roles.reject { |r| (obj & 2 ** config.roles.index(r)).zero? }
37
24
  elsif obj.kind_of?(Symbol)
38
- Array(config.roles.find { |role| role == obj })
25
+ Array(roles.find { |role| role == obj })
39
26
  elsif obj.kind_of?(String)
40
- Array(config.roles.find { |role| role == obj.to_sym })
27
+ Array(roles.find { |role| role == obj.to_sym })
41
28
  elsif obj.kind_of?(Array)
42
29
  obj.map { |obj| roles_for(obj) }.flatten.compact
43
30
  elsif obj.nil?
@@ -53,7 +40,7 @@ module EffectiveRoles
53
40
  end
54
41
 
55
42
  def self.roles_collection(resource, current_user = nil, only: nil, except: nil, multiple: nil)
56
- if config.assignable_roles.present?
43
+ if assignable_roles.present?
57
44
  raise('expected object to respond to is_role_restricted?') unless resource.respond_to?(:is_role_restricted?)
58
45
  raise('expected current_user to respond to is_role_restricted?') if current_user && !current_user.respond_to?(:is_role_restricted?)
59
46
  end
@@ -63,7 +50,7 @@ module EffectiveRoles
63
50
  multiple = resource.acts_as_role_restricted_options[:multiple] if multiple.nil?
64
51
  assignable = assignable_roles_collection(resource, current_user, multiple: multiple)
65
52
 
66
- config.roles.map do |role|
53
+ roles.map do |role|
67
54
  next if only.present? && !only.include?(role)
68
55
  next if except.present? && except.include?(role)
69
56
 
@@ -76,7 +63,7 @@ module EffectiveRoles
76
63
  end
77
64
 
78
65
  def self.assignable_roles_collection(resource, current_user = nil, multiple: nil)
79
- return config.roles unless assignable_roles_present?(resource)
66
+ return roles unless assignable_roles_present?(resource)
80
67
 
81
68
  if current_user && !current_user.respond_to?(:is_role_restricted?)
82
69
  raise('expected current_user to respond to is_role_restricted?')
@@ -86,8 +73,8 @@ module EffectiveRoles
86
73
  raise('expected current_user to respond to is_role_restricted?')
87
74
  end
88
75
 
89
- assigned_roles = if config.assignable_roles.kind_of?(Hash)
90
- assignable = (config.assignable_roles[resource.class.to_s] || config.assignable_roles || {})
76
+ assigned_roles = if assignable_roles.kind_of?(Hash)
77
+ assignable = (assignable_roles[resource.class.to_s] || assignable_roles || {})
91
78
  assigned = [] # our return value
92
79
 
93
80
  if current_user.blank?
@@ -107,8 +94,8 @@ module EffectiveRoles
107
94
  end
108
95
 
109
96
  assigned
110
- elsif config.assignable_roles.kind_of?(Array)
111
- config.assignable_roles
97
+ elsif assignable_roles.kind_of?(Array)
98
+ assignable_roles
112
99
  end.uniq
113
100
 
114
101
  # Check boxes
@@ -120,27 +107,27 @@ module EffectiveRoles
120
107
  end
121
108
 
122
109
  def self.assignable_roles_present?(resource)
123
- return false unless config.assignable_roles.present?
110
+ return false unless assignable_roles.present?
124
111
 
125
- raise 'EffectiveRoles config.assignable_roles_for must be a Hash or Array' unless [Hash, Array].include?(config.assignable_roles.class)
112
+ raise 'EffectiveRoles config.assignable_roles_for must be a Hash or Array' unless [Hash, Array].include?(assignable_roles.class)
126
113
  raise('expected resource to respond to is_role_restricted?') unless resource.respond_to?(:is_role_restricted?)
127
114
 
128
- if config.assignable_roles.kind_of?(Array)
129
- config.assignable_roles
130
- elsif config.assignable_roles.key?(resource.class.to_s)
131
- config.assignable_roles[resource.class.to_s]
115
+ if assignable_roles.kind_of?(Array)
116
+ assignable_roles
117
+ elsif assignable_roles.key?(resource.class.to_s)
118
+ assignable_roles[resource.class.to_s]
132
119
  else
133
- config.assignable_roles
120
+ assignable_roles
134
121
  end.present?
135
122
  end
136
123
 
137
124
  private
138
125
 
139
126
  def self.role_description(role, obj = nil)
140
- raise 'EffectiveRoles config.role_descriptions must be a Hash' unless config.role_descriptions.kind_of?(Hash)
127
+ raise 'EffectiveRoles config.role_descriptions must be a Hash' unless role_descriptions.kind_of?(Hash)
141
128
 
142
- description = config.role_descriptions.dig(obj.class.to_s, role) if obj.present?
143
- description ||= config.role_descriptions[role]
129
+ description = role_descriptions.dig(obj.class.to_s, role) if obj.present?
130
+ description ||= role_descriptions[role]
144
131
  description || ''
145
132
  end
146
133
 
@@ -1,3 +1,3 @@
1
1
  module EffectiveRoles
2
- VERSION = '2.1.0'.freeze
2
+ VERSION = '2.1.1'.freeze
3
3
  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: 2.1.0
4
+ version: 2.1.1
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: 2021-02-17 00:00:00.000000000 Z
11
+ date: 2021-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails