2021-04-18 18:23:29 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module AccessPermissions
|
|
|
|
class ProjectsController < ApplicationController
|
2023-03-17 18:25:36 +08:00
|
|
|
include InputSanitizeHelper
|
|
|
|
|
2021-04-18 18:23:29 +08:00
|
|
|
before_action :set_project
|
2021-05-02 17:36:10 +08:00
|
|
|
before_action :check_read_permissions, only: %i(show)
|
2021-12-01 23:23:24 +08:00
|
|
|
before_action :check_manage_permissions, except: %i(show)
|
2023-01-23 05:23:18 +08:00
|
|
|
before_action :available_users, only: %i(new create)
|
2021-04-18 23:19:16 +08:00
|
|
|
|
2023-12-05 03:59:16 +08:00
|
|
|
def show
|
|
|
|
render json: @project.user_assignments.includes(:user_role, :user).order('users.full_name ASC'),
|
|
|
|
each_serializer: UserAssignmentSerializer
|
2021-04-25 00:43:28 +08:00
|
|
|
end
|
|
|
|
|
2023-12-05 03:59:16 +08:00
|
|
|
def new
|
|
|
|
render json: @available_users, each_serializer: UserSerializer
|
|
|
|
end
|
2021-04-18 18:23:29 +08:00
|
|
|
|
2023-07-18 19:36:41 +08:00
|
|
|
def edit; end
|
2021-04-18 18:23:29 +08:00
|
|
|
|
2023-12-05 03:59:16 +08:00
|
|
|
def create
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
created_count = 0
|
|
|
|
if permitted_create_params[:user_id] == 'all'
|
|
|
|
@project.update!(visibility: :visible, default_public_user_role_id: permitted_create_params[:user_role_id])
|
|
|
|
log_activity(:project_grant_access_to_all_team_members,
|
|
|
|
{ visibility: t('projects.activity.visibility_visible'),
|
|
|
|
role: @project.default_public_user_role.name,
|
|
|
|
team: @project.team.id })
|
|
|
|
else
|
|
|
|
user_assignment = UserAssignment.find_or_initialize_by(
|
|
|
|
assignable: @project,
|
|
|
|
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(:assign_user_to_project, { user_target: user_assignment.user.id,
|
|
|
|
role: user_assignment.user_role.name })
|
|
|
|
created_count += 1
|
|
|
|
propagate_job(user_assignment)
|
|
|
|
end
|
|
|
|
|
|
|
|
@message = if created_count.zero?
|
|
|
|
t('access_permissions.create.success', member_name: t('access_permissions.all_team'))
|
|
|
|
else
|
|
|
|
t('access_permissions.create.success', member_name: escape_input(user_assignment.user.name))
|
|
|
|
end
|
|
|
|
render json: { message: @message }
|
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
|
|
Rails.logger.error e.message
|
|
|
|
errors = @project.errors.present? ? @project.errors&.map(&:message)&.join(',') : e.message
|
|
|
|
render json: { flash: errors }, status: :unprocessable_entity
|
|
|
|
raise ActiveRecord::Rollback
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-18 23:19:16 +08:00
|
|
|
def update
|
2023-01-23 23:01:40 +08:00
|
|
|
@user_assignment = @project.user_assignments.find_by(
|
|
|
|
user_id: permitted_update_params[:user_id],
|
|
|
|
team: current_team
|
|
|
|
)
|
2023-03-24 20:32:38 +08:00
|
|
|
|
2023-04-03 17:46:32 +08:00
|
|
|
# prevent role change if it would result in no manually assigned users having the user management permission
|
2023-03-24 20:32:38 +08:00
|
|
|
new_user_role = UserRole.find(permitted_update_params[:user_role_id])
|
|
|
|
if !new_user_role.has_permission?(ProjectPermissions::USERS_MANAGE) &&
|
2023-04-03 17:46:32 +08:00
|
|
|
@user_assignment.last_with_permission?(ProjectPermissions::USERS_MANAGE, assigned: :manually)
|
2023-03-24 20:32:38 +08:00
|
|
|
raise ActiveRecord::RecordInvalid
|
|
|
|
end
|
|
|
|
|
2023-01-23 05:23:18 +08:00
|
|
|
@user_assignment.update!(permitted_update_params)
|
|
|
|
|
2023-12-06 15:02:25 +08:00
|
|
|
unless current_user.id == @user_assignment.user.id
|
|
|
|
log_activity(:change_user_role_on_project, { user_target: @user_assignment.user.id,
|
|
|
|
role: @user_assignment.user_role.name })
|
|
|
|
end
|
2023-03-07 21:04:05 +08:00
|
|
|
propagate_job(@user_assignment)
|
2021-04-29 00:02:18 +08:00
|
|
|
|
2023-07-18 19:36:41 +08:00
|
|
|
render :project_member
|
2023-03-27 18:38:01 +08:00
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
render json: { flash: t('access_permissions.update.failure') }, status: :unprocessable_entity
|
2021-04-18 18:23:29 +08:00
|
|
|
end
|
|
|
|
|
2021-04-29 00:02:18 +08:00
|
|
|
def destroy
|
2023-01-23 05:23:18 +08:00
|
|
|
user = @project.assigned_users.find(params[:user_id])
|
2023-01-23 23:01:40 +08:00
|
|
|
user_assignment = @project.user_assignments.find_by(user: user, team: current_team)
|
2023-01-23 05:23:18 +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?(ProjectPermissions::USERS_MANAGE, assigned: :manually)
|
|
|
|
raise ActiveRecord::RecordInvalid
|
|
|
|
end
|
2023-03-24 20:32:38 +08:00
|
|
|
|
2023-03-15 01:48:06 +08:00
|
|
|
propagate_job(user_assignment, destroy: true)
|
2023-12-05 03:59:16 +08:00
|
|
|
|
|
|
|
user_assignment.destroy!
|
|
|
|
|
2023-03-30 22:04:57 +08:00
|
|
|
log_activity(:unassign_user_from_project, { user_target: user_assignment.user.id,
|
|
|
|
role: user_assignment.user_role.name })
|
2023-01-23 05:23:18 +08:00
|
|
|
|
2023-12-05 03:59:16 +08:00
|
|
|
render json: { message: t('access_permissions.destroy.success', member_name: escape_input(user.full_name)) }
|
2023-01-23 05:23:18 +08:00
|
|
|
rescue ActiveRecord::RecordInvalid
|
2023-12-05 03:59:16 +08:00
|
|
|
render json: { message: t('access_permissions.destroy.failure') },
|
2023-01-23 05:23:18 +08:00
|
|
|
status: :unprocessable_entity
|
2021-04-25 00:43:28 +08:00
|
|
|
end
|
|
|
|
|
2021-12-01 23:23:24 +08:00
|
|
|
def update_default_public_user_role
|
2023-02-25 00:24:25 +08:00
|
|
|
Project.transaction do
|
2023-04-21 17:58:34 +08:00
|
|
|
@project.visibility_will_change!
|
|
|
|
@project.last_modified_by = current_user
|
2023-03-30 22:04:57 +08:00
|
|
|
if permitted_default_public_user_role_params[:default_public_user_role_id].blank?
|
2023-04-21 17:58:34 +08:00
|
|
|
# revoke all team members access
|
|
|
|
@project.visibility = :hidden
|
2023-06-16 19:08:02 +08:00
|
|
|
previous_user_role_name = @project.default_public_user_role.name
|
|
|
|
@project.default_public_user_role_id = nil
|
2023-04-21 17:58:34 +08:00
|
|
|
@project.save!
|
2023-06-16 19:08:02 +08:00
|
|
|
log_activity(:project_remove_access_from_all_team_members,
|
|
|
|
{ visibility: t('projects.activity.visibility_hidden'),
|
|
|
|
role: previous_user_role_name,
|
|
|
|
team: @project.team.id })
|
2023-12-05 03:59:16 +08:00
|
|
|
render json: { message: t('access_permissions.update.revoke_all_team_members') }
|
2023-03-30 22:04:57 +08:00
|
|
|
else
|
2023-03-31 18:14:26 +08:00
|
|
|
# update all team members access
|
2023-04-21 17:58:34 +08:00
|
|
|
@project.visibility = :visible
|
|
|
|
@project.assign_attributes(permitted_default_public_user_role_params)
|
|
|
|
@project.save!
|
2023-03-30 22:04:57 +08:00
|
|
|
log_activity(:project_access_changed_all_team_members,
|
|
|
|
{ team: @project.team.id, role: @project.default_public_user_role&.name })
|
|
|
|
end
|
2023-04-13 21:34:36 +08:00
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
|
|
Rails.logger.error e.message
|
|
|
|
render json: { flash: @project.errors&.map(&:message)&.join(',') }, status: :unprocessable_entity
|
|
|
|
raise ActiveRecord::Rollback
|
2023-02-25 00:24:25 +08:00
|
|
|
end
|
2021-12-01 23:23:24 +08:00
|
|
|
end
|
|
|
|
|
2021-04-18 18:23:29 +08:00
|
|
|
private
|
|
|
|
|
2021-12-01 23:23:24 +08:00
|
|
|
def permitted_default_public_user_role_params
|
|
|
|
params.require(:project).permit(:default_public_user_role_id)
|
|
|
|
end
|
|
|
|
|
2021-04-18 23:19:16 +08:00
|
|
|
def permitted_update_params
|
2023-01-23 05:23:18 +08:00
|
|
|
params.require(:user_assignment)
|
2021-05-02 17:36:10 +08:00
|
|
|
.permit(%i(user_role_id user_id))
|
2021-04-18 23:19:16 +08:00
|
|
|
end
|
|
|
|
|
2021-04-25 00:43:28 +08:00
|
|
|
def permitted_create_params
|
2023-12-05 03:59:16 +08:00
|
|
|
params.require(:user_assignment)
|
|
|
|
.permit(%i(user_id user_role_id))
|
2021-04-25 00:43:28 +08:00
|
|
|
end
|
|
|
|
|
2021-04-18 18:23:29 +08:00
|
|
|
def set_project
|
2021-05-02 17:36:10 +08:00
|
|
|
@project = current_team.projects.includes(user_assignments: %i(user user_role)).find_by(id: params[:id])
|
2021-04-18 18:23:29 +08:00
|
|
|
|
|
|
|
render_404 unless @project
|
|
|
|
end
|
|
|
|
|
2023-03-15 01:48:06 +08:00
|
|
|
def propagate_job(user_assignment, destroy: false)
|
2023-01-23 05:23:18 +08:00
|
|
|
UserAssignments::PropagateAssignmentJob.perform_later(
|
|
|
|
@project,
|
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-03-15 01:48:06 +08:00
|
|
|
destroy: destroy
|
2023-01-23 05:23:18 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-04-18 18:23:29 +08:00
|
|
|
def check_manage_permissions
|
2021-09-07 17:27:52 +08:00
|
|
|
render_403 unless can_manage_project_users?(@project)
|
2021-04-18 18:23:29 +08:00
|
|
|
end
|
2021-04-18 23:19:16 +08:00
|
|
|
|
|
|
|
def check_read_permissions
|
|
|
|
render_403 unless can_read_project?(@project)
|
|
|
|
end
|
2023-01-23 05:23:18 +08:00
|
|
|
|
|
|
|
def available_users
|
|
|
|
# automatically assigned or not assigned to project
|
|
|
|
@available_users = current_team.users.where(
|
|
|
|
id: @project.user_assignments.automatically_assigned.select(:user_id)
|
|
|
|
).or(
|
|
|
|
current_team.users.where.not(id: @project.users.select(:id))
|
2023-12-05 03:59:16 +08:00
|
|
|
).order('users.full_name ASC')
|
2023-01-23 05:23:18 +08:00
|
|
|
end
|
|
|
|
|
2023-03-30 22:04:57 +08:00
|
|
|
def log_activity(type_of, message_items = {})
|
|
|
|
message_items = { project: @project.id }.merge(message_items)
|
|
|
|
|
2023-01-23 05:23:18 +08:00
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: type_of,
|
|
|
|
owner: current_user,
|
|
|
|
subject: @project,
|
|
|
|
team: @project.team,
|
|
|
|
project: @project,
|
2023-03-30 22:04:57 +08:00
|
|
|
message_items: message_items)
|
2023-01-23 05:23:18 +08:00
|
|
|
end
|
2021-04-18 18:23:29 +08:00
|
|
|
end
|
|
|
|
end
|