Setup user assignments relations for team, inventory and protocol [SCI-6818]

This commit is contained in:
Oleksii Kriuchykhin 2022-05-13 15:45:24 +02:00
parent 5c3ac0b33e
commit 03ec4cb319
13 changed files with 24 additions and 44 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -162,9 +162,7 @@ module ApplicationHelper
</div></div><div class='row'><div class='col-xs-12'>
<p class='silver'>#{user.email}</p>)
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 += %(<p>
#{I18n.t('atwho.users.popover_html',
role: user_t.role.capitalize,

View file

@ -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

View file

@ -4,7 +4,6 @@ class Project < ApplicationRecord
include SearchableByNameModel
include ViewableModel
include PermissionCheckableModel
include PermissionExtends
include Assignable
enum visibility: { hidden: 0, visible: 1 }

View file

@ -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

View file

@ -4,6 +4,8 @@ class Protocol < ApplicationRecord
include SearchableModel
include RenamingUtil
include SearchableByNameModel
include Assignable
include PermissionCheckableModel
include TinyMceImages
after_save :update_linked_children

View file

@ -3,6 +3,8 @@
class Repository < RepositoryBase
include SearchableModel
include SearchableByNameModel
include Assignable
include PermissionCheckableModel
include RepositoryImportParser
include ArchivableModel

View file

@ -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

View file

@ -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'

View file

@ -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}"

View file

@ -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