requirement_authorization 0.0.0 → 0.0.2
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 +17 -1
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +59 -0
- data/Rakefile +1 -38
- data/lib/requirement_authorization.rb +25 -19
- data/lib/requirement_authorization/version.rb +3 -0
- data/requirement_authorization.gemspec +23 -0
- data/spec/requirement_authorization_spec.rb +1 -1
- metadata +77 -41
- data/README.markdown +0 -64
- data/rails/init.rb +0 -3
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4a034f7d906176f135e97942a859d5cde20a6d7f
|
4
|
+
data.tar.gz: d46ced5f2d6fcdfd4bbb7e5a5d80fb473932825a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 00ba9c4129d193637a0be456b9824ff499ea6ebe11ae37500ace6c4c812d6c8f2b366c039bdef79edf2f797a4e8cb973fb00951556d6520d8cc8ee2bd9288c01
|
7
|
+
data.tar.gz: 03a1bd45f1748a38358b3e1b6a3ecd9130119e02a99a14f19b26b9ea0a1a7faaed46038e16633c4dfe39aced2b28ab3ff3ce713f48ca2b372391447e266a3a23
|
data/.gitignore
CHANGED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Brad Gessler
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# Requirement Authorization
|
2
|
+
|
3
|
+
Requirement authorization is a lightweight DSL designed to separate the concerns of resource access from gathering information required to access an action. It also lets you pass arguments into Rails before_filters for more control over what may or may not be accessed.
|
4
|
+
|
5
|
+
WARNING: This puppy isn't tested yet, but its the very next thing I plan on doing. I wanted to get this up on github first so that we could gemify this into our own project and build out tests.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'requirement_authorization'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install requirement_authorization
|
20
|
+
|
21
|
+
Then include it in your Rails application:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
class ApplicationController < ActionController::Base
|
25
|
+
include RequirementAuthorization
|
26
|
+
|
27
|
+
# Your requirements here..
|
28
|
+
|
29
|
+
# Your code here...
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
## Examples
|
34
|
+
|
35
|
+
A more interesting example may be to protect a paid feature from being accessed by users who did not pay for that feature:
|
36
|
+
|
37
|
+
requirement :feature do |r|
|
38
|
+
r.guard_unless { |feature| current_user.account.send("#{feature}_enabled?") }
|
39
|
+
r.resolution { |feature| redirect_to upgrade_path(feature) }
|
40
|
+
end
|
41
|
+
|
42
|
+
In the controller just add
|
43
|
+
|
44
|
+
class AwesomeSauceController < ActionController::Base
|
45
|
+
feature_required :awesome_sauce
|
46
|
+
end
|
47
|
+
|
48
|
+
A more trivial example for SSL
|
49
|
+
|
50
|
+
requirement :ssl do |r|
|
51
|
+
r.guard_unless { request.ssl? }
|
52
|
+
r.resolution { redirect_to "https://" + request.host + request.request_uri }
|
53
|
+
end
|
54
|
+
|
55
|
+
Then in the controller:
|
56
|
+
|
57
|
+
class PaymentMethodsController << ActionController::Base
|
58
|
+
ssl_required
|
59
|
+
end
|
data/Rakefile
CHANGED
@@ -1,38 +1 @@
|
|
1
|
-
require
|
2
|
-
require 'rake/testtask'
|
3
|
-
require 'rake/rdoctask'
|
4
|
-
|
5
|
-
desc 'Default: run specs.'
|
6
|
-
task :default => :spec
|
7
|
-
|
8
|
-
require 'rake'
|
9
|
-
require 'spec/rake/spectask'
|
10
|
-
|
11
|
-
desc "Run all examples"
|
12
|
-
Spec::Rake::SpecTask.new('spec') do |t|
|
13
|
-
t.spec_files = FileList['spec/*.rb']
|
14
|
-
t.fail_on_error = false
|
15
|
-
end
|
16
|
-
|
17
|
-
desc 'Generate documentation for the has_default plugin.'
|
18
|
-
Rake::RDocTask.new(:rdoc) do |rdoc|
|
19
|
-
rdoc.rdoc_dir = 'rdoc'
|
20
|
-
rdoc.title = 'HasDefault'
|
21
|
-
rdoc.options << '--line-numbers' << '--inline-source'
|
22
|
-
rdoc.rdoc_files.include('README')
|
23
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
24
|
-
end
|
25
|
-
|
26
|
-
begin
|
27
|
-
require 'jeweler'
|
28
|
-
Jeweler::Tasks.new do |gemspec|
|
29
|
-
gemspec.name = "requirement_authorization"
|
30
|
-
gemspec.summary = "A lightweight authorization framework that separates the concern of resource access from gathering information necessary to fulfill a request, like singing in with a username and password."
|
31
|
-
gemspec.description = ""
|
32
|
-
gemspec.email = "brad@conden.se"
|
33
|
-
gemspec.homepage = "http://github.com/bradgessler/requirement_authorization"
|
34
|
-
gemspec.authors = ["Brad Gessler"]
|
35
|
-
end
|
36
|
-
rescue LoadError
|
37
|
-
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
38
|
-
end
|
1
|
+
require "bundler/gem_tasks"
|
@@ -1,18 +1,19 @@
|
|
1
|
-
|
1
|
+
require "requirement_authorization/version"
|
2
|
+
|
2
3
|
module RequirementAuthorization
|
3
4
|
METHOD_SUFIX = '_required'
|
4
|
-
|
5
|
+
|
5
6
|
def self.included(base)
|
6
7
|
base.send :extend, ClassMethods
|
7
8
|
end
|
8
|
-
|
9
|
+
|
9
10
|
class Requirement
|
10
11
|
CONTROLLER_OPTIONS = [:only, :except, :if, :unless]
|
11
12
|
|
12
13
|
def initialize(opts={},&block)
|
13
14
|
yield self if block_given?
|
14
15
|
end
|
15
|
-
|
16
|
+
|
16
17
|
# A proc or method that must return a true for the requirement to be satisified
|
17
18
|
def guard(proc=nil, &block)
|
18
19
|
@guard = controller_filter_proc(proc, &block)
|
@@ -23,24 +24,26 @@ module RequirementAuthorization
|
|
23
24
|
# Lazily invert the return of the controller proc.
|
24
25
|
@guard = Proc.new{|c, args| not controller_filter_proc(proc, &block).call(c, *args)}
|
25
26
|
end
|
26
|
-
|
27
|
+
|
27
28
|
# This is the method that we'll call if the guard fails. The resolution should take the user
|
28
29
|
# through a flow where they can satisify the requirements for the requirement and pass on through.
|
29
30
|
def resolution(proc=nil, &block)
|
30
31
|
@resolution = controller_filter_proc(proc, &block)
|
31
32
|
end
|
32
|
-
|
33
|
+
|
33
34
|
# Sets up the filter for the controller by wrapping this requirement up in a proc.
|
34
35
|
def filter(controller, *args)
|
35
36
|
args, controller_options = extract_filter_args!(args)
|
36
|
-
|
37
|
+
# Capture the Method object here in case the scope of the block changes
|
38
|
+
resolve_method = method(:resolve)
|
39
|
+
controller.before_filter(controller_options){ |c| resolve_method.call(c, *args) }
|
37
40
|
end
|
38
|
-
|
41
|
+
|
39
42
|
# The gaurd, resolution process. This is where the magic happens.
|
40
|
-
def
|
43
|
+
def resolve(controller_instance, *args)
|
41
44
|
@resolution.call(controller_instance, *args) if @guard.call(controller_instance, *args)
|
42
45
|
end
|
43
|
-
|
46
|
+
|
44
47
|
protected
|
45
48
|
# Packages up a proc or a block into something that a controller can deal with
|
46
49
|
def controller_filter_proc(proc=nil, &block)
|
@@ -71,32 +74,35 @@ module RequirementAuthorization
|
|
71
74
|
[ args, controller_options ]
|
72
75
|
end
|
73
76
|
end
|
74
|
-
|
77
|
+
|
75
78
|
module ClassMethods
|
76
79
|
# Setups a hash table of requirements that may be used by class methods from
|
77
80
|
# other sub-classed controllers
|
78
81
|
def requirement(requirement, opts={}, &block)
|
79
82
|
self.requirements.merge! requirement.to_s => Requirement.new(opts, &block)
|
80
|
-
|
83
|
+
|
81
84
|
# Build out the class method for this requirement. This is primarly used towards the
|
82
85
|
# top of a controller.
|
83
|
-
|
86
|
+
instance_eval %{
|
84
87
|
def #{requirement}#{METHOD_SUFIX}(*args)
|
85
88
|
requirements['#{requirement}'].filter(self, *args)
|
86
89
|
end}
|
87
|
-
|
90
|
+
|
88
91
|
# Build out the instance method so that this requirement can be called from other
|
89
|
-
# instance methods. This proves to be insanely useful for composing requirements
|
92
|
+
# instance methods. This proves to be insanely useful for composing requirements
|
90
93
|
# together or reusing them from other methods.
|
91
|
-
|
94
|
+
class_eval %{
|
92
95
|
def #{requirement}#{METHOD_SUFIX}(*args)
|
93
|
-
|
96
|
+
req = '#{requirement}'
|
97
|
+
self.class.send(:requirements).
|
98
|
+
fetch(req).
|
99
|
+
resolve(self, *args)
|
94
100
|
end}
|
95
101
|
end
|
96
|
-
|
102
|
+
|
97
103
|
protected
|
98
104
|
def requirements
|
99
105
|
@@requirements ||= {}
|
100
106
|
end
|
101
107
|
end
|
102
|
-
end
|
108
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'requirement_authorization/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "requirement_authorization"
|
8
|
+
spec.version = RequirementAuthorization::VERSION
|
9
|
+
spec.authors = ["Brad Gessler"]
|
10
|
+
spec.email = ["brad@polleverywhere.com"]
|
11
|
+
spec.summary = %q{Quickly create before_filters in Rails that protect resources.}
|
12
|
+
spec.homepage = "https://github.com/bradgessler/requirement_authorization"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
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.5"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
spec.add_development_dependency "rspec"
|
23
|
+
end
|
metadata
CHANGED
@@ -1,61 +1,97 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: requirement_authorization
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
|
-
authors:
|
6
|
+
authors:
|
7
7
|
- Brad Gessler
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
11
|
+
date: 2014-02-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.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '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: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- brad@polleverywhere.com
|
18
58
|
executables: []
|
19
|
-
|
20
59
|
extensions: []
|
21
|
-
|
22
|
-
|
23
|
-
-
|
24
|
-
|
25
|
-
- .
|
26
|
-
- README.
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
27
66
|
- Rakefile
|
28
67
|
- VERSION
|
29
68
|
- lib/requirement_authorization.rb
|
30
|
-
-
|
69
|
+
- lib/requirement_authorization/version.rb
|
70
|
+
- requirement_authorization.gemspec
|
31
71
|
- spec/requirement_authorization_spec.rb
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
72
|
+
homepage: https://github.com/bradgessler/requirement_authorization
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
36
76
|
post_install_message:
|
37
|
-
rdoc_options:
|
38
|
-
|
39
|
-
require_paths:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
40
79
|
- lib
|
41
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
-
requirements:
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
43
82
|
- - ">="
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
version:
|
46
|
-
|
47
|
-
|
48
|
-
requirements:
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
49
87
|
- - ">="
|
50
|
-
- !ruby/object:Gem::Version
|
51
|
-
version:
|
52
|
-
version:
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
53
90
|
requirements: []
|
54
|
-
|
55
91
|
rubyforge_project:
|
56
|
-
rubygems_version:
|
92
|
+
rubygems_version: 2.2.0
|
57
93
|
signing_key:
|
58
|
-
specification_version:
|
59
|
-
summary:
|
60
|
-
test_files:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Quickly create before_filters in Rails that protect resources.
|
96
|
+
test_files:
|
61
97
|
- spec/requirement_authorization_spec.rb
|
data/README.markdown
DELETED
@@ -1,64 +0,0 @@
|
|
1
|
-
# Requirement Authorization
|
2
|
-
|
3
|
-
Requirement authorization is a lightweight DSL designed to separate the concerns of resource access from gathering information required to access the resource.
|
4
|
-
|
5
|
-
WARNING: This puppy isn't tested yet, but its the very next thing I plan on doing. I wanted to get this up on github first so that we could gemify this into our own project and build out tests.
|
6
|
-
|
7
|
-
## Installation
|
8
|
-
|
9
|
-
In your rails config/environment.rb file, just add
|
10
|
-
|
11
|
-
config.gem 'requirement_authorization', :source => 'http://gemcutter.org'
|
12
|
-
|
13
|
-
## Examples
|
14
|
-
|
15
|
-
A more interesting example may be to protect a paid feature from being accessed by users who did not pay for that feature:
|
16
|
-
|
17
|
-
requirement :feature do |r|
|
18
|
-
r.guard_unless { |feature| current_user.account.send("#{feature}_enabled?") }
|
19
|
-
r.resolution { |feature| redirect_to upgrade_path(feature) }
|
20
|
-
end
|
21
|
-
|
22
|
-
In the controller just add
|
23
|
-
|
24
|
-
class AwesomeSauceController < ActionController::Base
|
25
|
-
feature_required :awesome_sauce
|
26
|
-
end
|
27
|
-
|
28
|
-
A more trivial example for SSL
|
29
|
-
|
30
|
-
requirement :ssl do |r|
|
31
|
-
r.guard_unless { request.ssl? }
|
32
|
-
r.resolution { redirect_to "https://" + request.host + request.request_uri }
|
33
|
-
end
|
34
|
-
|
35
|
-
Then in the controller:
|
36
|
-
|
37
|
-
class PaymentMethodsController << ActionController::Base
|
38
|
-
ssl_required
|
39
|
-
end
|
40
|
-
|
41
|
-
## License
|
42
|
-
|
43
|
-
Copyright (c) 2009 Brad Gessler
|
44
|
-
|
45
|
-
Permission is hereby granted, free of charge, to any person
|
46
|
-
obtaining a copy of this software and associated documentation
|
47
|
-
files (the "Software"), to deal in the Software without
|
48
|
-
restriction, including without limitation the rights to use,
|
49
|
-
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
50
|
-
copies of the Software, and to permit persons to whom the
|
51
|
-
Software is furnished to do so, subject to the following
|
52
|
-
conditions:
|
53
|
-
|
54
|
-
The above copyright notice and this permission notice shall be
|
55
|
-
included in all copies or substantial portions of the Software.
|
56
|
-
|
57
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
58
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
59
|
-
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
60
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
61
|
-
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
62
|
-
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
63
|
-
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
64
|
-
OTHER DEALINGS IN THE SOFTWARE.
|
data/rails/init.rb
DELETED