From 7d501eaf81f32c6cf6e361192968aacbe18ff36c Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Tue, 13 Dec 2011 20:40:03 +0000 Subject: Code cleanup. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8200 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- lib/redmine/safe_attributes.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/redmine/safe_attributes.rb b/lib/redmine/safe_attributes.rb index 3c17f952d..3724b437d 100644 --- a/lib/redmine/safe_attributes.rb +++ b/lib/redmine/safe_attributes.rb @@ -44,14 +44,22 @@ module Redmine # Example: # book.safe_attributes # => ['title', 'pages'] # book.safe_attributes(book.author) # => ['title', 'pages', 'isbn'] - def safe_attribute_names(user=User.current) + def safe_attribute_names(user=nil) + return @safe_attribute_names if @safe_attribute_names && user.nil? names = [] self.class.safe_attributes.collect do |attrs, options| - if options[:if].nil? || options[:if].call(self, user) + if options[:if].nil? || options[:if].call(self, user || User.current) names += attrs.collect(&:to_s) end end - names.uniq + names.uniq! + @safe_attribute_names = names if user.nil? + names + end + + # Returns true if attr can be set by user or the current user + def safe_attribute?(attr, user=nil) + safe_attribute_names(user).include?(attr.to_s) end # Returns a hash with unsafe attributes removed -- cgit v1.2.3