porteiro 0.0.1

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
+ SHA1:
3
+ metadata.gz: 2796cb24e22ccb1603b16a55eb06b41f6fb6d061
4
+ data.tar.gz: 657fdc47611b5326edb73df3663f8e929545d5c4
5
+ SHA512:
6
+ metadata.gz: 0cfb3c97c0590a2794d446a56d820615f41e2f0b63e9b79e19cec509472483dcb131b713b5cd5405452661d53ff6f756a7e8db97a4d7d849b405d070ba01be49
7
+ data.tar.gz: 62d2e6868c0d5c9684c8107ad8eea4c059936f7061dd1577d7a4489156ae9937a33b04cf004215e0422bcdb648f39840a67ad303e0ef052b4f70d4a852f6c931
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in porteiro.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 bradwheel
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Porteiro
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'porteiro'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install porteiro
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/gem_tasks"
2
+
3
+
@@ -0,0 +1,16 @@
1
+ module Porteiro
2
+ class Base
3
+
4
+ def initialize(user, params)
5
+ @user = user
6
+ @params = params
7
+ end
8
+ attr_reader :user, :params
9
+
10
+ def authorize_action!
11
+ controller_action = params.fetch(:action)
12
+ self.send("#{controller_action}?") ? true : (raise NotAuthorizedError, "You aren't permitted to access this resource")
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,38 @@
1
+ module Porteiro
2
+ class PolicyFinder
3
+
4
+ attr_reader :user, :req_params, :klass
5
+
6
+ def initialize(user, req_params)
7
+ @user = user
8
+ @req_params = req_params
9
+ @klass = fetch_klass_from_params
10
+ end
11
+
12
+ def find!
13
+ begin
14
+ return instantiate_policy_class
15
+ rescue NameError
16
+ raise PolicyUndefinedError, "You must define your default policy: #{Porteiro.default_policy}"
17
+ end
18
+ end
19
+
20
+ def fetch_klass_from_params
21
+ return String(req_params.fetch(:controller).classify) rescue nil
22
+ end
23
+
24
+ ##
25
+ # Finds policy and instantiates it. If policy doesn't exist, the default
26
+ # policy is instantiated. This removes the need to define every policy if
27
+ # you want to use method_missing in the default policy.
28
+ ##
29
+
30
+ def instantiate_policy_class
31
+ policy = "#{klass}Policy".constantize.new(user, req_params) rescue nil
32
+ return (Porteiro.default_policy.constantize.new(user, req_params)) unless policy
33
+ return policy
34
+ end
35
+
36
+ end
37
+ end
38
+
@@ -0,0 +1,3 @@
1
+ module Porteiro
2
+ VERSION = "0.0.1"
3
+ end
data/lib/porteiro.rb ADDED
@@ -0,0 +1,54 @@
1
+ require "porteiro/version"
2
+ require "porteiro/policy_finder"
3
+ require "porteiro/base"
4
+ require "active_support/concern"
5
+ require "active_support/inflector"
6
+
7
+ module Porteiro
8
+ extend ActiveSupport::Concern
9
+
10
+ class NotAuthorizedError < StandardError;end
11
+ class PolicyUndefinedError < StandardError;end
12
+
13
+
14
+ class << self
15
+
16
+ ##
17
+ # ClassMethod to find policy with PolicyFinder using the current_user
18
+ # and request params.
19
+ ##
20
+
21
+ def policy(user, req_params)
22
+ policy = PolicyFinder.new(user, req_params).find!
23
+ return policy
24
+ end
25
+
26
+ ##
27
+ # Configuration methods for setting default policy name
28
+ ##
29
+
30
+ def default_policy
31
+ @default_policy ||= "ApplicationPolicy"
32
+ end
33
+ attr_writer :default_policy
34
+
35
+ end
36
+
37
+ ##
38
+ # Before action that can be called in the controller to check for authorization.
39
+ # If this is not called, no policy will be looked up.
40
+ ##
41
+
42
+ def authorize_user_access!
43
+ policy.authorize_action!
44
+ end
45
+
46
+ def policy
47
+ @policy || Porteiro.policy(porteiro_user, params)
48
+ end
49
+
50
+ def porteiro_user
51
+ current_user
52
+ end
53
+
54
+ end
data/porteiro.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'porteiro/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "porteiro"
8
+ spec.version = Porteiro::VERSION
9
+ spec.authors = ["bradwheel"]
10
+ spec.email = ["bradley.m.wheel@gmail.com"]
11
+ spec.description = %q{Authorization for controllers modeled after Pundit.}
12
+ spec.summary = %q{Authorization for controllers modeled after Pundit.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport", ">= 3.0.0"
22
+ spec.add_development_dependency "activerecord", ">= 3.0.0"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "pry"
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+ end
@@ -0,0 +1,109 @@
1
+ require "spec_helper"
2
+
3
+ describe Porteiro do
4
+
5
+ let!(:current_user) {User.new}
6
+ let!(:controller) {ControllerClass.new(current_user)}
7
+
8
+
9
+ describe "#porteiro_user" do
10
+
11
+ it "returns the current_user" do
12
+ expect(controller.porteiro_user).to eq current_user
13
+ end
14
+
15
+ end
16
+
17
+ describe "#policy" do
18
+
19
+ it "returns the instance of policy finder" do
20
+ expect(controller.policy.user).to eq(Porteiro.policy(controller.porteiro_user, controller.params).user)
21
+ expect(controller.policy.params).to eq(Porteiro.policy(controller.porteiro_user, controller.params).params)
22
+ end
23
+
24
+ end
25
+
26
+ describe "#default_policy" do
27
+
28
+ it "uses ApplicationPolicy as a default unless specified" do
29
+ expect(Porteiro.default_policy).to eq "ApplicationPolicy"
30
+ end
31
+
32
+ it "uses the specified default policy if supplied" do
33
+ Porteiro.default_policy=("SuppliedPolicy")
34
+ expect(Porteiro.default_policy).to eq "SuppliedPolicy"
35
+ end
36
+
37
+ end
38
+
39
+ describe Porteiro::PolicyFinder do
40
+
41
+ it "finds the correct policy from controller params" do
42
+ policy = Porteiro::PolicyFinder.new(controller.current_user, controller.params)
43
+ expect(policy.klass).to eq "Document"
44
+ end
45
+
46
+ context "when policy doesn't exist" do
47
+
48
+ it "instantiates the default policy" do
49
+ Porteiro.default_policy = "ApplicationPolicy"
50
+ policy = Porteiro::PolicyFinder.new(controller.current_user, controller.params).find!
51
+ expect(policy).to be_instance_of(ApplicationPolicy)
52
+ end
53
+
54
+ end
55
+
56
+ context "when policy does exist" do
57
+
58
+
59
+ before(:each) do
60
+ class DocumentPolicy < ApplicationPolicy; end
61
+ end
62
+
63
+ it "instantiates the correct policy" do
64
+ policy = Porteiro::PolicyFinder.new(controller.current_user, controller.params).find!
65
+ expect(policy).to be_instance_of(DocumentPolicy)
66
+ end
67
+
68
+ end
69
+
70
+ end
71
+
72
+ describe "#authorize_user_access!" do
73
+
74
+ context "when the action is permitted" do
75
+
76
+ it "returns true" do
77
+ expect(controller.authorize_user_access!).to be(true)
78
+ end
79
+
80
+ end
81
+
82
+ context "when the action is not permitted" do
83
+
84
+ it "raises Porteiro::NotAuthorizedError" do
85
+ controller.params[:action] = "edit"
86
+ expect {controller.authorize_user_access!}.to raise_error(Porteiro::NotAuthorizedError)
87
+ end
88
+
89
+ end
90
+
91
+ end
92
+
93
+
94
+ describe Porteiro::Base do
95
+
96
+ it "initializes with a user and request params" do
97
+ policy = Porteiro::Base.new(controller.current_user, controller.params)
98
+ expect(policy.user).to eq controller.current_user
99
+ expect(policy.params).to eq controller.params
100
+ end
101
+
102
+ it "#authorize_action!" do
103
+ policy = Porteiro::PolicyFinder.new(controller.current_user, controller.params).find!
104
+ expect(policy.authorize_action!).to eq true
105
+ end
106
+
107
+ end
108
+
109
+ end
@@ -0,0 +1,37 @@
1
+ require "porteiro"
2
+ require "porteiro/base"
3
+ require "pry"
4
+
5
+ class User < Struct.new(:name); end
6
+
7
+ class ControllerClass
8
+ include Porteiro
9
+
10
+ attr_accessor :current_user
11
+
12
+ def initialize(user)
13
+ @current_user = user
14
+ end
15
+
16
+ def params
17
+ @params ||= {controller: "document", action: "index"}
18
+ end
19
+
20
+ end
21
+
22
+
23
+ class ApplicationPolicy < Porteiro::Base
24
+
25
+ def index?
26
+ true
27
+ end
28
+
29
+ def show?
30
+ true
31
+ end
32
+
33
+ def edit?
34
+ false
35
+ end
36
+
37
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: porteiro
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - bradwheel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-01 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: activerecord
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
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
+ description: Authorization for controllers modeled after Pundit.
98
+ email:
99
+ - bradley.m.wheel@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - .rspec
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - lib/porteiro.rb
111
+ - lib/porteiro/base.rb
112
+ - lib/porteiro/policy_finder.rb
113
+ - lib/porteiro/version.rb
114
+ - porteiro.gemspec
115
+ - spec/porteiro_spec.rb
116
+ - spec/spec_helper.rb
117
+ homepage: ''
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.1.11
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Authorization for controllers modeled after Pundit.
141
+ test_files:
142
+ - spec/porteiro_spec.rb
143
+ - spec/spec_helper.rb