From 03ec4cb319294cbc7b7ca961858083c09e4299a9 Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Fri, 13 May 2022 15:45:24 +0200 Subject: [PATCH] Setup user assignments relations for team, inventory and protocol [SCI-6818] --- app/controllers/api/v1/users_controller.rb | 6 +++--- app/controllers/global_activities_controller.rb | 2 +- .../users/settings/teams_controller.rb | 15 +-------------- app/helpers/application_helper.rb | 4 +--- app/models/concerns/assignable.rb | 5 ++--- app/models/project.rb | 1 - app/models/project_folder.rb | 4 ++-- app/models/protocol.rb | 2 ++ app/models/repository.rb | 2 ++ app/models/team.rb | 4 +++- app/models/user.rb | 2 +- app/utilities/users_generator.rb | 13 +++---------- lib/tasks/data.rake | 8 +++----- 13 files changed, 24 insertions(+), 44 deletions(-) diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index 0952bdb63..1f0836d97 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -12,9 +12,9 @@ module Api private def load_user - @user = User.joins(:user_teams) - .where('user_teams.team': current_user.teams) - .find_by_id(params[:id]) + @user = User.joins(:teams) + .where(user_assignments: { assignable: current_user.teams }) + .find_by(id: params[:id]) raise PermissionError.new(User, :read) unless @user end end diff --git a/app/controllers/global_activities_controller.rb b/app/controllers/global_activities_controller.rb index dfe5b5adf..363506027 100644 --- a/app/controllers/global_activities_controller.rb +++ b/app/controllers/global_activities_controller.rb @@ -133,7 +133,7 @@ class GlobalActivitiesController < ApplicationController end filter_teams = if subject_search_params[:users].present? - User.where(id: subject_search_params[:users]).joins(:user_teams).group(:team_id).pluck(:team_id) + User.where(id: subject_search_params[:users]).joins(:teams).group(:team_id).pluck(:team_id) elsif subject_search_params[:teams].present? subject_search_params[:teams] else diff --git a/app/controllers/users/settings/teams_controller.rb b/app/controllers/users/settings/teams_controller.rb index fc46e4906..32c7eee73 100644 --- a/app/controllers/users/settings/teams_controller.rb +++ b/app/controllers/users/settings/teams_controller.rb @@ -30,12 +30,7 @@ module Users layout 'fluid' def index - @user_teams = - @user - .user_teams - .includes(team: :users) - .order(created_at: :asc) - @member_of = @user_teams.count + @member_of = @user.teams.count end def datatable @@ -55,14 +50,6 @@ module Users @new_team.created_by = @user if @new_team.save - # Okay, team is created, now - # add the current user as admin - UserTeam.create( - user: @user, - team: @new_team, - role: 2 - ) - # Redirect to new team page redirect_to team_path(@new_team) else diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index fd6164783..3791d4b6f 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -162,9 +162,7 @@ module ApplicationHelper

