Automatically assign team admins as project owners [SCI_6119] (#3575)

This commit is contained in:
artoscinote 2021-10-04 15:47:28 +02:00 committed by GitHub
parent d594778fa8
commit a18839f529
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 13 deletions

View file

@ -12,12 +12,30 @@ module UserAssignments
assign_users_to_experiment(object)
when MyModule
assign_users_to_my_module(object)
when Project
assign_team_admins_to_project(object)
end
end
end
private
def assign_team_admins_to_project(project)
# TEMPORARY UNTIL WE ADD TEAM USER ASSIGNMENTS
# Assigns all team admins as owners of project
User.joins(:user_teams).where(
user_teams: { role: UserTeam.roles[:admin], team: project.team }
).find_each do |user|
UserAssignment.find_or_create_by!(
user: user,
assignable: project,
assigned: :manually, # we set this to manually since was the user action to create the item
user_role: UserRole.find_by(name: I18n.t('user_roles.predefined.owner'))
)
end
end
def assign_users_to_experiment(experiment)
project = experiment.project
project.user_assignments.find_each do |user_assignment|

View file

@ -30,10 +30,7 @@ module Assignable
user_role: UserRole.find_by(name: I18n.t('user_roles.predefined.owner'))
)
# project is top level, so we do not need to create any more assignments for it
unless self.class.instance_of?(Project)
UserAssignments::GenerateUserAssignmentsJob.perform_later(self, created_by)
end
end
end
end

View file

@ -45,21 +45,44 @@ class MigrateToNewUserRoles < ActiveRecord::Migration[6.1]
private
def new_user_assignment(user, assignable, user_role)
UserAssignment.new(
user: user,
assignable: assignable,
assigned: :automatically,
user_role: user_role
)
end
def create_team_admin_assignments
user_assignments = []
owner_role = UserRole.find_by(name: I18n.t('user_roles.predefined.owner'))
admin_user_teams =
UserTeam.includes(:user, :team).where({ role: UserTeam.roles[:admin] })
admin_user_teams.find_each do |user_team|
user_team.team.projects.find_each do |project|
user_assignments << new_user_assignment(user_team.user, project, owner_role)
project.experiments.find_each do |experiment|
user_assignments << new_user_assignment(user_team.user, experiment, owner_role)
experiment.my_modules.find_each do |my_module|
user_assignments << new_user_assignment(user_team.user, my_module, owner_role)
end
end
end
end
UserAssignment.import(user_assignments)
end
def create_user_assignments(user_projects, user_role)
user_projects.includes(:user, :project).find_in_batches(batch_size: 100) do |user_project_batch|
user_assignments = []
user_project_batch.each do |user_project|
user_assignments << UserAssignment.new(user: user_project.user,
assignable: user_project.project,
user_role: user_role)
user_assignments << new_user_assignment(user_project.user, user_project.project, user_role)
user_project.project.experiments.each do |experiment|
user_assignments << UserAssignment.new(user: user_project.user,
assignable: experiment,
user_role: user_role)
user_assignments << new_user_assignment(user_project.user, experiment, user_role)
experiment.my_modules.each do |my_module|
user_assignments << UserAssignment.new(user: user_project.user,
assignable: my_module,
user_role: user_role)
user_assignments << new_user_assignment(user_project.user, my_module, user_role)
end
end
end