Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

3.4 KB
Raw Blame
# frozen_string_literal: true

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++

module Projects
  class CreateContract < BaseContract
    # TODO: differentiate on allowed types based on permissions.
    # Permissions will need to be added: project, program, portfolio.
    attribute :workspace_type

    include AdminWritableTimestamps

    # Projects update their updated_at timestamp due to awesome_nested_set
    # so allowing writing here would be useless.
    allow_writable_timestamps :created_at

    def writable_attributes
      if allowed_to_write_custom_fields?
        super
      else
        without_custom_fields(super)
      end
    end

    protected

    def collect_available_custom_field_attributes
      model.all_visible_custom_fields.map(&:attribute_name)
    end

    private

    def allowed_to_write_custom_fields?
      # Writable attributes are already restricted based on their visibility in the
      # ProjectCustomField.visible scope. Here it is enough to check whether the user
      # has permission to copy_projects or edit_project_attributes in any project.
      user.admin? ||
      user.allowed_globally?(:add_project) ||
      (model.parent && user.allowed_in_project?(:add_subprojects, model.parent)) ||
      user.allowed_in_any_project?(:copy_projects) ||
      user.allowed_in_any_project?(:edit_project_attributes)
    end

    def without_custom_fields(changes) = changes.grep_v(/^custom_field_/)

    def validate_user_allowed_to_manage
      if model.project?
        validate_user_allowed_to_manage_projects
      elsif model.portfolio?
        validate_user_allowed_to_manage_portfolios
      elsif model.program?
        validate_user_allowed_to_manage_programs
      end
    end

    def validate_user_allowed_to_manage_portfolios
      unless user.allowed_globally?(:add_portfolios)
        errors.add :base, :error_unauthorized
      end
    end

    def validate_user_allowed_to_manage_programs
      unless user.allowed_globally?(:add_programs)
        errors.add :base, :error_unauthorized
      end
    end

    def validate_user_allowed_to_manage_projects
      unless user.allowed_globally?(:add_project) ||
             (model.parent && user.allowed_in_project?(:add_subprojects, model.parent))
        errors.add :base, :error_unauthorized
      end
    end
  end
end