1
2use [IAM_NAME]
3go
4
5declare @target_tenant_id int = 1
6
7
8declare @users_to_move table (
9 tenant_id tenant_id,
10 usr_id usr_id
11)
12insert into @users_to_move (tenant_id, usr_id)
13select tenant_id, usr_id
14from usr
15where usr_id in (
16 '',
17 '',
18 '',
19 ''
20 )
21
22
23select * from @users_to_move
24
25
26
27
28
29
30
31
32
33
34declare @user_groups_to_move table (
35 tenant_id tenant_id,
36 usr_grp_id usr_grp_id
37)
38insert into @user_groups_to_move (tenant_id, usr_grp_id)
39select u.tenant_id, u.usr_grp_id
40from usr_grp_usr u
41join @users_to_move m
42 on m.tenant_id = u.tenant_id
43 and m.usr_id = u.usr_id
44group by u.tenant_id, u.usr_grp_id
45
46
47select * from @user_groups_to_move
48
49
50
51declare @include_authorization bit = 1,
52 @include_usr_grp_pref bit = 1
53
54begin try
55
56 insert into usr_grp
57 (
58 tenant_id,
59 usr_grp_id,
60 usr_grp_type,
61 usr_grp_description,
62 usr_grp_pref_priority,
63 usr_grp_resource_claim_priority,
64 allow_inactive_gui_appls,
65 active_directory_domain_name,
66 active_directory_grp_name,
67 net_bios_domain_name,
68 active_directory_updated_on,
69 insert_user,
70 insert_date_time,
71 update_user,
72 update_date_time
73 )
74 select
75 @target_tenant_id,
76 u.usr_grp_id,
77 t1.usr_grp_type,
78 t1.usr_grp_description,
79 t1.usr_grp_pref_priority,
80 t1.usr_grp_resource_claim_priority,
81 t1.allow_inactive_gui_appls,
82 t1.active_directory_domain_name,
83 t1.active_directory_grp_name,
84 t1.net_bios_domain_name,
85 t1.active_directory_updated_on,
86 dbo.tsf_user(),
87 sysdatetime(),
88 dbo.tsf_user(),
89 sysdatetime()
90 from usr_grp t1
91 join @user_groups_to_move u
92 on u.tenant_id = t1.tenant_id
93 and u.usr_grp_id = t1.usr_grp_id
94 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)
95
96
97 if @include_authorization = 1
98 begin
99 insert into gui_appl_authorization
100 (
101 tenant_id,
102 usr_grp_id,
103 project_id,
104 project_vrs_id,
105 gui_appl_id,
106 role_id,
107 insert_user,
108 insert_date_time,
109 update_user,
110 update_date_time
111 )
112 select
113 @target_tenant_id,
114 u.usr_grp_id,
115 t2.project_id,
116 t2.project_vrs_id,
117 t2.gui_appl_id,
118 t2.role_id,
119 dbo.tsf_user(),
120 sysdatetime(),
121 dbo.tsf_user(),
122 sysdatetime()
123 from gui_appl_authorization t2
124 join usr_grp t1
125 on t1.tenant_id = t2.tenant_id
126 and t1.usr_grp_id = t2.usr_grp_id
127 join @user_groups_to_move u
128 on u.tenant_id = t1.tenant_id
129 and u.usr_grp_id = t1.usr_grp_id
130 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)
131 end
132
133 if @include_usr_grp_pref = 1
134 begin
135
136 insert into usr_grp_pref_gui_appl
137 (
138 tenant_id,
139 usr_grp_id,
140 project_id,
141 project_vrs_id,
142 gui_appl_id,
143 load_on_start_up,
144 order_no,
145 insert_user,
146 insert_date_time,
147 update_user,
148 update_date_time
149 )
150 select
151 @target_tenant_id,
152 u.usr_grp_id,
153 t2.project_id,
154 t2.project_vrs_id,
155 t2.gui_appl_id,
156 t2.load_on_start_up,
157 t2.order_no,
158 dbo.tsf_user(),
159 sysdatetime(),
160 dbo.tsf_user(),
161 sysdatetime()
162 from usr_grp_pref_gui_appl t2
163 join usr_grp t1
164 on t1.tenant_id = t2.tenant_id
165 and t1.usr_grp_id = t2.usr_grp_id
166 join @user_groups_to_move u
167 on u.tenant_id = t1.tenant_id
168 and u.usr_grp_id = t1.usr_grp_id
169
170
171 insert into usr_grp_pref_platform
172 (
173 tenant_id,
174 usr_grp_id,
175 project_id,
176 project_vrs_id,
177 gui_appl_id,
178 platform,
179 theme_id,
180 default_menu_id,
181 insert_user,
182 insert_date_time,
183 update_user,
184 update_date_time
185 )
186 select
187 @target_tenant_id,
188 u.usr_grp_id,
189 t2.project_id,
190 t2.project_vrs_id,
191 t2.gui_appl_id,
192 t2.platform,
193 t2.theme_id,
194 t2.default_menu_id,
195 dbo.tsf_user(),
196 sysdatetime(),
197 dbo.tsf_user(),
198 sysdatetime()
199 from usr_grp_pref_platform t2
200 join usr_grp t1
201 on t1.tenant_id = t2.tenant_id
202 and t1.usr_grp_id = t2.usr_grp_id
203 join @user_groups_to_move u
204 on u.tenant_id = t1.tenant_id
205 and u.usr_grp_id = t1.usr_grp_id
206
207
208 insert into usr_grp_pref_start_object
209 (
210 tenant_id,
211 usr_grp_id,
212 project_id,
213 project_vrs_id,
214 gui_appl_id,
215 start_object_id,
216 start_object_type,
217 tab_id,
218 tab_variant_id,
219 task_id,
220 task_variant_id,
221 report_id,
222 report_variant_id,
223 process_id,
224 order_no,
225 abs_order_no,
226 insert_user,
227 insert_date_time,
228 update_user,
229 update_date_time
230 )
231 select
232 @target_tenant_id,
233 u.usr_grp_id,
234 t2.project_id,
235 t2.project_vrs_id,
236 t2.gui_appl_id,
237 t2.start_object_id,
238 t2.start_object_type,
239 t2.tab_id,
240 t2.tab_variant_id,
241 t2.task_id,
242 t2.task_variant_id,
243 t2.report_id,
244 t2.report_variant_id,
245 t2.process_id,
246 t2.order_no,
247 t2.abs_order_no,
248 dbo.tsf_user(),
249 sysdatetime(),
250 dbo.tsf_user(),
251 sysdatetime()
252 from usr_grp_pref_start_object t2
253 join usr_grp t1
254 on t1.tenant_id = t2.tenant_id
255 and t1.usr_grp_id = t2.usr_grp_id
256 join @user_groups_to_move u
257 on u.tenant_id = t1.tenant_id
258 and u.usr_grp_id = t1.usr_grp_id
259
260
261 insert into usr_grp_pref_tab_prefilter_status
262 (
263 tenant_id,
264 usr_grp_id,
265 project_id,
266 project_vrs_id,
267 gui_appl_id,
268 type_of_path,
269 path,
270 tab_id,
271 tab_variant_id,
272 tab_prefilter_id,
273 default_prefilter,
274 insert_user,
275 insert_date_time,
276 update_user,
277 update_date_time
278 )
279 select
280 @target_tenant_id,
281 u.usr_grp_id,
282 t2.project_id,
283 t2.project_vrs_id,
284 t2.gui_appl_id,
285 t2.type_of_path,
286 t2.path,
287 t2.tab_id,
288 t2.tab_variant_id,
289 t2.tab_prefilter_id,
290 t2.default_prefilter,
291 dbo.tsf_user(),
292 sysdatetime(),
293 dbo.tsf_user(),
294 sysdatetime()
295 from usr_grp_pref_tab_prefilter_status t2
296 join usr_grp t1
297 on t1.tenant_id = t2.tenant_id
298 and t1.usr_grp_id = t2.usr_grp_id
299 join @user_groups_to_move u
300 on u.tenant_id = t1.tenant_id
301 and u.usr_grp_id = t1.usr_grp_id
302 end
303end try
304begin catch
305
306 throw;
307
308end catch
309
310
311
312declare @temp_usr_id guid = newid(),
313 @to_first_name name,
314 @to_sur_name name,
315 @to_email email_address,
316 @usr_id usr_id,
317 @from_tenant_id int
318
319
320declare cursor_move_usr cursor for
321
322select tenant_id, usr_id
323from @users_to_move
324
325open cursor_move_usr;
326
327fetch next from cursor_move_usr into @from_tenant_id, @usr_id
328
329while @@fetch_status = 0
330begin
331
332 select @to_first_name = u.first_name,
333 @to_sur_name = u.sur_name,
334 @to_email = u.email,
335 @temp_usr_id = newid()
336 from usr_general u
337 where u.tenant_id = @from_tenant_id
338 and u.usr_id = @usr_id
339
340 begin try
341 begin transaction
342
343 update usr_general
344 set email = null
345 where tenant_id = @from_tenant_id
346 and usr_id = @usr_id
347
348
349 exec task_copy_usr_general @from_tenant_id, @usr_id, @target_tenant_id, @temp_usr_id, @to_first_name, @to_sur_name, @to_email
350
351
352 exec task_delete_usr_general @from_tenant_id, @usr_id
353
354
355 exec task_rename_usr_general @target_tenant_id, @temp_usr_id, @usr_id, @to_first_name, @to_sur_name
356
357 commit transaction;
358 end try
359 begin catch
360 rollback transaction;
361
362 declare @msg nvarchar(500)
363 select @msg = '<text>'+@usr_id+'</text><text>'+tenant_name+'</text>'
364 from tenant
365 where tenant_id = @target_tenant_id
366
367 exec dbo.tsf_send_message 'move_usr_to_tenant_failed', @msg, 1
368 return;
369 end catch
370
371 fetch next from cursor_move_usr into @from_tenant_id, @usr_id;
372end;
373
374close cursor_move_usr;
375
376deallocate cursor_move_usr;
377
378
379
380select u.tenant_id as from_tenant, @target_tenant_id as target_tenant, u.usr_id, uu.usr_grp_id
381from @users_to_move u
382join usr_grp_usr uu
383 on uu.tenant_id = @target_tenant_id
384 and uu.usr_id = u.usr_id