2021-05-23 00:36:30 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Api
|
|
|
|
module V1
|
|
|
|
class ExperimentUserAssignmentsController < BaseController
|
|
|
|
before_action :load_team
|
|
|
|
before_action :load_project
|
|
|
|
before_action :load_experiment
|
2021-10-28 20:48:28 +08:00
|
|
|
before_action :check_read_permissions
|
2021-06-06 15:54:47 +08:00
|
|
|
before_action :load_user_assignment, only: %i(update show)
|
|
|
|
before_action :load_user_assignment_for_managing, only: %i(update show)
|
2021-05-23 00:36:30 +08:00
|
|
|
|
|
|
|
def index
|
|
|
|
user_assignments = @experiment.user_assignments
|
2021-05-23 23:31:02 +08:00
|
|
|
.includes(:user_role)
|
2021-05-23 00:36:30 +08:00
|
|
|
.page(params.dig(:page, :number))
|
|
|
|
.per(params.dig(:page, :size))
|
|
|
|
|
|
|
|
render jsonapi: user_assignments,
|
2021-11-17 23:28:02 +08:00
|
|
|
each_serializer: UserAssignmentSerializer,
|
|
|
|
include: include_params
|
2021-05-23 00:36:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
render jsonapi: @user_assignment,
|
2021-11-17 23:28:02 +08:00
|
|
|
serializer: UserAssignmentSerializer,
|
|
|
|
include: include_params
|
2021-05-23 00:36:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2021-05-29 21:28:10 +08:00
|
|
|
user_role = UserRole.find user_assignment_params[:user_role_id]
|
2021-05-23 00:36:30 +08:00
|
|
|
user = @user_assignment.user
|
|
|
|
experiment_member = ExperimentMember.new(
|
|
|
|
current_user,
|
|
|
|
@experiment,
|
|
|
|
@project,
|
|
|
|
user,
|
|
|
|
@user_assignment
|
|
|
|
)
|
|
|
|
|
|
|
|
return render body: nil, status: :no_content if @user_assignment.user_role == user_role
|
|
|
|
|
|
|
|
experiment_member.update(user_role_id: user_role.id, user_id: user.id)
|
2021-05-30 01:25:46 +08:00
|
|
|
render jsonapi: experiment_member.user_assignment.reload,
|
2021-11-17 23:28:02 +08:00
|
|
|
serializer: UserAssignmentSerializer,
|
2021-05-30 01:25:46 +08:00
|
|
|
status: :ok
|
2021-05-23 00:36:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-10-28 20:48:28 +08:00
|
|
|
def check_read_permissions
|
|
|
|
raise PermissionError.new(Experiment, :read_users) unless can_read_experiment_users?(@experiment)
|
|
|
|
end
|
|
|
|
|
2021-05-23 00:36:30 +08:00
|
|
|
def load_user_assignment
|
|
|
|
@user_assignment = @experiment.user_assignments.find(params.require(:id))
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_user_assignment_for_managing
|
2021-10-28 20:48:28 +08:00
|
|
|
raise PermissionError.new(Experiment, :manage_users) unless can_manage_experiment_users?(@experiment)
|
2021-05-23 00:36:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def user_assignment_params
|
2021-11-17 23:28:02 +08:00
|
|
|
raise TypeError unless params.require(:data).require(:type) == 'user_assignments'
|
2021-05-23 00:36:30 +08:00
|
|
|
|
2021-05-29 21:28:10 +08:00
|
|
|
params.require(:data).require(:attributes).permit(:user_id, :user_role_id)
|
2021-05-23 00:36:30 +08:00
|
|
|
end
|
2021-11-17 23:28:02 +08:00
|
|
|
|
|
|
|
def permitted_includes
|
|
|
|
%w(user user_role assignable)
|
|
|
|
end
|
2021-05-23 00:36:30 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|