2021-04-25 00:43:28 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ProjectMember
|
|
|
|
include ActiveModel::Model
|
|
|
|
|
|
|
|
attr_accessor :user, :project, :assign, :user_role_id, :user_id
|
2021-05-23 00:36:30 +08:00
|
|
|
attr_reader :current_user, :user_assignment, :user_role, :user_project
|
2021-04-25 00:43:28 +08:00
|
|
|
|
2021-04-29 00:02:18 +08:00
|
|
|
delegate :user_role, to: :user_assignment, allow_nil: true
|
2021-04-25 00:43:28 +08:00
|
|
|
|
2021-04-29 00:02:18 +08:00
|
|
|
validates :user, :project, presence: true, if: -> { assign }
|
|
|
|
validates :user_role_id, presence: true, if: -> { assign }
|
|
|
|
validate :validate_role_presence, if: -> { assign }
|
|
|
|
|
|
|
|
def initialize(user, project, current_user = nil)
|
2021-04-25 00:43:28 +08:00
|
|
|
@user = user
|
|
|
|
@project = project
|
|
|
|
@current_user = current_user
|
2021-04-29 00:02:18 +08:00
|
|
|
@user_assignment = UserAssignment.find_by(assignable: @project, user: @user)
|
2021-04-25 00:43:28 +08:00
|
|
|
end
|
|
|
|
|
2021-12-01 23:23:24 +08:00
|
|
|
def save
|
2021-04-25 00:43:28 +08:00
|
|
|
return unless assign
|
|
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
2021-12-01 23:23:24 +08:00
|
|
|
@user_assignment = UserAssignment.find_or_initialize_by(
|
2021-05-02 17:22:59 +08:00
|
|
|
assignable: @project,
|
2021-12-01 23:23:24 +08:00
|
|
|
user: @user
|
|
|
|
)
|
|
|
|
|
|
|
|
@user_assignment.update!(
|
2021-11-22 16:50:14 +08:00
|
|
|
user_role_id: user_role_id,
|
2021-06-12 15:36:41 +08:00
|
|
|
assigned_by: current_user,
|
|
|
|
assigned: :manually
|
2021-05-02 17:22:59 +08:00
|
|
|
)
|
2021-12-01 23:23:24 +08:00
|
|
|
|
2021-05-02 17:22:59 +08:00
|
|
|
log_activity(:assign_user_to_project)
|
2021-06-06 15:54:47 +08:00
|
|
|
|
|
|
|
UserAssignments::PropagateAssignmentJob.perform_later(
|
|
|
|
@project,
|
|
|
|
@user,
|
|
|
|
user_role,
|
|
|
|
current_user
|
|
|
|
)
|
2021-04-25 00:43:28 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-29 00:02:18 +08:00
|
|
|
def update
|
|
|
|
validate_role_presence
|
|
|
|
return false unless valid?
|
2021-05-02 17:22:59 +08:00
|
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
2021-11-22 16:50:14 +08:00
|
|
|
@user_assignment = UserAssignment.find_by!(assignable: @project, user: @user)
|
|
|
|
@user_assignment.update!(user_role_id: user_role_id, assigned: :manually)
|
2021-05-02 17:22:59 +08:00
|
|
|
log_activity(:change_user_role_on_project)
|
2021-06-06 15:54:47 +08:00
|
|
|
|
|
|
|
UserAssignments::PropagateAssignmentJob.perform_later(
|
|
|
|
@project,
|
|
|
|
@user,
|
|
|
|
user_role,
|
|
|
|
current_user
|
|
|
|
)
|
2021-05-02 17:22:59 +08:00
|
|
|
end
|
2021-04-29 00:02:18 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
user_assignment = UserAssignment.find_by!(assignable: @project, user: @user)
|
2021-10-05 19:45:34 +08:00
|
|
|
user_project = UserProject.find_by(project: @project, user: @user)
|
2021-04-30 19:26:06 +08:00
|
|
|
return false if last_project_owner?
|
2021-05-02 17:22:59 +08:00
|
|
|
|
2021-04-29 00:02:18 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
2021-12-01 23:23:24 +08:00
|
|
|
# if project is public, the assignment
|
|
|
|
# will reset to the default public role
|
|
|
|
if @project.visible?
|
|
|
|
user_assignment.update!(
|
|
|
|
user_role: @project.default_public_user_role,
|
|
|
|
assigned: :automatically
|
|
|
|
)
|
|
|
|
else
|
|
|
|
user_assignment.destroy!
|
|
|
|
user_project&.destroy!
|
|
|
|
end
|
2021-06-06 15:54:47 +08:00
|
|
|
|
|
|
|
UserAssignments::PropagateAssignmentJob.perform_later(
|
|
|
|
@project,
|
|
|
|
@user,
|
|
|
|
user_role,
|
|
|
|
current_user,
|
|
|
|
destroy: true
|
|
|
|
)
|
2021-12-01 23:23:24 +08:00
|
|
|
|
|
|
|
log_activity(:unassign_user_from_project)
|
2021-04-29 00:02:18 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-25 00:43:28 +08:00
|
|
|
def assign=(value)
|
|
|
|
@assign = ActiveModel::Type::Boolean.new.cast(value)
|
|
|
|
end
|
|
|
|
|
2021-04-30 19:26:06 +08:00
|
|
|
def last_project_owner?
|
|
|
|
project_owners.count == 1 && user_role.owner?
|
|
|
|
end
|
|
|
|
|
2021-04-25 00:43:28 +08:00
|
|
|
private
|
|
|
|
|
2021-05-02 17:22:59 +08:00
|
|
|
def log_activity(type_of)
|
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: type_of,
|
|
|
|
owner: current_user,
|
|
|
|
subject: project,
|
|
|
|
team: project.team,
|
|
|
|
project: project,
|
|
|
|
message_items: { project: project.id,
|
|
|
|
user_target: user.id,
|
|
|
|
role: user_role.name })
|
|
|
|
end
|
|
|
|
|
2021-04-29 00:02:18 +08:00
|
|
|
def validate_role_presence
|
|
|
|
errors.add(:user_role_id, :not_found) if UserRole.find_by(id: user_role_id).nil?
|
2021-04-25 00:43:28 +08:00
|
|
|
end
|
|
|
|
|
2021-04-30 19:26:06 +08:00
|
|
|
def project_owners
|
|
|
|
@project_owners ||= @project.user_assignments
|
|
|
|
.includes(:user_role)
|
2021-09-15 20:39:47 +08:00
|
|
|
.where(user_roles: { name: I18n.t('user_roles.predefined.owner') })
|
2021-04-30 19:26:06 +08:00
|
|
|
end
|
2021-04-25 00:43:28 +08:00
|
|
|
end
|