grant-front 0.0.1 → 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 +4 -4
- data/README.md +21 -7
- data/lib/grant-front.rb +28 -2
- data/lib/grant-front/policy.rb +3 -1
- data/lib/grant-front/version.rb +1 -1
- data/spec/lib/policy_spec.rb +21 -0
- data/spec/lib/version_spec.rb +2 -2
- data/spec/spec_helper.rb +24 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1ca03d453436703cf2347bb50b4533e377232377
|
4
|
+
data.tar.gz: b362f5abbe106f1b64dcfa4e1726d70a13e5f922
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 952db46b0b3581b8e060ed536e6730c882ba187a37b6a0f73b816067710ea1e14adfefb59a740f8590b2b59a510065b7140d24e1b3d7e683077eb28592bf53ea
|
7
|
+
data.tar.gz: 94f61b0e99747671044ac3feedef507f10639779401bd96b14e608c84f2d1fc5d930cf02c8e48e702c19124b542736fa2a797b6cd24c6d772e411b8324b8940c
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# GrantFront
|
2
2
|
|
3
|
-
[](http://badge.fury.io/rb/grant-front) [](https://travis-ci.org/ogom/grant-front)
|
4
4
|
|
5
5
|
Authorization Grant Front on Rails.
|
6
6
|
|
@@ -26,17 +26,30 @@ $ gem install grant-front
|
|
26
26
|
|
27
27
|
## Usage
|
28
28
|
|
29
|
+
```
|
30
|
+
class ApplicationPolicy
|
31
|
+
include GrantFront
|
32
|
+
attr_reader :user, :record
|
33
|
+
|
34
|
+
def initialize(user, record)
|
35
|
+
@user = user
|
36
|
+
@record = record
|
37
|
+
end
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
29
41
|
```
|
30
42
|
class UserPolicy < ApplicationPolicy
|
31
43
|
def create?
|
32
44
|
grant :foo, :bar, :baz
|
33
45
|
end
|
34
46
|
|
35
|
-
def
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
47
|
+
def update?
|
48
|
+
grant :foo, :bar
|
49
|
+
end
|
50
|
+
|
51
|
+
def destroy?
|
52
|
+
grant :bar, :baz
|
40
53
|
end
|
41
54
|
end
|
42
55
|
```
|
@@ -48,7 +61,8 @@ $ rake grant_front:draw
|
|
48
61
|
||foo|bar|baz|
|
49
62
|
|:-:|:-:|:-:|:-:|
|
50
63
|
|create|o|o|o|
|
51
|
-
|
|
64
|
+
|update|o|o||
|
65
|
+
|destroy||o|o|
|
52
66
|
|
53
67
|
|
54
68
|
## License
|
data/lib/grant-front.rb
CHANGED
@@ -1,9 +1,35 @@
|
|
1
1
|
require 'grant-front/version'
|
2
2
|
require 'grant-front/grant'
|
3
3
|
require 'grant-front/policy'
|
4
|
-
require 'grant-front/rails' if defined? Rails::Railtie
|
5
4
|
|
6
5
|
module GrantFront
|
7
|
-
|
6
|
+
def self.included(klass)
|
7
|
+
klass.extend(ClassMethods)
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def mock!
|
12
|
+
alias_method :keep_grant, :grant
|
13
|
+
alias_method :grant, :mock_grant
|
14
|
+
end
|
15
|
+
|
16
|
+
def unmock!
|
17
|
+
return unless method_defined? :mock_grant
|
18
|
+
alias_method :grant, :keep_grant
|
19
|
+
end
|
8
20
|
end
|
21
|
+
|
22
|
+
private
|
23
|
+
def grant(*roles)
|
24
|
+
roles.each do |role|
|
25
|
+
return true if user.roles.include? role
|
26
|
+
end
|
27
|
+
return false
|
28
|
+
end
|
29
|
+
|
30
|
+
def mock_grant(*roles)
|
31
|
+
roles
|
32
|
+
end
|
9
33
|
end
|
34
|
+
|
35
|
+
require 'grant-front/rails' if defined? Rails::Railtie
|
data/lib/grant-front/policy.rb
CHANGED
@@ -19,13 +19,14 @@ module GrantFront
|
|
19
19
|
raw = {methods: {}, roles: []}
|
20
20
|
reg = Regexp.new(/\?$/)
|
21
21
|
user = Struct.new(:id, :roles).new(1, [])
|
22
|
+
ApplicationPolicy.mock!
|
22
23
|
|
23
24
|
policy = klass.new(user, user)
|
24
25
|
policy.methods.each do |name|
|
25
26
|
if name =~ reg
|
26
27
|
owner = policy.method(name).owner
|
27
28
|
if owner == klass or owner == ApplicationPolicy
|
28
|
-
|
29
|
+
roles = policy.send(name)
|
29
30
|
roles ||= []
|
30
31
|
raw[:methods][name.to_s.gsub(reg, '').to_sym] = roles
|
31
32
|
raw[:roles] += roles
|
@@ -33,6 +34,7 @@ module GrantFront
|
|
33
34
|
end
|
34
35
|
end
|
35
36
|
end
|
37
|
+
ApplicationPolicy.unmock!
|
36
38
|
|
37
39
|
raw
|
38
40
|
end
|
data/lib/grant-front/version.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe GrantFront::Policy do
|
4
|
+
describe ".all" do
|
5
|
+
it "returns a policy" do
|
6
|
+
expect(GrantFront::Policy.all.count).to eq(1)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe ".find" do
|
11
|
+
let(:policy) { GrantFront::Policy.all.first }
|
12
|
+
|
13
|
+
it "returns some methods" do
|
14
|
+
expect(GrantFront::Policy.find(policy)[:methods].keys).to eq([:create, :update, :destroy])
|
15
|
+
end
|
16
|
+
|
17
|
+
it "returns some roles" do
|
18
|
+
expect(GrantFront::Policy.find(policy)[:roles]).to eq([:foo, :bar, :baz])
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/spec/lib/version_spec.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -1 +1,25 @@
|
|
1
1
|
require 'grant-front'
|
2
|
+
|
3
|
+
class ApplicationPolicy
|
4
|
+
include GrantFront
|
5
|
+
attr_reader :user, :record
|
6
|
+
|
7
|
+
def initialize(user, record)
|
8
|
+
@user = user
|
9
|
+
@record = record
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class UserPolicy < ApplicationPolicy
|
14
|
+
def create?
|
15
|
+
grant :foo, :bar, :baz
|
16
|
+
end
|
17
|
+
|
18
|
+
def update?
|
19
|
+
grant :foo, :bar
|
20
|
+
end
|
21
|
+
|
22
|
+
def destroy?
|
23
|
+
grant :bar, :baz
|
24
|
+
end
|
25
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grant-front
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ogom
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -59,6 +59,7 @@ files:
|
|
59
59
|
- lib/grant-front/version.rb
|
60
60
|
- lib/tasks/grant-front.rake
|
61
61
|
- spec/grant-front_spec.rb
|
62
|
+
- spec/lib/policy_spec.rb
|
62
63
|
- spec/lib/version_spec.rb
|
63
64
|
- spec/spec_helper.rb
|
64
65
|
homepage: http://ogom.github.io/grant-front
|
@@ -87,5 +88,6 @@ specification_version: 4
|
|
87
88
|
summary: Authorization Grant Front.
|
88
89
|
test_files:
|
89
90
|
- spec/grant-front_spec.rb
|
91
|
+
- spec/lib/policy_spec.rb
|
90
92
|
- spec/lib/version_spec.rb
|
91
93
|
- spec/spec_helper.rb
|