actioncontroller-parameter_filter 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.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in activerecord-parameter_filter.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Alex McHale
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.markdown ADDED
@@ -0,0 +1,43 @@
1
+ actioncontroller-parameter_filter
2
+ =================================
3
+
4
+ Summary
5
+ -------
6
+
7
+ ParameterFilter is a module to mix into ActionController subclasses. It inserts
8
+ a before_filter which will automatically remove any fields in params that are
9
+ not explicitly allowed.
10
+
11
+ Installation
12
+ ------------
13
+
14
+ Include the following in your Gemfile:
15
+
16
+ gem "actioncontroller-parameter_filter"
17
+
18
+ Usage
19
+ -----
20
+
21
+ For global security, include the following in your ApplicationController:
22
+
23
+ include ParameterFilter
24
+
25
+ Then, inside each of you controllers, specify what fields you want each action
26
+ to receive:
27
+
28
+ # Accept user[email] and user[password] on the create and update actions.
29
+ accepts :fields => { :user => [ :email, :password ] }, :on => [ :create, :update ]
30
+
31
+ # Accept user[email] and user[password] on all actions.
32
+ accepts fields: { user: %w( email password ) }
33
+
34
+ # Accept q on the search action.
35
+ accepts field: "q", on: "search"
36
+
37
+ # Accept q and sort on the search and index actions.
38
+ accepts fields: [ :q, :sort ], on: %w( search index )
39
+
40
+ ParameterFilter should be pretty flexible in what you throw at it.
41
+
42
+ NOTE: All actions are automatically allowed to receive :controller, :action and
43
+ :id.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "actioncontroller-parameter_filter/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "actioncontroller-parameter_filter"
7
+ s.version = Activerecord::ParameterFilter::VERSION
8
+ s.authors = ["Alex McHale"]
9
+ s.email = ["alex@anticlever.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{A gem to easily filter out unwanted parameters in ActionController.}
12
+ s.description = %q{A gem to easily filter out unwanted parameters in ActionController.}
13
+
14
+ s.rubyforge_project = "actioncontroller-parameter_filter"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
@@ -0,0 +1,106 @@
1
+ require "actioncontroller-parameter_filter/version"
2
+
3
+ # When a controller has ParameterFilter included, it will by default remove
4
+ # everything from params. The way to receive parameters is to specifically
5
+ # allow them with accept_fields.
6
+
7
+ module ActionController
8
+
9
+ # accept_fields user: [ :email, :password ]
10
+ # accept_fields { user: { company: [ :name, :address ] }, on: [ :update, :create ]
11
+
12
+ module ParameterFilter
13
+
14
+ module ClassMethods
15
+
16
+ def accept_fields_parser fields
17
+ table = {}
18
+
19
+ [fields].flatten.compact.uniq.each do |field|
20
+ case field
21
+
22
+ when Symbol, String
23
+ table[field.to_s] = {}
24
+
25
+ when Hash
26
+ field.each do |key, value|
27
+ table[key.to_s] = accept_fields_parser value
28
+ end
29
+
30
+ end
31
+ end
32
+
33
+ table
34
+ end
35
+
36
+ def accepts options = {}
37
+ @_accepted_fields ||= { nil: { controller: {}, action: {}, id: {} } }
38
+ fields = options[:fields] || options[:field] || {}
39
+
40
+ case options[:on]
41
+ when Array
42
+ options[:on].each do |k|
43
+ @_accepted_fields[k.to_s] = accept_fields_parser fields
44
+ end
45
+
46
+ when Symbol, String
47
+ @_accepted_fields[options[:on].to_s] = accept_fields_parser fields
48
+
49
+ else
50
+ @_accepted_fields[nil] ||= {}
51
+ @_accepted_fields[nil].merge! accept_fields_parser fields
52
+
53
+ end
54
+ end
55
+
56
+ end
57
+
58
+ module InstanceMethods
59
+
60
+ def remove_filtered_parameters accepted_fields = nil, parameters = nil
61
+ if !accepted_fields && !parameters
62
+ accepted_fields = self.class.instance_variable_get("@_accepted_fields")
63
+ raise [ :af, accepted_fields ].inspect
64
+ fields = (accepted_fields[nil] || {}).merge(accepted_fields[self.action_name] || {})
65
+ remove_filtered_parameters fields, self.params
66
+ elsif parameters
67
+ accepted_keys = ParameterFilter.field_keys accepted_fields
68
+ accepted_keys += [ :controller, :action, :id ] if parameters == params
69
+ parameters.slice! *accepted_keys
70
+
71
+ ParameterFilter.each_field accepted_fields do |k, v|
72
+ remove_filtered_parameters v, parameters[k] if parameters[k].kind_of? Hash
73
+ end
74
+ end
75
+ end
76
+
77
+ end
78
+
79
+ def self.each_field fields
80
+ [ fields ].flatten.compact.uniq.each do |f|
81
+ case f
82
+ when Hash then f.each { |k, v| yield k, v }
83
+ when String, Symbol then yield f
84
+ end
85
+ end
86
+ end
87
+
88
+ def self.field_keys fields
89
+ fields.map do |field|
90
+ case field
91
+ when Array then field_keys field
92
+ when Hash then field.keys
93
+ else field
94
+ end
95
+ end.flatten.compact.uniq
96
+ end
97
+
98
+ def self.included base
99
+ base.send :extend, ClassMethods
100
+ base.send :include, InstanceMethods
101
+ base.send :before_filter, :remove_filtered_parameters
102
+ end
103
+
104
+ end
105
+
106
+ end
@@ -0,0 +1,5 @@
1
+ module Activerecord
2
+ module ParameterFilter
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: actioncontroller-parameter_filter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alex McHale
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-26 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A gem to easily filter out unwanted parameters in ActionController.
15
+ email:
16
+ - alex@anticlever.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.markdown
25
+ - Rakefile
26
+ - actioncontroller-parameter_filter.gemspec
27
+ - lib/actioncontroller-parameter_filter.rb
28
+ - lib/actioncontroller-parameter_filter/version.rb
29
+ homepage: ''
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project: actioncontroller-parameter_filter
49
+ rubygems_version: 1.8.11
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: A gem to easily filter out unwanted parameters in ActionController.
53
+ test_files: []