perm 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/perm.rb +1 -0
- data/lib/perm/has_authorizer.rb +22 -0
- data/lib/perm/version.rb +1 -1
- data/test/authorizer_test.rb +1 -8
- data/test/has_authorizer_test.rb +38 -0
- data/test/test_helper.rb +8 -0
- metadata +6 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 712b2f9eb9df31bcd6f824064ada3f172463369f
|
4
|
+
data.tar.gz: 4294f43680738d9e0288b692c36d89fa3d5a5ba5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 50030e29d11afd37313356220d0f9c0c61cd68cf38903b6e095ad24d71741dae56bbc9e58d5694fa63c6abe67ea113316d6b6ea7b2064e87a5b2734840b4986a
|
7
|
+
data.tar.gz: 8ace162e770291491995a59c93e6a593dc750e79334b272c85aadb39ff1aefccd677c7caee85f41e628d19e45d4b15e1002b6e18ad824a24b64d1be589d7c7a5
|
data/lib/perm.rb
CHANGED
@@ -0,0 +1,22 @@
|
|
1
|
+
require "forwardable"
|
2
|
+
|
3
|
+
module Perm
|
4
|
+
module HasAuthorizer
|
5
|
+
extend Forwardable
|
6
|
+
def_delegators :"self.class", :authorizer_class, :user_method
|
7
|
+
|
8
|
+
def self.included(mod)
|
9
|
+
class << mod
|
10
|
+
attr_reader :authorizer_class, :user_method
|
11
|
+
def authorizes_with(klass, user_method)
|
12
|
+
@authorizer_class = klass
|
13
|
+
@user_method = user_method
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def authorized_user
|
19
|
+
@authorized_user ||= authorizer_class.new(send(user_method))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/perm/version.rb
CHANGED
data/test/authorizer_test.rb
CHANGED
@@ -0,0 +1,38 @@
|
|
1
|
+
require_relative "test_helper"
|
2
|
+
|
3
|
+
module Perm
|
4
|
+
class HasAuthorizerTest < MicroTest::Test
|
5
|
+
|
6
|
+
class ExampleAuthorizer < Authorizer
|
7
|
+
def can_view?(object)
|
8
|
+
user[:roles].include? :viewer
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Example
|
13
|
+
include HasAuthorizer
|
14
|
+
authorizes_with ExampleAuthorizer, :current_user
|
15
|
+
|
16
|
+
attr_writer :current_user
|
17
|
+
def current_user
|
18
|
+
@current_user ||= { roles: [:viewer] }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
before do
|
23
|
+
@example = Example.new
|
24
|
+
end
|
25
|
+
|
26
|
+
test "viewer can view" do
|
27
|
+
assert @example.authorized_user.can_view?({})
|
28
|
+
end
|
29
|
+
|
30
|
+
test "non-viewer cannot view" do
|
31
|
+
example = Example.new
|
32
|
+
example.current_user = { roles: [:other] }
|
33
|
+
assert !example.authorized_user.can_view?({})
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
data/test/test_helper.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: perm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathan Hopkins
|
@@ -89,8 +89,11 @@ extra_rdoc_files: []
|
|
89
89
|
files:
|
90
90
|
- lib/perm.rb
|
91
91
|
- lib/perm/authorizer.rb
|
92
|
+
- lib/perm/has_authorizer.rb
|
92
93
|
- lib/perm/version.rb
|
93
94
|
- test/authorizer_test.rb
|
95
|
+
- test/has_authorizer_test.rb
|
96
|
+
- test/test_helper.rb
|
94
97
|
homepage: https://github.com/hopsoft/perm
|
95
98
|
licenses:
|
96
99
|
- MIT
|
@@ -117,3 +120,5 @@ specification_version: 4
|
|
117
120
|
summary: Simple permission management
|
118
121
|
test_files:
|
119
122
|
- test/authorizer_test.rb
|
123
|
+
- test/has_authorizer_test.rb
|
124
|
+
- test/test_helper.rb
|