active_policy 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6a7a19a6521efb04caf3097318c71312947e0b0e962ae6c93ce17ac57a8dc908
4
+ data.tar.gz: 804ef773567ea93e40422765e9dae16fee0a2b78d0429e47384eaa21a1abb06f
5
+ SHA512:
6
+ metadata.gz: 80ef1909e56d9c608a76b33c6ce27a7da9651a481ba6ca71e6cdd9cea31bd298e6b446b0173e94315a35b7df3590b3352f40cf05c0449bdf24665afd7e84fe49
7
+ data.tar.gz: 686b25058690e70f31db52826639a211be0da38fa5e105c06f878acc8e8bad9310058f0d15d1943be258d612216b3153101aadd540b89c5e4ae8c603befcac71
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .idea
13
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.4
6
+ before_install: gem install bundler -v 2.1.0.pre.3
7
+ install:
8
+ - bundle
9
+ scrip:
10
+ - bundle exec rspec
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in active_policy.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
8
+ gem 'codecov', :require => false, :group => :test
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Panos Dalitsouris
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,56 @@
1
+ [![Build Status](https://travis-ci.org/PanosCodes/active_policy.svg?branch=master)](https://travis-ci.org/PanosCodes/active_policy)
2
+ [![codecov](https://codecov.io/gh/PanosCodes/active_policy/branch/master/graph/badge.svg)](https://codecov.io/gh/PanosCodes/active_policy)
3
+
4
+ # ActivePolicy
5
+ Active policy is meant to be a way to authorize a request before hitting the controller.
6
+
7
+ ## Installation
8
+
9
+ ```ruby
10
+ gem 'active_policy'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install active_policy
20
+
21
+ ## Usage
22
+
23
+ 1. Inside your route.rb
24
+ ```ruby
25
+ post 'users/:user_id/segments', to: 'users#register_to_segment', policy: UserPolicy, policy_models: {user_id: User}
26
+ ```
27
+
28
+ 2. Create the user_policy.rb
29
+
30
+ ```ruby
31
+ class UserPolicy < ActivePolicy::Base
32
+ # @param [User] user
33
+ #
34
+ # @return [TrueClass, FalseClass]
35
+ def register_to_segment?(user)
36
+ return true if @current_user.is_admin?
37
+ return true if user.is_staff?
38
+ false
39
+ end
40
+ end
41
+ ```
42
+
43
+ ## Development
44
+
45
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
46
+
47
+ 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).
48
+
49
+ ## Contributing
50
+
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/PanosCodes/active_policy.
52
+
53
+
54
+ ## License
55
+
56
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,29 @@
1
+ require_relative 'lib/active_policy/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "active_policy"
5
+ spec.version = ActivePolicy::VERSION
6
+ spec.authors = ["Panos Dalitsouris"]
7
+ spec.email = ["hello@panos.codes"]
8
+
9
+ spec.summary = "Authorize a request before hitting the controller."
10
+ spec.description = "Active policy is meant to be a way to authorize a request before hitting the controller."
11
+ spec.homepage = "https://github.com/PanosCodes/active_policy"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["allowed_push_host"] = "https://rubygems.org/"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/PanosCodes/active_policy"
19
+ spec.metadata["changelog_uri"] = "https://github.com/PanosCodes/active_policy"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "active_policy"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,8 @@
1
+ require "active_policy/version"
2
+ require 'active_policy/railtie' if defined?(Rails)
3
+ require 'active_policy/base'
4
+
5
+ module ActivePolicy
6
+ class Error < StandardError; end
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1,42 @@
1
+ require 'active_policy/utilities/utilities'
2
+ class ActivePolicyMiddleware
3
+ ENV_KEY_REQUEST_METHOD = 'REQUEST_METHOD'
4
+ ENV_KEY_WARDEN = 'warden'
5
+
6
+ # @param [Class] app
7
+ def initialize(app)
8
+ @app = app
9
+ end
10
+
11
+ # @param [Hash] env
12
+ def call(env)
13
+ params = ActivePolicy::Utilities.route_params(
14
+ ActionDispatch::Request.new(env).path,
15
+ env[ENV_KEY_REQUEST_METHOD],
16
+ Rails.application.routes
17
+ )
18
+
19
+ if params.key?(:policy)
20
+ policy = params[:policy].new current_user(env)
21
+ method_name = params[:action] + '?'
22
+ models = ActivePolicy::Utilities.models_from_route_params(params)
23
+
24
+ policy.send(method_name, *models)
25
+ end
26
+
27
+ @app.call(env)
28
+ end
29
+
30
+ private
31
+
32
+ # @param [Hash] env
33
+ #
34
+ # @return [User, nil]
35
+ def current_user(env)
36
+ if env.key?(ENV_KEY_WARDEN)
37
+ return env[ENV_KEY_WARDEN].user
38
+ end
39
+
40
+ nil
41
+ end
42
+ end
@@ -0,0 +1,8 @@
1
+ module ActivePolicy
2
+ class Base
3
+ # @param [User] current_user
4
+ def initialize(current_user)
5
+ @current_user = current_user
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require 'active_policy/active_policy_middleware'
2
+ module ActivePolicy
3
+ class Railtie < Rails::Railtie
4
+ initializer "railtie.configure_rails_initialization" do |app|
5
+ app.middleware.use ActivePolicyMiddleware
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,24 @@
1
+ module ActivePolicy
2
+ class Utilities
3
+ # @param [ActionDispatch::Routing::RouteSet] route_set
4
+ # @param [String] path
5
+ # @param [String] method
6
+ #
7
+ # @return [Hash]
8
+ def self.route_params(path, method, route_set)
9
+ route_set.recognize_path(path, {method: method})
10
+ end
11
+
12
+ # @param [Hash] params
13
+ #
14
+ # @return [Array<ActiveRecord>]
15
+ def self.models_from_route_params(params)
16
+ models = []
17
+ raise 'policy_models is missing from route' if params[:policy_models].nil?
18
+ params[:policy_models].each do |key, value|
19
+ models << value.find(params[key])
20
+ end
21
+ models
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module ActivePolicy
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_policy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Panos Dalitsouris
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-02-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Active policy is meant to be a way to authorize a request before hitting
14
+ the controller.
15
+ email:
16
+ - hello@panos.codes
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - ".rspec"
23
+ - ".travis.yml"
24
+ - Gemfile
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - active_policy.gemspec
29
+ - bin/console
30
+ - bin/setup
31
+ - lib/active_policy.rb
32
+ - lib/active_policy/active_policy_middleware.rb
33
+ - lib/active_policy/base.rb
34
+ - lib/active_policy/railtie.rb
35
+ - lib/active_policy/utilities/utilities.rb
36
+ - lib/active_policy/version.rb
37
+ homepage: https://github.com/PanosCodes/active_policy
38
+ licenses:
39
+ - MIT
40
+ metadata:
41
+ allowed_push_host: https://rubygems.org/
42
+ homepage_uri: https://github.com/PanosCodes/active_policy
43
+ source_code_uri: https://github.com/PanosCodes/active_policy
44
+ changelog_uri: https://github.com/PanosCodes/active_policy
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 2.3.0
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubygems_version: 3.0.3
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Authorize a request before hitting the controller.
64
+ test_files: []