grant-front 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1ca03d453436703cf2347bb50b4533e377232377
4
- data.tar.gz: b362f5abbe106f1b64dcfa4e1726d70a13e5f922
3
+ metadata.gz: d962be2a4ad6dc6601aa2286f7d4de23349605da
4
+ data.tar.gz: bda9107afc0721585ff8304c6d7a531eea480d58
5
5
  SHA512:
6
- metadata.gz: 952db46b0b3581b8e060ed536e6730c882ba187a37b6a0f73b816067710ea1e14adfefb59a740f8590b2b59a510065b7140d24e1b3d7e683077eb28592bf53ea
7
- data.tar.gz: 94f61b0e99747671044ac3feedef507f10639779401bd96b14e608c84f2d1fc5d930cf02c8e48e702c19124b542736fa2a797b6cd24c6d772e411b8324b8940c
6
+ metadata.gz: 310d425f08fec632a0f4c81ca2cf62ea45fb2087bd450aa903fa99b06e90ed778d3eaadc8e905f3861e164739322a368ee3491875b7032b5bf16e7800b8e17b3
7
+ data.tar.gz: bda285de8ccd444fb5bfc5419757461f0d909f4467b75a820714c44c1c913f9d01c85cbfd08b7653e66af3d978a75e80fe0524625e610ae5f5b12cefd7cdfbca
data/README.md CHANGED
@@ -26,6 +26,8 @@ $ gem install grant-front
26
26
 
27
27
  ## Usage
28
28
 
29
+ ### Policy Example
30
+
29
31
  ```
30
32
  class ApplicationPolicy
31
33
  include GrantFront
@@ -54,6 +56,8 @@ class UserPolicy < ApplicationPolicy
54
56
  end
55
57
  ```
56
58
 
59
+ ### Rake
60
+
57
61
  ```
58
62
  $ rake grant_front:draw
59
63
  ```
@@ -64,6 +68,13 @@ $ rake grant_front:draw
64
68
  |update|o|o||
65
69
  |destroy||o|o|
66
70
 
71
+ ### Rack
72
+
73
+ Add this line to your `config/routes.rb`:
74
+
75
+ ```
76
+ mount GrantFront::Engine, at: '/rails/info/policies'
77
+ ```
67
78
 
68
79
  ## License
69
80
 
@@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_dependency 'kramdown'
21
22
  spec.add_development_dependency 'bundler'
22
23
  spec.add_development_dependency 'rake'
23
24
  end
@@ -1,6 +1,7 @@
1
- require 'grant-front/version'
2
- require 'grant-front/grant'
3
- require 'grant-front/policy'
1
+ require_relative 'grant-front/version'
2
+ require_relative 'grant-front/policy'
3
+ require_relative 'grant-front/diagram'
4
+ require_relative 'grant-front/engine'
4
5
 
5
6
  module GrantFront
6
7
  def self.included(klass)
@@ -32,4 +33,4 @@ module GrantFront
32
33
  end
33
34
  end
34
35
 
35
- require 'grant-front/rails' if defined? Rails::Railtie
36
+ require_relative 'grant-front/rails' if defined? Rails::Railtie
@@ -0,0 +1,40 @@
1
+ module GrantFront
2
+ class Diagram
3
+ class << self
4
+ def draw(options={})
5
+ puts self.new(options).create
6
+ end
7
+ end
8
+
9
+ def initialize(options={})
10
+ @options = options
11
+ end
12
+
13
+ def create
14
+ policies = {}
15
+ GrantFront::Policy.all(@options).each do |klass|
16
+ policies[klass.to_s.to_sym] = GrantFront::Policy.find(klass)
17
+ end
18
+
19
+ text = ''
20
+ policies.keys.each do |policy|
21
+ text += "\n### #{policy.to_s.gsub(/Policy$/, '')} \n\n"
22
+ if policies[policy][:roles].count > 0
23
+ text += "||#{policies[policy][:roles].join('|')}|\n"
24
+ text += "|:-:|#{policies[policy][:roles].map{':-:'}.join('|')}|\n"
25
+ policies[policy][:methods].keys.each do |method|
26
+ raw = "|#{method}|"
27
+ policies[policy][:roles].each do |role|
28
+ raw += 'o' if policies[policy][:methods][method].include?(role)
29
+ raw += '|'
30
+ end
31
+ text += "#{raw}\n"
32
+ end
33
+ else
34
+ text += "* no policy\n"
35
+ end
36
+ end
37
+ text
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,40 @@
1
+ require 'rack'
2
+ require 'kramdown'
3
+ require 'erb'
4
+ require 'pathname'
5
+
6
+ module GrantFront
7
+ class Engine
8
+ attr_accessor :request, :tree
9
+
10
+ def call(env)
11
+ @request = Rack::Request.new(env)
12
+ text = Diagram.new(rake: false).create
13
+ policy_tag = Kramdown::Document.new(text).to_html
14
+
15
+ status = 200
16
+ headers = {'Content-Type' => 'text/html'}
17
+ body = ERB.new(application_template).result(binding)
18
+
19
+ [status, headers, [body]]
20
+ end
21
+
22
+ private
23
+ def application_template
24
+ root_path = Pathname.new(File.expand_path('..', File.dirname(__FILE__)))
25
+ templates_path = File.join(root_path, 'templates')
26
+ application_layout = File.expand_path('application.html.erb', File.join(templates_path, 'layouts'))
27
+ File.read(application_layout)
28
+ end
29
+
30
+ class << self
31
+ def prototype
32
+ @prototype ||= new
33
+ end
34
+
35
+ def call(env)
36
+ prototype.call(env)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,26 +1,42 @@
1
1
  module GrantFront
