protector-inherited_resources 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3149a214bcfc75190aa02c687fb16a5a883783a7
4
+ data.tar.gz: b9eecb812c1d7e270eed405adcffb72728682d6d
5
+ SHA512:
6
+ metadata.gz: 97df73f2b38b49a54dbf5fd9f4a7d4a0d266b4fbb5f94c676a69f73371289447c51d2ad24879c6251aab96011c542b48a9f0381a4877e6794a85235bf8f38897
7
+ data.tar.gz: 368db60ca59d63b8bdcba1f6eec9c4422b2df3c90547e6bdb71d38b5d68ee2336254e8927a26eb64e4b01d9cc8640ed0215e687e047bd5483ca7266d311d3494
@@ -0,0 +1,18 @@
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
18
+ spec/internal/log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --tty
2
+ --color
3
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'pry'
6
+ gem 'rspec'
7
+
8
+ gem 'combustion', github: 'pat/combustion'
9
+ gem 'rails', '>= 4.0.0'
10
+ gem 'rspec-rails'
11
+
12
+ gem 'sqlite3', platform: :ruby
13
+ gem 'jdbc-sqlite3', platform: :jruby, require: 'jdbc/sqlite3'
14
+ gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby, github: 'jruby/activerecord-jdbc-adapter'
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Sergey Gridasov
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.
@@ -0,0 +1,36 @@
1
+ # Protector::InheritedResources
2
+
3
+ Integrates [Protector](https://github.com/inossidabile/protector) and [Inherited Resources](https://github.com/josevalim/inherited_resources.git).
4
+
5
+ With this gem installed, Inherited Resources will automatically restrict collections and resources with `current_user`.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'protector-inherited_resources'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Example
18
+
19
+ No changes to controller code are required if `current_user` should be used to restrict models. However, if it's necessary to change that behavior, you can use `protector_subject` method:
20
+
21
+ ```ruby
22
+ class FoosController < InheritedResources::Base
23
+ protector_subject :current_admin_user
24
+ # or
25
+ protector_subject { User.first }
26
+ # or, to disable integration entirely,
27
+ protector_subject false
28
+ ```
29
+
30
+ ## Maintainers
31
+
32
+ * Sergey Gridasov (@grindars)
33
+
34
+ ## License
35
+
36
+ It is free software, and may be redistributed under the terms of MIT license.
@@ -0,0 +1,7 @@
1
+ require 'bundler/setup'
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -0,0 +1,10 @@
1
+ require 'protector'
2
+ require 'inherited_resources'
3
+ require 'active_support/all'
4
+
5
+ require "protector/inherited_resources/version"
6
+ require "protector/inherited_resources/instance_methods"
7
+ require "protector/inherited_resources/class_methods"
8
+
9
+ InheritedResources::BaseHelpers.send :include, Protector::InheritedResources::InstanceMethods
10
+ InheritedResources::ClassMethods.send :include, Protector::InheritedResources::ClassMethods
@@ -0,0 +1,18 @@
1
+ module Protector::InheritedResources
2
+ module ClassMethods
3
+ def protector_subject(subject)
4
+ subject = false if subject.nil?
5
+ @protector_subject = subject
6
+ end
7
+
8
+ def effective_protector_subject
9
+ return @protector_subject unless @protector_subject.nil?
10
+
11
+ if superclass.respond_to? :effective_protector_subject
12
+ superclass.effective_protector_subject
13
+ else
14
+ :current_user
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,29 @@
1
+ module Protector::InheritedResources
2
+ module InstanceMethods
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ alias_method_chain :end_of_association_chain, :protector
7
+ end
8
+
9
+ private
10
+
11
+ def end_of_association_chain_with_protector
12
+ resource = end_of_association_chain_without_protector
13
+
14
+ subject = self.class.effective_protector_subject
15
+
16
+ if subject.kind_of? Symbol
17
+ subject = send subject
18
+ elsif subject
19
+ subject = instance_exec &subject
20
+ end
21
+
22
+ if subject && resource.respond_to?(:restrict!)
23
+ resource.restrict! subject
24
+ else
25
+ resource
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ module Protector
2
+ module InheritedResources
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'protector/inherited_resources/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "protector-inherited_resources"
8
+ spec.version = Protector::InheritedResources::VERSION
9
+ spec.authors = ["Sergey Gridasov"]
10
+ spec.email = ["grindars@gmail.com"]
11
+ spec.description = %q{Integration layer between Protector and Inherited Resources}
12
+ spec.summary = spec.description
13
+ spec.homepage = "https://github.com/grindars/protector-inherited_resources"
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_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_dependency "protector", ">= 0.6.4"
24
+ spec.add_dependency "inherited_resources"
25
+ spec.add_dependency "activesupport"
26
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helpers/boot'
2
+
3
+ describe CustomUserController do
4
+ it 'should have user-specified protector subject' do
5
+ CustomUserController.effective_protector_subject.should == :custom_current_user
6
+ end
7
+
8
+ it 'assigns proper user' do
9
+ get :show, :id => 1
10
+ assigns(:custom_user).protector_subject?.should be_true
11
+ assigns(:custom_user).protector_subject.should == 'custom_user'
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helpers/boot'
2
+
3
+ describe DerivedController do
4
+ it 'should inherit user-specified protector subject' do
5
+ DerivedController.effective_protector_subject.should == :custom_current_user
6
+ end
7
+
8
+ it 'assigns proper user' do
9
+ get :show, :id => 1
10
+ assigns(:derived).protector_subject?.should be_true
11
+ assigns(:derived).protector_subject.should == 'custom_user'
12
+ end
13
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helpers/boot'
2
+
3
+ describe DummiesController do
4
+ it 'should have default protector subject' do
5
+ DummiesController.effective_protector_subject.should == :current_user
6
+ end
7
+
8
+ it 'restricts collections' do
9
+ get :index
10
+ first_dummy = assigns(:dummies).first!
11
+ first_dummy.protector_subject?.should be_true
12
+ first_dummy.protector_subject.should == 'default_user'
13
+ end
14
+
15
+ it 'restricts resources' do
16
+ get :show, :id => 1
17
+ assigns(:dummy).protector_subject?.should be_true
18
+ assigns(:dummy).protector_subject.should == 'default_user'
19
+ end
20
+
21
+ it 'restricts newly created resources' do
22
+ get :new
23
+ assigns(:dummy).protector_subject?.should be_true
24
+ assigns(:dummy).protector_subject.should == 'default_user'
25
+ end
26
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helpers/boot'
2
+
3
+ describe OptoutController do
4
+ it 'should not have protector subject' do
5
+ OptoutController.effective_protector_subject.should == false
6
+ end
7
+
8
+ it 'do not restricts resources' do
9
+ get :show, :id => 1
10
+ assigns(:optout).protector_subject?.should be_false
11
+ end
12
+ end
13
+
@@ -0,0 +1,4 @@
1
+ class ApplicationController < ActionController::Base
2
+ def current_user; 'default_user'; end
3
+ def custom_current_user; 'custom_user'; end
4
+ end
@@ -0,0 +1,4 @@
1
+ class CustomUserController < InheritedResources::Base
2
+ protector_subject :custom_current_user
3
+ defaults :resource_class => Dummy
4
+ end
@@ -0,0 +1,3 @@
1
+ class DerivedController < CustomUserController
2
+ defaults :resource_class => Dummy
3
+ end
@@ -0,0 +1,3 @@
1
+ class DummiesController < InheritedResources::Base
2
+
3
+ end
@@ -0,0 +1,4 @@
1
+ class OptoutController < InheritedResources::Base
2
+ protector_subject nil
3
+ defaults :resource_class => Dummy
4
+ end
@@ -0,0 +1,3 @@
1
+ class Dummy < ActiveRecord::Base
2
+
3
+ end
@@ -0,0 +1,4 @@
1
+ test:
2
+ adapter: <%= "jdbc" if defined? JRUBY_VERSION %>sqlite3
3
+ database: ":memory:"
4
+ verbosity: quiet
@@ -0,0 +1,6 @@
1
+ Rails.application.routes.draw do
2
+ resources :dummies
3
+ resources :derived
4
+ resources :optout
5
+ resources :custom_user
6
+ end
@@ -0,0 +1,5 @@
1
+ ActiveRecord::Schema.define do
2
+ create_table(:dummies) do |t|
3
+ t.timestamps
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+ require 'rspec'
2
+ require 'combustion'
3
+
4
+ RSpec.configure do |config|
5
+ config.treat_symbols_as_metadata_keys_with_true_values = true
6
+ config.run_all_when_everything_filtered = true
7
+ config.filter_run :focus
8
+
9
+ # Run specs in random order to surface order dependencies. If you find an
10
+ # order dependency and want to debug it, you can fix the order by providing
11
+ # the seed, which is printed after each run.
12
+ # --seed 1234
13
+ config.order = 'random'
14
+
15
+ config.before(:suite) do
16
+ Dummy.create! id: 1
17
+ end
18
+
19
+ config.after(:suite) do
20
+ Dummy.find(1).destroy
21
+ end
22
+ end
23
+
24
+ Combustion.initialize! :active_record, :action_controller
25
+
26
+ require 'rspec/rails'
27
+ require 'rspec/autorun'
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: protector-inherited_resources
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Gridasov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: protector
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.6.4
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 0.6.4
55
+ - !ruby/object:Gem::Dependency
56
+ name: inherited_resources
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
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: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Integration layer between Protector and Inherited Resources
84
+ email:
85
+ - grindars@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .rspec
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - lib/protector/inherited_resources.rb
97
+ - lib/protector/inherited_resources/class_methods.rb
98
+ - lib/protector/inherited_resources/instance_methods.rb
99
+ - lib/protector/inherited_resources/version.rb
100
+ - protector-inherited_resources.gemspec
101
+ - spec/controllers/custom_user_spec.rb
102
+ - spec/controllers/derived_spec.rb
103
+ - spec/controllers/dummies_spec.rb
104
+ - spec/controllers/optout_spec.rb
105
+ - spec/internal/app/controllers/application_controller.rb
106
+ - spec/internal/app/controllers/custom_user_controller.rb
107
+ - spec/internal/app/controllers/derived_controller.rb
108
+ - spec/internal/app/controllers/dummies_controller.rb
109
+ - spec/internal/app/controllers/optout_controller.rb
110
+ - spec/internal/app/models/dummy.rb
111
+ - spec/internal/app/views/application/index.html.erb
112
+ - spec/internal/app/views/application/new.html.erb
113
+ - spec/internal/app/views/application/show.html.erb
114
+ - spec/internal/config/database.yml
115
+ - spec/internal/config/routes.rb
116
+ - spec/internal/db/schema.rb
117
+ - spec/spec_helpers/boot.rb
118
+ homepage: https://github.com/grindars/protector-inherited_resources
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.0.3
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Integration layer between Protector and Inherited Resources
142
+ test_files:
143
+ - spec/controllers/custom_user_spec.rb
144
+ - spec/controllers/derived_spec.rb
145
+ - spec/controllers/dummies_spec.rb
146
+ - spec/controllers/optout_spec.rb
147
+ - spec/internal/app/controllers/application_controller.rb
148
+ - spec/internal/app/controllers/custom_user_controller.rb
149
+ - spec/internal/app/controllers/derived_controller.rb
150
+ - spec/internal/app/controllers/dummies_controller.rb
151
+ - spec/internal/app/controllers/optout_controller.rb
152
+ - spec/internal/app/models/dummy.rb
153
+ - spec/internal/app/views/application/index.html.erb
154
+ - spec/internal/app/views/application/new.html.erb
155
+ - spec/internal/app/views/application/show.html.erb
156
+ - spec/internal/config/database.yml
157
+ - spec/internal/config/routes.rb
158
+ - spec/internal/db/schema.rb
159
+ - spec/spec_helpers/boot.rb