#{user.email}

) if user_still_in_team - user_t = user.user_teams - .where('user_teams.team_id = ?', team) - .first + user_t = user.teams.find_by(id: team) user_description += %(

#{I18n.t('atwho.users.popover_html', role: user_t.role.capitalize, diff --git a/app/models/concerns/assignable.rb b/app/models/concerns/assignable.rb index 2b92a2ba9..ab65aa0fd 100644 --- a/app/models/concerns/assignable.rb +++ b/app/models/concerns/assignable.rb @@ -42,7 +42,8 @@ module Assignable def create_users_assignments return if skip_user_assignments - role = if is_a?(Project) + + role = if is_a?(Project) || is_a?(Team) UserRole.find_by(name: I18n.t('user_roles.predefined.owner')) else permission_parent.user_assignments.find_by(user: created_by).user_role @@ -54,8 +55,6 @@ module Assignable assigned: is_a?(Project) ? :manually : :automatically, user_role: role ) - - UserAssignments::GenerateUserAssignmentsJob.perform_later(self, created_by) end end end diff --git a/app/models/project.rb b/app/models/project.rb index 2df597e8c..cad254213 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -4,7 +4,6 @@ class Project < ApplicationRecord include SearchableByNameModel include ViewableModel include PermissionCheckableModel - include PermissionExtends include Assignable enum visibility: { hidden: 0, visible: 1 } diff --git a/app/models/project_folder.rb b/app/models/project_folder.rb index f529f7c0e..f9b8bf324 100644 --- a/app/models/project_folder.rb +++ b/app/models/project_folder.rb @@ -34,8 +34,8 @@ class ProjectFolder < ApplicationRecord new_query = if current_team current_team.project_folders.where_attributes_like(:name, query, options) else - distinct.joins(team: :user_teams) - .where(teams: { user_teams: { user: user } }) + distinct.joins(team: :users) + .where(teams: { user_assignments: { user: user } }) .where_attributes_like('project_folders.name', query, options) end diff --git a/app/models/protocol.rb b/app/models/protocol.rb index 3e7d4905e..df3db09de 100644 --- a/app/models/protocol.rb +++ b/app/models/protocol.rb @@ -4,6 +4,8 @@ class Protocol < ApplicationRecord include SearchableModel include RenamingUtil include SearchableByNameModel + include Assignable + include PermissionCheckableModel include TinyMceImages after_save :update_linked_children diff --git a/app/models/repository.rb b/app/models/repository.rb index 0add8c20c..3131e68cb 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -3,6 +3,8 @@ class Repository < RepositoryBase include SearchableModel include SearchableByNameModel + include Assignable + include PermissionCheckableModel include RepositoryImportParser include ArchivableModel diff --git a/app/models/team.rb b/app/models/team.rb index caeaaaa67..da0722b96 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -3,6 +3,8 @@ class Team < ApplicationRecord include SearchableModel include ViewableModel + include Assignable + include PermissionCheckableModel include TeamBySubjectModel include TinyMceImages @@ -29,7 +31,7 @@ class Team < ApplicationRecord class_name: 'User', optional: true has_many :user_teams, inverse_of: :team, dependent: :destroy - has_many :users, through: :user_teams + has_many :users, through: :user_assignments has_many :projects, inverse_of: :team has_many :project_folders, inverse_of: :team, dependent: :destroy has_many :protocols, inverse_of: :team, dependent: :destroy diff --git a/app/models/user.rb b/app/models/user.rb index 12be7646c..2e3734391 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -60,9 +60,9 @@ class User < ApplicationRecord # Relations has_many :user_identities, inverse_of: :user has_many :user_teams, inverse_of: :user - has_many :teams, through: :user_teams has_many :user_assignments, dependent: :destroy has_many :user_projects, inverse_of: :user + has_many :teams, through: :user_assignments, source: :assignable, source_type: 'Team' has_many :projects, through: :user_assignments, source: :assignable, source_type: 'Project' has_many :user_my_modules, inverse_of: :user has_many :my_modules, through: :user_assignments, source: :assignable, source_type: 'MyModule' diff --git a/app/utilities/users_generator.rb b/app/utilities/users_generator.rb index fba6facb9..6362af6f6 100644 --- a/app/utilities/users_generator.rb +++ b/app/utilities/users_generator.rb @@ -38,14 +38,12 @@ module UsersGenerator # needs to be sent with his/her password & email? # Create user's own team of needed - if private_team_name.present? - create_private_user_team(nu, private_team_name) - end + Team.create!(name: private_team_name, created_by: nu) if private_team_name.present? # Assign user to additional teams team_ids.each do |team_id| - team = Team.find_by_id(team_id) - UserTeam.create(user: nu, team: team, role: :admin) if team.present? + team = Team.find(team_id) + UserAssignment.create!(user: nu, assignable: team, user_role: UserRole.owner_role) if team.present? end # Assign user team as user current team @@ -56,11 +54,6 @@ module UsersGenerator nu end - def create_private_user_team(user, private_team_name) - no = Team.create(name: private_team_name, created_by: user) - UserTeam.create(user: user, team: no, role: :admin) - end - def print_user(user, password) puts "USER ##{user.id}" puts " Full name: #{user.full_name}" diff --git a/lib/tasks/data.rake b/lib/tasks/data.rake index 44e9131f9..f075ff335 100644 --- a/lib/tasks/data.rake +++ b/lib/tasks/data.rake @@ -24,16 +24,14 @@ namespace :data do User.transaction do begin - # Destroy user_team, and possibly team + # Destroy user team assignments, and possibly team if user.teams.count > 0 oids = user.teams.pluck(:id) oids.each do |oid| team = Team.find(oid) - user_team = user.user_teams.where(team: team).first + team_assignment = user.user_assignments.where(assignable: team).take destroy_team = (team.users.count == 1 && team.created_by == user) - if !user_team.destroy(nil) then - raise Exception - end + team_assignment.destroy! team.destroy! if destroy_team end end