2020-02-25 00:09:10 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Dashboard
|
|
|
|
class QuickStartController < ApplicationController
|
2020-03-05 06:26:01 +08:00
|
|
|
include InputSanitizeHelper
|
|
|
|
|
|
|
|
before_action :load_project, only: %i(create_task experiment_filter)
|
2020-02-25 20:46:44 +08:00
|
|
|
before_action :load_experiment, only: :create_task
|
|
|
|
before_action :check_task_create_permissions, only: :create_task
|
|
|
|
|
2020-02-25 00:09:10 +08:00
|
|
|
def create_task
|
2020-02-25 20:46:44 +08:00
|
|
|
my_module = CreateMyModuleService.new(current_user, current_team,
|
2024-05-06 18:42:19 +08:00
|
|
|
my_module: create_my_module_params,
|
2020-02-25 20:46:44 +08:00
|
|
|
project: @project || create_project_params,
|
|
|
|
experiment: @experiment || create_experiment_params).call
|
2021-07-23 17:56:28 +08:00
|
|
|
if my_module.errors.blank?
|
2020-02-25 00:09:10 +08:00
|
|
|
render json: { my_module_path: protocols_my_module_path(my_module) }
|
|
|
|
else
|
2020-03-10 20:16:56 +08:00
|
|
|
render json: { errors: my_module.errors, error_object: my_module.class.name }, status: :unprocessable_entity
|
2020-02-25 00:09:10 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-05 06:26:01 +08:00
|
|
|
def project_filter
|
2021-12-20 17:44:17 +08:00
|
|
|
projects = Project.readable_by_user(current_user)
|
2021-10-01 18:20:24 +08:00
|
|
|
.search(current_user, false, params[:query], 1, current_team)
|
|
|
|
.select(:id, :name)
|
2024-05-06 18:42:19 +08:00
|
|
|
projects = projects.map { |i| [i.id, escape_input(i.name)] }
|
|
|
|
if (projects.map { |i| i[1] }.exclude? params[:query]) && params[:query].present?
|
|
|
|
projects = [[0, params[:query]]] + projects
|
2020-03-05 06:26:01 +08:00
|
|
|
end
|
2024-05-06 18:42:19 +08:00
|
|
|
render json: { data: projects }, status: :ok
|
2020-03-05 06:26:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def experiment_filter
|
|
|
|
if create_project_params.present? && params[:query].present?
|
2024-05-06 18:42:19 +08:00
|
|
|
experiments = [[0, params[:query]]]
|
2020-03-05 06:26:01 +08:00
|
|
|
elsif @project
|
|
|
|
experiments = @project.experiments
|
2021-10-08 18:43:26 +08:00
|
|
|
.managable_by_user(current_user)
|
2020-03-05 06:26:01 +08:00
|
|
|
.search(current_user, false, params[:query], 1, current_team)
|
|
|
|
.select(:id, :name)
|
2024-05-06 18:42:19 +08:00
|
|
|
experiments = experiments.map { |i| [i.id, escape_input(i.name)] }
|
|
|
|
if (experiments.map { |i| i[1] }.exclude? params[:query]) &&
|
2021-12-20 17:44:17 +08:00
|
|
|
params[:query].present? &&
|
|
|
|
can_create_project_experiments?(@project)
|
2024-05-06 18:42:19 +08:00
|
|
|
experiments = [[0, params[:query]]] + experiments
|
2020-03-05 06:26:01 +08:00
|
|
|
end
|
|
|
|
end
|
2024-05-06 18:42:19 +08:00
|
|
|
render json: { data: experiments || [] }, status: :ok
|
2020-03-05 06:26:01 +08:00
|
|
|
end
|
|
|
|
|
2020-02-25 00:09:10 +08:00
|
|
|
private
|
|
|
|
|
2024-05-06 18:42:19 +08:00
|
|
|
def create_my_module_params
|
|
|
|
params.require(:my_module).permit(:name)
|
|
|
|
end
|
|
|
|
|
2020-02-25 20:46:44 +08:00
|
|
|
def create_project_params
|
2024-05-06 18:42:19 +08:00
|
|
|
params.require(:project).permit(:name, :visibility, :default_public_user_role_id)
|
2020-02-25 20:46:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_experiment_params
|
|
|
|
params.require(:experiment).permit(:name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_project
|
2021-12-20 17:44:17 +08:00
|
|
|
@project = current_team.projects.readable_by_user(current_user).find_by(id: params.dig(:project, :id))
|
2020-02-25 20:46:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def load_experiment
|
2021-10-14 19:13:29 +08:00
|
|
|
return unless @project
|
|
|
|
|
|
|
|
@experiment =
|
|
|
|
@project.experiments.managable_by_user(current_user).find_by(id: params.dig(:experiment, :id))
|
2020-02-25 20:46:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_task_create_permissions
|
|
|
|
unless @project
|
|
|
|
render_403 unless can_create_projects?(current_user, current_team)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
unless @experiment
|
2021-09-07 17:27:52 +08:00
|
|
|
render_403 unless can_create_project_experiments?(current_user, @project)
|
2020-02-25 20:46:44 +08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
render_403 unless can_manage_experiment?(current_user, @experiment)
|
2020-02-25 00:09:10 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|