grant-front 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +38 -25
- data/lib/grant-front/diagram.rb +13 -11
- data/lib/grant-front/engine.rb +41 -8
- data/lib/grant-front/policy.rb +26 -12
- data/lib/grant-front/version.rb +1 -1
- data/lib/templates/layouts/application.html.erb +40 -3
- data/spec/lib/diagram_spec.rb +19 -0
- data/spec/lib/policy_spec.rb +15 -5
- data/spec/lib/version_spec.rb +1 -1
- 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: 5e3f6aaccaca24c6a5e7dff34a3002008b1ee470
|
4
|
+
data.tar.gz: 44be6c4edf11da60d4f76185a5f45915d17d6b41
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 327888963d8caae5220afa74ddb45779956d283ae7f0ac179e5d086b2c17d8afbbe84d3582bfe4147dfc7675a0a9d2909ff8971a43fa14f5a687b0681136277a
|
7
|
+
data.tar.gz: 199be717bcf61ac252ea1e954a87dd3b205346c50b1c30cdbe5cd6eb3910791d56620978656269c6ff0322caba613b4a9ae98bade1c49e4c25150b222d044936
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/grant-front.svg)](http://badge.fury.io/rb/grant-front) [![Build Status](https://travis-ci.org/ogom/grant-front.png?branch=master)](https://travis-ci.org/ogom/grant-front)
|
4
4
|
|
5
|
-
|
5
|
+
Policies grant of role for authorization on Rails.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -18,15 +18,46 @@ And then execute:
|
|
18
18
|
$ bundle
|
19
19
|
```
|
20
20
|
|
21
|
-
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Rack
|
24
|
+
|
25
|
+
Add this line to your `config/routes.rb`:
|
22
26
|
|
23
27
|
```
|
24
|
-
|
28
|
+
mount GrantFront::Engine, at: '/rails/info/policies'
|
25
29
|
```
|
26
30
|
|
27
|
-
|
31
|
+
Draw by selecting the policy:
|
32
|
+
|
33
|
+
![example](http://ogom.github.io/grant-front/assets/img/example.png)
|
34
|
+
|
35
|
+
### Rake
|
36
|
+
|
37
|
+
Draw by rake command:
|
38
|
+
|
39
|
+
```
|
40
|
+
$ rake grant_front:draw
|
41
|
+
|
42
|
+
### User
|
43
|
+
|
44
|
+
||foo|bar|baz|
|
45
|
+
|:-|:-:|:-:|:-:|
|
46
|
+
|create|o|o|o|
|
47
|
+
|update|o|o||
|
48
|
+
|destroy||o|o|
|
49
|
+
```
|
50
|
+
|
51
|
+
## Policy Example
|
52
|
+
|
53
|
+
Install the [Pundit](https://github.com/elabs/pundit), and to the generate:
|
28
54
|
|
29
|
-
|
55
|
+
```
|
56
|
+
$ rails generate pundit:install
|
57
|
+
$ rails generate pundit:policy user
|
58
|
+
```
|
59
|
+
|
60
|
+
Include **GrantFront** line to your `policies/application_policy.rb`:
|
30
61
|
|
31
62
|
```
|
32
63
|
class ApplicationPolicy
|
@@ -40,6 +71,8 @@ class ApplicationPolicy
|
|
40
71
|
end
|
41
72
|
```
|
42
73
|
|
74
|
+
Add **grant** line to your `policies/user_policy.rb`:
|
75
|
+
|
43
76
|
```
|
44
77
|
class UserPolicy < ApplicationPolicy
|
45
78
|
def create?
|
@@ -56,26 +89,6 @@ class UserPolicy < ApplicationPolicy
|
|
56
89
|
end
|
57
90
|
```
|
58
91
|
|
59
|
-
### Rake
|
60
|
-
|
61
|
-
```
|
62
|
-
$ rake grant_front:draw
|
63
|
-
```
|
64
|
-
|
65
|
-
||foo|bar|baz|
|
66
|
-
|:-:|:-:|:-:|:-:|
|
67
|
-
|create|o|o|o|
|
68
|
-
|update|o|o||
|
69
|
-
|destroy||o|o|
|
70
|
-
|
71
|
-
### Rack
|
72
|
-
|
73
|
-
Add this line to your `config/routes.rb`:
|
74
|
-
|
75
|
-
```
|
76
|
-
mount GrantFront::Engine, at: '/rails/info/policies'
|
77
|
-
```
|
78
|
-
|
79
92
|
## License
|
80
93
|
|
81
94
|
* MIT
|
data/lib/grant-front/diagram.rb
CHANGED
@@ -11,21 +11,23 @@ module GrantFront
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def create
|
14
|
-
policies =
|
15
|
-
GrantFront::Policy.all(@options).each do |
|
16
|
-
|
14
|
+
policies = []
|
15
|
+
GrantFront::Policy.all(@options).each do |policy|
|
16
|
+
if @options[:classes].nil? or @options[:classes].include?(policy.klass)
|
17
|
+
policies << GrantFront::Policy.find(policy.klass)
|
18
|
+
end
|
17
19
|
end
|
18
20
|
|
19
21
|
text = ''
|
20
|
-
policies.
|
21
|
-
text += "\n### #{policy.
|
22
|
-
if
|
23
|
-
text += "||#{
|
24
|
-
text += "|:-|#{
|
25
|
-
|
22
|
+
policies.each do |policy|
|
23
|
+
text += "\n### #{policy.name}\n\n"
|
24
|
+
if policy.roles.count > 0
|
25
|
+
text += "||#{policy.roles.join('|')}|\n"
|
26
|
+
text += "|:-|#{policy.roles.map{':-:'}.join('|')}|\n"
|
27
|
+
policy.methods.keys.each do |method|
|
26
28
|
raw = "|#{method}|"
|
27
|
-
|
28
|
-
raw += 'o' if
|
29
|
+
policy.roles.each do |role|
|
30
|
+
raw += 'o' if policy.methods[method].include?(role)
|
29
31
|
raw += '|'
|
30
32
|
end
|
31
33
|
text += "#{raw}\n"
|
data/lib/grant-front/engine.rb
CHANGED
@@ -9,8 +9,6 @@ module GrantFront
|
|
9
9
|
|
10
10
|
def call(env)
|
11
11
|
@request = Rack::Request.new(env)
|
12
|
-
text = Diagram.new(rake: false).create
|
13
|
-
policy_tag = Kramdown::Document.new(text).to_html
|
14
12
|
|
15
13
|
status = 200
|
16
14
|
headers = {'Content-Type' => 'text/html'}
|
@@ -20,12 +18,47 @@ module GrantFront
|
|
20
18
|
end
|
21
19
|
|
22
20
|
private
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
21
|
+
def policy_link_to
|
22
|
+
"<a href=#{request.script_name}>Policy</a>"
|
23
|
+
end
|
24
|
+
|
25
|
+
def policies_tag
|
26
|
+
raw = ""
|
27
|
+
policies = GrantFront::Policy.all(rake: false)
|
28
|
+
|
29
|
+
raw += "<div><ul>"
|
30
|
+
raw += policies.map do |policy|
|
31
|
+
raw = "<li"
|
32
|
+
if '/' + policy.urn == request.path_info
|
33
|
+
raw += " class='active'"
|
34
|
+
end
|
35
|
+
raw += "><a href=#{request.script_name}/#{policy.urn}>#{policy.name}</a>"
|
36
|
+
raw += "</li>"
|
37
|
+
end.join("\n")
|
38
|
+
raw += "</ul></div>"
|
39
|
+
|
40
|
+
raw
|
41
|
+
end
|
42
|
+
|
43
|
+
def policy_tag
|
44
|
+
policies = GrantFront::Policy.all(rake: false)
|
45
|
+
classes = policies.inject([]) do |arr, policy|
|
46
|
+
arr << policy.klass if '/' + policy.urn == request.path_info
|
47
|
+
arr
|
48
|
+
end
|
49
|
+
classes = nil if classes.count == 0
|
50
|
+
|
51
|
+
text = Diagram.new(rake: false, classes: classes).create
|
52
|
+
Kramdown::Document.new(text).to_html
|
53
|
+
end
|
54
|
+
|
55
|
+
def application_template
|
56
|
+
root_path = Pathname.new(File.expand_path('..', File.dirname(__FILE__)))
|
57
|
+
templates_path = File.join(root_path, 'templates')
|
58
|
+
application_layout = File.expand_path('application.html.erb', File.join(templates_path, 'layouts'))
|
59
|
+
File.read(application_layout)
|
60
|
+
end
|
61
|
+
# end private
|
29
62
|
|
30
63
|
class << self
|
31
64
|
def prototype
|
data/lib/grant-front/policy.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
module GrantFront
|
2
2
|
class Policy
|
3
|
+
attr_accessor :klass, :name, :methods, :roles
|
4
|
+
|
3
5
|
class << self
|
4
6
|
def all(options={})
|
5
7
|
options[:rake] = true if options[:rake].nil?
|
@@ -10,7 +12,7 @@ module GrantFront
|
|
10
12
|
require item if options[:rake]
|
11
13
|
name = File.basename(item, '.*')
|
12
14
|
unless name == 'application_policy'
|
13
|
-
arr <<
|
15
|
+
arr << self.new(name.camelize)
|
14
16
|
end
|
15
17
|
arr
|
16
18
|
end
|
@@ -21,7 +23,7 @@ module GrantFront
|
|
21
23
|
unless name == :Config
|
22
24
|
klass = Object.const_get(name)
|
23
25
|
if klass.class == Class && klass.superclass == ApplicationPolicy
|
24
|
-
arr << klass
|
26
|
+
arr << self.new(klass)
|
25
27
|
end
|
26
28
|
end
|
27
29
|
arr
|
@@ -31,29 +33,41 @@ module GrantFront
|
|
31
33
|
constants
|
32
34
|
end
|
33
35
|
|
34
|
-
def find(klass
|
35
|
-
|
36
|
+
def find(klass)
|
37
|
+
policy = self.new(klass.to_s)
|
38
|
+
klass = Object.const_get(klass.to_s)
|
36
39
|
reg = Regexp.new(/\?$/)
|
37
40
|
user = Struct.new(:id, :roles).new(1, [])
|
38
41
|
|
39
42
|
klass.mock!
|
40
|
-
|
41
|
-
|
43
|
+
klass_policy = klass.new(user, user)
|
44
|
+
klass_policy.methods.each do |name|
|
42
45
|
if name =~ reg
|
43
|
-
owner =
|
46
|
+
owner = klass_policy.method(name).owner
|
44
47
|
if owner == klass or owner == ApplicationPolicy
|
45
|
-
roles =
|
48
|
+
roles = klass_policy.send(name)
|
46
49
|
roles ||= []
|
47
|
-
|
48
|
-
|
49
|
-
raw[:roles].uniq!
|
50
|
+
policy.methods[name.to_s.gsub(reg, '').to_sym] = roles
|
51
|
+
policy.roles += roles
|
50
52
|
end
|
51
53
|
end
|
52
54
|
end
|
53
55
|
klass.unmock!
|
54
56
|
|
55
|
-
|
57
|
+
policy.roles.uniq!
|
58
|
+
policy
|
56
59
|
end
|
57
60
|
end
|
61
|
+
|
62
|
+
def initialize(klass)
|
63
|
+
@klass = klass.to_s
|
64
|
+
@name = @klass.gsub(/Policy$/, '')
|
65
|
+
@methods = {}
|
66
|
+
@roles = []
|
67
|
+
end
|
68
|
+
|
69
|
+
def urn
|
70
|
+
self.name.downcase
|
71
|
+
end
|
58
72
|
end
|
59
73
|
end
|
data/lib/grant-front/version.rb
CHANGED
@@ -17,11 +17,43 @@
|
|
17
17
|
white-space: pre-wrap;
|
18
18
|
}
|
19
19
|
|
20
|
+
a {
|
21
|
+
color: #000;
|
22
|
+
text-decoration: none;
|
23
|
+
padding-left: 4px;
|
24
|
+
padding-right: 10px;
|
25
|
+
}
|
26
|
+
a:visited { color: #000; }
|
27
|
+
a:hover { color: #000; background-color:#fff; }
|
28
|
+
|
20
29
|
h2 { padding-left: 10px; }
|
21
30
|
|
31
|
+
li { padding: 0px 4px; }
|
32
|
+
li.active { background-color: #666;}
|
33
|
+
li.active > a { color: #fff;}
|
34
|
+
li.active > a:hover { color: #fff; background-color:#666; }
|
35
|
+
|
36
|
+
div.policies {
|
37
|
+
float:left;
|
38
|
+
padding-right: 20px;
|
39
|
+
padding-bottom: 400px;
|
40
|
+
border-right: solid #eee;
|
41
|
+
}
|
42
|
+
|
43
|
+
div.policies h3 {
|
44
|
+
padding-left: 16px;
|
45
|
+
}
|
46
|
+
|
22
47
|
div.policy {
|
23
|
-
padding-
|
24
|
-
|
48
|
+
padding-left: 150px;
|
49
|
+
}
|
50
|
+
|
51
|
+
div.policy h3 {
|
52
|
+
padding-top: 12px;
|
53
|
+
}
|
54
|
+
|
55
|
+
div.policy table {
|
56
|
+
padding-left: 10px;
|
25
57
|
}
|
26
58
|
|
27
59
|
div.policy th,
|
@@ -41,7 +73,12 @@
|
|
41
73
|
</head>
|
42
74
|
<body>
|
43
75
|
<h2>Policies</h2>
|
44
|
-
<p>
|
76
|
+
<p>Policies grant of role for authorization</p>
|
77
|
+
|
78
|
+
<div class='policies'>
|
79
|
+
<h3><%= policy_link_to %></h3>
|
80
|
+
<%= policies_tag %>
|
81
|
+
</div>
|
45
82
|
|
46
83
|
<div class='policy'>
|
47
84
|
<%= policy_tag %>
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe GrantFront::Diagram do
|
4
|
+
describe "#create" do
|
5
|
+
let(:diagram) { GrantFront::Diagram.new(classes: ['UserPolicy']) }
|
6
|
+
|
7
|
+
it "returns diagram" do
|
8
|
+
expect(diagram.create).to eq(
|
9
|
+
"\n### User\n" +
|
10
|
+
"\n" +
|
11
|
+
"||foo|bar|baz|\n" +
|
12
|
+
"|:-|:-:|:-:|:-:|\n" +
|
13
|
+
"|create|o|o|o|\n" +
|
14
|
+
"|update|o|o||\n" +
|
15
|
+
"|destroy||o|o|\n"
|
16
|
+
)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/spec/lib/policy_spec.rb
CHANGED
@@ -2,20 +2,30 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe GrantFront::Policy do
|
4
4
|
describe ".all" do
|
5
|
-
|
6
|
-
|
5
|
+
let(:policy) { GrantFront::Policy.all.first}
|
6
|
+
|
7
|
+
it "returns klass of policy" do
|
8
|
+
expect(policy.klass).to eq('UserPolicy')
|
9
|
+
end
|
10
|
+
|
11
|
+
it "returns name of policy" do
|
12
|
+
expect(policy.name).to eq('User')
|
13
|
+
end
|
14
|
+
|
15
|
+
it "returns urn of policy" do
|
16
|
+
expect(policy.urn).to eq('user')
|
7
17
|
end
|
8
18
|
end
|
9
19
|
|
10
20
|
describe ".find" do
|
11
|
-
let(:
|
21
|
+
let(:klass) {GrantFront::Policy.all.first.klass}
|
12
22
|
|
13
23
|
it "returns some methods" do
|
14
|
-
expect(GrantFront::Policy.find(
|
24
|
+
expect(GrantFront::Policy.find(klass).methods.keys).to eq([:create, :update, :destroy])
|
15
25
|
end
|
16
26
|
|
17
27
|
it "returns some roles" do
|
18
|
-
expect(GrantFront::Policy.find(
|
28
|
+
expect(GrantFront::Policy.find(klass).roles).to eq([:foo, :bar, :baz])
|
19
29
|
end
|
20
30
|
end
|
21
31
|
end
|
data/spec/lib/version_spec.rb
CHANGED
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.
|
4
|
+
version: 0.2.0
|
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-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: kramdown
|
@@ -89,6 +89,7 @@ files:
|
|
89
89
|
- lib/tasks/grant-front.rake
|
90
90
|
- lib/templates/layouts/application.html.erb
|
91
91
|
- spec/grant-front_spec.rb
|
92
|
+
- spec/lib/diagram_spec.rb
|
92
93
|
- spec/lib/policy_spec.rb
|
93
94
|
- spec/lib/version_spec.rb
|
94
95
|
- spec/spec_helper.rb
|
@@ -118,6 +119,7 @@ specification_version: 4
|
|
118
119
|
summary: Authorization Grant Front.
|
119
120
|
test_files:
|
120
121
|
- spec/grant-front_spec.rb
|
122
|
+
- spec/lib/diagram_spec.rb
|
121
123
|
- spec/lib/policy_spec.rb
|
122
124
|
- spec/lib/version_spec.rb
|
123
125
|
- spec/spec_helper.rb
|