strong_attributes 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: da121740cc4a0a6f17cb4f4c60d330c830a7b4de
4
+ data.tar.gz: 81639530a105615c335f1bda7bb9bc3d8640b0d3
5
+ SHA512:
6
+ metadata.gz: c4f2d82dede1d5bbbb52bdf4e217eeac317b1840a17c1e2ed78965a23356a1b8a82b0bbe8b7f9f9b67764cdd91d8d84383c9294ba924ee405770765edf5782b1
7
+ data.tar.gz: d18af71fbf9a1e071cc16ad255b790ff3e774cd16faa2c485a5b850d0a4e6e26aa1fad49d9cea7387e0144ed269d89bb3bab6b79f0a85696b130f3b4afbc68da
data/.gitignore ADDED
@@ -0,0 +1,19 @@
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
+ *.swp
19
+
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --order rand
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - rbx-19mode
7
+ - jruby-19mode
8
+
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ yardoc 'lib/strong_presenter/**/*.rb' -m markdown --no-private
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # StrongAttributes Changelog
2
+
3
+ ## v0.0.1
4
+
5
+ - code extracted from strong_presenter
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in strong_attributes.gemspec
4
+ gemspec
5
+
6
+ gem 'debugger', platforms: :mri_19
7
+ gem 'byebug', platforms: :mri_20
8
+
9
+ platforms :mri_19, :mri_20 do
10
+ gem 'simplecov', require: false
11
+ gem 'coveralls', require: false
12
+ end
13
+
data/LICENSE ADDED
@@ -0,0 +1,179 @@
1
+ Copyright (c) 2013 Ronald Ping Man Chan
2
+
3
+ The Source Code Form referred to in this LICENSE file applies to all files stored within the same directory as this file, or in a subdirectory of the directory of this file, unless it is explicitly licensed or released otherwise. The official source of this software is found at https://github.com/ronalchn/.
4
+
5
+ This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
+
7
+ =============================================
8
+ A copy of the Mozilla Public License follows.
9
+ =============================================
10
+
11
+ Mozilla Public License
12
+ Version 2.0
13
+
14
+ 1. Definitions
15
+
16
+ 1.1. “Contributor”
17
+ means each individual or legal entity that creates, contributes to the creation of, or owns Covered Software.
18
+
19
+ 1.2. “Contributor Version”
20
+ means the combination of the Contributions of others (if any) used by a Contributor and that particular Contributor’s Contribution.
21
+
22
+ 1.3. “Contribution”
23
+ means Covered Software of a particular Contributor.
24
+
25
+ 1.4. “Covered Software”
26
+ means Source Code Form to which the initial Contributor has attached the notice in Exhibit A, the Executable Form of such Source Code Form, and Modifications of such Source Code Form, in each case including portions thereof.
27
+
28
+ 1.5. “Incompatible With Secondary Licenses”
29
+ means
30
+
31
+ that the initial Contributor has attached the notice described in Exhibit B to the Covered Software; or
32
+
33
+ that the Covered Software was made available under the terms of version 1.1 or earlier of the License, but not also under the terms of a Secondary License.
34
+
35
+ 1.6. “Executable Form”
36
+ means any form of the work other than Source Code Form.
37
+
38
+ 1.7. “Larger Work”
39
+ means a work that combines Covered Software with other material, in a separate file or files, that is not Covered Software.
40
+
41
+ 1.8. “License”
42
+ means this document.
43
+
44
+ 1.9. “Licensable”
45
+ means having the right to grant, to the maximum extent possible, whether at the time of the initial grant or subsequently, any and all of the rights conveyed by this License.
46
+
47
+ 1.10. “Modifications”
48
+ means any of the following:
49
+
50
+ any file in Source Code Form that results from an addition to, deletion from, or modification of the contents of Covered Software; or
51
+
52
+ any new file in Source Code Form that contains any Covered Software.
53
+
54
+ 1.11. “Patent Claims” of a Contributor
55
+ means any patent claim(s), including without limitation, method, process, and apparatus claims, in any patent Licensable by such Contributor that would be infringed, but for the grant of the License, by the making, using, selling, offering for sale, having made, import, or transfer of either its Contributions or its Contributor Version.
56
+
57
+ 1.12. “Secondary License”
58
+ means either the GNU General Public License, Version 2.0, the GNU Lesser General Public License, Version 2.1, the GNU Affero General Public License, Version 3.0, or any later versions of those licenses.
59
+
60
+ 1.13. “Source Code Form”
61
+ means the form of the work preferred for making modifications.
62
+
63
+ 1.14. “You” (or “Your”)
64
+ means an individual or a legal entity exercising rights under this License. For legal entities, “You” includes any entity that controls, is controlled by, or is under common control with You. For purposes of this definition, “control” means (a) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (b) ownership of more than fifty percent (50%) of the outstanding shares or beneficial ownership of such entity.
65
+
66
+ 2. License Grants and Conditions
67
+
68
+ 2.1. Grants
69
+
70
+ Each Contributor hereby grants You a world-wide, royalty-free, non-exclusive license:
71
+
72
+ under intellectual property rights (other than patent or trademark) Licensable by such Contributor to use, reproduce, make available, modify, display, perform, distribute, and otherwise exploit its Contributions, either on an unmodified basis, with Modifications, or as part of a Larger Work; and
73
+
74
+ under Patent Claims of such Contributor to make, use, sell, offer for sale, have made, import, and otherwise transfer either its Contributions or its Contributor Version.
75
+
76
+ 2.2. Effective Date
77
+
78
+ The licenses granted in Section 2.1 with respect to any Contribution become effective for each Contribution on the date the Contributor first distributes such Contribution.
79
+
80
+ 2.3. Limitations on Grant Scope
81
+
82
+ The licenses granted in this Section 2 are the only rights granted under this License. No additional rights or licenses will be implied from the distribution or licensing of Covered Software under this License. Notwithstanding Section 2.1(b) above, no patent license is granted by a Contributor:
83
+
84
+ for any code that a Contributor has removed from Covered Software; or
85
+
86
+ for infringements caused by: (i) Your and any other third party’s modifications of Covered Software, or (ii) the combination of its Contributions with other software (except as part of its Contributor Version); or
87
+
88
+ under Patent Claims infringed by Covered Software in the absence of its Contributions.
89
+
90
+ This License does not grant any rights in the trademarks, service marks, or logos of any Contributor (except as may be necessary to comply with the notice requirements in Section 3.4).
91
+
92
+ 2.4. Subsequent Licenses
93
+
94
+ No Contributor makes additional grants as a result of Your choice to distribute the Covered Software under a subsequent version of this License (see Section 10.2) or under the terms of a Secondary License (if permitted under the terms of Section 3.3).
95
+
96
+ 2.5. Representation
97
+
98
+ Each Contributor represents that the Contributor believes its Contributions are its original creation(s) or it has sufficient rights to grant the rights to its Contributions conveyed by this License.
99
+
100
+ 2.6. Fair Use
101
+
102
+ This License is not intended to limit any rights You have under applicable copyright doctrines of fair use, fair dealing, or other equivalents.
103
+
104
+ 2.7. Conditions
105
+
106
+ Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in Section 2.1.
107
+
108
+ 3. Responsibilities
109
+
110
+ 3.1. Distribution of Source Form
111
+
112
+ All distribution of Covered Software in Source Code Form, including any Modifications that You create or to which You contribute, must be under the terms of this License. You must inform recipients that the Source Code Form of the Covered Software is governed by the terms of this License, and how they can obtain a copy of this License. You may not attempt to alter or restrict the recipients’ rights in the Source Code Form.
113
+
114
+ 3.2. Distribution of Executable Form
115
+
116
+ If You distribute Covered Software in Executable Form then:
117
+
118
+ such Covered Software must also be made available in Source Code Form, as described in Section 3.1, and You must inform recipients of the Executable Form how they can obtain a copy of such Source Code Form by reasonable means in a timely manner, at a charge no more than the cost of distribution to the recipient; and
119
+
120
+ You may distribute such Executable Form under the terms of this License, or sublicense it under different terms, provided that the license for the Executable Form does not attempt to limit or alter the recipients’ rights in the Source Code Form under this License.
121
+
122
+ 3.3. Distribution of a Larger Work
123
+
124
+ You may create and distribute a Larger Work under terms of Your choice, provided that You also comply with the requirements of this License for the Covered Software. If the Larger Work is a combination of Covered Software with a work governed by one or more Secondary Licenses, and the Covered Software is not Incompatible With Secondary Licenses, this License permits You to additionally distribute such Covered Software under the terms of such Secondary License(s), so that the recipient of the Larger Work may, at their option, further distribute the Covered Software under the terms of either this License or such Secondary License(s).
125
+
126
+ 3.4. Notices
127
+
128
+ You may not remove or alter the substance of any license notices (including copyright notices, patent notices, disclaimers of warranty, or limitations of liability) contained within the Source Code Form of the Covered Software, except that You may alter any license notices to the extent required to remedy known factual inaccuracies.
129
+
130
+ 3.5. Application of Additional Terms
131
+
132
+ You may choose to offer, and to charge a fee for, warranty, support, indemnity or liability obligations to one or more recipients of Covered Software. However, You may do so only on Your own behalf, and not on behalf of any Contributor. You must make it absolutely clear that any such warranty, support, indemnity, or liability obligation is offered by You alone, and You hereby agree to indemnify every Contributor for any liability incurred by such Contributor as a result of warranty, support, indemnity or liability terms You offer. You may include additional disclaimers of warranty and limitations of liability specific to any jurisdiction.
133
+
134
+ 4. Inability to Comply Due to Statute or Regulation
135
+
136
+ If it is impossible for You to comply with any of the terms of this License with respect to some or all of the Covered Software due to statute, judicial order, or regulation then You must: (a) comply with the terms of this License to the maximum extent possible; and (b) describe the limitations and the code they affect. Such description must be placed in a text file included with all distributions of the Covered Software under this License. Except to the extent prohibited by statute or regulation, such description must be sufficiently detailed for a recipient of ordinary skill to be able to understand it.
137
+
138
+ 5. Termination
139
+
140
+ 5.1. The rights granted under this License will terminate automatically if You fail to comply with any of its terms. However, if You become compliant, then the rights granted under this License from a particular Contributor are reinstated (a) provisionally, unless and until such Contributor explicitly and finally terminates Your grants, and (b) on an ongoing basis, if such Contributor fails to notify You of the non-compliance by some reasonable means prior to 60 days after You have come back into compliance. Moreover, Your grants from a particular Contributor are reinstated on an ongoing basis if such Contributor notifies You of the non-compliance by some reasonable means, this is the first time You have received notice of non-compliance with this License from such Contributor, and You become compliant prior to 30 days after Your receipt of the notice.
141
+
142
+ 5.2. If You initiate litigation against any entity by asserting a patent infringement claim (excluding declaratory judgment actions, counter-claims, and cross-claims) alleging that a Contributor Version directly or indirectly infringes any patent, then the rights granted to You by any and all Contributors for the Covered Software under Section 2.1 of this License shall terminate.
143
+
144
+ 5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user license agreements (excluding distributors and resellers) which have been validly granted by You or Your distributors under this License prior to termination shall survive termination.
145
+
146
+ 6. Disclaimer of Warranty
147
+
148
+ Covered Software is provided under this License on an “as is” basis, without warranty of any kind, either expressed, implied, or statutory, including, without limitation, warranties that the Covered Software is free of defects, merchantable, fit for a particular purpose or non-infringing. The entire risk as to the quality and performance of the Covered Software is with You. Should any Covered Software prove defective in any respect, You (not any Contributor) assume the cost of any necessary servicing, repair, or correction. This disclaimer of warranty constitutes an essential part of this License. No use of any Covered Software is authorized under this License except under this disclaimer.
149
+
150
+ 7. Limitation of Liability
151
+
152
+ Under no circumstances and under no legal theory, whether tort (including negligence), contract, or otherwise, shall any Contributor, or anyone who distributes Covered Software as permitted above, be liable to You for any direct, indirect, special, incidental, or consequential damages of any character including, without limitation, damages for lost profits, loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses, even if such party shall have been informed of the possibility of such damages. This limitation of liability shall not apply to liability for death or personal injury resulting from such party’s negligence to the extent applicable law prohibits such limitation. Some jurisdictions do not allow the exclusion or limitation of incidental or consequential damages, so this exclusion and limitation may not apply to You.
153
+
154
+ 8. Litigation
155
+
156
+ Any litigation relating to this License may be brought only in the courts of a jurisdiction where the defendant maintains its principal place of business and such litigation shall be governed by laws of that jurisdiction, without reference to its conflict-of-law provisions. Nothing in this Section shall prevent a party’s ability to bring cross-claims or counter-claims.
157
+
158
+ 9. Miscellaneous
159
+
160
+ This License represents the complete agreement concerning the subject matter hereof. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable. Any law or regulation which provides that the language of a contract shall be construed against the drafter shall not be used to construe this License against a Contributor.
161
+
162
+ 10. Versions of the License
163
+
164
+ 10.1. New Versions
165
+
166
+ Mozilla Foundation is the license steward. Except as provided in Section 10.3, no one other than the license steward has the right to modify or publish new versions of this License. Each version will be given a distinguishing version number.
167
+
168
+ 10.2. Effect of New Versions
169
+
170
+ You may distribute the Covered Software under the terms of the version of the License under which You originally received the Covered Software, or under the terms of any subsequent version published by the license steward.
171
+
172
+ 10.3. Modified Versions
173
+
174
+ If you create software not governed by this License, and you want to create a new license for such software, you may create and use a modified version of this License if you rename the license and remove any references to the name of the license steward (except to note that such modified license differs from this License).
175
+
176
+ 10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses
177
+
178
+ If You choose to distribute Source Code Form that is Incompatible With Secondary Licenses under the terms of this version of the License, the notice described in Exhibit B of this License must be attached.
179
+
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # StrongAttributes
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'strong_attributes'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install strong_attributes
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,32 @@
1
+ require 'rake'
2
+ require 'bundler/gem_tasks'
3
+ require 'rspec/core/rake_task'
4
+
5
+ task "default" => "ci"
6
+
7
+ desc "Run all tests for CI"
8
+ citask = ["spec"]
9
+ begin
10
+ require 'coveralls/rake/task'
11
+ Coveralls::RakeTask.new
12
+ citask << "coveralls:push" if defined? 'Coveralls'
13
+ rescue LoadError
14
+ end
15
+ task "ci" => citask
16
+
17
+ desc "Run all specs"
18
+ task "spec" => "spec:all"
19
+
20
+ namespace "spec" do
21
+ task "all" => ["strong_attributes"]
22
+
23
+ def spec_task(name)
24
+ desc "Run #{name} specs"
25
+ RSpec::Core::RakeTask.new(name) do |t|
26
+ t.pattern = "spec/#{name}/**/*_spec.rb"
27
+ end
28
+ end
29
+
30
+ spec_task "strong_attributes"
31
+ end
32
+
@@ -0,0 +1,58 @@
1
+ module StrongAttributes
2
+ module Displayable
3
+ extend ActiveSupport::Concern
4
+ include StrongAttributes::Permissible
5
+
6
+ # Displays the result of each method call if permitted. To permit all without checking, call `permit!` first.
7
+ #
8
+ # Calls and returns the result of each method call in the argument list. If a block is given, then each result
9
+ # is passed to the block.
10
+ #
11
+ # user.displays :username, :email # returns [user.username, user.email]
12
+ #
13
+ # Or with two arguments, the name of the field is passed first:
14
+ #
15
+ # <ul>
16
+ # <% user.displays :username, :email, :address do |field, value| %>
17
+ # <li><%= field.capitalize %>: <% value %></li>
18
+ # <% end %>
19
+ # </ul>
20
+ #
21
+ # If only the presented value is desired, use `each`:
22
+ #
23
+ # <% user.displays(:username, :email).each do |value| %>
24
+ # <td><%= value %></td>
25
+ # <% end %>
26
+ #
27
+ # Arguments can be included in an array:
28
+ #
29
+ # user.displays :username, [:notifications, :unread] # returns [user.username, user.notifications(:unread)]
30
+ #
31
+ def displays *attributes
32
+ select_permitted(*attributes).map do |attribute|
33
+ args = Array(attribute).dup
34
+ value = self
35
+ until args.empty? do
36
+ arity = value.method(args[0]).arity
37
+ if arity >= 0
38
+ value = value.public_send *args.slice!(0, arity+1)
39
+ else
40
+ value = value.public_send *args
41
+ break
42
+ end
43
+ end
44
+ yield attribute, value if block_given?
45
+ value
46
+ end
47
+ end
48
+
49
+ # Same as displays, but for a single attribute. The differences are:
50
+ # - the return value is not in an Array
51
+ # - passes the value only (without attribute key as the 1st argument) to a block
52
+ def display field
53
+ displays field do |key, value|
54
+ yield value if block_given?
55
+ end.first
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,50 @@
1
+ module StrongAttributes
2
+ module Permissible
3
+ extend ActiveSupport::Concern
4
+
5
+ # Permits given attributes. May be invoked multiple times.
6
+ #
7
+ # @example Each argument represents a single attribute:
8
+ # ArticlePresenter.new(@article).permit(:heading, :article)
9
+ #
10
+ # @example Attribute paths can be specified using symbol arrays. If an author name is normally accessed using @article.author.name:
11
+ # ArticlePresenter.new(@article).permit([:author, :name])
12
+ #
13
+ # @param [[Symbols*]*] attribute_paths
14
+ # the attributes to permit. An array of symbols represents an attribute path.
15
+ # @return [self]
16
+ def permit! *attribute_paths
17
+ permitted_attributes.permit *attribute_paths
18
+ self
19
+ end
20
+
21
+ # Permits all presenter attributes for presents, present & filter methods.
22
+ def permit_all!
23
+ permitted_attributes.permit_all!
24
+ self
25
+ end
26
+
27
+ # Selects the attributes given which have been permitted - an array of attributes
28
+ # @param [Array<Symbols>*] attribute_paths
29
+ # the attribute paths to check. The attribute paths may also have arguments.
30
+ # @return [Array<Array<Symbol>>] attribute (paths)
31
+ def select_permitted *attribute_paths
32
+ permitted_attributes.select_permitted *attribute_paths
33
+ end
34
+
35
+ protected
36
+ def permitted_attributes
37
+ @permitted_attributes ||= StrongAttributes::Permissions.new
38
+ end
39
+
40
+ # Links presenter to permissions group of given presenter.
41
+ # @param [Presenter] parent_presenter
42
+ # @param [Array<Symbol>] relative_path
43
+ # The prefix prepended before every permission check relative to parent presenter.
44
+ def link_permissions parent_presenter, relative_path = []
45
+ @permitted_attributes = StrongAttributes::Permissions.new(parent_presenter.permitted_attributes, relative_path)
46
+ end
47
+
48
+ end
49
+ end
50
+
@@ -0,0 +1,141 @@
1
+ module StrongAttributes
2
+ # @private
3
+ #
4
+ # Storage format:
5
+ # Each attribute path is stored as an array of objects, usually strings in a Set. For indifferent access,
6
+ # all symbols are converted to strings.
7
+ #
8
+ # Arguments can also be part of permissions control. They are simply additional elements in the attribute path array,
9
+ # and need not be strings or symbols. Permitting a string or symbol argument automatically permits both.
10
+ #
11
+ # When checking if paths with tainted strings/elements are permitted, only exact matches are allowed
12
+ class Permissions
13
+
14
+ def prefix_path
15
+ @prefix_path || []
16
+ end
17
+
18
+ # Initialize, optionally with link to permitted paths, and the prefix to that (with copy on write semantics)
19
+ def initialize(permissions = nil, prefix_path = [])
20
+ unless permissions.nil?
21
+ @permitted_paths = permissions.permitted_paths
22
+ @prefix_path = permissions.prefix_path + canonicalize(prefix_path) # copy on write
23
+ end
24
+ end
25
+
26
+ # Checks whether everything is permitted. Considers :*, which permits all methods
27
+ # but not association methods to be complete.
28
+ # @return [Boolean]
29
+ def complete?
30
+ permitted? prefix_path + [:*]
31
+ end
32
+
33
+ # Permits wildcard method, but not association methods.
34
+ # @return self
35
+ def permit_all!
36
+ copy_on_write!
37
+ permitted_paths << [:*]
38
+ self
39
+ end
40
+
41
+ # Checks if the attribute path is permitted. This is the case if
42
+ # any array prefix has been permitted.
43
+ # @param [Object, Array<Object>] prefix_path
44
+ # @param [Object, Array<Object>] attribute_path
45
+ # @return [Boolean]
46
+ def permitted? attribute_path
47
+ attribute_path = canonicalize(attribute_path)
48
+ return true if permitted_paths.include? prefix_path + attribute_path # exact match
49
+ !path_tainted?(attribute_path) and permitted_by_wildcard?(prefix_path + attribute_path) # wildcard match only if not tainted
50
+ end
51
+
52
+ # Selects the attribute paths which are permitted.
53
+ # @param [Array] prefix_path
54
+ # namespace in which each of the given attribute paths are in
55
+ # @param [[Object, Array<Object>]*] *attribute_paths
56
+ # each attribute path is a symbol or array of symbols
57
+ # @return [Array<Object, Array<Object>>] array of attribute paths permitted
58
+ def select_permitted *attribute_paths
59
+ attribute_paths.select { |attribute_path| permitted?(attribute_path) }
60
+ end
61
+
62
+ # Rejects the attribute paths which are permitted. Opposite of select_permitted.
63
+ # Returns the attribute paths which are not permitted.
64
+ #
65
+ # @param [Array] prefix_path
66
+ # namespace in which each of the given attribute paths are in
67
+ # @param [[Object, Array<Object>]*] *attribute_paths
68
+ # each attribute path is an object(string) or array
69
+ # @return [Array<Object, Array<Object>>] array of attribute paths remaining
70
+ def reject_permitted *attribute_paths
71
+ attribute_paths.reject { |attribute_path| permitted?(attribute_path) }
72
+ end
73
+
74
+ # Permits some attribute paths
75
+ #
76
+ # @param [Array] prefix_path
77
+ # path to prepend to each attribute path
78
+ # @param [[Object, Array]*] *attribute_paths
79
+ def permit *attribute_paths
80
+ copy_on_write!
81
+ attribute_paths = attribute_paths.map{|path|canonicalize(path).taint} # exact match
82
+ reject_permitted(*attribute_paths).each do |attribute_path| # don't permit if already permitted
83
+ permitted_paths << attribute_path # prefix_path = [] because of copy on write
84
+ end
85
+ self
86
+ end
87
+
88
+ protected
89
+
90
+ def permitted_paths
91
+ @permitted_paths ||= Set.new
92
+ end
93
+
94
+ private
95
+ # Is this still referencing another objects permissions?
96
+ def reference?
97
+ !@prefix_path.nil?
98
+ end
99
+
100
+ # Make a copy if this still references something else, since we are planning on writing soon
101
+ def copy_on_write!
102
+ if prefix_path == []
103
+ @permitted_paths = permitted_paths.dup
104
+ elsif reference?
105
+ @permitted_paths, old_set = Set.new, permitted_paths
106
+ old_set.each do |path|
107
+ @permitted_paths << path[prefix_path.size...path.size] if path[0...prefix_path.size] == prefix_path
108
+ end
109
+ end
110
+ @prefix_path = nil
111
+ end
112
+
113
+ def path_tainted? attribute_path
114
+ attribute_path.tainted? or attribute_path.any? { |element| element.tainted? }
115
+ end
116
+
117
+ # Caution: Will mutate path
118
+ def permitted_by_wildcard? path
119
+ unless path.empty?
120
+ path[-1] = :*
121
+ return true if permitted_paths.include? path
122
+ end
123
+ until path.empty?
124
+ path[-1] = :**
125
+ return true if permitted_paths.include? path
126
+ path.pop
127
+ end
128
+ false
129
+ end
130
+
131
+ # Converts symbols to strings (except for wildcard symbol)
132
+ def canonicalize array
133
+ array = Array(array)
134
+ canonical_array = array.map{|e|e.is_a?(Symbol) ? e.to_s : e}
135
+ canonical_array[-1] = array.last if [:*, :**].include? array.last
136
+ canonical_array.taint if array.tainted?
137
+ canonical_array
138
+ end
139
+
140
+ end
141
+ end
@@ -0,0 +1,3 @@
1
+ module StrongAttributes
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'active_support/concern'
2
+ #require 'active_support/core_ext/hash/keys'
3
+
4
+ require "strong_attributes/version"
5
+ require "strong_attributes/permissions"
6
+ require "strong_attributes/permissible"
7
+ require "strong_attributes/displayable"
8
+
9
+ module StrongAttributes
10
+ # Your code goes here...
11
+ end
@@ -0,0 +1,17 @@
1
+ begin
2
+ require 'simplecov'
3
+ SimpleCov.start do
4
+ add_filter 'spec'
5
+ end
6
+ rescue LoadError
7
+ end
8
+
9
+ require 'bundler/setup'
10
+ require 'strong_attributes'
11
+
12
+ RSpec.configure do |config|
13
+ config.treat_symbols_as_metadata_keys_with_true_values = true
14
+ config.expect_with(:rspec) {|c| c.syntax = :expect}
15
+ config.order = :random
16
+ end
17
+
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ module StrongAttributes
4
+ describe Permissible do
5
+ it "selects permitted attributes" do
6
+ permissible = Object.new
7
+ permissible.extend(Permissible)
8
+
9
+ permissible.permit! :a, :b, :c
10
+ permitted = permissible.select_permitted :a, :c, :z
11
+ expect(permitted).to include(:a, :c)
12
+ expect(permitted).to_not include(:b, :z)
13
+ end
14
+
15
+ it "permits all if permit_all!" do
16
+ permissible = Object.new
17
+ permissible.extend(Permissible)
18
+
19
+ permissible.permit_all!
20
+ permitted = permissible.select_permitted :a, :b, :c
21
+ expect(permitted).to include(:a, :b, :c)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,202 @@
1
+ require 'spec_helper'
2
+
3
+ module StrongAttributes
4
+ describe Permissions do
5
+ describe "#complete?" do
6
+ it 'is initially incomplete' do
7
+ expect(Permissions.new.complete?).to be false
8
+ end
9
+ end
10
+
11
+ describe "#permit_all!" do
12
+ it 'completes the object' do
13
+ expect(Permissions.new.permit_all!.complete?).to be true
14
+ end
15
+ end
16
+
17
+ describe "#permit" do
18
+ it 'permits an attribute' do
19
+ expect(Permissions.new.permit(:attr).permitted?(:attr)).to be true
20
+ end
21
+
22
+ it 'permits with arrays of symbols' do
23
+ expect(Permissions.new.permit([:attr, :array]).permitted?([:attr, :array])).to be true
24
+ end
25
+
26
+ it 'permits multiple at once' do
27
+ permissions = Permissions.new.permit([:another, :array], [:attr, :array], :attr2, :attr3)
28
+ expect(permissions.permitted?(:attr2)).to be true
29
+ expect(permissions.permitted?([:attr, :array])).to be true
30
+ end
31
+
32
+ it 'does not mutate arguments' do
33
+ attrpaths = [[:attr, :path], :s]
34
+ Permissions.new.permit(attrpathsarg = attrpaths.dup)
35
+
36
+ expect(attrpathsarg).to eq attrpaths
37
+ end
38
+
39
+ it 'permits specific path after wildcard' do
40
+ permissions = Permissions.new.permit([:prefix, :*])
41
+ expect(permissions.permitted?([:prefix, "specific".taint])).to be false
42
+ permissions.permit([:prefix, :specific])
43
+ expect(permissions.permitted?([:prefix, "specific".taint])).to be true
44
+ end
45
+ end
46
+
47
+ describe "#permitted?" do
48
+ context 'with some attributes permitted' do
49
+ before(:all) do
50
+ @permissions = Permissions.new.permit([:another, :array], [:attr, :array], :attr2, :attr3, [:a, :wildcard, :*], [:wild, :**])
51
+ end
52
+
53
+ it 'permits single wildcard' do
54
+ expect(@permissions.permitted?([:a, :wildcard, :irrelevant])).to be true
55
+ end
56
+
57
+ it 'permits full wildcard' do
58
+ expect(@permissions.permitted?([:wild, :wildcard, :irrelevant])).to be true
59
+ expect(@permissions.permitted?([:wild, :irrelevant])).to be true
60
+ end
61
+
62
+ it 'does not permit un-prefixed path' do
63
+ permissions = Permissions.new(@permissions, :attr)
64
+ expect(permissions.permitted?(:attr2)).to be false
65
+ expect(permissions.permitted?([:wild, :irrelevant])).to be false
66
+ end
67
+
68
+ it 'permits with prefix' do
69
+ permissions = Permissions.new(@permissions, :attr)
70
+ expect(permissions.permitted?(:array)).to be true
71
+ end
72
+
73
+ it 'does not permit other attributes' do
74
+ expect(@permissions.permitted?([:attr4, :irrelevant])).to be false
75
+ end
76
+
77
+ it 'does not permit shortened paths' do
78
+ expect(@permissions.permitted?(:attr)).to be false
79
+ end
80
+
81
+ it 'does not mutate arguments' do
82
+ attrpaths = [[:attr, :path], :s, [:array, :attr2]]
83
+
84
+ @permissions.permitted?(attrpathsarg = attrpaths.dup)
85
+ expect(attrpathsarg).to eq attrpaths
86
+
87
+ prefix = [:prefix]
88
+ @permissions.permitted?(attrpathsarg = attrpaths.dup)
89
+ expect(attrpathsarg).to eq attrpaths
90
+ end
91
+
92
+ it 'is indifferent to strings' do
93
+ expect(@permissions.permitted?(["attr","array"])).to be true
94
+ expect(@permissions.permitted?("a/wildcard/irrelevant".split("/"))).to be true
95
+ end
96
+
97
+ it 'does not permit single wildcard if tainted' do
98
+ expect(@permissions.permitted?([:a, :wildcard, "irrelevant".taint])).to be false
99
+ end
100
+
101
+ it 'does not permit full wildcard if tainted' do
102
+ expect(@permissions.permitted?(["wild".taint, :wildcard, :irrelevant])).to be false
103
+ expect(@permissions.permitted?(["wild", "irrelevant"].taint)).to be false
104
+ expect(@permissions.permitted?("a/wildcard/irrelevant".taint.split("/"))).to be false
105
+ end
106
+ end
107
+
108
+ context 'with permit all' do
109
+ it 'all methods are true' do
110
+ permissions = Permissions.new.permit_all!
111
+ expect(permissions.permitted?(:a)).to be true
112
+ expect(permissions.permitted?(:b)).to be true
113
+ end
114
+
115
+ it 'association methods are false' do
116
+ permissions = Permissions.new.permit_all!
117
+ expect(permissions.permitted?([:a,:a])).to be false
118
+ end
119
+ end
120
+ end
121
+ describe "#select_permitted" do
122
+ context 'with some attributes permitted' do
123
+ before(:all) do
124
+ @permissions = Permissions.new.permit([:another, :array], [:attr, :array, :*], :attr2, :attr3)
125
+ @permissions.permit([], [:attr, :arrays], :attr2, :attr3)
126
+ end
127
+
128
+ it 'can select something which is permitted' do
129
+ expect(@permissions.select_permitted(:attr2)).to eq [:attr2]
130
+ expect(@permissions.select_permitted([:attr, :array, :irrelevant])).to eq [[:attr, :array, :irrelevant]]
131
+ expect(@permissions.select_permitted([:another, :array])).to eq [[:another, :array]]
132
+ end
133
+
134
+ it 'does not select wildcard path without wildcard' do
135
+ expect(@permissions.select_permitted([:attr, :array])).to be_empty
136
+ end
137
+
138
+ it 'can select multiple permitted attributes' do
139
+ attribute_paths = [[:attr2], :attr3, [:another, :array], [:attr, :array, :method]]
140
+ permitted = @permissions.select_permitted(*attribute_paths)
141
+ attribute_paths.each do |attribute_path|
142
+ expect(permitted).to include attribute_path
143
+ end
144
+ end
145
+
146
+ it 'selects only permitted attributes in the original order' do
147
+ permitted_paths = [:attr2, :attr3, [:attr, :arrays], [:attr, :array, :meth], [:attr3]]
148
+ unpermitted_paths = [:attr4, :attr5, [:attrk, :irrelevant], [:attr, :ar], [:attr, :arrays, :more]]
149
+ attribute_paths = (unpermitted_paths + permitted_paths).shuffle
150
+ expect(@permissions.select_permitted(*attribute_paths)).to eq((attribute_paths & permitted_paths))
151
+ end
152
+
153
+ it 'does not mutate arguments' do
154
+ attrpaths = [[:attr, :path], :s, [:array, :attr2]]
155
+
156
+ @permissions.select_permitted(attrpathsarg = attrpaths.dup)
157
+ expect(attrpathsarg).to eq attrpaths
158
+
159
+ attrpaths = [[:attr, :array, :meth], :s, [:array, :attr2]]
160
+ @permissions.select_permitted(attrpathsarg = attrpaths.dup)
161
+ expect(attrpathsarg).to eq attrpaths
162
+ end
163
+ end
164
+
165
+ context 'with permit all' do
166
+ it 'everything is selected' do
167
+ permissions = Permissions.new.permit_all!
168
+ attribute_paths = [:attr2, :attr3, :attr2, :attr, :array]
169
+ permitted = permissions.select_permitted([:prefix, :array], *attribute_paths)
170
+ expect(permitted).to eq(attribute_paths)
171
+ end
172
+ end
173
+ end
174
+ describe "#reject_permitted" do
175
+ context 'with some attributes permitted' do
176
+ before(:all) do
177
+ @permissions = Permissions.new.permit([:another, :array], [:attr, :array, :*], :attr2, :attr3)
178
+ @permissions.permit([], [:attr, :arrays], :attr2, :attr3)
179
+ end
180
+
181
+ it 'selects only unpermitted attributes in the original order' do
182
+ permitted_paths = [:attr2, :attr3, [:attr, :arrays], [:attr, :array, :meth], [:attr3]]
183
+ unpermitted_paths = [:attr4, :attr5, [:attrk, :irrelevant], [:attr, :ar], [:attr, :arrays, :more]]
184
+ attribute_paths = (unpermitted_paths + permitted_paths).shuffle
185
+ expect(@permissions.reject_permitted(*attribute_paths)).to eq((attribute_paths & unpermitted_paths))
186
+ end
187
+
188
+ it 'does not mutate arguments' do
189
+ attrpaths = [[:attr, :path], :s, [:array, :attr2]]
190
+
191
+ @permissions.reject_permitted(attrpathsarg = attrpaths.dup)
192
+ expect(attrpathsarg).to eq attrpaths
193
+
194
+ attrpaths = [[:attr, :array, :meth], :s, [:array, :attr2]]
195
+ @permissions.reject_permitted(attrpathsarg = attrpaths.dup)
196
+ expect(attrpathsarg).to eq attrpaths
197
+ end
198
+ end
199
+ end
200
+ end
201
+ end
202
+
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+
3
+ SimpleCov.command_name 'spec:strong_presenter' if defined? SimpleCov
4
+
@@ -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 'strong_attributes/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "strong_attributes"
8
+ gem.version = StrongAttributes::VERSION
9
+ gem.authors = ["Ronald Chan"]
10
+ gem.email = ["ronalchn@gmail.com"]
11
+ gem.description = %q{strong_attributes provides a strong_parameters-style permit interface using a wrapper around your model. By accessing model attributes in your views only through this interface, you ensure that any attributes displayed are explicitly permitted, and sensitive data is not displayed to unauthorized users. This keeps your views free of authorization logic.}
12
+ gem.summary = %q{strong_attributes provides a strong_parameters-style permit interface using a wrapper around your model. By accessing model attributes in your views only through this interface, you ensure that any attributes displayed are explicitly permitted, and sensitive data is not displayed to unauthorized users. This keeps your views free of authorization logic.}
13
+ gem.homepage = "https://github.com/ronalchn/strong_attributes"
14
+ gem.license = "MPLv2"
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|gem|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_dependency 'activesupport', '>= 3.0'
22
+
23
+ gem.add_development_dependency "bundler", "~> 1.3"
24
+ gem.add_development_dependency 'rake', '>= 0.9.2'
25
+ gem.add_development_dependency 'rspec', '~> 2.12'
26
+ gem.add_development_dependency 'rspec-mocks', '>= 2.12.1'
27
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: strong_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ronald Chan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-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'
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'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.9.2
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 0.9.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '2.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-mocks
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: 2.12.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: 2.12.1
83
+ description: strong_attributes provides a strong_parameters-style permit interface
84
+ using a wrapper around your model. By accessing model attributes in your views only
85
+ through this interface, you ensure that any attributes displayed are explicitly
86
+ permitted, and sensitive data is not displayed to unauthorized users. This keeps
87
+ your views free of authorization logic.
88
+ email:
89
+ - ronalchn@gmail.com
90
+ executables: []
91
+ extensions: []
92
+ extra_rdoc_files: []
93
+ files:
94
+ - .gitignore
95
+ - .rspec
96
+ - .travis.yml
97
+ - .yardopts
98
+ - CHANGELOG.md
99
+ - Gemfile
100
+ - LICENSE
101
+ - README.md
102
+ - Rakefile
103
+ - lib/strong_attributes.rb
104
+ - lib/strong_attributes/displayable.rb
105
+ - lib/strong_attributes/permissible.rb
106
+ - lib/strong_attributes/permissions.rb
107
+ - lib/strong_attributes/version.rb
108
+ - spec/spec_helper.rb
109
+ - spec/strong_attributes/permissible_spec.rb
110
+ - spec/strong_attributes/permissions_spec.rb
111
+ - spec/strong_attributes/simplecov_spec.rb
112
+ - strong_attributes.gemspec
113
+ homepage: https://github.com/ronalchn/strong_attributes
114
+ licenses:
115
+ - MPLv2
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.0.3
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: strong_attributes provides a strong_parameters-style permit interface using
137
+ a wrapper around your model. By accessing model attributes in your views only through
138
+ this interface, you ensure that any attributes displayed are explicitly permitted,
139
+ and sensitive data is not displayed to unauthorized users. This keeps your views
140
+ free of authorization logic.
141
+ test_files: []