sudo_attributes 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Peter Brown
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1,81 @@
1
+ h1. sudo_attributes
2
+
3
+ Adds 'sudo' methods to active record classes, allowing you to easily override protected attributes.
4
+
5
+ h2. The Problem
6
+
7
+ ActiveRecord (ActiveModel in Rails 3) provides a convenient way to make your application more secure by using "protected" attributes. Protected attributes are assigned using either @attr_protected@ or @attr_accessible@. This adds security by preventing mass assignment of attributes when doing things like @user.update_attributes(params[:user])@. The issue is that it can be tedious to always manually assign protected attributes in an administrative area of your application. You may find yourself doing things like:
8
+
9
+ <pre>
10
+ user = User.find(params[:id])
11
+ user.update_attributes(params[:user])
12
+ user.admin = true
13
+ user.something_else = true
14
+ user.save
15
+ </pre>
16
+
17
+ h2. The Solution
18
+
19
+ SudoAttributes adds a few 'sudo' methods to your models, allowing you to override the protected attributes **when you know the input can be trusted**.
20
+
21
+ It's as easy as adding one method call to your models like so:
22
+
23
+ <pre>
24
+ class User < ActiveRecord::Base
25
+ has_sudo_attributes :protected => :admin
26
+ end
27
+ </pre>
28
+
29
+ h2. Class Methods
30
+
31
+ The class method *has_sudo_attributes* will be available to all rails models. When called with or without arguments, it adds numerous 'sudo' methods to the class. You may still use the default methods @attr_protected@ or @attr_accessible@ provided by rails, but you must still call @has_sudo_attributes@ in order to gain access to the sudo methods.
32
+
33
+ Here are four different ways it can be used:
34
+
35
+ @has_sudo_attributes :attribute1, :attribute2@ - Defines protected attributes
36
+
37
+ @has_sudo_attributes :protected => :attribute1@ - Identical behavior to previous
38
+
39
+ @has_sudo_attributes :accessible => [:attribute1, :attribute2]@ - Defines accessible attributes
40
+
41
+ @has_sudo_attributes@ - With no arguments, it will rely on calls to @attr_protected@ or @attr_accessible@
42
+
43
+ Any model that calls @has_sudo_attributes@ will also be able to create new instances that override protected attributes using the following methods:
44
+
45
+ @Model.sudo_create@ - Uses same syntax as @Model.create@ to instantiate and save an object with protected attributes
46
+
47
+ @Model.sudo_new@ - Uses same syntax as @Model.new@ to instantiate, but not save an object with protected attributes
48
+
49
+ h2. Instance Methods
50
+
51
+ The following instance method is available to any ActiveRecord model that calls @has_sudo_attributes@
52
+
53
+ * @sudo_update_attributes@ - Uses identical syntax to @update_attributes@, but overrides protected attributes.
54
+
55
+ h2. Examples
56
+
57
+ **Protect an admin boolean attribute**
58
+
59
+ <pre>
60
+ class User < ActiveRecord::Base
61
+ has_sudo_attributes :protected => :admin
62
+ end
63
+ </pre>
64
+
65
+ In your admin controller...
66
+
67
+ <pre>
68
+ params[:user] = {:name => "Pete", :admin => true} (Typically set from a form)
69
+
70
+ @user = User.sudo_create(params[:user])
71
+
72
+ Somewhere else in your admin controller...
73
+
74
+ params[:user] = {:admin => false, :name => "Pete"}
75
+
76
+ @user.sudo_update_attributes(params[:user])
77
+ </pre>
78
+
79
+ h2. Copyright
80
+
81
+ Copyright (c) 2010 Peter Brown. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "sudo_attributes"
8
+ gem.summary = %Q{Override ActiveRecord protected attributes with mass assignment}
9
+ gem.description = %Q{Adds 'sudo' methods to update protected ActiveRecord attributes with mass assignment}
10
+ gem.email = "github@lette.us"
11
+ gem.homepage = "http://github.com/beerlington/sudo_attributes"
12
+ gem.authors = ["Peter Brown"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_dependency "rails", ">= 2.3.0"
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "sudo_attributes #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require "sudo_attributes"
@@ -0,0 +1,74 @@
1
+ module SudoAttributes
2
+
3
+ module Base
4
+ # This is the method that will be called from each model you want to enable SudoAttributes in
5
+ def has_sudo_attributes(*attrs)
6
+ raise "Invalid argument passed to has_sudo_attributes" unless valid_attributes? attrs
7
+
8
+ set_protected_attributes(attrs) unless attrs.empty?
9
+
10
+ self.send :extend, SudoAttributes::ClassMethods
11
+ self.send :include, SudoAttributes::InstanceMethods
12
+ end
13
+
14
+ private
15
+
16
+ def valid_attributes?(attrs)
17
+ attrs.empty? || hash_syntax?(attrs) || all_symbols?(attrs)
18
+ end
19
+
20
+ # True if argument is in the form ":protected => :field1" or ":accessible => :field2"
21
+ def hash_syntax?(attrs)
22
+ return false unless attrs.size == 1
23
+
24
+ hash = attrs.first
25
+
26
+ hash.is_a?(Hash) && (hash.has_key?(:protected) || hash.has_key?(:accessible))
27
+ end
28
+
29
+ # True if argument is in the form ":field1, :field2, :field3"
30
+ def all_symbols?(attrs)
31
+ attrs.all? {|e| e.class == Symbol}
32
+ end
33
+
34
+ # Set the protected attributes depending on the key
35
+ def set_protected_attributes(attrs)
36
+ if all_symbols? attrs
37
+ self.attr_protected *attrs
38
+ else
39
+ key = attrs[0].has_key?(:protected) ? :protected : :accessible
40
+
41
+ # Call either attr_protected or attr_accessible
42
+ self.send("attr_#{key}", *attrs[0][key])
43
+ end
44
+ end
45
+
46
+ end
47
+
48
+ # Added to ActiveRecord model only if has_sudo_attributes is called
49
+ module ClassMethods
50
+ def sudo_create(attributes=nil)
51
+ instance = sudo_new(attributes)
52
+ instance.save
53
+ instance
54
+ end
55
+
56
+ def sudo_new(attributes=nil)
57
+ new(attributes, false)
58
+ end
59
+ end
60
+
61
+ module InstanceMethods
62
+ def initialize(attributes=nil, attributes_protected=true)
63
+ super(nil)
64
+ send(:attributes=, attributes, attributes_protected)
65
+ end
66
+
67
+ def sudo_update_attributes(new_attributes)
68
+ self.send(:attributes=, new_attributes, false)
69
+ save
70
+ end
71
+ end
72
+ end
73
+
74
+ ActiveRecord::Base.send :extend, SudoAttributes::Base
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,7 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'rubygems'
5
+ require 'active_record'
6
+ require 'sudo_attributes'
7
+ require 'spec'
@@ -0,0 +1,134 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
4
+
5
+ ActiveRecord::Schema.define(:version => 1) do
6
+ create_table :cats, :force => true do |t|
7
+ t.string :name
8
+ t.string :color
9
+ t.integer :age
10
+ end
11
+ end
12
+
13
+ module SudoAttributesTest
14
+ ARGUMENTS = [
15
+ { :protected => :name},
16
+ { :accessible => [:color, :age] },
17
+ :name,
18
+ nil # No arguments passed in
19
+ ]
20
+
21
+ def self.build_cat_class(arguments)
22
+
23
+ # Remove the Cat class if it's already been defined in previous run
24
+ Object.class_eval { remove_const "Cat" if const_defined? "Cat" }
25
+
26
+ # Create a new Cat class and evaluate 'has_sudo_attributes :arguments
27
+ klass = Class.new(ActiveRecord::Base)
28
+ Object.const_set("Cat", klass)
29
+
30
+ if arguments.nil?
31
+ Cat.class_eval do
32
+ attr_protected :name
33
+ has_sudo_attributes
34
+ end
35
+ else
36
+ Cat.class_eval { has_sudo_attributes arguments }
37
+ end
38
+ end
39
+ end
40
+
41
+ describe "Cat" do
42
+
43
+ SudoAttributesTest::ARGUMENTS.each do |arguments|
44
+
45
+ SudoAttributesTest::build_cat_class(arguments)
46
+
47
+ context "calling has_sudo_attributes #{arguments.inspect}" do
48
+
49
+ before(:all) do
50
+ @attributes = {:name => "Smiles", :color => "gray", :age => 6}
51
+ end
52
+
53
+ context "that is built using" do
54
+
55
+ context "default rails initializer" do
56
+ before(:each) { @cat = Cat.new @attributes}
57
+
58
+ it "should not have a name" do
59
+ @cat.name.should be_nil
60
+ end
61
+
62
+ it "should not set the name with update_attributes" do
63
+ @cat.update_attributes(:name => "Smiles")
64
+ @cat.name.should be_nil
65
+ end
66
+
67
+ it "should set the name with sudo_update_attributes" do
68
+ @cat.sudo_update_attributes(:name => "Smiles")
69
+ @cat.name.should == "Smiles"
70
+ end
71
+
72
+ it "should have a color" do
73
+ @cat.color.should == @attributes[:color]
74
+ end
75
+
76
+ it "should have an age" do
77
+ @cat.age.should == @attributes[:age]
78
+ end
79
+
80
+ end
81
+
82
+ context "SudoAttributes sudo_new initializer" do
83
+ before(:each) { @cat = Cat.sudo_new @attributes}
84
+
85
+ it "should have a name" do
86
+ @cat.name.should == @attributes[:name]
87
+ end
88
+
89
+ it "should set the name with sudo_update_attributes" do
90
+ @cat.sudo_update_attributes(:name => "Portia")
91
+ @cat.name.should == "Portia"
92
+ end
93
+
94
+ it "should have a color" do
95
+ @cat.color.should == @attributes[:color]
96
+ end
97
+
98
+ it "should not have an id" do
99
+ @cat.id.should be_nil
100
+ end
101
+
102
+ it "should have an age" do
103
+ @cat.age.should == @attributes[:age]
104
+ end
105
+ end
106
+
107
+ context "SudoAttributes sudo_create initializer" do
108
+ before(:each) { @cat = Cat.sudo_create @attributes}
109
+
110
+ it "should have a name" do
111
+ @cat.name.should == @attributes[:name]
112
+ end
113
+
114
+ it "should set the name with sudo_update_attributes" do
115
+ @cat.sudo_update_attributes(:name => "Portia")
116
+ @cat.name.should == "Portia"
117
+ end
118
+
119
+ it "should have a color" do
120
+ @cat.color.should == @attributes[:color]
121
+ end
122
+
123
+ it "should have an id" do
124
+ @cat.id.should_not be_nil
125
+ end
126
+
127
+ it "should have an age" do
128
+ @cat.age.should == @attributes[:age]
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,63 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{sudo_attributes}
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Peter Brown"]
12
+ s.date = %q{2010-09-29}
13
+ s.description = %q{Adds 'sudo' methods to update protected ActiveRecord attributes with mass assignment}
14
+ s.email = %q{github@lette.us}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.textile"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.textile",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "init.rb",
27
+ "lib/sudo_attributes.rb",
28
+ "spec/spec.opts",
29
+ "spec/spec_helper.rb",
30
+ "spec/sudo_attributes_spec.rb",
31
+ "sudo_attributes.gemspec",
32
+ "test/helper.rb",
33
+ "test/test_sudo_attributes.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/beerlington/sudo_attributes}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.7}
39
+ s.summary = %q{Override ActiveRecord protected attributes with mass assignment}
40
+ s.test_files = [
41
+ "spec/spec_helper.rb",
42
+ "spec/sudo_attributes_spec.rb",
43
+ "test/helper.rb",
44
+ "test/test_sudo_attributes.rb"
45
+ ]
46
+
47
+ if s.respond_to? :specification_version then
48
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
52
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
53
+ s.add_runtime_dependency(%q<rails>, [">= 2.3.0"])
54
+ else
55
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
56
+ s.add_dependency(%q<rails>, [">= 2.3.0"])
57
+ end
58
+ else
59
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
60
+ s.add_dependency(%q<rails>, [">= 2.3.0"])
61
+ end
62
+ end
63
+
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'sudo_attributes'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestSudoAttributes < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sudo_attributes
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
+ platform: ruby
12
+ authors:
13
+ - Peter Brown
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-29 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rails
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 2
48
+ - 3
49
+ - 0
50
+ version: 2.3.0
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ description: Adds 'sudo' methods to update protected ActiveRecord attributes with mass assignment
54
+ email: github@lette.us
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ extra_rdoc_files:
60
+ - LICENSE
61
+ - README.textile
62
+ files:
63
+ - .document
64
+ - .gitignore
65
+ - LICENSE
66
+ - README.textile
67
+ - Rakefile
68
+ - VERSION
69
+ - init.rb
70
+ - lib/sudo_attributes.rb
71
+ - spec/spec.opts
72
+ - spec/spec_helper.rb
73
+ - spec/sudo_attributes_spec.rb
74
+ - sudo_attributes.gemspec
75
+ - test/helper.rb
76
+ - test/test_sudo_attributes.rb
77
+ has_rdoc: true
78
+ homepage: http://github.com/beerlington/sudo_attributes
79
+ licenses: []
80
+
81
+ post_install_message:
82
+ rdoc_options:
83
+ - --charset=UTF-8
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ hash: 3
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ hash: 3
101
+ segments:
102
+ - 0
103
+ version: "0"
104
+ requirements: []
105
+
106
+ rubyforge_project:
107
+ rubygems_version: 1.3.7
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: Override ActiveRecord protected attributes with mass assignment
111
+ test_files:
112
+ - spec/spec_helper.rb
113
+ - spec/sudo_attributes_spec.rb
114
+ - test/helper.rb
115
+ - test/test_sudo_attributes.rb