|
| 1 | +#-- encoding: UTF-8 |
| 2 | + |
| 3 | +#-- copyright |
| 4 | +# OpenProject is an open source project management software. |
| 5 | +# Copyright (C) 2012-2021 the OpenProject GmbH |
| 6 | +# |
| 7 | +# This program is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU General Public License version 3. |
| 9 | +# |
| 10 | +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
| 11 | +# Copyright (C) 2006-2013 Jean-Philippe Lang |
| 12 | +# Copyright (C) 2010-2013 the ChiliProject Team |
| 13 | +# |
| 14 | +# This program is free software; you can redistribute it and/or |
| 15 | +# modify it under the terms of the GNU General Public License |
| 16 | +# as published by the Free Software Foundation; either version 2 |
| 17 | +# of the License, or (at your option) any later version. |
| 18 | +# |
| 19 | +# This program is distributed in the hope that it will be useful, |
| 20 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | +# GNU General Public License for more details. |
| 23 | +# |
| 24 | +# You should have received a copy of the GNU General Public License |
| 25 | +# along with this program; if not, write to the Free Software |
| 26 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 27 | +# |
| 28 | +# See docs/COPYRIGHT.rdoc for more details. |
| 29 | +#++ |
| 30 | + |
| 31 | +module Attachments |
| 32 | + class CreateContract < ::ModelContract |
| 33 | + attribute :file |
| 34 | + attribute :filename |
| 35 | + attribute :filesize |
| 36 | + attribute :digest |
| 37 | + attribute :description |
| 38 | + attribute :content_type |
| 39 | + attribute :container |
| 40 | + attribute :container_type |
| 41 | + attribute :author |
| 42 | + |
| 43 | + validates :filename, presence: true |
| 44 | + |
| 45 | + validate :validate_attachments_addable |
| 46 | + validate :validate_container_addable |
| 47 | + validate :validate_author |
| 48 | + validate :validate_content_type |
| 49 | + |
| 50 | + private |
| 51 | + |
| 52 | + def validate_attachments_addable |
| 53 | + return if model.container |
| 54 | + |
| 55 | + if Redmine::Acts::Attachable.attachables.none?(&:attachments_addable?) |
| 56 | + errors.add(:base, :error_unauthorized) |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + def validate_author |
| 61 | + unless model.author == user |
| 62 | + errors.add(:author, :invalid) |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + def validate_container_addable |
| 67 | + return unless model.container |
| 68 | + |
| 69 | + errors.add(:base, :error_unauthorized) unless model.container.attachments_addable?(user) |
| 70 | + end |
| 71 | + |
| 72 | + ## |
| 73 | + # Validates the content type, if a whitelist is set |
| 74 | + def validate_content_type |
| 75 | + # If the whitelist is empty, assume all files are allowed |
| 76 | + # as before |
| 77 | + unless matches_whitelist?(attachment_whitelist) |
| 78 | + Rails.logger.info { "Uploaded file #{model.filename} with type #{model.content_type} does not match whitelist" } |
| 79 | + errors.add :content_type, :not_whitelisted, value: model.content_type |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + ## |
| 84 | + # Get the user-defined whitelist or a custom whitelist |
| 85 | + # defined for this invocation |
| 86 | + def attachment_whitelist |
| 87 | + Array(options.fetch(:whitelist, Setting.attachment_whitelist)) |
| 88 | + end |
| 89 | + |
| 90 | + ## |
| 91 | + # Returns whether the attachment matches the whitelist |
| 92 | + def matches_whitelist?(whitelist) |
| 93 | + return true if whitelist.empty? |
| 94 | + |
| 95 | + whitelist.include?(model.content_type) || whitelist.include?("*#{model.extension}") |
| 96 | + end |
| 97 | + end |
| 98 | +end |
0 commit comments