banken 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6b535c2ea0ee44062ee539386142447ab1db0054
4
+ data.tar.gz: 74d457b467b4ec2c8a4d39899732aad37ff90d36
5
+ SHA512:
6
+ metadata.gz: 0aa2d9fd1a1eb732252d0093fa407f654fc6d47396b16888771d35c719226ebb9c151d51650354856959dc90b3ddd2987508c8f02a8ce90ed883ed697aae3133
7
+ data.tar.gz: e081bc1f541fe7476cd46170a72dcac38fc12c01ef4709c7d1c6da27dc20154242dbedccfeb0cd60550507daba95ba2b7a737659ba16a3ce8c7d70ffeec74d59
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in banken.gemspec
4
+ gem "rspec", ENV["RSPEC_VERSION"] unless ENV["RSPEC_VERSION"].to_s.empty?
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Kyuden
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Banken
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/banken`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'banken'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install banken
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/banken. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/banken.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'banken/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "banken"
8
+ spec.version = Banken::VERSION
9
+ spec.authors = ["kyuden"]
10
+ spec.email = ["msmsms.um@gmail.com"]
11
+
12
+ spec.summary = %q{OO authorization for Rails.}
13
+ spec.description = %q{Object oriented authorization like pundit for Rails applications.}
14
+ spec.homepage = "https://github.com/kyuden/banken"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency 'activesupport', '>= 3.0.0'
22
+
23
+ spec.add_development_dependency "activemodel", ">= 3.0.0"
24
+ spec.add_development_dependency "actionpack", ">= 3.0.0"
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rspec", ">=2.0.0"
27
+ spec.add_development_dependency "pry"
28
+ spec.add_development_dependency "rake"
29
+ end
data/lib/banken.rb ADDED
@@ -0,0 +1,119 @@
1
+ require "active_support/concern"
2
+ require "active_support/core_ext/string/inflections"
3
+ require "active_support/core_ext/object/blank"
4
+ require "active_support/core_ext/module/introspection"
5
+ require "banken/version"
6
+ require "banken/error"
7
+ require "banken/helper"
8
+ require "banken/policy_finder"
9
+
10
+ module Banken
11
+ extend ActiveSupport::Concern
12
+
13
+ included do
14
+ # TODO
15
+ # helper Helper if respond_to?(:helper)
16
+ if respond_to?(:helper_method)
17
+ # TODO
18
+ # helper_method :banken_policy_scope
19
+ helper_method :banken_user
20
+ end
21
+ if respond_to?(:hide_action)
22
+ hide_action :policy_scope
23
+ hide_action :permitted_attributes
24
+ hide_action :policy
25
+ hide_action :banken_user
26
+ hide_action :skip_authorization
27
+ hide_action :skip_policy_scope
28
+ hide_action :verify_authorized
29
+ hide_action :verify_policy_scoped
30
+ hide_action :policies
31
+ hide_action :policy_scopes
32
+ end
33
+ end
34
+
35
+ class << self
36
+ def policy_scope!(controller, user, scope)
37
+ PolicyFinder.new(controller).scope!.new(user, scope).resolve
38
+ end
39
+
40
+ def policy!(controller, user, record)
41
+ PolicyFinder.new(controller).policy!.new(user, record)
42
+ end
43
+ end
44
+
45
+ def authorize!(record=nil)
46
+ @_banken_policy_authorized = true
47
+
48
+ policy = policy(record)
49
+ unless policy.public_send("#{banken_action_name}?")
50
+ raise NotAuthorizedError.new(controller: banken_controller_name, action: banken_action_name, policy: policy)
51
+ end
52
+
53
+ true
54
+ end
55
+
56
+ def policy_scope(scope)
57
+ @_banken_policy_scoped = true
58
+ banken_policy_scope(scope)
59
+ end
60
+
61
+ def permitted_attributes(record)
62
+ name = record.class.to_s.demodulize.underscore
63
+ params.require(name).permit(policy(record).permitted_attributes)
64
+ end
65
+
66
+ def policy(record)
67
+ policies[banken_action_name] ||= Banken.policy!(banken_controller_name, banken_user, record)
68
+ end
69
+
70
+ def banken_user
71
+ current_user
72
+ end
73
+
74
+ def skip_authorization
75
+ @_banken_policy_authorized = true
76
+ end
77
+
78
+ def skip_policy_scope
79
+ @_banken_policy_scoped = true
80
+ end
81
+
82
+ def verify_authorized
83
+ raise AuthorizationNotPerformedError unless banken_policy_authorized?
84
+ end
85
+
86
+ def verify_policy_scoped
87
+ raise PolicyScopingNotPerformedError unless banken_policy_scoped?
88
+ end
89
+
90
+ def banken_policy_authorized?
91
+ !!@_banken_policy_authorized
92
+ end
93
+
94
+ def banken_policy_scoped?
95
+ !!@_banken_policy_scoped
96
+ end
97
+
98
+ def policies
99
+ @_banken_policies ||= {}
100
+ end
101
+
102
+ def policy_scopes
103
+ @_banken_policy_scopes ||= {}
104
+ end
105
+
106
+ private
107
+
108
+ def banken_policy_scope(scope)
109
+ policy_scopes[scope] ||= Banken.policy_scope!(banken_controller_name, banken_user, scope)
110
+ end
111
+
112
+ def banken_action_name
113
+ params[:action].to_s
114
+ end
115
+
116
+ def banken_controller_name
117
+ params[:controller].to_s
118
+ end
119
+ end
@@ -0,0 +1,26 @@
1
+ module Banken
2
+ class Error < StandardError; end
3
+
4
+ class NotAuthorizedError < Error
5
+ attr_reader :controller, :action, :policy
6
+
7
+ def initialize(options={})
8
+ if options.is_a? String
9
+ message = options
10
+ else
11
+ @controller = options[:controller]
12
+ @action = options[:action]
13
+ @policy = options[:policy]
14
+
15
+ message = options.fetch(:message) { "not allowed to #{action} of #{controller} by #{policy.inspect}" }
16
+ end
17
+
18
+ super(message)
19
+ end
20
+ end
21
+
22
+ class NotDefinedError < Error; end
23
+ class AuthorizationNotPerformedError < Error; end
24
+
25
+ class PolicyScopingNotPerformedError < AuthorizationNotPerformedError; end
26
+ end
@@ -0,0 +1,8 @@
1
+ module Banken
2
+ module Helper
3
+ # TODO
4
+ # def policy_scope(scope)
5
+ # banken_policy_scope(scope)
6
+ # end
7
+ end
8
+ end
@@ -0,0 +1,41 @@
1
+ module Banken
2
+ class PolicyFinder
3
+ SUFFIX = "Policy"
4
+
5
+ attr_reader :controller
6
+
7
+ def initialize(controller)
8
+ @controller = controller
9
+ end
10
+
11
+ def scope
12
+ policy::Scope if policy
13
+ rescue NameError
14
+ nil
15
+ end
16
+
17
+ def policy
18
+ klass = find
19
+ klass = klass.constantize if klass.is_a?(String)
20
+ klass
21
+ rescue NameError
22
+ nil
23
+ end
24
+
25
+ def scope!
26
+ raise NotDefinedError, "unable to find policy scope of nil" unless controller
27
+ scope || raise(NotDefinedError, "unable to find scope `#{find}::Scope` for `#{controller}`")
28
+ end
29
+
30
+ def policy!
31
+ raise NotDefinedError, "unable to find policy of nil" unless controller
32
+ policy || raise(NotDefinedError, "unable to find policy `#{find}` for `#{controller}`")
33
+ end
34
+
35
+ private
36
+
37
+ def find
38
+ "#{controller.camelize}#{SUFFIX}"
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module Banken
2
+ VERSION = "0.0.1"
3
+ 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 Banken
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
+ Scope.new(user, record.class).resolve
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 controller with the given name.
3
+
4
+ Example:
5
+ rails generate banken:policy users
6
+
7
+ This will create:
8
+ app/policies/users_policy.rb
@@ -0,0 +1,13 @@
1
+ module Banken
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 -%>
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: banken
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - kyuden
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activemodel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 3.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 3.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: actionpack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 2.0.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 2.0.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Object oriented authorization like pundit for Rails applications.
112
+ email:
113
+ - msmsms.um@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - CODE_OF_CONDUCT.md
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - banken.gemspec
125
+ - lib/banken.rb
126
+ - lib/banken/error.rb
127
+ - lib/banken/helper.rb
128
+ - lib/banken/policy_finder.rb
129
+ - lib/banken/version.rb
130
+ - lib/generators/banken/install/USAGE
131
+ - lib/generators/banken/install/install_generator.rb
132
+ - lib/generators/banken/install/templates/application_policy.rb
133
+ - lib/generators/banken/policy/USAGE
134
+ - lib/generators/banken/policy/policy_generator.rb
135
+ - lib/generators/banken/policy/templates/policy.rb
136
+ homepage: https://github.com/kyuden/banken
137
+ licenses:
138
+ - MIT
139
+ metadata: {}
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 2.4.8
157
+ signing_key:
158
+ specification_version: 4
159
+ summary: OO authorization for Rails.
160
+ test_files: []