right_on 0.3.0 → 0.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 +5 -5
- data/.hound.yml +2 -0
- data/.rubocop.yml +5 -0
- data/.travis.yml +5 -2
- data/CHANGELOG.md +13 -0
- data/db/migration.rb +6 -6
- data/lib/right_on.rb +8 -1
- data/lib/right_on/ability.rb +9 -0
- data/lib/right_on/by_group.rb +13 -10
- data/lib/right_on/controller_additions.rb +52 -0
- data/lib/right_on/error.rb +3 -0
- data/lib/right_on/permission_denied_response.rb +2 -1
- data/lib/right_on/rails.rb +6 -1
- data/lib/right_on/right.rb +2 -94
- data/lib/right_on/right_allowed.rb +71 -0
- data/lib/right_on/role_model.rb +7 -10
- data/lib/right_on/rule.rb +51 -0
- data/lib/right_on/version.rb +1 -1
- data/right_on.gemspec +4 -2
- data/spec/ability_spec.rb +29 -0
- data/spec/by_group_spec.rb +22 -0
- data/spec/controller_additions_spec.rb +134 -0
- data/spec/{permission_defnied_spec.rb → permission_denied_response_spec.rb} +8 -8
- data/spec/right_allowed_spec.rb +89 -0
- data/spec/right_on_spec.rb +35 -123
- data/spec/role_model_spec.rb +5 -0
- data/spec/rule_spec.rb +81 -0
- data/spec/spec_helper.rb +1 -3
- data/spec/support/bootstrap.rb +19 -9
- data/spec/support/coverage_loader.rb +1 -1
- metadata +54 -13
- data/gemfiles/rails3.gemfile +0 -7
- data/lib/right_on/action_controller_extensions.rb +0 -68
- data/spec/action_controller_extensions_spec.rb +0 -34
data/spec/role_model_spec.rb
CHANGED
@@ -25,4 +25,9 @@ describe RightOn::RoleModel do
|
|
25
25
|
expect(admin.has_privileges_of?(basic_user)).to be true
|
26
26
|
expect(basic_user.has_privileges_of?(admin)).to be false
|
27
27
|
end
|
28
|
+
|
29
|
+
it 'links back to users' do
|
30
|
+
admin # load admin
|
31
|
+
expect(admin_role.users.size).to eq 1
|
32
|
+
end
|
28
33
|
end
|
data/spec/rule_spec.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'active_support/all'
|
3
|
+
require 'cancan/rule'
|
4
|
+
require 'right_on/error'
|
5
|
+
require 'right_on/rule'
|
6
|
+
require 'spec_helper'
|
7
|
+
|
8
|
+
describe RightOn::Rule do
|
9
|
+
subject(:rule) { RightOn::Rule.rule_for(right) }
|
10
|
+
|
11
|
+
describe '#self.rule_for' do
|
12
|
+
let(:right) {
|
13
|
+
double(name: 'Do Something', can: true, action: 'action', subject: 'subject', conditions: {})
|
14
|
+
}
|
15
|
+
|
16
|
+
it 'should return a cancan rule' do
|
17
|
+
is_expected.to be_a(CanCan::Rule)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '#call' do
|
22
|
+
context 'when an action is not specified' do
|
23
|
+
let(:right) {
|
24
|
+
double(name: 'Do Something', can: true, action: nil, subject: 'subject', conditions: {})
|
25
|
+
}
|
26
|
+
|
27
|
+
it 'should fail with exception' do
|
28
|
+
expect{rule}.to raise_error(RightOn::Error, 'must specify an action')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'when the subject is not a model' do
|
33
|
+
let(:right) {
|
34
|
+
double(name: 'Do Something', can: true, action: 'action', subject: 'subject', conditions: {})
|
35
|
+
}
|
36
|
+
|
37
|
+
it 'should return a CanCan::Rule' do
|
38
|
+
is_expected.to be_a(CanCan::Rule)
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should convert the action to a symbol' do
|
42
|
+
expect(rule.actions).to eq([:action])
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should set the subject' do
|
46
|
+
expect(rule.subjects).to eq(['subject'])
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should not have any conditions' do
|
50
|
+
expect(rule.conditions).to eq({})
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context 'when the subject is a model' do
|
55
|
+
let(:right) {
|
56
|
+
double(name: 'Do Something', can: true, action: 'action', subject: 'Model', conditions: {})
|
57
|
+
}
|
58
|
+
|
59
|
+
before do
|
60
|
+
class Model < ActiveRecord::Base
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'should return a CanCan::Rule' do
|
65
|
+
is_expected.to be_a(CanCan::Rule)
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'should convert the action to a symbol' do
|
69
|
+
expect(rule.actions).to eq([:action])
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'should convert the subject to a model' do
|
73
|
+
expect(rule.subjects).to eq([Model])
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'should not have any conditions' do
|
77
|
+
expect(rule.conditions).to eq({})
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -19,7 +19,7 @@ RSpec.configure do |config|
|
|
19
19
|
config.run_all_when_everything_filtered = true
|
20
20
|
config.filter_run :focus
|
21
21
|
config.before :all do
|
22
|
-
RightOn::
|
22
|
+
RightOn::RightAllowed.cache = ActiveSupport::Cache::MemoryStore.new
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
@@ -31,8 +31,6 @@ ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => DB_F
|
|
31
31
|
|
32
32
|
load('spec/schema.rb')
|
33
33
|
|
34
|
-
RightOn::Right.rights_yaml 'db/rights_roles.yml'
|
35
|
-
|
36
34
|
class Model < ActiveRecord::Base
|
37
35
|
end
|
38
36
|
|
data/spec/support/bootstrap.rb
CHANGED
@@ -1,15 +1,25 @@
|
|
1
1
|
class Bootstrap
|
2
|
-
def self.
|
2
|
+
def self.various_rights_with_actions
|
3
3
|
RightOn::Right.delete_all
|
4
|
-
|
5
|
-
|
4
|
+
{
|
5
|
+
users: create_right('users'),
|
6
|
+
models: create_right('models'),
|
7
|
+
models_index: create_right('models#index'),
|
8
|
+
models_change: create_right('models#change'),
|
9
|
+
models_view: create_right('models#view')
|
10
|
+
}
|
11
|
+
end
|
6
12
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
admin_role = RightOn::Role.create!(:title => 'Admin', :rights => [admin_right])
|
13
|
+
def self.create_right(name)
|
14
|
+
RightOn::Right.create!(build_right_attrs(name))
|
15
|
+
end
|
11
16
|
|
12
|
-
|
13
|
-
|
17
|
+
def self.build_right_attrs(name)
|
18
|
+
if name['#']
|
19
|
+
controller, action = name.split('#')
|
20
|
+
{ name: name, controller: controller, action: action }
|
21
|
+
else
|
22
|
+
{ name: name, controller: name }
|
23
|
+
end
|
14
24
|
end
|
15
25
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: right_on
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Noack
|
@@ -9,36 +9,50 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2018-04-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: cancancan
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
14
28
|
- !ruby/object:Gem::Dependency
|
15
29
|
name: activerecord
|
16
30
|
requirement: !ruby/object:Gem::Requirement
|
17
31
|
requirements:
|
18
32
|
- - ">="
|
19
33
|
- !ruby/object:Gem::Version
|
20
|
-
version:
|
34
|
+
version: 4.0.0
|
21
35
|
type: :runtime
|
22
36
|
prerelease: false
|
23
37
|
version_requirements: !ruby/object:Gem::Requirement
|
24
38
|
requirements:
|
25
39
|
- - ">="
|
26
40
|
- !ruby/object:Gem::Version
|
27
|
-
version:
|
41
|
+
version: 4.0.0
|
28
42
|
- !ruby/object:Gem::Dependency
|
29
43
|
name: activesupport
|
30
44
|
requirement: !ruby/object:Gem::Requirement
|
31
45
|
requirements:
|
32
46
|
- - ">="
|
33
47
|
- !ruby/object:Gem::Version
|
34
|
-
version:
|
48
|
+
version: 4.0.0
|
35
49
|
type: :runtime
|
36
50
|
prerelease: false
|
37
51
|
version_requirements: !ruby/object:Gem::Requirement
|
38
52
|
requirements:
|
39
53
|
- - ">="
|
40
54
|
- !ruby/object:Gem::Version
|
41
|
-
version:
|
55
|
+
version: 4.0.0
|
42
56
|
- !ruby/object:Gem::Dependency
|
43
57
|
name: input_reader
|
44
58
|
requirement: !ruby/object:Gem::Requirement
|
@@ -137,6 +151,20 @@ dependencies:
|
|
137
151
|
- - ">="
|
138
152
|
- !ruby/object:Gem::Version
|
139
153
|
version: '0'
|
154
|
+
- !ruby/object:Gem::Dependency
|
155
|
+
name: rubocop
|
156
|
+
requirement: !ruby/object:Gem::Requirement
|
157
|
+
requirements:
|
158
|
+
- - ">="
|
159
|
+
- !ruby/object:Gem::Version
|
160
|
+
version: '0'
|
161
|
+
type: :development
|
162
|
+
prerelease: false
|
163
|
+
version_requirements: !ruby/object:Gem::Requirement
|
164
|
+
requirements:
|
165
|
+
- - ">="
|
166
|
+
- !ruby/object:Gem::Version
|
167
|
+
version: '0'
|
140
168
|
- !ruby/object:Gem::Dependency
|
141
169
|
name: sqlite3
|
142
170
|
requirement: !ruby/object:Gem::Requirement
|
@@ -172,7 +200,9 @@ extensions: []
|
|
172
200
|
extra_rdoc_files: []
|
173
201
|
files:
|
174
202
|
- ".gitignore"
|
203
|
+
- ".hound.yml"
|
175
204
|
- ".rspec"
|
205
|
+
- ".rubocop.yml"
|
176
206
|
- ".travis.yml"
|
177
207
|
- CHANGELOG.md
|
178
208
|
- Gemfile
|
@@ -181,12 +211,13 @@ files:
|
|
181
211
|
- Rakefile
|
182
212
|
- db/migration.rb
|
183
213
|
- db/rights_roles.yml
|
184
|
-
- gemfiles/rails3.gemfile
|
185
214
|
- gemfiles/rails4.gemfile
|
186
215
|
- gemfiles/rails5.gemfile
|
187
216
|
- lib/right_on.rb
|
188
|
-
- lib/right_on/
|
217
|
+
- lib/right_on/ability.rb
|
189
218
|
- lib/right_on/by_group.rb
|
219
|
+
- lib/right_on/controller_additions.rb
|
220
|
+
- lib/right_on/error.rb
|
190
221
|
- lib/right_on/generators/USAGE
|
191
222
|
- lib/right_on/generators/right_migration_generator.rb
|
192
223
|
- lib/right_on/generators/templates/right_migration.rb
|
@@ -194,17 +225,23 @@ files:
|
|
194
225
|
- lib/right_on/rails.rb
|
195
226
|
- lib/right_on/railtie.rb
|
196
227
|
- lib/right_on/right.rb
|
228
|
+
- lib/right_on/right_allowed.rb
|
197
229
|
- lib/right_on/rights_manager.rb
|
198
230
|
- lib/right_on/role.rb
|
199
231
|
- lib/right_on/role_model.rb
|
232
|
+
- lib/right_on/rule.rb
|
200
233
|
- lib/right_on/tasks/rights_roles.rake
|
201
234
|
- lib/right_on/tasks/seeds_rights.rake
|
202
235
|
- lib/right_on/version.rb
|
203
236
|
- right_on.gemspec
|
204
|
-
- spec/
|
205
|
-
- spec/
|
237
|
+
- spec/ability_spec.rb
|
238
|
+
- spec/by_group_spec.rb
|
239
|
+
- spec/controller_additions_spec.rb
|
240
|
+
- spec/permission_denied_response_spec.rb
|
241
|
+
- spec/right_allowed_spec.rb
|
206
242
|
- spec/right_on_spec.rb
|
207
243
|
- spec/role_model_spec.rb
|
244
|
+
- spec/rule_spec.rb
|
208
245
|
- spec/schema.rb
|
209
246
|
- spec/spec_helper.rb
|
210
247
|
- spec/support/bootstrap.rb
|
@@ -231,15 +268,19 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
231
268
|
version: '0'
|
232
269
|
requirements: []
|
233
270
|
rubyforge_project:
|
234
|
-
rubygems_version: 2.
|
271
|
+
rubygems_version: 2.7.3
|
235
272
|
signing_key:
|
236
273
|
specification_version: 4
|
237
274
|
summary: Set of extensions to core rails to give rights and roles.
|
238
275
|
test_files:
|
239
|
-
- spec/
|
240
|
-
- spec/
|
276
|
+
- spec/ability_spec.rb
|
277
|
+
- spec/by_group_spec.rb
|
278
|
+
- spec/controller_additions_spec.rb
|
279
|
+
- spec/permission_denied_response_spec.rb
|
280
|
+
- spec/right_allowed_spec.rb
|
241
281
|
- spec/right_on_spec.rb
|
242
282
|
- spec/role_model_spec.rb
|
283
|
+
- spec/rule_spec.rb
|
243
284
|
- spec/schema.rb
|
244
285
|
- spec/spec_helper.rb
|
245
286
|
- spec/support/bootstrap.rb
|
data/gemfiles/rails3.gemfile
DELETED
@@ -1,68 +0,0 @@
|
|
1
|
-
module RightOn
|
2
|
-
|
3
|
-
module ActionControllerExtensions
|
4
|
-
|
5
|
-
def self.included(base)
|
6
|
-
base.module_eval do
|
7
|
-
helper_method :access_allowed?, :access_allowed_to_controller?
|
8
|
-
class_attribute :rights_from
|
9
|
-
class_attribute :permission_denied_layout
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
# Checks the access privilege of the user and renders permission_denied page if required
|
14
|
-
def verify_rights
|
15
|
-
access_allowed?(controller_action_options) || permission_denied
|
16
|
-
end
|
17
|
-
|
18
|
-
# Checks the access privilege for a controller
|
19
|
-
def access_allowed_to_controller?(controller)
|
20
|
-
controller_class = "#{controller.to_s.camelcase}Controller".safe_constantize
|
21
|
-
|
22
|
-
# Handle inheritance of rights
|
23
|
-
if controller_class && controller_class.rights_from.present?
|
24
|
-
controller = controller_class.rights_from.to_s
|
25
|
-
end
|
26
|
-
|
27
|
-
access_allowed?(controller)
|
28
|
-
end
|
29
|
-
|
30
|
-
# Checks the access privilege of the user and returns true or false
|
31
|
-
def access_allowed?(opts={})
|
32
|
-
if opts.is_a?(String)
|
33
|
-
controller, action = opts.split('#')
|
34
|
-
opts = {:controller => controller, :action => action}
|
35
|
-
end
|
36
|
-
opts[:controller] ||= params[:controller]
|
37
|
-
opts[:action] ||= params[:action]
|
38
|
-
current_user.rights.any? { |r| r.allowed?(opts.slice(:controller, :action)) }
|
39
|
-
end
|
40
|
-
|
41
|
-
# Called if a security check determines permission is denied
|
42
|
-
def permission_denied
|
43
|
-
@permission_denied_response = RightOn::PermissionDeniedResponse.new(params, controller_action_options)
|
44
|
-
|
45
|
-
respond_to do |format|
|
46
|
-
format.html { render status: 401, template: 'permission_denied', layout: (permission_denied_layout || false) }
|
47
|
-
format.json do
|
48
|
-
render status: 401, json: @permission_denied_response.to_json
|
49
|
-
end
|
50
|
-
format.js do
|
51
|
-
render :update, status: 401 do |page|
|
52
|
-
page.alert(@permission_denied_layout.text_message)
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
false
|
58
|
-
end
|
59
|
-
|
60
|
-
def controller_action_options
|
61
|
-
opts = params.slice(:controller, :action)
|
62
|
-
opts[:controller] = rights_from.to_s if rights_from
|
63
|
-
opts
|
64
|
-
end
|
65
|
-
|
66
|
-
end
|
67
|
-
|
68
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
require 'action_controller'
|
4
|
-
class AdminController < ActionController::Base
|
5
|
-
include RightOn::ActionControllerExtensions
|
6
|
-
def current_user
|
7
|
-
Thread.current[:user]
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
describe AdminController do
|
12
|
-
let(:basic_user) { User.where(name: 'basic').first }
|
13
|
-
let(:admin_user) { User.where(name: 'admin').first }
|
14
|
-
|
15
|
-
before do
|
16
|
-
Bootstrap.reset_database
|
17
|
-
controller.params = {controller: 'admin', action: 'index'}
|
18
|
-
end
|
19
|
-
|
20
|
-
let(:controller) { AdminController.new }
|
21
|
-
context 'basic user' do
|
22
|
-
before { Thread.current[:user] = basic_user }
|
23
|
-
it 'should not allow access' do
|
24
|
-
expect(controller.access_allowed?).to be false
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
context 'admin user' do
|
29
|
-
before { Thread.current[:user] = admin_user }
|
30
|
-
it 'should allow access' do
|
31
|
-
expect(controller.access_allowed?).to be true
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|