use [IAM_NAME]
go
declare @target_tenant_id int = 1
declare @users_to_move table (
tenant_id tenant_id,
usr_id usr_id
)
insert into @users_to_move (tenant_id, usr_id)
select tenant_id, usr_id
from usr
where usr_id in (
'',
'',
'',
''
)
--Select users to move
select * from @users_to_move
/**********/
/* CODE */
/**********/
declare @user_groups_to_move table (
tenant_id tenant_id,
usr_grp_id usr_grp_id
)
insert into @user_groups_to_move (tenant_id, usr_grp_id)
select u.tenant_id, u.usr_grp_id
from usr_grp_usr u
join @users_to_move m
on m.tenant_id = u.tenant_id
and m.usr_id = u.usr_id
group by u.tenant_id, u.usr_grp_id
--User groups to move
select * from @user_groups_to_move
--Copy user groups to target tenant
declare @include_authorization bit = 1,
@include_usr_grp_pref bit = 1
begin try
--Copy the main table
insert into usr_grp
(
tenant_id,
usr_grp_id,
usr_grp_type,
usr_grp_description,
usr_grp_pref_priority,
usr_grp_resource_claim_priority,
allow_inactive_gui_appls,
active_directory_domain_name,
active_directory_grp_name,
net_bios_domain_name,
active_directory_updated_on,
insert_user,
insert_date_time,
update_user,
update_date_time
)
select
@target_tenant_id,
u.usr_grp_id,
t1.usr_grp_type,
t1.usr_grp_description,
t1.usr_grp_pref_priority,
t1.usr_grp_resource_claim_priority,
t1.allow_inactive_gui_appls,
t1.active_directory_domain_name,
t1.active_directory_grp_name,
t1.net_bios_domain_name,
t1.active_directory_updated_on,
dbo.tsf_user(),
sysdatetime(),
dbo.tsf_user(),
sysdatetime()
from usr_grp t1
join @user_groups_to_move u
on u.tenant_id = t1.tenant_id
and u.usr_grp_id = t1.usr_grp_id
where not exists (select 1 from usr_grp t2 where t2.tenant_id = @target_tenant_id and t2.usr_grp_id = u.usr_grp_id)
--Copy authorization
if @include_authorization = 1
begin
insert into gui_appl_authorization
(
tenant_id,
usr_grp_id,
project_id,
project_vrs_id,
gui_appl_id,
role_id,
insert_user,
insert_date_time,
update_user,
update_date_time
)
select
@target_tenant_id,
u.usr_grp_id,
t2.project_id,
t2.project_vrs_id,
t2.gui_appl_id,
t2.role_id,
dbo.tsf_user(),
sysdatetime(),
dbo.tsf_user(),
sysdatetime()
from gui_appl_authorization t2
join usr_grp t1
on t1.tenant_id = t2.tenant_id
and t1.usr_grp_id = t2.usr_grp_id
join @user_groups_to_move u
on u.tenant_id = t1.tenant_id
and u.usr_grp_id = t1.usr_grp_id
where not exists (select 1 from gui_appl_authorization t3 where t3.tenant_id = @target_tenant_id and t3.usr_grp_id = u.usr_grp_id and t3.project_id = t2.project_id and t3.project_vrs_id = t2.project_vrs_id and t3.gui_appl_id = t2.gui_appl_id and t3.role_id = t2.role_id)
end
if @include_usr_grp_pref = 1
begin
--Copy user group preferences
insert into usr_grp_pref_gui_appl
(
tenant_id,
usr_grp_id,
project_id,
project_vrs_id,
gui_appl_id,
load_on_start_up,
order_no,
insert_user,
insert_date_time,
update_user,
update_date_time
)
select
@target_tenant_id,
u.usr_grp_id,
t2.project_id,
t2.project_vrs_id,
t2.gui_appl_id,
t2.load_on_start_up,
t2.order_no,
dbo.tsf_user(),
sysdatetime(),
dbo.tsf_user(),
sysdatetime()
from usr_grp_pref_gui_appl t2
join usr_grp t1
on t1.tenant_id = t2.tenant_id
and t1.usr_grp_id = t2.usr_grp_id
join @user_groups_to_move u
on u.tenant_id = t1.tenant_id
and u.usr_grp_id = t1.usr_grp_id
insert into usr_grp_pref_platform
(
tenant_id,
usr_grp_id,
project_id,
project_vrs_id,
gui_appl_id,
platform,
theme_id,
default_menu_id,
insert_user,
insert_date_time,
update_user,
update_date_time
)
select
@target_tenant_id,
u.usr_grp_id,
t2.project_id,
t2.project_vrs_id,
t2.gui_appl_id,
t2.platform,
t2.theme_id,
t2.default_menu_id,
dbo.tsf_user(),
sysdatetime(),
dbo.tsf_user(),
sysdatetime()
from usr_grp_pref_platform t2
join usr_grp t1
on t1.tenant_id = t2.tenant_id
and t1.usr_grp_id = t2.usr_grp_id
join @user_groups_to_move u
on u.tenant_id = t1.tenant_id
and u.usr_grp_id = t1.usr_grp_id
insert into usr_grp_pref_start_object
(
tenant_id,
usr_grp_id,
project_id,
project_vrs_id,
gui_appl_id,
start_object_id,
start_object_type,
tab_id,
tab_variant_id,
task_id,
task_variant_id,
report_id,
report_variant_id,
process_id,
order_no,
abs_order_no,
insert_user,
insert_date_time,
update_user,
update_date_time
)
select
@target_tenant_id,
u.usr_grp_id,
t2.project_id,
t2.project_vrs_id,
t2.gui_appl_id,
t2.start_object_id,
t2.start_object_type,
t2.tab_id,
t2.tab_variant_id,
t2.task_id,
t2.task_variant_id,
t2.report_id,
t2.report_variant_id,
t2.process_id,
t2.order_no,
t2.abs_order_no,
dbo.tsf_user(),
sysdatetime(),
dbo.tsf_user(),
sysdatetime()
from usr_grp_pref_start_object t2
join usr_grp t1
on t1.tenant_id = t2.tenant_id
and t1.usr_grp_id = t2.usr_grp_id
join @user_groups_to_move u
on u.tenant_id = t1.tenant_id
and u.usr_grp_id = t1.usr_grp_id
insert into usr_grp_pref_tab_prefilter_status
(
tenant_id,
usr_grp_id,
project_id,
project_vrs_id,
gui_appl_id,
type_of_path,
path,
tab_id,
tab_variant_id,
tab_prefilter_id,
default_prefilter,
insert_user,
insert_date_time,
update_user,
update_date_time
)
select
@target_tenant_id,
u.usr_grp_id,
t2.project_id,
t2.project_vrs_id,
t2.gui_appl_id,
t2.type_of_path,
t2.path,
t2.tab_id,
t2.tab_variant_id,
t2.tab_prefilter_id,
t2.default_prefilter,
dbo.tsf_user(),
sysdatetime(),
dbo.tsf_user(),
sysdatetime()
from usr_grp_pref_tab_prefilter_status t2
join usr_grp t1
on t1.tenant_id = t2.tenant_id
and t1.usr_grp_id = t2.usr_grp_id
join @user_groups_to_move u
on u.tenant_id = t1.tenant_id
and u.usr_grp_id = t1.usr_grp_id
end
end try
begin catch
throw;
end catch
--Declare temp name for user
declare @temp_usr_id guid = newid(),
@to_first_name name,
@to_sur_name name,
@to_email email_address,
@usr_id usr_id,
@from_tenant_id int
declare cursor_move_usr cursor for
select tenant_id, usr_id
from @users_to_move
open cursor_move_usr;
fetch next from cursor_move_usr into @from_tenant_id, @usr_id
while @@fetch_status = 0
begin
--Get user info
select @to_first_name = u.first_name,
@to_sur_name = u.sur_name,
@to_email = u.email,
@temp_usr_id = newid() --Refresh temp username
from usr_general u
where u.tenant_id = @from_tenant_id
and u.usr_id = @usr_id
begin try
begin transaction
--Update email of the user to null to prevent error on duplicate email
update usr_general
set email = null
where tenant_id = @from_tenant_id
and usr_id = @usr_id
--Make a copy of the user, name it GUID
exec task_copy_usr_general @from_tenant_id, @usr_id, @target_tenant_id, @temp_usr_id, @to_first_name, @to_sur_name, @to_email
--Delete the original user
exec task_delete_usr_general @from_tenant_id, @usr_id
--Rename the GUID user back to original user
exec task_rename_usr_general @target_tenant_id, @temp_usr_id, @usr_id, @to_first_name, @to_sur_name
commit transaction;
end try
begin catch
rollback transaction;
declare @msg nvarchar(500)
select @msg = '<text>'+@usr_id+'</text><text>'+tenant_name+'</text>'
from tenant
where tenant_id = @target_tenant_id
exec dbo.tsf_send_message 'move_usr_to_tenant_failed', @msg, 1
return;
end catch
fetch next from cursor_move_usr into @from_tenant_id, @usr_id;
end;
close cursor_move_usr;
deallocate cursor_move_usr;
select u.tenant_id as from_tenant, @target_tenant_id as target_tenant, u.usr_id, uu.usr_grp_id
from @users_to_move u
join usr_grp_usr uu
on uu.tenant_id = @target_tenant_id
and uu.usr_id = u.usr_id