diff --git a/app/jobs/user_assignments/generate_user_assignments_job.rb b/app/jobs/user_assignments/generate_user_assignments_job.rb index 37c4b72ca..97e4ac00f 100644 --- a/app/jobs/user_assignments/generate_user_assignments_job.rb +++ b/app/jobs/user_assignments/generate_user_assignments_job.rb @@ -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| diff --git a/app/models/concerns/assignable.rb b/app/models/concerns/assignable.rb index d58d41c6f..cbede314d 100644 --- a/app/models/concerns/assignable.rb +++ b/app/models/concerns/assignable.rb @@ -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 + UserAssignments::GenerateUserAssignmentsJob.perform_later(self, created_by) end end end diff --git a/db/migrate/20210222123823_migrate_to_new_user_roles.rb b/db/migrate/20210222123823_migrate_to_new_user_roles.rb index 00153f519..cef8e5d9f 100644 --- a/db/migrate/20210222123823_migrate_to_new_user_roles.rb +++ b/db/migrate/20210222123823_migrate_to_new_user_roles.rb @@ -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