regulator 0.1.1 → 0.1.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/.gitignore +1 -0
- data/.travis.yml +0 -3
- data/CHANGELOG.md +4 -1
- data/README.md +40 -3
- data/lib/generators/.DS_Store +0 -0
- data/lib/generators/regulator/.DS_Store +0 -0
- data/lib/generators/regulator/adapter/.DS_Store +0 -0
- data/lib/generators/regulator/adapter/USAGE +8 -0
- data/lib/generators/regulator/adapter/adapter_generator.rb +41 -0
- data/lib/generators/regulator/adapter/templates/regulator_active_admin_adapter.rb +74 -0
- data/lib/generators/regulator/install/USAGE +2 -0
- data/lib/generators/regulator/install/install_generator.rb +11 -0
- data/lib/generators/regulator/install/templates/application_policy.rb +53 -0
- data/lib/generators/regulator/policy/USAGE +8 -0
- data/lib/generators/regulator/policy/policy_generator.rb +13 -0
- data/lib/generators/regulator/policy/templates/policy.rb +9 -0
- data/lib/generators/rspec/policy_generator.rb +11 -0
- data/lib/generators/rspec/templates/policy_spec.rb +28 -0
- data/lib/generators/test_unit/policy_generator.rb +11 -0
- data/lib/generators/test_unit/templates/policy_test.rb +19 -0
- data/lib/regulator/version.rb +1 -1
- metadata +17 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 911cd1700d6c57b00ca93599c53af6b38aeb5e29
|
4
|
+
data.tar.gz: 483493a6ff6ddc269e374fedb22a3df20b917f6a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5b2d0979c34ac379599b231e43aa79954a247a5c4bcac14379cce38a264d462258f98ee9e474f7954a84c1ea2df92630efb9c8ac55e021f2f67fb5d423e20143
|
7
|
+
data.tar.gz: 3ea1165fa80b0d4caf10a07f9496dd623fe31f81c9c1cd49f3108eb7e046cd1fbbe448e3a718d735505f2f0d3da1f21cd967c53342881aa53416d8650e44acff
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
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
|
-
|
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
|
Binary file
|
@@ -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,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,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,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
|
data/lib/regulator/version.rb
CHANGED
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.
|
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
|