2019-05-08 21:28:07 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-23 21:19:08 +08:00
|
|
|
class UserTeam < ApplicationRecord
|
2016-02-12 23:52:43 +08:00
|
|
|
enum role: { guest: 0, normal_user: 1, admin: 2 }
|
|
|
|
|
2019-05-08 21:28:07 +08:00
|
|
|
validates :role, :user, :team, presence: true
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2019-05-08 21:28:07 +08:00
|
|
|
belongs_to :user, inverse_of: :user_teams, touch: true
|
|
|
|
belongs_to :assigned_by, foreign_key: 'assigned_by_id', class_name: 'User', optional: true
|
|
|
|
belongs_to :team, inverse_of: :user_teams
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2021-06-27 19:22:19 +08:00
|
|
|
after_create :assign_user_to_visible_projects
|
2021-09-15 20:39:47 +08:00
|
|
|
before_destroy :destroy_associations
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
def role_str
|
2017-01-31 20:33:55 +08:00
|
|
|
I18n.t("user_teams.enums.role.#{role}")
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2016-12-07 21:14:32 +08:00
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
def destroy_associations
|
2017-01-24 23:34:21 +08:00
|
|
|
# Destroy the user from all team's projects
|
|
|
|
team.projects.each do |project|
|
2016-02-12 23:52:43 +08:00
|
|
|
up2 = (project.user_projects.select { |up| up.user == self.user }).first
|
|
|
|
if up2.present?
|
|
|
|
up2.destroy
|
|
|
|
end
|
|
|
|
end
|
2021-06-27 19:22:19 +08:00
|
|
|
# destroy all assignments
|
2021-11-08 23:06:28 +08:00
|
|
|
UserAssignments::RemoveUserAssignmentJob.perform_now(user, team)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2017-01-24 23:34:21 +08:00
|
|
|
# returns user_teams where the user is in team
|
|
|
|
def self.user_in_team(user, team)
|
|
|
|
where(user: user, team: team)
|
2017-01-23 17:53:42 +08:00
|
|
|
end
|
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
def destroy(new_owner)
|
2019-05-07 22:02:45 +08:00
|
|
|
return super() unless new_owner
|
2016-07-21 19:11:15 +08:00
|
|
|
|
|
|
|
# Also, make new owner author of all protocols that belong
|
2017-06-26 21:30:18 +08:00
|
|
|
# to the departing user and belong to this team.
|
|
|
|
p_ids = user.added_protocols.where(team: team).pluck(:id)
|
2016-07-21 19:11:15 +08:00
|
|
|
Protocol.find(p_ids).each do |protocol|
|
|
|
|
protocol.record_timestamps = false
|
|
|
|
protocol.added_by = new_owner
|
|
|
|
if protocol.archived_by != nil
|
|
|
|
protocol.archived_by = new_owner
|
|
|
|
end
|
|
|
|
if protocol.restored_by != nil
|
|
|
|
protocol.restored_by = new_owner
|
|
|
|
end
|
|
|
|
protocol.save
|
|
|
|
end
|
|
|
|
|
2019-04-26 16:47:08 +08:00
|
|
|
# Make new owner author of all inventory items that were added
|
|
|
|
# by departing user and belong to this team.
|
2019-04-29 20:02:02 +08:00
|
|
|
RepositoryRow.change_owner(team, user, new_owner)
|
2019-04-26 16:47:08 +08:00
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
super()
|
|
|
|
end
|
|
|
|
|
2021-06-27 19:22:19 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def assign_user_to_visible_projects
|
|
|
|
team.projects.visible.each do |project|
|
|
|
|
UserAssignments::GroupAssignmentJob.perform_later(
|
|
|
|
team,
|
|
|
|
project,
|
|
|
|
assigned_by
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|