2020-10-26 23:17:39 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
class ProjectsController < ApplicationController
|
2016-07-21 19:11:15 +08:00
|
|
|
include RenamingUtil
|
2017-01-25 00:06:51 +08:00
|
|
|
include TeamsHelper
|
2017-01-04 22:04:12 +08:00
|
|
|
include InputSanitizeHelper
|
2020-12-08 23:07:26 +08:00
|
|
|
include ProjectsHelper
|
2021-02-11 18:50:59 +08:00
|
|
|
include CardsViewHelper
|
2021-02-03 21:23:38 +08:00
|
|
|
include ExperimentsHelper
|
2023-04-19 16:53:39 +08:00
|
|
|
include Breadcrumbs
|
2023-12-01 07:01:08 +08:00
|
|
|
include UserRolesHelper
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2021-01-19 18:28:59 +08:00
|
|
|
attr_reader :current_folder
|
2023-05-24 16:47:27 +08:00
|
|
|
|
2021-01-19 18:28:59 +08:00
|
|
|
helper_method :current_folder
|
|
|
|
|
2020-09-28 22:55:20 +08:00
|
|
|
before_action :switch_team_with_param, only: :index
|
2024-01-16 20:53:41 +08:00
|
|
|
before_action :load_vars, only: %i(update notifications create_tag)
|
|
|
|
before_action :load_current_folder, only: :index
|
|
|
|
before_action :check_view_permissions, except: %i(index create update archive_group restore_group
|
|
|
|
inventory_assigning_project_filter
|
|
|
|
actions_toolbar user_roles users_filter)
|
|
|
|
before_action :check_create_permissions, only: :create
|
|
|
|
before_action :check_manage_permissions, only: :update
|
2021-07-22 03:33:13 +08:00
|
|
|
before_action :set_folder_inline_name_editing, only: %i(index cards)
|
2023-12-07 03:53:11 +08:00
|
|
|
before_action :set_breadcrumbs_items, only: :index
|
|
|
|
before_action :set_navigator, only: :index
|
2018-03-22 18:41:33 +08:00
|
|
|
layout 'fluid'
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2023-11-10 20:34:36 +08:00
|
|
|
def index
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
projects = Lists::ProjectsService.new(current_team, current_user, current_folder, params).call
|
2023-12-07 03:53:11 +08:00
|
|
|
render json: projects, each_serializer: Lists::ProjectAndFolderSerializer, user: current_user,
|
|
|
|
meta: pagination_dict(projects)
|
|
|
|
end
|
|
|
|
format.html do
|
|
|
|
render 'projects/index'
|
2023-11-10 20:34:36 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-10-12 15:30:55 +08:00
|
|
|
|
2018-11-29 21:20:20 +08:00
|
|
|
|
2023-06-06 19:53:12 +08:00
|
|
|
def inventory_assigning_project_filter
|
2023-06-09 20:20:41 +08:00
|
|
|
viewable_experiments = Experiment.viewable_by_user(current_user, current_team)
|
2023-06-06 19:53:12 +08:00
|
|
|
assignable_my_modules = MyModule.repository_row_assignable_by_user(current_user)
|
2023-05-24 16:47:27 +08:00
|
|
|
|
2023-06-09 20:20:41 +08:00
|
|
|
projects = Project.viewable_by_user(current_user, current_team)
|
|
|
|
.active
|
2023-05-24 16:47:27 +08:00
|
|
|
.joins(experiments: :my_modules)
|
2023-06-09 20:20:41 +08:00
|
|
|
.where(experiments: { id: viewable_experiments })
|
2023-06-06 19:53:12 +08:00
|
|
|
.where(my_modules: { id: assignable_my_modules })
|
2023-05-24 16:47:27 +08:00
|
|
|
.distinct
|
2023-05-10 22:12:20 +08:00
|
|
|
.pluck(:id, :name)
|
|
|
|
|
|
|
|
return render plain: [].to_json if projects.blank?
|
|
|
|
|
|
|
|
render json: projects
|
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
def create
|
2020-12-29 05:49:52 +08:00
|
|
|
@project = current_team.projects.new(project_params)
|
2016-10-12 18:56:44 +08:00
|
|
|
@project.created_by = current_user
|
2016-02-12 23:52:43 +08:00
|
|
|
@project.last_modified_by = current_user
|
2020-12-29 05:49:52 +08:00
|
|
|
if @project.save
|
2019-03-08 00:26:42 +08:00
|
|
|
log_activity(:create_project)
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2019-05-08 23:38:24 +08:00
|
|
|
message = t('projects.create.success_flash', name: escape_input(@project.name))
|
2023-07-18 19:36:41 +08:00
|
|
|
render json: { message: message }, status: :ok
|
2016-02-12 23:52:43 +08:00
|
|
|
else
|
2023-07-18 19:36:41 +08:00
|
|
|
render json: @project.errors, status: :unprocessable_entity
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2023-06-13 22:55:40 +08:00
|
|
|
@project.assign_attributes(project_update_params)
|
2016-02-12 23:52:43 +08:00
|
|
|
return_error = false
|
2019-05-08 23:38:24 +08:00
|
|
|
flash_error = t('projects.update.error_flash', name: escape_input(@project.name))
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2023-06-13 22:55:40 +08:00
|
|
|
return render_403 unless can_manage_project?(@project) || @project.archived_changed?
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
# Check archive permissions if archiving/restoring
|
2023-06-13 22:55:40 +08:00
|
|
|
if @project.archived_changed? &&
|
|
|
|
((@project.archived == 'true' && !can_archive_project?(@project)) ||
|
|
|
|
(@project.archived == 'false' && !can_restore_project?(@project)))
|
2016-02-12 23:52:43 +08:00
|
|
|
return_error = true
|
2023-06-13 22:55:40 +08:00
|
|
|
is_archive = @project.archived? ? 'archive' : 'restore'
|
2018-09-27 16:43:44 +08:00
|
|
|
flash_error =
|
2019-05-08 23:38:24 +08:00
|
|
|
t("projects.#{is_archive}.error_flash", name: escape_input(@project.name))
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2023-06-13 22:55:40 +08:00
|
|
|
message_renamed = @project.name_changed?
|
2023-06-16 19:08:02 +08:00
|
|
|
message_visibility = if !@project.visibility_changed?
|
2023-06-13 22:55:40 +08:00
|
|
|
nil
|
|
|
|
elsif @project.visible?
|
|
|
|
t('projects.activity.visibility_visible')
|
|
|
|
else
|
|
|
|
t('projects.activity.visibility_hidden')
|
|
|
|
end
|
|
|
|
|
|
|
|
message_archived = if !@project.archived_changed?
|
|
|
|
nil
|
|
|
|
elsif @project.archived?
|
|
|
|
'archive'
|
|
|
|
else
|
|
|
|
'restore'
|
|
|
|
end
|
|
|
|
|
2023-06-16 19:08:02 +08:00
|
|
|
default_public_user_role_name = nil
|
|
|
|
if !@project.visibility_changed? && @project.default_public_user_role_id_changed?
|
|
|
|
default_public_user_role_name = UserRole.find(project_params[:default_public_user_role_id]).name
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
@project.last_modified_by = current_user
|
2023-06-13 22:55:40 +08:00
|
|
|
if !return_error && @project.save
|
2019-03-28 20:43:50 +08:00
|
|
|
|
2023-06-13 22:55:40 +08:00
|
|
|
# Add activities if needed
|
2023-06-16 19:08:02 +08:00
|
|
|
if message_visibility.present? && @project.visible?
|
|
|
|
log_activity(:project_grant_access_to_all_team_members,
|
|
|
|
@project,
|
|
|
|
{ visibility: message_visibility,
|
|
|
|
role: @project.default_public_user_role.name,
|
|
|
|
team: @project.team.id })
|
|
|
|
end
|
|
|
|
if message_visibility.present? && !@project.visible?
|
|
|
|
log_activity(:project_remove_access_from_all_team_members,
|
|
|
|
@project,
|
|
|
|
{ visibility: message_visibility,
|
|
|
|
role: @project.default_public_user_role.name,
|
|
|
|
team: @project.team.id })
|
|
|
|
end
|
|
|
|
|
2019-03-28 20:43:50 +08:00
|
|
|
log_activity(:rename_project) if message_renamed.present?
|
2023-06-13 22:55:40 +08:00
|
|
|
log_activity(:archive_project) if message_archived == 'archive'
|
|
|
|
log_activity(:restore_project) if message_archived == 'restore'
|
|
|
|
|
2023-06-16 19:08:02 +08:00
|
|
|
if default_public_user_role_name.present?
|
2023-06-13 22:55:40 +08:00
|
|
|
log_activity(:project_access_changed_all_team_members,
|
|
|
|
@project,
|
2023-06-16 19:08:02 +08:00
|
|
|
{ team: @project.team.id, role: default_public_user_role_name })
|
2023-06-13 22:55:40 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2019-05-08 23:38:24 +08:00
|
|
|
flash_success = t('projects.update.success_flash', name: escape_input(@project.name))
|
2023-06-13 22:55:40 +08:00
|
|
|
if message_archived == 'archive'
|
2019-05-08 23:38:24 +08:00
|
|
|
flash_success = t('projects.archive.success_flash', name: escape_input(@project.name))
|
2023-06-13 22:55:40 +08:00
|
|
|
elsif message_archived == 'restore'
|
2019-05-08 23:38:24 +08:00
|
|
|
flash_success = t('projects.restore.success_flash', name: escape_input(@project.name))
|
2018-09-27 16:43:44 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
respond_to do |format|
|
2018-09-27 16:43:44 +08:00
|
|
|
format.html do
|
2023-06-13 22:55:40 +08:00
|
|
|
@project.restore(current_user) if message_archived == 'restore'
|
|
|
|
@project.archive(current_user) if message_archived == 'archive'
|
|
|
|
|
2018-09-27 16:43:44 +08:00
|
|
|
redirect_to projects_path
|
2016-02-12 23:52:43 +08:00
|
|
|
flash[:success] = flash_success
|
2018-09-27 16:43:44 +08:00
|
|
|
end
|
|
|
|
format.json do
|
2016-02-12 23:52:43 +08:00
|
|
|
render json: {
|
|
|
|
status: :ok,
|
2018-09-27 16:43:44 +08:00
|
|
|
message: flash_success
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
2018-09-27 16:43:44 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
else
|
|
|
|
return_error = true
|
|
|
|
end
|
|
|
|
|
2018-09-27 16:43:44 +08:00
|
|
|
if return_error
|
2016-02-12 23:52:43 +08:00
|
|
|
respond_to do |format|
|
2018-09-27 16:43:44 +08:00
|
|
|
format.html do
|
2016-02-12 23:52:43 +08:00
|
|
|
flash[:error] = flash_error
|
|
|
|
# Redirect URL for archive view is different as for other views.
|
|
|
|
if URI(request.referer).path == projects_archive_path
|
|
|
|
redirect_to projects_archive_path
|
|
|
|
else
|
|
|
|
redirect_to projects_path
|
|
|
|
end
|
2018-09-27 16:43:44 +08:00
|
|
|
end
|
|
|
|
format.json do
|
|
|
|
render json: { message: flash_error, errors: @project.errors },
|
|
|
|
status: :unprocessable_entity
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-29 05:49:52 +08:00
|
|
|
def archive_group
|
2023-04-26 17:20:10 +08:00
|
|
|
projects = current_team.projects.active.where(id: params[:project_ids])
|
2020-12-29 05:49:52 +08:00
|
|
|
counter = 0
|
|
|
|
projects.each do |project|
|
|
|
|
next unless can_archive_project?(project)
|
|
|
|
|
|
|
|
project.transaction do
|
|
|
|
project.archive!(current_user)
|
|
|
|
log_activity(:archive_project, project)
|
|
|
|
counter += 1
|
|
|
|
rescue StandardError => e
|
|
|
|
Rails.logger.error e.message
|
|
|
|
raise ActiveRecord::Rollback
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if counter.positive?
|
|
|
|
render json: { message: t('projects.archive_group.success_flash', number: counter) }
|
|
|
|
else
|
|
|
|
render json: { message: t('projects.archive_group.error_flash') }, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-19 18:55:38 +08:00
|
|
|
def create_tag
|
|
|
|
render_403 unless can_manage_project_tags?(@project)
|
|
|
|
|
|
|
|
@tag = @project.tags.create(tag_params.merge({
|
2022-12-20 14:37:40 +08:00
|
|
|
created_by: current_user,
|
|
|
|
last_modified_by: current_user,
|
|
|
|
color: Constants::TAG_COLORS.sample
|
|
|
|
}))
|
2022-12-19 18:55:38 +08:00
|
|
|
|
|
|
|
render json: {
|
|
|
|
tag: {
|
|
|
|
id: @tag.id,
|
|
|
|
name: @tag.name,
|
|
|
|
color: @tag.color
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-01-10 04:27:19 +08:00
|
|
|
def restore_group
|
2023-04-26 17:20:10 +08:00
|
|
|
projects = current_team.projects.archived.where(id: params[:project_ids])
|
2021-01-10 04:27:19 +08:00
|
|
|
counter = 0
|
|
|
|
projects.each do |project|
|
|
|
|
next unless can_restore_project?(project)
|
|
|
|
|
|
|
|
project.transaction do
|
|
|
|
project.restore!(current_user)
|
|
|
|
log_activity(:restore_project, project)
|
|
|
|
counter += 1
|
|
|
|
rescue StandardError => e
|
|
|
|
Rails.logger.error e.message
|
|
|
|
raise ActiveRecord::Rollback
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if counter.positive?
|
|
|
|
render json: { message: t('projects.restore_group.success_flash', number: counter) }
|
|
|
|
else
|
|
|
|
render json: { message: t('projects.restore_group.error_flash') }, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-25 01:10:58 +08:00
|
|
|
def users_filter
|
2021-01-07 20:01:14 +08:00
|
|
|
users = current_team.users.search(false, params[:query]).map do |u|
|
2023-12-01 07:01:08 +08:00
|
|
|
[u.id, u.name, { avatar_url: avatar_path(u, :icon_small) }]
|
2020-11-25 01:10:58 +08:00
|
|
|
end
|
|
|
|
|
2023-12-01 07:01:08 +08:00
|
|
|
render json: { data: users }, status: :ok
|
2020-11-25 01:10:58 +08:00
|
|
|
end
|
|
|
|
|
2023-12-01 07:01:08 +08:00
|
|
|
def user_roles
|
|
|
|
render json: { data: user_roles_collection(Project.new).map(&:reverse) }
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2023-04-26 17:20:10 +08:00
|
|
|
def actions_toolbar
|
|
|
|
render json: {
|
|
|
|
actions:
|
|
|
|
Toolbars::ProjectsService.new(
|
|
|
|
current_user,
|
2023-11-24 18:08:28 +08:00
|
|
|
items: JSON.parse(params[:items])
|
2023-04-26 17:20:10 +08:00
|
|
|
).actions
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def project_params
|
2021-06-27 19:22:19 +08:00
|
|
|
params.require(:project)
|
|
|
|
.permit(
|
2023-06-13 22:55:40 +08:00
|
|
|
:name, :visibility,
|
2021-06-27 19:22:19 +08:00
|
|
|
:archived, :project_folder_id,
|
2021-11-10 16:52:37 +08:00
|
|
|
:default_public_user_role_id
|
2021-06-27 19:22:19 +08:00
|
|
|
)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2023-06-13 22:55:40 +08:00
|
|
|
def project_update_params
|
|
|
|
params.require(:project)
|
|
|
|
.permit(:name, :visibility, :archived, :default_public_user_role_id)
|
|
|
|
end
|
|
|
|
|
2021-02-11 18:50:59 +08:00
|
|
|
def view_type_params
|
|
|
|
params.require(:project).require(:view_type)
|
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
def load_vars
|
2022-12-19 18:55:38 +08:00
|
|
|
@project = Project.find_by(id: params[:id] || params[:project_id])
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2020-10-26 23:17:39 +08:00
|
|
|
render_404 unless @project
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2022-12-19 18:55:38 +08:00
|
|
|
def tag_params
|
|
|
|
params.require(:tag).permit(:name)
|
|
|
|
end
|
|
|
|
|
2020-11-24 22:42:06 +08:00
|
|
|
def load_current_folder
|
|
|
|
if current_team && params[:project_folder_id].present?
|
|
|
|
@current_folder = current_team.project_folders.find_by(id: params[:project_folder_id])
|
2021-01-25 23:06:48 +08:00
|
|
|
elsif @project&.project_folder
|
|
|
|
@current_folder = @project&.project_folder
|
2020-11-24 22:42:06 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
def check_view_permissions
|
2022-12-01 21:55:23 +08:00
|
|
|
current_team_switch(@project.team) if current_team != @project.team
|
2018-01-25 19:00:30 +08:00
|
|
|
render_403 unless can_read_project?(@project)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2017-12-08 00:08:41 +08:00
|
|
|
def check_create_permissions
|
2018-01-05 22:15:50 +08:00
|
|
|
render_403 unless can_create_projects?(current_team)
|
2017-12-08 00:08:41 +08:00
|
|
|
end
|
|
|
|
|
2018-02-03 01:48:55 +08:00
|
|
|
def check_manage_permissions
|
|
|
|
render_403 unless can_manage_project?(@project)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
2019-03-08 00:26:42 +08:00
|
|
|
|
2019-05-20 19:13:31 +08:00
|
|
|
def set_inline_name_editing
|
|
|
|
@inline_editable_title_config = {
|
|
|
|
name: 'title',
|
|
|
|
params_group: 'project',
|
2020-01-06 23:07:23 +08:00
|
|
|
item_id: @project.id,
|
2019-05-20 19:13:31 +08:00
|
|
|
field_to_udpate: 'name',
|
|
|
|
path_to_update: project_path(@project)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-07-22 03:33:13 +08:00
|
|
|
def set_folder_inline_name_editing
|
2022-05-19 19:46:03 +08:00
|
|
|
return if !can_manage_team?(current_team) || @current_folder.nil?
|
2021-07-22 03:33:13 +08:00
|
|
|
|
|
|
|
@inline_editable_title_config = {
|
|
|
|
name: 'title',
|
|
|
|
params_group: 'project_folder',
|
|
|
|
item_id: @current_folder.id,
|
|
|
|
field_to_udpate: 'name',
|
|
|
|
path_to_update: project_folder_path(@current_folder)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-12-29 05:49:52 +08:00
|
|
|
def log_activity(type_of, project = nil, message_items = {})
|
|
|
|
project ||= @project
|
|
|
|
message_items = { project: project.id }.merge(message_items)
|
2019-03-21 04:34:47 +08:00
|
|
|
|
2019-03-08 00:26:42 +08:00
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: type_of,
|
|
|
|
owner: current_user,
|
2020-12-29 05:49:52 +08:00
|
|
|
subject: project,
|
|
|
|
team: project.team,
|
|
|
|
project: project,
|
2019-03-21 04:34:47 +08:00
|
|
|
message_items: message_items)
|
2019-03-08 00:26:42 +08:00
|
|
|
end
|
2023-04-19 20:16:22 +08:00
|
|
|
|
|
|
|
def set_navigator
|
2023-04-21 21:25:52 +08:00
|
|
|
@navigator = if @project
|
|
|
|
{
|
|
|
|
url: tree_navigator_project_path(@project),
|
2023-04-25 18:30:45 +08:00
|
|
|
archived: params[:view_mode] == 'archived',
|
2023-04-21 21:25:52 +08:00
|
|
|
id: @project.code
|
|
|
|
}
|
|
|
|
elsif current_folder
|
|
|
|
{
|
|
|
|
url: tree_navigator_project_folder_path(current_folder),
|
2023-04-25 18:30:45 +08:00
|
|
|
archived: params[:view_mode] == 'archived',
|
2023-04-21 21:25:52 +08:00
|
|
|
id: current_folder.code
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-25 18:30:45 +08:00
|
|
|
url: navigator_projects_path,
|
|
|
|
archived: params[:view_mode] == 'archived'
|
2023-04-21 21:25:52 +08:00
|
|
|
}
|
|
|
|
end
|
2023-04-19 20:16:22 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|