allow 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +19 -0
- data/README.md +72 -0
- data/Rakefile +38 -0
- data/lib/allow.rb +32 -0
- data/test/actor.rb +36 -0
- data/test/allow.rb +38 -0
- data/test/permissions.rb +44 -0
- metadata +66 -0
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2012 Arun Srinivasan
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# Allow
|
2
|
+
|
3
|
+
A very small authorization library.
|
4
|
+
|
5
|
+
It packs a surprising punch for being 26 lines of code.
|
6
|
+
|
7
|
+
# Installation
|
8
|
+
|
9
|
+
gem install allow
|
10
|
+
|
11
|
+
# Usage
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
require 'allow'
|
15
|
+
|
16
|
+
class User
|
17
|
+
include Allow::Actor # Gives you a :can? method
|
18
|
+
end
|
19
|
+
|
20
|
+
class Post
|
21
|
+
attr_reader :author
|
22
|
+
|
23
|
+
def initialize(author)
|
24
|
+
@author = author
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# You can call your permissions-checking class whatever you want,
|
29
|
+
# just be sure to include Allow::Permissions.
|
30
|
+
class Permissions
|
31
|
+
include Allow::Permissions # Gives you :permitted?, though you won't
|
32
|
+
# be using it directly very much.
|
33
|
+
def update_post(user, post)
|
34
|
+
user == post.author
|
35
|
+
end # Note: all permitter methods must accept at
|
36
|
+
# least 1 argument - they will always receive
|
37
|
+
# an `actor` argument. Everything else is up
|
38
|
+
# to you.
|
39
|
+
end
|
40
|
+
|
41
|
+
# Tell Allow about your Permissions class
|
42
|
+
Allow.permissions = Permissions.new
|
43
|
+
|
44
|
+
# Create some objects
|
45
|
+
author = User.new
|
46
|
+
reader = User.new
|
47
|
+
post = Post.new(author)
|
48
|
+
|
49
|
+
# User's have a :can? method:
|
50
|
+
author.can?(:update_post, post) # => true
|
51
|
+
reader.can?(:update_post, post) # => false
|
52
|
+
|
53
|
+
# Alternatively (and equivalently):
|
54
|
+
Allow.ed?(author, :update_post, post) # => true
|
55
|
+
Allow.ed?(reader, :update_post, post) # => false
|
56
|
+
|
57
|
+
# Both :can? and Allow.ed? accept an optional block that
|
58
|
+
# only gets executed if the permitter method returns a truthy
|
59
|
+
# value.
|
60
|
+
|
61
|
+
blocks_called = []
|
62
|
+
|
63
|
+
author.can?(:update_post, post) do
|
64
|
+
blocks_called << [:author_block] # this gets run
|
65
|
+
end
|
66
|
+
|
67
|
+
reader.can?(:update_post, post) do
|
68
|
+
blocks_called << [:reader_block] # this does NOT get run
|
69
|
+
end
|
70
|
+
|
71
|
+
blocks_called # => [:author_block]
|
72
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
$:.push("lib")
|
2
|
+
require "allow"
|
3
|
+
|
4
|
+
name = "allow"
|
5
|
+
version = Allow::VERSION
|
6
|
+
|
7
|
+
desc 'Run the tests'
|
8
|
+
task :test do
|
9
|
+
require 'tst'
|
10
|
+
Tst.run 'test/*.rb',
|
11
|
+
:load_paths => ['lib'],
|
12
|
+
:reporter => Tst::Reporters::Pretty.new
|
13
|
+
end
|
14
|
+
|
15
|
+
namespace :gem do
|
16
|
+
desc 'Clean up generated files'
|
17
|
+
task :clean do
|
18
|
+
sh 'rm -rf pkg'
|
19
|
+
end
|
20
|
+
|
21
|
+
desc "Build the gem"
|
22
|
+
task :build => :clean do
|
23
|
+
sh "mkdir pkg"
|
24
|
+
sh "gem build #{name}.gemspec"
|
25
|
+
sh "mv #{name}-#{version}.gem pkg/"
|
26
|
+
end
|
27
|
+
|
28
|
+
desc "Release v#{version}"
|
29
|
+
task :release => :build do
|
30
|
+
sh "git commit --allow-empty -a -m 'Release #{version}'"
|
31
|
+
sh "git tag v#{version}"
|
32
|
+
sh "git push origin master"
|
33
|
+
sh "git push origin v#{version}"
|
34
|
+
sh "gem push pkg/#{name}-#{version}.gem"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
task :default => :test
|
data/lib/allow.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
module Allow
|
2
|
+
VERSION = "0.0.1"
|
3
|
+
|
4
|
+
module Permissions
|
5
|
+
def permitted?(actor, verb, *objects)
|
6
|
+
return false unless respond_to?(verb)
|
7
|
+
!!send(verb, actor, *objects)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module Actor
|
12
|
+
def can?(verb, *objects, &block)
|
13
|
+
Allow.ed?(self, verb, *objects, &block)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class << self
|
18
|
+
attr_accessor :permissions
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.reset!
|
22
|
+
@permissions = Class.new { include Permissions }.new
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.ed?(actor, verb, *objects, &block)
|
26
|
+
permitted = permissions.permitted?(actor, verb, *objects)
|
27
|
+
block.call if permitted && block
|
28
|
+
permitted
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
Allow.reset!
|
data/test/actor.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'allow'
|
2
|
+
|
3
|
+
class User
|
4
|
+
include Allow::Actor
|
5
|
+
end
|
6
|
+
|
7
|
+
class Post
|
8
|
+
attr_reader :author
|
9
|
+
def initialize(author)
|
10
|
+
@author = author
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Permissions
|
15
|
+
include Allow::Permissions
|
16
|
+
def update_post(user, post)
|
17
|
+
user == post.author
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
tst "including Allow::Can gets you the :can? method" do
|
22
|
+
user = User.new
|
23
|
+
assert user.respond_to?(:can?)
|
24
|
+
end
|
25
|
+
|
26
|
+
tst "#can? does what you'd expect" do
|
27
|
+
author = User.new
|
28
|
+
reader = User.new
|
29
|
+
post = Post.new(author)
|
30
|
+
Allow.permissions = Permissions.new
|
31
|
+
|
32
|
+
assert_equal true, author.can?(:update_post, post)
|
33
|
+
assert_equal false, reader.can?(:update_post, post)
|
34
|
+
|
35
|
+
Allow.reset!
|
36
|
+
end
|
data/test/allow.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'allow'
|
2
|
+
|
3
|
+
tst "Allow.ed? delegates to the permissions object :permitted? method" do
|
4
|
+
Allow.permissions = Class.new {
|
5
|
+
def permitted?(*args) "permitted? called with: #{args}" end
|
6
|
+
}.new
|
7
|
+
|
8
|
+
result = Allow.ed?(:actor, :verb, :object)
|
9
|
+
assert_equal 'permitted? called with: [:actor, :verb, :object]', result
|
10
|
+
|
11
|
+
Allow.reset!
|
12
|
+
end
|
13
|
+
|
14
|
+
#
|
15
|
+
# Tests for when Allow.ed? is passed a block:
|
16
|
+
#
|
17
|
+
|
18
|
+
tst "Allow.ed? does NOT execute the block if permitted? => false" do
|
19
|
+
Allow.permissions = Class.new {
|
20
|
+
def permitted?(*args) false end
|
21
|
+
}.new
|
22
|
+
|
23
|
+
Allow.ed?(:actor, :verb, :object) { raise "This shouldn't get reached!!" }
|
24
|
+
|
25
|
+
Allow.reset!
|
26
|
+
end
|
27
|
+
|
28
|
+
tst "Allow.ed? executes the block if permitted? => true" do
|
29
|
+
Allow.permissions = Class.new {
|
30
|
+
def permitted?(*args) true end
|
31
|
+
}.new
|
32
|
+
|
33
|
+
assert_raises RegexpError do
|
34
|
+
Allow.ed?(:actor, :verb, :object) { raise RegexpError }
|
35
|
+
end
|
36
|
+
|
37
|
+
Allow.reset!
|
38
|
+
end
|
data/test/permissions.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'allow'
|
2
|
+
|
3
|
+
class Permissions
|
4
|
+
include Allow::Permissions
|
5
|
+
def answer(s, o) 42 end
|
6
|
+
def number(s, o) 42 end
|
7
|
+
def string(s, o) 'a string' end
|
8
|
+
def nil(s, o) nil end
|
9
|
+
end
|
10
|
+
|
11
|
+
tst "#permitted? defaults to false" do
|
12
|
+
permissions = Permissions.new
|
13
|
+
permitted = permissions.permitted?(:actor, :verb, :object)
|
14
|
+
assert_equal false, permitted
|
15
|
+
end
|
16
|
+
|
17
|
+
tst "#permitted? coerces the result to a boolean" do
|
18
|
+
permissions = Permissions.new
|
19
|
+
|
20
|
+
assert_equal true, permissions.permitted?('', :number, '')
|
21
|
+
assert_equal true, permissions.permitted?('', :string, '')
|
22
|
+
assert_equal false, permissions.permitted?('', :nil, '')
|
23
|
+
assert_equal false, permissions.permitted?('', :undefined, '')
|
24
|
+
end
|
25
|
+
|
26
|
+
tst "#permitted? passes the actor and object(s) as args" do
|
27
|
+
permissions = Permissions.new
|
28
|
+
|
29
|
+
def permissions.do_something(*args)
|
30
|
+
args == [:actor, :object]
|
31
|
+
end
|
32
|
+
|
33
|
+
assert permissions.permitted?(:actor, :do_something, :object)
|
34
|
+
end
|
35
|
+
|
36
|
+
tst "#permitted? can pass multiple objects through" do
|
37
|
+
permissions = Permissions.new
|
38
|
+
|
39
|
+
def permissions.do_something(*args)
|
40
|
+
args == [:actor, :object1, :object2]
|
41
|
+
end
|
42
|
+
|
43
|
+
assert permissions.permitted?(:actor, :do_something, :object1, :object2)
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: allow
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Arun Srinivasan
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-27 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: tst
|
16
|
+
requirement: &70314285938480 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70314285938480
|
25
|
+
description: A very small authorization library.
|
26
|
+
email: satchmorun@gmail.com
|
27
|
+
executables: []
|
28
|
+
extensions: []
|
29
|
+
extra_rdoc_files: []
|
30
|
+
files:
|
31
|
+
- README.md
|
32
|
+
- LICENSE
|
33
|
+
- Rakefile
|
34
|
+
- lib/allow.rb
|
35
|
+
- test/actor.rb
|
36
|
+
- test/allow.rb
|
37
|
+
- test/permissions.rb
|
38
|
+
homepage: http://github.com/satchmorun/tst
|
39
|
+
licenses:
|
40
|
+
- MIT
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
requirements: []
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 1.8.10
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: A very small authorization library.
|
63
|
+
test_files:
|
64
|
+
- test/actor.rb
|
65
|
+
- test/allow.rb
|
66
|
+
- test/permissions.rb
|