regulator 0.1.1 → 0.1.2

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: debaf2d2834f1f40dfb69f5d16f20a8b0f887d7e
4
- data.tar.gz: a894788600e922d6fca85ae7897a08f76d7fdbcd
3
+ metadata.gz: 911cd1700d6c57b00ca93599c53af6b38aeb5e29
4
+ data.tar.gz: 483493a6ff6ddc269e374fedb22a3df20b917f6a
5
5
  SHA512:
6
- metadata.gz: 10a08ae8dc44b46b4a3bde10f5802ea32543b3f9ae55668ceae101439dc594dd0ef11f25a548706a44e07e8928527198a7a5369f6f4742c4db80dde8e00c1178
7
- data.tar.gz: fe9af60ae01fd42e0ce9ad509c4feff5c696d244bf57318603844f3908a35955f7760b7449a48fb17ce552fdc21715869d8bd0d77eca22461f8bac302de405b0
6
+ metadata.gz: 5b2d0979c34ac379599b231e43aa79954a247a5c4bcac14379cce38a264d462258f98ee9e474f7954a84c1ea2df92630efb9c8ac55e021f2f67fb5d423e20143
7
+ data.tar.gz: 3ea1165fa80b0d4caf10a07f9496dd623fe31f81c9c1cd49f3108eb7e046cd1fbbe448e3a718d735505f2f0d3da1f21cd967c53342881aa53416d8650e44acff
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ .DS_Store
data/.travis.yml CHANGED
@@ -4,8 +4,5 @@ rvm:
4
4
  - 2.2.0
5
5
  - 2.0.0
6
6
  - 2.1.5
7
- - jruby-19mode
8
7
  env:
9
- - RSPEC_VERSION="<2.99"
10
- - RSPEC_VERSION="~>3.0
11
8
  - CODECLIMATE_REPO_TOKEN=561cc65a0f136c90b4661c76c96e4ca0717d49a1336cb08d7a617f2cb7d4f6c6
data/CHANGELOG.md CHANGED
@@ -1,11 +1,14 @@
1
1
  # Regulator
2
2
 
3
+ ## 0.1.2 (2015-07-23)
4
+ - Add generators for install, policy, and activeadmin adapter
5
+
3
6
  ## 0.1.1 (2015-07-23)
4
7
  - Regulator.authorize support for controller namespacing
5
8
  - Regulator can accept a controller instance or an explicity modules name
6
9
  - Regulator.policy!(user,resource, my_controller_instance)
7
10
  - Regulator.policy!(user,resource, Api::V2)
8
-
11
+
9
12
  ## 0.1.0 (2015-07-23)
10
13
  - initial release
11
14
  - pundit compatible
