2022-12-22 20:27:46 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module AccessPermissions
|
|
|
|
class ProtocolsController < ApplicationController
|
2023-12-28 03:09:36 +08:00
|
|
|
include InputSanitizeHelper
|
|
|
|
|
2022-12-22 20:27:46 +08:00
|
|
|
before_action :set_protocol
|
|
|
|
before_action :check_read_permissions, only: %i(show)
|
|
|
|
before_action :check_manage_permissions, except: %i(show)
|
2023-12-28 03:09:36 +08:00
|
|
|
before_action :available_users, only: %i(new create)
|
2022-12-22 20:27:46 +08:00
|
|
|
|
2023-12-28 03:09:36 +08:00
|
|
|
def show
|
|
|
|
render json: @protocol.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-28 03:09:36 +08:00
|
|
|
end
|
2023-12-05 03:59:16 +08:00
|
|
|
|
2022-12-22 20:27:46 +08:00
|
|
|
def new
|
2024-02-21 19:33:30 +08:00
|
|
|
render json: @available_users, each_serializer: UserSerializer, user: current_user
|
2022-12-22 20:27:46 +08:00
|
|
|
end
|
|
|
|
|
2023-07-18 19:36:41 +08:00
|
|
|
def edit; end
|
2022-12-22 20:27:46 +08:00
|
|
|
|
|
|
|
def create
|
2023-01-03 21:43:03 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
2023-02-23 21:57:38 +08:00
|
|
|
created_count = 0
|
2023-12-28 03:09:36 +08:00
|
|
|
if permitted_create_params[:user_id] == 'all'
|
|
|
|
@protocol.update!(visibility: :visible, default_public_user_role_id: permitted_create_params[:user_role_id])
|
|
|
|
log_activity(:protocol_template_access_granted_all_team_members,
|
2023-03-30 22:04:57 +08:00
|
|
|
{ team: @protocol.team.id, role: @protocol.default_public_user_role&.name })
|
2023-12-28 03:09:36 +08:00
|
|
|
else
|
|
|
|
user_assignment = UserAssignment.find_or_initialize_by(
|
|
|
|
assignable: @protocol,
|
|
|
|
user_id: permitted_create_params[:user_id],
|
|
|
|
team: current_team
|
|
|
|
)
|
|
|
|
|
|
|
|
user_assignment.update!(
|
|
|
|
user_role_id: permitted_create_params[:user_role_id],
|
|
|
|
assigned_by: current_user,
|
|
|
|
assigned: :manually
|
|
|
|
)
|
|
|
|
|
|
|
|
log_activity(:protocol_template_access_granted, { user_target: user_assignment.user.id,
|
2023-03-30 22:04:57 +08:00
|
|
|
role: user_assignment.user_role.name })
|
2023-12-28 03:09:36 +08:00
|
|
|
created_count += 1
|
2023-02-21 22:25:01 +08:00
|
|
|
end
|
2023-03-09 21:13:01 +08:00
|
|
|
|
2023-07-18 19:36:41 +08:00
|
|
|
@message = if created_count.zero?
|
2023-12-28 03:09:36 +08:00
|
|
|
t('access_permissions.create.success', member_name: t('access_permissions.all_team'))
|
2023-07-18 19:36:41 +08:00
|
|
|
else
|
2023-12-28 03:09:36 +08:00
|
|
|
t('access_permissions.create.success', member_name: escape_input(user_assignment.user.name))
|
2023-07-18 19:36:41 +08:00
|
|
|
end
|
2023-12-28 03:09:36 +08:00
|
|
|
render json: { message: @message }
|
2023-04-13 21:34:36 +08:00
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
|
|
Rails.logger.error e.message
|
2023-04-18 17:22:17 +08:00
|
|
|
errors = @protocol.errors.present? ? @protocol.errors&.map(&:message)&.join(',') : e.message
|
2023-04-13 21:34:36 +08:00
|
|
|
render json: { flash: errors }, status: :unprocessable_entity
|
|
|
|
raise ActiveRecord::Rollback
|
2022-12-22 20:27:46 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-12-05 03:59:16 +08:00
|
|
|
def update
|
|
|
|
@user_assignment = @protocol.user_assignments.find_by(
|
|
|
|
user_id: permitted_update_params[:user_id],
|
|
|
|
team: current_team
|
|
|
|
)
|
|
|
|
|
|
|
|
# prevent role change if it would result in no manually assigned users having the user management permission
|
|
|
|
new_user_role = UserRole.find(permitted_update_params[:user_role_id])
|
|
|
|
if !new_user_role.has_permission?(ProtocolPermissions::USERS_MANAGE) &&
|
|
|
|
@user_assignment.last_with_permission?(ProtocolPermissions::USERS_MANAGE, assigned: :manually)
|
|
|
|
raise ActiveRecord::RecordInvalid
|
|
|
|
end
|
|
|
|
|
|
|
|
@user_assignment.update!(permitted_update_params)
|
|
|
|
log_activity(:protocol_template_access_changed, { user_target: @user_assignment.user.id,
|
|
|
|
role: @user_assignment.user_role.name })
|
|
|
|
|
|
|
|
render :protocol_member
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
render json: { flash: t('access_permissions.update.failure') }, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
2022-12-22 20:27:46 +08:00
|
|
|
def destroy
|
2023-01-03 21:43:03 +08:00
|
|
|
user = @protocol.assigned_users.find(params[:user_id])
|
2023-01-23 23:01:40 +08:00
|
|
|
user_assignment = @protocol.user_assignments.find_by(user: user, team: current_team)
|
2023-02-21 22:25:01 +08:00
|
|
|
|
2023-04-03 17:46:32 +08:00
|
|
|
# prevent deletion of last manually assigned user that can manage users
|
|
|
|
if user_assignment.last_with_permission?(ProtocolPermissions::USERS_MANAGE, assigned: :manually)
|
|
|
|
raise ActiveRecord::RecordInvalid
|
|
|
|
end
|
2023-03-24 20:32:38 +08:00
|
|
|
|
2023-03-14 21:49:11 +08:00
|
|
|
Protocol.transaction do
|
|
|
|
if @protocol.visible?
|
|
|
|
user_assignment.update!(
|
|
|
|
user_role: @protocol.default_public_user_role,
|
|
|
|
assigned: :automatically
|
|
|
|
)
|
2022-12-22 20:27:46 +08:00
|
|
|
else
|
2023-03-14 21:49:11 +08:00
|
|
|
user_assignment.destroy!
|
2022-12-22 20:27:46 +08:00
|
|
|
end
|
2023-03-30 22:04:57 +08:00
|
|
|
log_activity(:protocol_template_access_revoked, { user_target: user_assignment.user.id,
|
|
|
|
role: user_assignment.user_role.name })
|
2022-12-22 20:27:46 +08:00
|
|
|
end
|
2023-03-14 21:49:11 +08:00
|
|
|
|
2024-02-21 19:33:30 +08:00
|
|
|
render json: { message: t('access_permissions.destroy.success', member_name: user.full_name) }
|
2023-03-14 21:49:11 +08:00
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
|
|
Rails.logger.error e.message
|
2024-02-21 19:33:30 +08:00
|
|
|
render json: { message: t('access_permissions.destroy.failure') }, status: :unprocessable_entity
|
2023-03-14 21:49:11 +08:00
|
|
|
raise ActiveRecord::Rollback
|
2022-12-22 20:27:46 +08:00
|
|
|
end
|
|
|
|
|
2023-02-25 00:24:25 +08:00
|
|
|
def update_default_public_user_role
|
2023-04-13 21:34:36 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
current_role = @protocol.default_public_user_role.name
|
|
|
|
@protocol.update!(permitted_default_public_user_role_params)
|
|
|
|
|
|
|
|
# revoke all team members access
|
|
|
|
if permitted_default_public_user_role_params[:default_public_user_role_id].blank?
|
|
|
|
log_activity(:protocol_template_access_revoked_all_team_members,
|
|
|
|
{ team: @protocol.team.id, role: current_role })
|
|
|
|
render json: { flash: t('access_permissions.update.revoke_all_team_members') }, status: :ok
|
|
|
|
else
|
|
|
|
# update all team members access
|
|
|
|
log_activity(:protocol_template_access_changed_all_team_members,
|
|
|
|
{ team: @protocol.team.id, role: @protocol.default_public_user_role&.name })
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
|
|
Rails.logger.error e.message
|
|
|
|
render json: { flash: @protocol&.errors&.map(&:message)&.join(',') }, status: :unprocessable_entity
|
|
|
|
raise ActiveRecord::Rollback
|
2023-03-31 18:14:26 +08:00
|
|
|
end
|
2023-02-25 00:24:25 +08:00
|
|
|
end
|
|
|
|
|
2022-12-22 20:27:46 +08:00
|
|
|
private
|
|
|
|
|
2023-02-25 00:24:25 +08:00
|
|
|
def permitted_default_public_user_role_params
|
2024-02-21 19:33:30 +08:00
|
|
|
params.require(:object).permit(:default_public_user_role_id)
|
2023-02-25 00:24:25 +08:00
|
|
|
end
|
|
|
|
|
2022-12-22 20:27:46 +08:00
|
|
|
def permitted_update_params
|
|
|
|
params.require(:user_assignment)
|
|
|
|
.permit(%i(user_role_id user_id))
|
|
|
|
end
|
|
|
|
|
|
|
|
def permitted_create_params
|
2023-12-28 03:09:36 +08:00
|
|
|
params.require(:user_assignment)
|
|
|
|
.permit(%i(user_id user_role_id))
|
|
|
|
end
|
|
|
|
|
|
|
|
def available_users
|
|
|
|
# automatically assigned or not assigned to project
|
|
|
|
@available_users = current_team.users.where(
|
|
|
|
id: @protocol.user_assignments.automatically_assigned.select(:user_id)
|
|
|
|
).or(
|
|
|
|
current_team.users.where.not(id: @protocol.users.select(:id))
|
|
|
|
).order('users.full_name ASC')
|
2022-12-22 20:27:46 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_protocol
|
|
|
|
@protocol = current_team.protocols.includes(user_assignments: %i(user user_role)).find_by(id: params[:id])
|
|
|
|
|
2023-04-04 19:42:20 +08:00
|
|
|
return render_404 unless @protocol
|
2023-03-14 21:49:11 +08:00
|
|
|
|
|
|
|
@protocol = @protocol.parent if @protocol.parent_id
|
2022-12-22 20:27:46 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_manage_permissions
|
|
|
|
render_403 unless can_manage_protocol_users?(@protocol)
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_read_permissions
|
2023-05-16 19:25:33 +08:00
|
|
|
render_403 unless can_read_protocol_in_repository?(@protocol) || can_manage_team?(@protocol.team)
|
2022-12-22 20:27:46 +08:00
|
|
|
end
|
2023-02-21 22:25:01 +08:00
|
|
|
|
2023-03-30 22:04:57 +08:00
|
|
|
def log_activity(type_of, message_items = {})
|
|
|
|
message_items = { protocol: @protocol.id }.merge(message_items)
|
|
|
|
|
2023-02-21 22:25:01 +08:00
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: type_of,
|
|
|
|
owner: current_user,
|
|
|
|
subject: @protocol,
|
|
|
|
team: @protocol.team,
|
|
|
|
project: nil,
|
2023-03-30 22:04:57 +08:00
|
|
|
message_items: message_items)
|
2023-02-21 22:25:01 +08:00
|
|
|
end
|
2022-12-22 20:27:46 +08:00
|
|
|
end
|
|
|
|
end
|