2025-06-17 20:40:11 +02:00
|
|
|
# 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.
|
|
|
|
|
#++
|
|
|
|
|
class Users::InviteController < ApplicationController
|
|
|
|
|
include OpTurbo::ComponentStream
|
2025-10-09 09:57:50 +02:00
|
|
|
include MemberHelper
|
2025-06-17 20:40:11 +02:00
|
|
|
|
|
|
|
|
authorize_with_permission :manage_members, global: true
|
2025-10-15 08:43:29 +02:00
|
|
|
before_action :set_project, only: :start_dialog
|
2025-06-17 20:40:11 +02:00
|
|
|
|
|
|
|
|
def start_dialog
|
|
|
|
|
respond_with_dialog(
|
2025-10-15 08:43:29 +02:00
|
|
|
Users::Invitation::DialogComponent.new(form_model, project: @project)
|
2025-06-17 20:40:11 +02:00
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def step
|
2025-10-09 10:21:36 +02:00
|
|
|
if form_model.valid?(validation_context) || params[:step] == "initial"
|
2025-06-17 20:40:11 +02:00
|
|
|
respond_with_next_step
|
|
|
|
|
else
|
|
|
|
|
handle_errors_in_step
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def handle_errors_in_step
|
|
|
|
|
case params[:step]
|
|
|
|
|
when "project"
|
2025-10-09 09:40:25 +02:00
|
|
|
replace_via_turbo_stream(component: Users::Invitation::ProjectStep::FormComponent.new(form_model))
|
2025-06-17 20:40:11 +02:00
|
|
|
respond_with_turbo_streams
|
|
|
|
|
when "principal"
|
2025-10-09 09:40:25 +02:00
|
|
|
replace_via_turbo_stream(component: Users::Invitation::PrincipalStep::FormComponent.new(form_model))
|
2025-06-17 20:40:11 +02:00
|
|
|
respond_with_turbo_streams
|
|
|
|
|
else
|
|
|
|
|
render_400 message: "Invalid step"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2025-10-09 10:21:36 +02:00
|
|
|
def respond_with_next_step # rubocop:disable Metrics/AbcSize
|
2025-06-17 20:40:11 +02:00
|
|
|
case params[:step]
|
2025-10-09 10:21:36 +02:00
|
|
|
when "initial"
|
|
|
|
|
update_dialog_title_via_turbo_stream(Users::Invitation::DialogComponent::DIALOG_ID,
|
|
|
|
|
new_title: I18n.t("users.invite_user_modal.title.invite"))
|
|
|
|
|
replace_via_turbo_stream(component: Users::Invitation::ProjectStep::FormComponent.new(form_model))
|
|
|
|
|
replace_via_turbo_stream(component: Users::Invitation::ProjectStep::FooterComponent.new(form_model))
|
|
|
|
|
respond_with_turbo_streams
|
2025-06-17 20:40:11 +02:00
|
|
|
when "project"
|
2025-10-09 10:21:36 +02:00
|
|
|
update_dialog_title_via_turbo_stream(Users::Invitation::DialogComponent::DIALOG_ID, new_title: dialog_title)
|
2025-10-09 09:40:25 +02:00
|
|
|
replace_via_turbo_stream(component: Users::Invitation::PrincipalStep::FormComponent.new(form_model))
|
|
|
|
|
replace_via_turbo_stream(component: Users::Invitation::PrincipalStep::FooterComponent.new(form_model))
|
2025-06-17 20:40:11 +02:00
|
|
|
respond_with_turbo_streams
|
|
|
|
|
when "principal"
|
|
|
|
|
create_invitation
|
|
|
|
|
else
|
|
|
|
|
render_400 message: "Invalid step"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2025-10-10 20:04:39 +02:00
|
|
|
def create_invitation # rubocop:disable Metrics/AbcSize
|
2025-10-09 09:57:50 +02:00
|
|
|
call = create_member_call
|
2025-06-17 20:40:11 +02:00
|
|
|
|
|
|
|
|
if call.success?
|
2025-10-09 10:21:36 +02:00
|
|
|
render_success_flash_message_via_turbo_stream(
|
|
|
|
|
message: I18n.t("users.invite_user_modal.success_message.#{form_model.principal_type.underscore}",
|
|
|
|
|
project: form_model.project.name)
|
|
|
|
|
)
|
2025-06-17 20:40:11 +02:00
|
|
|
close_dialog_via_turbo_stream("##{Users::Invitation::DialogComponent::DIALOG_ID}",
|
2025-10-10 20:04:39 +02:00
|
|
|
additional: { user_id: call.result.user_id })
|
2025-06-17 20:40:11 +02:00
|
|
|
else
|
2025-10-09 09:40:25 +02:00
|
|
|
replace_via_turbo_stream(component: Users::Invitation::PrincipalStep::FormComponent.new(form_model))
|
2025-06-17 20:40:11 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
respond_with_turbo_streams
|
|
|
|
|
end
|
|
|
|
|
|
2025-10-09 09:57:50 +02:00
|
|
|
def create_member_call
|
2025-10-15 16:06:44 +02:00
|
|
|
# The form validation worked, now is the time to invite the user
|
|
|
|
|
invite_user!
|
|
|
|
|
|
2025-10-09 10:21:36 +02:00
|
|
|
Members::CreateService
|
|
|
|
|
.new(user: current_user)
|
|
|
|
|
.call(
|
|
|
|
|
project_id: form_model.project_id,
|
|
|
|
|
user_id: form_model.id_or_email,
|
|
|
|
|
role_ids: [form_model.role_id],
|
|
|
|
|
notification_message: form_model.message
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
2025-10-15 16:06:44 +02:00
|
|
|
def invite_user!
|
2025-10-09 09:57:50 +02:00
|
|
|
# Invite new user by email if needed, or use existing user ID
|
2025-10-09 10:21:36 +02:00
|
|
|
form_model.id_or_email = invite_new_user(form_model.id_or_email, send_notification: true)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def validation_context
|
|
|
|
|
if params[:step] == "project"
|
|
|
|
|
:project_step
|
2025-10-09 09:57:50 +02:00
|
|
|
else
|
2025-10-09 10:21:36 +02:00
|
|
|
%i[project_step principal_step]
|
2025-10-09 09:57:50 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2025-10-09 09:40:25 +02:00
|
|
|
def form_model
|
2025-10-15 08:43:29 +02:00
|
|
|
@form_model ||= Users::Invitation::FormModel.new(form_model_params).tap do |model|
|
|
|
|
|
model.project = @project if @project && current_user.allowed_in_project?(:manage_members, @project)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def set_project
|
|
|
|
|
@project = Project.find(params[:project_id]) if params[:project_id].present?
|
2025-06-17 20:40:11 +02:00
|
|
|
end
|
|
|
|
|
|
2025-10-09 10:21:36 +02:00
|
|
|
def dialog_title
|
|
|
|
|
I18n.t("users.invite_user_modal.type.#{form_model.principal_type.underscore}.title",
|
|
|
|
|
project_name: form_model.project_name)
|
|
|
|
|
end
|
|
|
|
|
|
2025-06-17 20:40:11 +02:00
|
|
|
def form_model_params
|
|
|
|
|
return {} unless params[:user_invitation]
|
|
|
|
|
|
|
|
|
|
permitted_params.user_invitation
|
|
|
|
|
end
|
|
|
|
|
end
|