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]
|
2021-12-01 23:23:24 +08:00
|
|
|
project_member.save
|
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)
|
|
|
|
|
2022-03-09 21:12:16 +08:00
|
|
|
return render body: nil, status: :no_content if project_member.user_assignment&.user_role == user_role
|
2021-05-29 21:28:10 +08:00
|
|
|
|
|
|
|
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
|
2021-11-19 20:58:54 +08:00
|
|
|
# team admins can always manage users, so they should also be able to read them
|
|
|
|
unless can_read_project_users?(@project) || can_manage_project_users?(@project)
|
|
|
|
raise PermissionError.new(Project, :read_users)
|
|
|
|
end
|
2021-10-28 20:48:28 +08:00
|
|
|
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
|