annotate lib/redmine/safe_attributes.rb @ 1180:14058c37047a feature_14

Close obsolete branch feature_14
author Chris Cannam
date Thu, 24 Nov 2011 12:48:19 +0000
parents af80e5618e9b
children cbb26bc654de
rev   line source
Chris@117 1 # Redmine - project management software
Chris@117 2 # Copyright (C) 2006-2010 Jean-Philippe Lang
Chris@117 3 #
Chris@117 4 # This program is free software; you can redistribute it and/or
Chris@117 5 # modify it under the terms of the GNU General Public License
Chris@117 6 # as published by the Free Software Foundation; either version 2
Chris@117 7 # of the License, or (at your option) any later version.
Chris@117 8 #
Chris@117 9 # This program is distributed in the hope that it will be useful,
Chris@117 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@117 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@117 12 # GNU General Public License for more details.
Chris@117 13 #
Chris@117 14 # You should have received a copy of the GNU General Public License
Chris@117 15 # along with this program; if not, write to the Free Software
Chris@117 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@117 17
Chris@117 18 module Redmine
Chris@117 19 module SafeAttributes
Chris@117 20 def self.included(base)
Chris@117 21 base.extend(ClassMethods)
Chris@117 22 end
Chris@117 23
Chris@117 24 module ClassMethods
Chris@117 25 # Declares safe attributes
Chris@117 26 # An optional Proc can be given for conditional inclusion
Chris@117 27 #
Chris@117 28 # Example:
Chris@117 29 # safe_attributes 'title', 'pages'
Chris@117 30 # safe_attributes 'isbn', :if => {|book, user| book.author == user}
Chris@117 31 def safe_attributes(*args)
Chris@117 32 @safe_attributes ||= []
Chris@117 33 if args.empty?
Chris@117 34 @safe_attributes
Chris@117 35 else
Chris@117 36 options = args.last.is_a?(Hash) ? args.pop : {}
Chris@117 37 @safe_attributes << [args, options]
Chris@117 38 end
Chris@117 39 end
Chris@117 40 end
Chris@117 41
Chris@117 42 # Returns an array that can be safely set by user or current user
Chris@117 43 #
Chris@117 44 # Example:
Chris@117 45 # book.safe_attributes # => ['title', 'pages']
Chris@117 46 # book.safe_attributes(book.author) # => ['title', 'pages', 'isbn']
Chris@117 47 def safe_attribute_names(user=User.current)
Chris@117 48 names = []
Chris@117 49 self.class.safe_attributes.collect do |attrs, options|
Chris@117 50 if options[:if].nil? || options[:if].call(self, user)
Chris@117 51 names += attrs.collect(&:to_s)
Chris@117 52 end
Chris@117 53 end
Chris@117 54 names.uniq
Chris@117 55 end
Chris@117 56
Chris@117 57 # Returns a hash with unsafe attributes removed
Chris@117 58 # from the given attrs hash
Chris@117 59 #
Chris@117 60 # Example:
Chris@117 61 # book.delete_unsafe_attributes({'title' => 'My book', 'foo' => 'bar'})
Chris@117 62 # # => {'title' => 'My book'}
Chris@117 63 def delete_unsafe_attributes(attrs, user=User.current)
Chris@117 64 safe = safe_attribute_names(user)
Chris@117 65 attrs.dup.delete_if {|k,v| !safe.include?(k)}
Chris@117 66 end
Chris@117 67
Chris@117 68 # Sets attributes from attrs that are safe
Chris@117 69 # attrs is a Hash with string keys
Chris@117 70 def safe_attributes=(attrs, user=User.current)
Chris@117 71 return unless attrs.is_a?(Hash)
Chris@117 72 self.attributes = delete_unsafe_attributes(attrs, user)
Chris@117 73 end
Chris@117 74 end
Chris@117 75 end