spine-restrictions 0.1.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 +7 -0
- data/.gitignore +36 -0
- data/.rspec +3 -0
- data/CHANGELOG.md +6 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +45 -0
- data/LICENSE +12 -0
- data/README.md +41 -0
- data/Rakefile +1 -0
- data/lib/spine/restrictions.rb +6 -0
- data/lib/spine/restrictions/collection.rb +31 -0
- data/lib/spine/restrictions/registration.rb +47 -0
- data/lib/spine/restrictions/version.rb +5 -0
- data/spec/config/coverage.rb +4 -0
- data/spec/config/default.rb +20 -0
- data/spec/spine/restrictions/collection_spec.rb +47 -0
- data/spine-restrictions.gemspec +24 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ca8f1c71b1c5b353ced6b8a79cd57676060135f9
|
4
|
+
data.tar.gz: 142da03091c0d71b9e70fbafc417adab9276d4b1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b65f6dfd635e23bcdd305356fc1bb96dddc5b2175a9f144c993398aa6187c6740c84818a7bb18dad3be49206a4780844ea62d67e980710b6c0524e0e59413062
|
7
|
+
data.tar.gz: b4bd3f9745ac970ebcf32013e4455dedb87e7a14d9b4b61dd571fbc690f2691166b362033379aaccbdf40359c602534420fbe9f2c141899c1e17eddb0c59ff2f
|
data/.gitignore
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/test/tmp/
|
9
|
+
/test/version_tmp/
|
10
|
+
/tmp/
|
11
|
+
|
12
|
+
## Specific to RubyMotion:
|
13
|
+
.dat*
|
14
|
+
.repl_history
|
15
|
+
build/
|
16
|
+
|
17
|
+
## Documentation cache and generated files:
|
18
|
+
/.yardoc/
|
19
|
+
/_yardoc/
|
20
|
+
/doc/
|
21
|
+
/rdoc/
|
22
|
+
|
23
|
+
## Environment normalisation:
|
24
|
+
/bin/
|
25
|
+
/.bundle/
|
26
|
+
/vendor/bundle
|
27
|
+
/lib/bundler/man/
|
28
|
+
|
29
|
+
# for a library or gem, you might want to ignore these files since the code is
|
30
|
+
# intended to run in multiple environments; otherwise, check them in:
|
31
|
+
# Gemfile.lock
|
32
|
+
# .ruby-version
|
33
|
+
# .ruby-gemset
|
34
|
+
|
35
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
36
|
+
.rvmrc
|
data/.rspec
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
spine-restrictions (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
codeclimate-test-reporter (0.4.7)
|
10
|
+
simplecov (>= 0.7.1, < 1.0.0)
|
11
|
+
diff-lcs (1.2.5)
|
12
|
+
docile (1.1.5)
|
13
|
+
json (1.8.2)
|
14
|
+
rake (10.4.2)
|
15
|
+
rspec (3.2.0)
|
16
|
+
rspec-core (~> 3.2.0)
|
17
|
+
rspec-expectations (~> 3.2.0)
|
18
|
+
rspec-mocks (~> 3.2.0)
|
19
|
+
rspec-core (3.2.3)
|
20
|
+
rspec-support (~> 3.2.0)
|
21
|
+
rspec-expectations (3.2.1)
|
22
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
23
|
+
rspec-support (~> 3.2.0)
|
24
|
+
rspec-mocks (3.2.1)
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
26
|
+
rspec-support (~> 3.2.0)
|
27
|
+
rspec-support (3.2.2)
|
28
|
+
simplecov (0.10.0)
|
29
|
+
docile (~> 1.1.0)
|
30
|
+
json (~> 1.8)
|
31
|
+
simplecov-html (~> 0.10.0)
|
32
|
+
simplecov-html (0.10.0)
|
33
|
+
|
34
|
+
PLATFORMS
|
35
|
+
ruby
|
36
|
+
|
37
|
+
DEPENDENCIES
|
38
|
+
bundler (~> 1.7)
|
39
|
+
codeclimate-test-reporter (~> 0.4)
|
40
|
+
rake (~> 10.0)
|
41
|
+
rspec (~> 3.2)
|
42
|
+
spine-restrictions!
|
43
|
+
|
44
|
+
BUNDLED WITH
|
45
|
+
1.10.2
|
data/LICENSE
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
Copyright (c) 2015, TOGGL LLC
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
5
|
+
|
6
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
7
|
+
|
8
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
9
|
+
|
10
|
+
3. Neither the name of the TOGGL LLC nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
11
|
+
|
12
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Spine::Restrictions
|
2
|
+
|
3
|
+
[](http://badge.fury.io/rb/spine-restrictions)
|
4
|
+
[](https://gemnasium.com/rspine/restrictions)
|
5
|
+
[](https://codeclimate.com/github/rspine/restrictions/coverage)
|
6
|
+
[](https://codeclimate.com/github/rspine/restrictions)
|
7
|
+
[](https://codeship.com/projects/84018)
|
8
|
+
|
9
|
+
Restrictions registration for application context.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
To install it, add the gem to your Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'spine-restrictions'
|
17
|
+
```
|
18
|
+
|
19
|
+
Then run `bundle`. If you're not using Bundler, just `gem install spine-restrictions`.
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
# Restriction can be module or class, which defines `restricted?` method based
|
25
|
+
# on context.
|
26
|
+
class UserSuspensionRestriction
|
27
|
+
def restricted?(context)
|
28
|
+
!!context.subject.suspended_at
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
restrictions = Spine::Restrictions::Collection.new
|
33
|
+
restrictions.register(UserSuspensionRestriction)
|
34
|
+
.restrict(:write, :all)
|
35
|
+
.restrict(:update, :all)
|
36
|
+
.restrict(:delete, :all)
|
37
|
+
.except(:all, :billing)
|
38
|
+
|
39
|
+
restrictions.restricted?(context, :write, :tasks)
|
40
|
+
# => <UserSuspensionRestriction:0x007f8e51ad7960>
|
41
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Spine
|
2
|
+
module Restrictions
|
3
|
+
class Collection
|
4
|
+
attr_reader :registrations
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@registrations = []
|
8
|
+
end
|
9
|
+
|
10
|
+
def register(restriction)
|
11
|
+
registration = Registration.new(restriction)
|
12
|
+
registrations.unshift(registration)
|
13
|
+
registration
|
14
|
+
end
|
15
|
+
|
16
|
+
def restricted?(context, action, resource)
|
17
|
+
restrictions(action, resource).detect { |restriction|
|
18
|
+
restriction.restricted?(context)
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
def restrictions(action, resource)
|
23
|
+
registrations.select { |registration|
|
24
|
+
registration.applies?(action, resource)
|
25
|
+
}.map { |registration|
|
26
|
+
registration.restriction
|
27
|
+
}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Spine
|
2
|
+
module Restrictions
|
3
|
+
class Registration
|
4
|
+
attr_reader :restriction, :restrictions, :exceptions
|
5
|
+
|
6
|
+
def initialize(restriction)
|
7
|
+
@restriction = restriction
|
8
|
+
@exceptions = {}
|
9
|
+
@restrictions = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def restrict(action, resource)
|
13
|
+
add(restrictions, action, resource)
|
14
|
+
self
|
15
|
+
end
|
16
|
+
|
17
|
+
def except(action, resource)
|
18
|
+
add(exceptions, action, resource)
|
19
|
+
self
|
20
|
+
end
|
21
|
+
|
22
|
+
def applies?(action, resource)
|
23
|
+
return false if exception?(action, resource)
|
24
|
+
|
25
|
+
includes?(restrictions, action, resource)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def exception?(action, resource)
|
31
|
+
includes?(exceptions, action, resource)
|
32
|
+
end
|
33
|
+
|
34
|
+
def includes?(collection, action, resource)
|
35
|
+
actions = collection[resource] || collection[:all]
|
36
|
+
return false unless actions
|
37
|
+
|
38
|
+
actions[action] || actions[:all]
|
39
|
+
end
|
40
|
+
|
41
|
+
def add(collection, action, resource)
|
42
|
+
collection[resource] ||= {}
|
43
|
+
collection[resource][action] = true
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
RSpec.configure do |config|
|
2
|
+
config.filter_run :focus
|
3
|
+
config.run_all_when_everything_filtered = true
|
4
|
+
|
5
|
+
if config.files_to_run.one?
|
6
|
+
config.default_formatter = 'doc'
|
7
|
+
end
|
8
|
+
|
9
|
+
config.order = :random
|
10
|
+
|
11
|
+
Kernel.srand config.seed
|
12
|
+
|
13
|
+
config.expect_with :rspec do |expectations|
|
14
|
+
expectations.syntax = :expect
|
15
|
+
end
|
16
|
+
|
17
|
+
config.mock_with :rspec do |mocks|
|
18
|
+
mocks.syntax = :expect
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spine/restrictions'
|
2
|
+
|
3
|
+
module Spine
|
4
|
+
module Restrictions
|
5
|
+
describe Collection do
|
6
|
+
let(:restriction) { double(restricted?: true) }
|
7
|
+
let(:context) { double }
|
8
|
+
|
9
|
+
it 'registers restriction' do
|
10
|
+
subject.register(restriction).restrict(:read, :all)
|
11
|
+
expect(subject.registrations.size).to be > 0
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'registers restriction on specific action and specific resource' do
|
15
|
+
subject.register(restriction).restrict(:read, :data)
|
16
|
+
expect(subject.restricted?(context, :read, :data)).to be
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'registers restriction on all actions and specific resource' do
|
20
|
+
subject.register(restriction).restrict(:all, :data)
|
21
|
+
expect(subject.restricted?(context, :read, :data)).to be
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'registers restriction on specific action and all resources' do
|
25
|
+
subject.register(restriction).restrict(:read, :all)
|
26
|
+
expect(subject.restricted?(context, :read, :data)).to be
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'registers restriction on all actions and all resources' do
|
30
|
+
subject.register(restriction).restrict(:all, :all)
|
31
|
+
expect(subject.restricted?(context, :read, :data)).to be
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'registers exception' do
|
35
|
+
subject.register(restriction)
|
36
|
+
.restrict(:read, :all)
|
37
|
+
.except(:read, :data)
|
38
|
+
expect(subject.restricted?(context, :read, :data)).to be_falsy
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'restricts' do
|
42
|
+
subject.register(restriction).restrict(:read, :all)
|
43
|
+
expect(subject.restricted?(context, :read, :data)).to be
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'spine/restrictions/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "spine-restrictions"
|
7
|
+
spec.version = Spine::Restrictions::VERSION
|
8
|
+
spec.authors = ["TOGGL LLC"]
|
9
|
+
spec.email = ["support@toggl.com"]
|
10
|
+
spec.summary = 'Restrictions registration for application context'
|
11
|
+
spec.description = ''
|
12
|
+
spec.homepage = 'https://github.com/rspine/restrictions'
|
13
|
+
spec.license = 'BSD-3-Clause'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency 'bundler', "~> 1.7"
|
21
|
+
spec.add_development_dependency 'rake', "~> 10.0"
|
22
|
+
spec.add_development_dependency 'rspec', "~> 3.2"
|
23
|
+
spec.add_development_dependency 'codeclimate-test-reporter', "~> 0.4"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: spine-restrictions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- TOGGL LLC
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.2'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: codeclimate-test-reporter
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.4'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.4'
|
69
|
+
description: ''
|
70
|
+
email:
|
71
|
+
- support@toggl.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- CHANGELOG.md
|
79
|
+
- Gemfile
|
80
|
+
- Gemfile.lock
|
81
|
+
- LICENSE
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- lib/spine/restrictions.rb
|
85
|
+
- lib/spine/restrictions/collection.rb
|
86
|
+
- lib/spine/restrictions/registration.rb
|
87
|
+
- lib/spine/restrictions/version.rb
|
88
|
+
- spec/config/coverage.rb
|
89
|
+
- spec/config/default.rb
|
90
|
+
- spec/spine/restrictions/collection_spec.rb
|
91
|
+
- spine-restrictions.gemspec
|
92
|
+
homepage: https://github.com/rspine/restrictions
|
93
|
+
licenses:
|
94
|
+
- BSD-3-Clause
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.4.5
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Restrictions registration for application context
|
116
|
+
test_files:
|
117
|
+
- spec/config/coverage.rb
|
118
|
+
- spec/config/default.rb
|
119
|
+
- spec/spine/restrictions/collection_spec.rb
|