You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

safe_attributes.rb 3.1KB

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