2021-05-29 21:28:10 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Api
|
|
|
|
module V1
|
|
|
|
class ProjectUserAssignmentsController < BaseController
|
|
|
|
before_action :load_team
|
|
|
|
before_action :load_project
|
2021-10-28 20:48:28 +08:00
|
|
|
before_action :check_read_permissions
|
2021-05-29 21:28:10 +08:00
|
|
|
before_action :load_user_assignment, only: %i(show update destroy)
|
|
|
|
before_action :load_user_project_for_managing, only: %i(show update destroy)
|
|
|
|
|
2021-11-17 23:28:02 +08:00
|
|
|
rescue_from TypeError do
|
|
|
|
# TEMP (17.10.2021) additional error details when using the deprecated user_projects
|
|
|
|
detail_key = params.dig('data', 'type') == 'user_projects' ? 'user_projects_detail' : 'detail'
|
|
|
|
|
|
|
|
render_error(I18n.t('api.core.errors.type.title'),
|
|
|
|
I18n.t("api.core.errors.type.#{detail_key}"),
|
|
|
|
:bad_request)
|
|
|
|
end
|
|
|
|
|
2021-05-29 21:28:10 +08:00
|
|
|
def index
|
|
|
|
user_assignments = @project.user_assignments
|
|
|
|
.includes(:user_role)
|
|
|
|
.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-29 21:28:10 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
render jsonapi: @user_assignment,
|
2021-11-17 23:28:02 +08:00
|
|
|
serializer: UserAssignmentSerializer,
|
|
|
|
include: include_params
|
2021-05-29 21:28:10 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2021-10-28 20:48:28 +08:00
|
|
|
raise PermissionError.new(Project, :manage) unless can_manage_project_users?(@project)
|
2021-05-29 21:28:10 +08:00
|
|
|
|
|
|
|
# internally we reuse the same logic as for user project assignment
|
|
|
|
user = @team.users.find(user_project_params[:user_id])
|
|
|
|
|
|
|
|
project_member = ProjectMember.new(user, @project, current_user)
|
|
|
|
project_member.assign = true
|
|
|
|
project_member.user_role_id = user_project_params[:user_role_id]
|
|
|
|
project_member.create
|
|
|
|
|
2021-05-30 01:25:46 +08:00
|
|
|
render jsonapi: project_member.user_assignment.reload,
|
2021-11-17 23:28:02 +08:00
|
|
|
serializer: UserAssignmentSerializer,
|
2021-05-30 01:25:46 +08:00
|
|
|
status: :created
|
2021-05-29 21:28:10 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
user_role = UserRole.find user_project_params[:user_role_id]
|
|
|
|
project_member = ProjectMember.new(@user_assignment.user, @project, current_user)
|
|
|
|
|
|
|
|
if project_member.user_assignment&.user_role == user_role
|
|
|
|
return render body: nil, status: :no_content
|
|
|
|
end
|
|
|
|
|
|
|
|
project_member.user_role_id = user_role.id
|
|
|
|
project_member.update
|
2021-11-17 23:28:02 +08:00
|
|
|
render jsonapi: @user_assignment.reload, serializer: UserAssignmentSerializer, status: :ok
|
2021-05-29 21:28:10 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
project_member = ProjectMember.new(@user_assignment.user, @project, current_user)
|
|
|
|
project_member.destroy
|
|
|
|
render body: nil
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-10-28 20:48:28 +08:00
|
|
|
def check_read_permissions
|
|
|
|
raise PermissionError.new(Project, :read_users) unless can_read_project_users?(@project)
|
|
|
|
end
|
|
|
|
|
2021-05-29 21:28:10 +08:00
|
|
|
def load_user_assignment
|
|
|
|
@user_assignment = @project.user_assignments.find(params.require(:id))
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_user_project_for_managing
|
2021-10-28 20:48:28 +08:00
|
|
|
raise PermissionError.new(Project, :manage_users) unless can_manage_project_users?(@project)
|
2021-05-29 21:28:10 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def user_project_params
|
2021-11-17 23:28:02 +08:00
|
|
|
raise TypeError unless params.require(:data).require(:type) == 'user_assignments'
|
2021-05-29 21:28:10 +08:00
|
|
|
|
|
|
|
params.require(:data).require(:attributes).permit(:user_id, :user_role_id)
|
|
|
|
end
|
2021-11-17 23:28:02 +08:00
|
|
|
|
|
|
|
def permitted_includes
|
|
|
|
%w(user user_role assignable)
|
|
|
|
end
|
2021-05-29 21:28:10 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|