data/README.md CHANGED
@@ -14,8 +14,6 @@ I built this because I believe authorization should be controller-based, not mod
14
14
  Why not contribute to pundit? [It's](https://github.com/elabs/pundit/issues/12) [been](https://github.com/elabs/pundit/issues/178) an [on going](https://github.com/elabs/pundit/search?q=namespace&type=Issues&utf8=%E2%9C%93) 'issue' in pundit and it doesn't look [like it'll be reality.](https://github.com/elabs/pundit/pull/190#issuecomment-53052356)
15
15
 
16
16
  ## TODOs
17
- * [ ] generators
18
- * [ ] activeadmin-regulator-adapter gem or generator
19
17
  * [ ] documentation
20
18
  * [ ] Usage section below, mock pundit's
21
19
  * [ ] yard doc
@@ -45,7 +43,46 @@ Or install it yourself as:
45
43
 
46
44
  ## Usage
47
45
 
48
- TODO: Write usage instructions here
46
+ No docs yet, check out the [specs](https://github.com/coryodaniel/regulator/blob/master/spec/regulator_spec.rb)
47
+
48
+ ### Generators
49
+
50
+ Install regulator
51
+ ```bash
52
+ rails g regulator:install
53
+ ```
54
+
55
+ Create a new policy and policy test/spec
56
+ ```bash
57
+ rails g regulator:policy User
58
+ ```
59
+
60
+ Regulator comes with a generator for creating an ActiveAdmin adapter
61
+ ```bash
62
+ rails g regulator:activeadmin
63
+ ```
64
+
65
+ This will create an adapter in your ```lib``` folder.
66
+
67
+ Be sure to set the following in your ActiveAdmin initializer:
68
+ ```ruby
69
+ config.authorization_adapter = "ActiveAdmin::RegulatorAdapter"
70
+
71
+ # Optional
72
+ # Sets a scope for all ActiveAdmin polices to exist in
73
+ #
74
+ # Example
75
+ # app/policies/admin_policies/user_policy.rb #=> AdminPolicies::UserPolicy
76
+ #
77
+ # config.regulator_policy_namespace = "AdminPolicies"
78
+ config.regulator_policy_namespace = nil
79
+
80
+ # Optional
81
+ # Sets the default policy to use if no policy is found
82
+ #
83
+ # config.regulator_default_policy = BlackListPolicy
84
+ config.regulator_default_policy = nil
85
+ ```
49
86
 
50
87
  ## Development
51
88
 
Binary file
Binary file
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Generates an ActiveAdmin Regulator adapter
3
+
4
+ Example:
5
+ rails generate regulator:active_admin
6
+
7
+ This will create:
8
+ lib/regulator_active_admin_adapter.rb
@@ -0,0 +1,41 @@
1
+ module Regulator
2
+ module Generators
3
+ class AdapterGenerator < ::Rails::Generators::Base
4
+ source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
5
+
6
+ def inject_into_file_require
7
+ inject_into_file 'config/initializers/active_admin.rb', after: "ActiveAdmin.setup do |config|\n" do <<-'RUBY'
8
+ require 'regulator_active_admin_adapter'
9
+ RUBY
10
+ end
11
+ end
12
+
13
+ def copy_regulator_active_admin_adapter
14
+ # inject_into_file_require
15
+ # inject_info_file_config_options
16
+ template 'regulator_active_admin_adapter.rb', 'lib/regulator_active_admin_adapter.rb'
17
+ end
18
+
19
+ def inject_info_file_config_options
20
+ inject_into_file 'config/initializers/active_admin.rb', after: "# == User Authentication\n" do <<-'RUBY'
21
+ config.authorization_adapter = "ActiveAdmin::RegulatorAdapter"
22
+ # Optional
23
+ # Sets a scope for all ActiveAdmin polices to exist in
24
+ #
25
+ # Example
26
+ # app/policies/admin_policies/user_policy.rb #=> AdminPolicies::UserPolicy
27
+ #
28
+ # config.regulator_policy_namespace = "AdminPolicies"
29
+ config.regulator_policy_namespace = nil
30
+ # Optional
31
+ # Sets the default policy to use if no policy is found
32
+ #
33
+ # config.regulator_default_policy = BlackListPolicy
34
+ config.regulator_default_policy = nil
35
+ RUBY
36
+
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,74 @@
1
+ ActiveAdmin::Dependency.regulator!
2
+
3
+ require 'regulator'
4
+
5
+ # Add a setting to the application to configure the regulator default policy
6
+ ActiveAdmin::Application.inheritable_setting :regulator_default_policy, nil
7
+
8
+ # policy_namespace will default to ActiveAdmin, override it here
9
+ ActiveAdmin::Application.inheritable_setting :regulator_policy_namespace, nil
10
+
11
+ module ActiveAdmin
12
+ class RegulatorAdapter < AuthorizationAdapter
13
+
14
+ def authorized?(action, subject = nil)
15
+ policy = retrieve_policy(subject)
16
+ action = format_action(action, subject)
17
+
18
+ policy.respond_to?(action) && policy.public_send(action)
19
+ end
20
+
21
+ def scope_collection(collection, action = Auth::READ)
22
+ # scoping is appliable only to read/index action
23
+ # which means there is no way how to scope other actions
24
+ Regulator.policy_scope!(user, collection, regulator_policy_namespace)
25
+ rescue Regulator::NotDefinedError => e
26
+ if default_policy_class && default_policy_class.const_defined?(:Scope)
27
+ default_policy_class::Scope.new(user, collection).resolve
28
+ else
29
+ raise e
30
+ end
31
+ end
32
+
33
+ def retrieve_policy(subject)
34
+ case subject
35
+ when nil then Regulator.policy!(user, resource, regulator_policy_namespace)
36
+ when Class then Regulator.policy!(user, subject.new, regulator_policy_namespace)
37
+ else Regulator.policy!(user, subject, regulator_policy_namespace)
38
+ end
39
+ rescue Regulator::NotDefinedError => e
40
+ if default_policy_class
41
+ default_policy(user, subject)
42
+ else
43
+ raise e
44
+ end
45
+ end
46
+
47
+ def format_action(action, subject)
48
+ # https://github.com/elabs/regulator/blob/master/lib/generators/regulator/install/templates/application_policy.rb
49
+ case action
50
+ when Auth::CREATE then :create?
51
+ when Auth::UPDATE then :update?
52
+ when Auth::READ then subject.is_a?(Class) ? :index? : :show?
53
+ when Auth::DESTROY then subject.is_a?(Class) ? :destroy_all? : :destroy?
54
+ else "#{action}?"
55
+ end
56
+ end
57
+
58
+ private
59
+
60
+ def regulator_policy_namespace
61
+ ActiveAdmin.application.regulator_policy_namespace && ActiveAdmin.application.regulator_policy_namespace.constantize
62
+ end
63
+
64
+ def default_policy_class
65
+ ActiveAdmin.application.regulator_default_policy && ActiveAdmin.application.regulator_default_policy.constantize
66
+ end
67
+
68
+ def default_policy(user, subject)
69
+ default_policy_class.new(user, subject)
70
+ end
71
+
72
+ end
73
+
74
+ end
@@ -0,0 +1,2 @@
1
+ Description:
2
+ Generates an application policy as a starting point for your application.
@@ -0,0 +1,11 @@
1
+ module Regulator
2
+ module Generators
3
+ class InstallGenerator < ::Rails::Generators::Base
4
+ source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
5
+
6
+ def copy_application_policy
7
+ template 'application_policy.rb', 'app/policies/application_policy.rb'
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,53 @@
1
+ class ApplicationPolicy
2
+ attr_reader :user, :record
3
+
4
+ def initialize(user, record)
5
+ @user = user
6
+ @record = record
7
+ end
8
+
9
+ def index?
10
+ false
11
+ end
12
+
13
+ def show?
14
+ scope.where(:id => record.id).exists?
15
+ end
16
+
17
+ def create?
18
+ false
19
+ end
20
+
21
+ def new?
22
+ create?
23
+ end
24
+
25
+ def update?
26
+ false
27
+ end
28
+
29
+ def edit?
30
+ update?
31
+ end
32
+
33
+ def destroy?
34
+ false
35
+ end
36
+
37
+ def scope
38
+ Regulator.policy_scope!(user, record.class)
39
+ end
40
+
41
+ class Scope
42
+ attr_reader :user, :scope
43
+
44
+ def initialize(user, scope)
45
+ @user = user
46
+ @scope = scope
47
+ end
48
+
49
+ def resolve
50
+ scope
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Generates a policy for a model with the given name.
3
+
4
+ Example:
5
+ rails generate regulator:policy user
6
+
7
+ This will create:
8
+ app/policies/user_policy.rb
@@ -0,0 +1,13 @@
1
+ module Regulator
2
+ module Generators
3
+ class PolicyGenerator < ::Rails::Generators::NamedBase
4
+ source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
5
+
6
+ def create_policy
7
+ template 'policy.rb', File.join('app/policies', class_path, "#{file_name}_policy.rb")
8
+ end
9
+
10
+ hook_for :test_framework
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ <% module_namespacing do -%>
2
+ class <%= class_name %>Policy < ApplicationPolicy
3
+ class Scope < Scope
4
+ def resolve
5
+ scope
6
+ end
7
+ end
8
+ end
9
+ <% end -%>
@@ -0,0 +1,11 @@
1
+ module Rspec
2
+ module Generators
3
+ class PolicyGenerator < ::Rails::Generators::NamedBase
4
+ source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
5
+
6
+ def create_policy_spec
7
+ template 'policy_spec.rb', File.join('spec/policies', class_path, "#{file_name}_policy_spec.rb")
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,28 @@
1
+ require '<%= File.exists?('spec/rails_helper.rb') ? 'rails_helper' : 'spec_helper' %>'
2
+
3
+ describe <%= class_name %>Policy do
4
+
5
+ let(:user) { User.new }
6
+
7
+ subject { described_class }
8
+
9
+ permissions ".scope" do
10
+ pending "add some examples to (or delete) #{__FILE__}"
11
+ end
12
+
13
+ permissions :show? do
14
+ pending "add some examples to (or delete) #{__FILE__}"
15
+ end
16
+
17
+ permissions :create? do
18
+ pending "add some examples to (or delete) #{__FILE__}"
19
+ end
20
+
21
+ permissions :update? do
22
+ pending "add some examples to (or delete) #{__FILE__}"
23
+ end
24
+
25
+ permissions :destroy? do
26
+ pending "add some examples to (or delete) #{__FILE__}"
27
+ end
28
+ end
@@ -0,0 +1,11 @@
1
+ module TestUnit
2
+ module Generators
3
+ class PolicyGenerator < ::Rails::Generators::NamedBase
4
+ source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
5
+
6
+ def create_policy_test
7
+ template 'policy_test.rb', File.join('test/policies', class_path, "#{file_name}_policy_test.rb")
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ class <%= class_name %>PolicyTest < ActiveSupport::TestCase
4
+
5
+ def test_scope
6
+ end
7
+
8
+ def test_show
9
+ end
10
+
11
+ def test_create
12
+ end
13
+
14
+ def test_update
15
+ end
16
+
17
+ def test_destroy
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module Regulator
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: regulator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cory O'Daniel
@@ -142,6 +142,22 @@ files:
142
142
  - Rakefile
143
143
  - bin/console
144
144
  - bin/setup
145
+ - lib/generators/.DS_Store
146
+ - lib/generators/regulator/.DS_Store
147
+ - lib/generators/regulator/adapter/.DS_Store
148
+ - lib/generators/regulator/adapter/USAGE
149
+ - lib/generators/regulator/adapter/adapter_generator.rb
150
+ - lib/generators/regulator/adapter/templates/regulator_active_admin_adapter.rb
151
+ - lib/generators/regulator/install/USAGE
152
+ - lib/generators/regulator/install/install_generator.rb
153
+ - lib/generators/regulator/install/templates/application_policy.rb
154
+ - lib/generators/regulator/policy/USAGE
155
+ - lib/generators/regulator/policy/policy_generator.rb
156
+ - lib/generators/regulator/policy/templates/policy.rb
157
+ - lib/generators/rspec/policy_generator.rb
158
+ - lib/generators/rspec/templates/policy_spec.rb
159
+ - lib/generators/test_unit/policy_generator.rb
160
+ - lib/generators/test_unit/templates/policy_test.rb
145
161
  - lib/regulator.rb
146
162
  - lib/regulator/policy_finder.rb
147
163
  - lib/regulator/rspec.rb