2021-05-09 00:06:07 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module AccessPermissions
|
|
|
|
class ExperimentsController < ApplicationController
|
|
|
|
before_action :set_experiment
|
2023-02-23 21:57:38 +08:00
|
|
|
before_action :set_project
|
2021-05-09 00:06:07 +08:00
|
|
|
before_action :check_read_permissions, only: %i(show)
|
2021-11-05 22:44:14 +08:00
|
|
|
before_action :check_manage_permissions, only: %i(edit update)
|
2021-05-09 00:06:07 +08:00
|
|
|
|
2023-12-11 22:41:03 +08:00
|
|
|
def show
|
|
|
|
render json: @experiment.user_assignments.includes(:user_role, :user).order('users.full_name ASC'),
|
2024-02-21 19:33:30 +08:00
|
|
|
each_serializer: UserAssignmentSerializer, user: current_user
|
2023-12-11 22:41:03 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
2024-02-21 19:33:30 +08:00
|
|
|
render json: @available_users, each_serializer: UserSerializer, user: current_user
|
2023-12-11 22:41:03 +08:00
|
|
|
end
|
|
|
|
|
2021-05-09 00:06:07 +08:00
|
|
|
|
2023-07-18 19:36:41 +08:00
|
|
|
def edit; end
|
2021-05-09 00:06:07 +08:00
|
|
|
|
|
|
|
def update
|
2023-01-23 05:23:18 +08:00
|
|
|
user_id = permitted_update_params[:user_id]
|
2023-01-23 23:01:40 +08:00
|
|
|
@user_assignment = @experiment.user_assignments.find_by(user_id: user_id, team: current_team)
|
2021-11-15 18:12:31 +08:00
|
|
|
|
|
|
|
if permitted_update_params[:user_role_id] == 'reset'
|
2023-01-23 23:01:40 +08:00
|
|
|
@user_assignment.update!(
|
|
|
|
user_role_id: @project.user_assignments.find_by(user_id: user_id, team: current_team).user_role_id,
|
2023-01-23 05:23:18 +08:00
|
|
|
assigned: :automatically
|
|
|
|
)
|
2021-11-15 18:12:31 +08:00
|
|
|
else
|
2023-01-23 23:01:40 +08:00
|
|
|
@user_assignment.update!(
|
2023-01-23 05:23:18 +08:00
|
|
|
user_role_id: permitted_update_params[:user_role_id],
|
|
|
|
assigned: :manually
|
|
|
|
)
|
2021-11-15 18:12:31 +08:00
|
|
|
end
|
2021-05-09 00:06:07 +08:00
|
|
|
|
2023-01-23 05:23:18 +08:00
|
|
|
UserAssignments::PropagateAssignmentJob.perform_later(
|
|
|
|
@experiment,
|
2023-08-11 21:31:29 +08:00
|
|
|
@user_assignment.user.id,
|
2023-01-23 05:23:18 +08:00
|
|
|
@user_assignment.user_role,
|
2023-08-11 21:31:29 +08:00
|
|
|
current_user.id
|
2023-01-23 05:23:18 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
log_change_activity
|
|
|
|
|
2023-12-11 22:41:03 +08:00
|
|
|
render json: {}, status: :ok
|
2021-05-09 00:06:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def permitted_update_params
|
2023-01-23 05:23:18 +08:00
|
|
|
params.require(:user_assignment)
|
2021-05-09 00:06:07 +08:00
|
|
|
.permit(%i(user_role_id user_id))
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_project
|
2023-02-23 21:57:38 +08:00
|
|
|
@project = @experiment.project
|
2021-05-09 00:06:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_experiment
|
2023-02-23 21:57:38 +08:00
|
|
|
@experiment = Experiment.includes(user_assignments: %i(user user_role)).find_by(id: params[:id])
|
2021-05-09 00:06:07 +08:00
|
|
|
|
|
|
|
render_404 unless @experiment
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_manage_permissions
|
2021-09-30 17:32:11 +08:00
|
|
|
render_403 unless can_manage_experiment_users?(@experiment)
|
2021-05-09 00:06:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_read_permissions
|
|
|
|
render_403 unless can_read_experiment?(@experiment)
|
|
|
|
end
|
2023-01-23 05:23:18 +08:00
|
|
|
|
|
|
|
def log_change_activity
|
|
|
|
Activities::CreateActivityService.call(
|
|
|
|
activity_type: :change_user_role_on_experiment,
|
|
|
|
owner: current_user,
|
|
|
|
subject: @experiment,
|
|
|
|
team: @project.team,
|
|
|
|
project: @project,
|
|
|
|
message_items: {
|
|
|
|
experiment: @experiment.id,
|
|
|
|
user_target: @user_assignment.user_id,
|
|
|
|
role: @user_assignment.user_role.name
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
2021-05-09 00:06:07 +08:00
|
|
|
end
|
|
|
|
end
|