barristan 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/LICENSE +21 -0
  2. data/lib/barristan/version.rb +3 -0
  3. data/lib/barristan.rb +62 -0
  4. metadata +64 -0
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT LICENSE
2
+
3
+ Copyright (c) chatgris <jboyer@af83.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,3 @@
1
+ module Barristan
2
+ VERSION = "0.0.1"
3
+ end
data/lib/barristan.rb ADDED
@@ -0,0 +1,62 @@
1
+ # encoding: utf-8
2
+ module Barristan
3
+ class Able
4
+ def can(klasses, action, &block)
5
+ Array(klasses).each do |klass|
6
+ Barristan::Can.send(:define_method, "#{action}_#{klass.to_s.downcase}?",
7
+ block || lambda {|r, u| true })
8
+ end
9
+ end
10
+ end
11
+
12
+ class Can
13
+ attr :resource, :action, :user, :klass
14
+ private :resource, :action, :user, :klass
15
+
16
+ def initialize(resource, action, user)
17
+ @resource = resource
18
+ @klass = resource.class == Class ? resource : resource.class
19
+ @action = action
20
+ @user = user
21
+ end
22
+
23
+ def able?
24
+ send("#{action}_#{klass.to_s.downcase}?", resource, user)
25
+ end
26
+ end
27
+
28
+ class Guarded
29
+ def forbidden(&block)
30
+ @forbidden = block
31
+ end
32
+
33
+ def authorized(&block)
34
+ @authorized = block
35
+ end
36
+
37
+ def authorized!
38
+ throw :guarded, @authorized.call
39
+ end
40
+
41
+ def forbidden!
42
+ throw :guarded, @forbidden.call
43
+ end
44
+ end
45
+
46
+ class Acl
47
+ class << self
48
+ def new(&block)
49
+ yield Able.new
50
+ Module.new do
51
+ def guard(resource, action, user)
52
+ yield(guarded = Guarded.new)
53
+ catch(:guarded) do
54
+ Can.new(resource, action, user).
55
+ able? ? guarded.authorized! : guarded.forbidden!
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: barristan
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - chatgris
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !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: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Ruby authorization system.
31
+ email:
32
+ - jboyer@af83.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - LICENSE
38
+ - lib/barristan.rb
39
+ - lib/barristan/version.rb
40
+ homepage: https://github.com/chatgris/barristan
41
+ licenses: []
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 1.8.24
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Ruby authorization system.
64
+ test_files: []