simple_can 1.0.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/LICENSE +25 -0
- data/lib/simple_can/basic_strategy.rb +26 -0
- data/lib/simple_can.rb +88 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5daf03f9ba885c925c8babfb731665322b46e6b0
|
4
|
+
data.tar.gz: 5c94acabaa8e22c4ef19f6c7c0d81fccdaf4ec80
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 504b9ba0bcdf5d56de83e80b12499d027a967e161eda69af9c3c80aae3fcc7af57c56ff11418da233a78add825b99a192a867a028a9442c73d1e5ae409fad2f1
|
7
|
+
data.tar.gz: 8244f00ad7c0720279abe255612841db5730e02591d978ba3ef00ad63036367f43cebac22776212a97b9f8dd09aa0700e9b540636fae888baf9f19b37db93125
|
data/LICENSE
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
BSD 2-Clause License
|
2
|
+
|
3
|
+
Copyright (c) 2017, Matthias Geier
|
4
|
+
All rights reserved.
|
5
|
+
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
8
|
+
|
9
|
+
* Redistributions of source code must retain the above copyright notice, this
|
10
|
+
list of conditions and the following disclaimer.
|
11
|
+
|
12
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
14
|
+
and/or other materials provided with the distribution.
|
15
|
+
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
17
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
18
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
19
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
20
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
21
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
22
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
23
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
24
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
25
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module SimpleCan
|
2
|
+
module BasicStrategy
|
3
|
+
extend self
|
4
|
+
|
5
|
+
ROLES = %w(read write manage).freeze
|
6
|
+
REV_ROLES = ROLES.map.with_index.to_h.freeze
|
7
|
+
|
8
|
+
def test(role, capability)
|
9
|
+
capability = 0 if capability.nil?
|
10
|
+
capability >= to_capability(role)
|
11
|
+
end
|
12
|
+
|
13
|
+
def roles
|
14
|
+
ROLES
|
15
|
+
end
|
16
|
+
|
17
|
+
def fail(_role, _name)
|
18
|
+
:unauthorized
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_capability(role)
|
22
|
+
return if role.nil?
|
23
|
+
REV_ROLES[role]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/simple_can.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
require "simple_can/basic_strategy"
|
2
|
+
|
3
|
+
module SimpleCan
|
4
|
+
THREAD_VAR = "simple_can.capability"
|
5
|
+
|
6
|
+
class << self; attr_accessor :strategy; end
|
7
|
+
|
8
|
+
class Unauthorized < StandardError; end
|
9
|
+
|
10
|
+
def self.included(mod)
|
11
|
+
meta = class << mod; self; end
|
12
|
+
meta.send(:alias_method, :orig_method_added, :method_added)
|
13
|
+
meta.send(:alias_method, :orig_singleton_method_added,
|
14
|
+
:singleton_method_added)
|
15
|
+
mod.extend(ClassMethods)
|
16
|
+
|
17
|
+
strategy.roles.each do |role|
|
18
|
+
[meta, mod].each do |scope|
|
19
|
+
scope.send(:define_method, "#{role}?") do
|
20
|
+
mod.strategy_set!
|
21
|
+
SimpleCan.strategy.test(role, mod.capability)
|
22
|
+
end
|
23
|
+
scope.send(:define_method, "#{role}!") do
|
24
|
+
mod.strategy_set!
|
25
|
+
next if SimpleCan.strategy.test(role, mod.capability)
|
26
|
+
raise SimpleCan::Unauthorized, "unauthorized with #{role}"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
module ClassMethods
|
33
|
+
def strategy_set!
|
34
|
+
raise "strategy missing" if SimpleCan.strategy.nil?
|
35
|
+
end
|
36
|
+
|
37
|
+
def method_added(method)
|
38
|
+
orig_method_added(method)
|
39
|
+
add_method_to(self, method)
|
40
|
+
end
|
41
|
+
|
42
|
+
def singleton_method_added(method)
|
43
|
+
orig_singleton_method_added(method)
|
44
|
+
add_method_to((class << self; self; end), method)
|
45
|
+
end
|
46
|
+
|
47
|
+
def add_method_to(scope, method)
|
48
|
+
strategy_set!
|
49
|
+
|
50
|
+
klass = self
|
51
|
+
method = method.to_s
|
52
|
+
role, name, do_raise = SimpleCan.strategy.roles.reduce(nil) do |acc, r|
|
53
|
+
acc || method.match(/^#{r}_(.+(!)|.+)$/)&.captures&.unshift(r)
|
54
|
+
end
|
55
|
+
return if name.nil?
|
56
|
+
scope.send(:define_method, name) do |*args, &blk|
|
57
|
+
can = SimpleCan.strategy.test(role, klass.capability)
|
58
|
+
if !can && !do_raise.nil?
|
59
|
+
raise SimpleCan::Unauthorized, "unauthorized for #{name} with #{role}"
|
60
|
+
elsif !can
|
61
|
+
if respond_to?("fail_#{name}")
|
62
|
+
return send("fail_#{name}")
|
63
|
+
else
|
64
|
+
return SimpleCan.strategy.fail(role, name)
|
65
|
+
end
|
66
|
+
else
|
67
|
+
return send(method, *args, &blk)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def capability=(role)
|
73
|
+
strategy_set!
|
74
|
+
Thread.current[THREAD_VAR] = SimpleCan.strategy.to_capability(role)
|
75
|
+
end
|
76
|
+
|
77
|
+
def capability
|
78
|
+
Thread.current[THREAD_VAR]
|
79
|
+
end
|
80
|
+
|
81
|
+
def with_capability(role)
|
82
|
+
self.capability = role
|
83
|
+
yield
|
84
|
+
ensure
|
85
|
+
self.capability = nil
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_can
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matthias Geier
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-03-17 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- LICENSE
|
20
|
+
- lib/simple_can.rb
|
21
|
+
- lib/simple_can/basic_strategy.rb
|
22
|
+
homepage: https://github.com/matthias-geier/simple_can
|
23
|
+
licenses:
|
24
|
+
- BSD-2-Clause
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 2.5.1
|
43
|
+
signing_key:
|
44
|
+
specification_version: 4
|
45
|
+
summary: A thread-safe minimal authorization helper
|
46
|
+
test_files: []
|