2
2
  class Policy
3
3
  class << self
4
- def all
5
- requires
4
+ def all(options={})
5
+ options[:rake] = true if options[:rake].nil?
6
6
 
7
- Object.constants.inject([]) do |arr, name|
8
- unless name == :Config
9
- klass = Object.const_get(name)
10
- if klass.class == Class && klass.superclass == ApplicationPolicy
11
- arr << klass
7
+ if defined? Rails
8
+ path = Rails.root.join('app/policies/*.rb').to_s
9
+ constants = Dir.glob(path).inject([]) do |arr, item|
10
+ require item if options[:rake]
11
+ name = File.basename(item, '.*')
12
+ unless name == 'application_policy'
13
+ arr << Object.const_get(name.camelize)
14
+ end
15
+ arr
16
+ end
17
+ end
18
+
19
+ if options[:rake]
20
+ constants = Object.constants.inject([]) do |arr, name|
21
+ unless name == :Config
22
+ klass = Object.const_get(name)
23
+ if klass.class == Class && klass.superclass == ApplicationPolicy
24
+ arr << klass
25
+ end
12
26
  end
27
+ arr
13
28
  end
14
- arr
15
29
  end
30
+
31
+ constants
16
32
  end
17
33
 
18
34
  def find(klass=nil)
19
35
  raw = {methods: {}, roles: []}
20
36
  reg = Regexp.new(/\?$/)
21
37
  user = Struct.new(:id, :roles).new(1, [])
22
- ApplicationPolicy.mock!
23
38
 
39
+ klass.mock!
24
40
  policy = klass.new(user, user)
25
41
  policy.methods.each do |name|
26
42
  if name =~ reg
@@ -34,19 +50,10 @@ module GrantFront
34
50
  end
35
51
  end
36
52
  end
37
- ApplicationPolicy.unmock!
53
+ klass.unmock!
38
54
 
39
55
  raw
40
56
  end
41
-
42
- private
43
- def requires
44
- if defined? Rails
45
- Dir.glob(Rails.root.join('app/policies/*.rb').to_s).each do |name|
46
- require name
47
- end
48
- end
49
- end
50
57
  end
51
58
  end
52
59
  end
@@ -1,3 +1,3 @@
1
1
  module GrantFront
2
- VERSION = '0.0.2'.freeze
2
+ VERSION = '0.1.0'.freeze
3
3
  end
@@ -1,6 +1,6 @@
1
1
  namespace :grant_front do
2
2
  desc 'Draws Authorization Grant Roles'
3
3
  task :draw do
4
- GrantFront::Grant.draw
4
+ GrantFront::Diagram.draw
5
5
  end
6
6
  end
@@ -0,0 +1,37 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head><title>Policies</title>
4
+ <style type="text/css">
5
+ body { background-color: #fff; color: #333; }
6
+
7
+ body, p, ol, ul, td {
8
+ font-family: helvetica, verdana, arial, sans-serif;
9
+ font-size: 13px;
10
+ line-height: 18px;
11
+ }
12
+
13
+ pre {
14
+ background-color: #eee;
15
+ padding: 10px;
16
+ font-size: 11px;
17
+ white-space: pre-wrap;
18
+ }
19
+
20
+ div.policy {
21
+ padding-top: 8px;
22
+ padding-left: 80px;
23
+ }
24
+ </style>
25
+
26
+ <script type="text/ecmascript">
27
+ </script>
28
+ </head>
29
+ <body>
30
+ <h2>Policies</h2>
31
+ <p>Draws Authorization Grant Roles</p>
32
+
33
+ <div class='policy'>
34
+ <%= policy_tag %>
35
+ </div>
36
+ </body>
37
+ </html>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grant-front
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ogom
@@ -10,6 +10,20 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2014-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: kramdown
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -53,11 +67,13 @@ files:
53
67
  - Rakefile
54
68
  - grant-front.gemspec
55
69
  - lib/grant-front.rb
56
- - lib/grant-front/grant.rb
70
+ - lib/grant-front/diagram.rb
71
+ - lib/grant-front/engine.rb
57
72
  - lib/grant-front/policy.rb
58
73
  - lib/grant-front/rails.rb
59
74
  - lib/grant-front/version.rb
60
75
  - lib/tasks/grant-front.rake
76
+ - lib/templates/layouts/application.html.erb
61
77
  - spec/grant-front_spec.rb
62
78
  - spec/lib/policy_spec.rb
63
79
  - spec/lib/version_spec.rb
@@ -1,26 +0,0 @@
1
- module GrantFront
2
- class Grant
3
- class << self
4
- def draw
5
- policies = {}
6
- GrantFront::Policy.all.each do |klass|
7
- policies[klass.to_s.to_sym] = GrantFront::Policy.find(klass)
8
- end
9
-
10
- policies.keys.each do |policy|
11
- puts "\n# #{policy} \n\n"
12
- puts "||#{policies[policy][:roles].join('|')}|"
13
- puts "|:-:|#{policies[policy][:roles].map{':-:'}.join('|')}|"
14
- policies[policy][:methods].keys.each do |method|
15
- raw = "|#{method}|"
16
- policies[policy][:roles].each do |role|
17
- raw += 'o' if policies[policy][:methods][method].include?(role)
18
- raw += '|'
19
- end
20
- puts raw
21
- end
22
- end
23
- end
24
- end
25
- end
26
- end