Remove user_team model and related code [SCI-8815] (#5815)

This commit is contained in:
Soufiane 2023-07-20 10:57:29 +02:00 committed by GitHub
parent 8039908642
commit c12023f8aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 13 additions and 238 deletions

View file

@ -41,7 +41,8 @@ class GlobalActivitiesController < ApplicationController
@teams.order(name: :asc)
end
@activity_types = Activity.activity_types_list
@user_list = User.where(id: UserTeam.where(team: current_user.teams).select(:user_id))
@user_list = User.where(id: current_user.teams.joins(:users).group('users.id').select('users.id'))
.distinct
.order(full_name: :asc)
.pluck(:full_name, :id)

View file

@ -57,9 +57,7 @@ module Users
end
end
def show
@user_team = UserTeam.find_by(user: @user, team: @team)
end
def show; end
def users_datatable
render json: ::TeamUsersDatatable.new(view_context, @team, @user)

View file

@ -172,19 +172,6 @@ class Project < ApplicationRecord
ProjectComment.from(comments, :comments).order(created_at: :asc)
end
def unassigned_users
User.joins(:user_teams)
.joins(
"LEFT OUTER JOIN user_assignments ON user_assignments.user_id = users.id "\
"AND user_assignments.assignable_id = #{id} "\
"AND user_assignments.assignable_type = 'Project'"
)
.where(user_teams: { team_id: team_id })
.where(user_assignments: { id: nil })
.where.not(confirmed_at: nil)
.distinct
end
def user_role(user)
user_assignments.includes(:user_role).references(:user_role).find_by(user: user)&.user_role&.name
end

View file

@ -31,7 +31,6 @@ class Team < ApplicationRecord
foreign_key: 'last_modified_by_id',
class_name: 'User',
optional: true
has_many :user_teams, inverse_of: :team, dependent: :destroy
has_many :users, through: :user_assignments
has_many :projects, inverse_of: :team
has_many :project_folders, inverse_of: :team, dependent: :destroy
@ -171,8 +170,8 @@ class Team < ApplicationRecord
def self.global_activity_filter(filters, search_query)
query = where('name ILIKE ?', "%#{search_query}%")
if filters[:users]
users_team = User.where(id: filters[:users]).joins(:user_teams).group(:team_id).pluck(:team_id)
query = query.where(id: users_team)
user_teams = User.where(id: filters[:users]).joins(:teams).group(:team_id).select(:team_id)
query = query.where(id: user_teams)
end
query = query.where(id: team_by_subject(filters[:subjects])) if filters[:subjects]
query.select(:id, :name).map { |i| { value: i[:id], label: ApplicationController.helpers.escape_input(i[:name]) } }

View file

@ -58,7 +58,6 @@ class User < ApplicationRecord
# Relations
has_many :user_identities, inverse_of: :user
has_many :user_teams, inverse_of: :user
has_many :user_assignments, dependent: :destroy
has_many :user_projects, inverse_of: :user
has_many :teams, through: :user_assignments, source: :assignable, source_type: 'Team'
@ -166,9 +165,6 @@ class User < ApplicationRecord
has_many :assigned_user_my_modules,
class_name: 'UserMyModule',
foreign_key: 'assigned_by_id'
has_many :assigned_user_teams,
class_name: 'UserTeam',
foreign_key: 'assigned_by_id'
has_many :assigned_user_projects,
class_name: 'UserProject',
foreign_key: 'assigned_by_id'
@ -313,7 +309,6 @@ class User < ApplicationRecord
has_many :user_system_notifications, dependent: :destroy
has_many :system_notifications, through: :user_system_notifications
has_many :zip_exports, inverse_of: :user, dependent: :destroy
has_many :datatables_teams, class_name: '::Views::Datatables::DatatablesTeam'
has_many :view_states, dependent: :destroy
has_many :access_grants, class_name: 'Doorkeeper::AccessGrant',
@ -436,46 +431,6 @@ class User < ApplicationRecord
end
end
def projects_by_teams(team_id = 0, sort_by = nil, archived = false)
archived = archived ? true : false
query = Project.all.joins(:user_projects)
sql = 'projects.team_id IN (SELECT DISTINCT team_id ' \
'FROM user_teams WHERE user_teams.user_id = :user_id)'
if team_id.zero? || !user_teams.find_by(team_id: team_id).try(:admin?)
# Admins see all projects of team
sql += ' AND (projects.visibility=1 OR user_projects.user_id=:user_id)'
end
sql += ' AND projects.archived = :archived '
sort =
case sort_by
when 'old'
{ created_at: :asc }
when 'atoz'
{ name: :asc }
when 'ztoa'
{ name: :desc }
else
{ created_at: :desc }
end
if team_id > 0
result = query
.where('projects.team_id = ?', team_id)
.where(sql, user_id: id, archived: archived)
.order(sort)
.distinct
.group_by(&:team)
else
result = query
.where(sql, user_id: id, archived: archived)
.order(sort)
.distinct
.group_by(&:team)
end
result || []
end
# Finds all activities of user that is assigned to project. If user
# is not an owner of the project, user must be also assigned to
# module.

View file

@ -1,67 +0,0 @@
# frozen_string_literal: true
class UserTeam < ApplicationRecord
enum role: { guest: 0, normal_user: 1, admin: 2 }
validates :role, :user, :team, presence: true
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
after_create :assign_user_to_visible_projects
before_destroy :destroy_associations
def role_str
I18n.t("user_teams.enums.role.#{role}")
end
def destroy_associations
# Destroy the user from all team's projects
user.user_projects.joins(:project).where(project: team.projects).destroy_all
# destroy all assignments
UserAssignments::RemoveUserAssignmentJob.perform_now(user, team)
end
# returns user_teams where the user is in team
def self.user_in_team(user, team)
where(user: user, team: team)
end
def destroy(new_owner)
return super() unless new_owner
# Also, make new owner author of all protocols that belong
# to the departing user and belong to this team.
p_ids = user.added_protocols.where(team: team).pluck(:id)
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
# Make new owner author of all inventory items that were added
# by departing user and belong to this team.
RepositoryRow.change_owner(team, user, new_owner)
super()
end
private
def assign_user_to_visible_projects
team.projects.visible.each do |project|
UserAssignments::ProjectGroupAssignmentJob.perform_later(
team,
project,
assigned_by
)
end
end
end

View file

@ -86,7 +86,7 @@ class ProjectsOverviewService
def fetch_project_records
@team.projects
.includes(user_assignments: %i(user user_role), team: :user_teams)
.includes(:team, user_assignments: %i(user user_role))
.includes(:project_comments, experiments: { my_modules: { my_module_status: :my_module_status_implications } })
.visible_to(@user, @team)
.left_outer_joins(:project_comments)
@ -97,7 +97,7 @@ class ProjectsOverviewService
def fetch_project_folder_records
project_folders = @team.project_folders
.includes(team: :user_teams)
.includes(:team)
.joins('LEFT OUTER JOIN project_folders child_folders
ON child_folders.parent_folder_id = project_folders.id')
.left_outer_joins(:projects)

View file

@ -74,7 +74,6 @@ class UserDataDeletion
destroy_protocol(protocol)
end
team.protocol_keywords.destroy_all
team.user_teams.delete_all
User.where(current_team_id: team).each do |user|
user.update(current_team_id: nil)
end

View file

@ -1,44 +0,0 @@
class MigrateOrganizationsStructure < ActiveRecord::Migration[4.2]
def up
# Update estimated size of all assets
Asset.includes(:asset_text_datum).find_each do |asset|
asset.update_estimated_size
asset.update(file_present: true)
end
# Calculate teams' taken space
Team.find_each do |team|
team.calculate_space_taken
team.save
end
# Finally, the trickiest task: Re-define teams
demo_team = Team.find_by(name: "Demo team")
if demo_team
demo_team.user_teams.each do |uo|
uo.destroy
end
end
Team.find_each do |team|
user = team.users.first
team.update(created_by: user)
end
UserTeam.find_each do |uo|
uo.update(role: 2)
end
end
def down
# We cannot re-assign users to demo team or re-update
# their previous user-team roles
# But we can remove created_by field from teams
Team.find_each do |team|
team.update(created_by: nil)
end
# Resetting calculated assets & teams' space
# to 0 doesn't make much sense even when downgrading migration
end
end

View file

@ -71,10 +71,6 @@ class AddTeamLevelPermissions < ActiveRecord::Migration[6.1]
@normal_user_role.save(validate: false)
@viewer_role.permissions = @viewer_role.permissions | VIEWER_PERMISSIONS
@viewer_role.save(validate: false)
create_user_assignments(UserTeam.admin, @owner_role)
create_user_assignments(UserTeam.normal_user, @normal_user_role)
create_user_assignments(UserTeam.guest, @viewer_role)
end
dir.down do
@ -104,31 +100,4 @@ class AddTeamLevelPermissions < ActiveRecord::Migration[6.1]
user_role: user_role
)
end
def create_user_assignments(user_teams, user_role)
user_teams.includes(:user, team: %i(reports repositories repository_protocols))
.find_in_batches(batch_size: 100) do |user_team_batch|
user_assignments = []
user_team_batch.each do |user_team|
team_user_assignment = new_user_assignment(user_team.user, user_team.team, user_role, :manually)
team_user_assignment.assign_attributes(created_at: user_team.created_at,
updated_at: user_team.updated_at)
user_assignments << team_user_assignment
user_team.team.repositories.each do |repository|
user_assignments << new_user_assignment(user_team.user, repository, user_role, :automatically)
end
user_team.team.repository_protocols.each do |protocol|
if user_team.user_id == protocol.added_by_id
user_assignments << new_user_assignment(user_team.user, protocol, @owner_role, :automatically)
elsif (protocol.in_repository_archived? && protocol.published_on.present?) || protocol.in_repository_public?
user_assignments << new_user_assignment(user_team.user, protocol, @viewer_role, :automatically)
end
end
user_team.team.reports.each do |report|
user_assignments << new_user_assignment(user_team.user, report, user_role, :automatically)
end
end
UserAssignment.import(user_assignments)
end
end
end

View file

@ -0,0 +1,5 @@
class DropDatatablesTeamsView < ActiveRecord::Migration[7.0]
def change
drop_view :datatables_teams
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2023_06_16_140951) do
ActiveRecord::Schema[7.0].define(version: 2023_07_20_070830) do
# These are extensions that must be enabled in order to support this database
enable_extension "btree_gist"
enable_extension "pg_trgm"
@ -106,15 +106,6 @@ ActiveRecord::Schema[7.0].define(version: 2023_06_16_140951) do
t.index ["team_id"], name: "index_assets_on_team_id"
end
create_table "bmt_filters", force: :cascade do |t|
t.string "name", null: false
t.json "filters", null: false
t.bigint "created_by_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["created_by_id"], name: "index_bmt_filters_on_created_by_id"
end
create_table "checklist_items", force: :cascade do |t|
t.string "text", null: false
t.boolean "checked", default: false, null: false
@ -1315,7 +1306,6 @@ ActiveRecord::Schema[7.0].define(version: 2023_06_16_140951) do
add_foreign_key "asset_text_data", "assets"
add_foreign_key "assets", "users", column: "created_by_id"
add_foreign_key "assets", "users", column: "last_modified_by_id"
add_foreign_key "bmt_filters", "users", column: "created_by_id"
add_foreign_key "checklist_items", "checklists"
add_foreign_key "checklist_items", "users", column: "created_by_id"
add_foreign_key "checklist_items", "users", column: "last_modified_by_id"
@ -1491,21 +1481,4 @@ ActiveRecord::Schema[7.0].define(version: 2023_06_16_140951) do
add_foreign_key "wopi_actions", "wopi_apps"
add_foreign_key "wopi_apps", "wopi_discoveries"
add_foreign_key "zip_exports", "users"
create_view "datatables_teams", sql_definition: <<-SQL
SELECT teams.id,
teams.name,
user_teams.role,
( SELECT count(*) AS count
FROM user_teams user_teams_1
WHERE (user_teams_1.team_id = teams.id)) AS members,
CASE
WHEN (teams.created_by_id = user_teams.user_id) THEN false
ELSE true
END AS can_be_left,
user_teams.id AS user_team_id,
user_teams.user_id
FROM (teams
JOIN user_teams ON ((teams.id = user_teams.team_id)));
SQL
end