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