Skip to main content
Solved

Move users and user groups to other tenant


Forum|alt.badge.img+15

Hi, with the update of IAM 2021.2 tenants are introduced. We’ve already had some kind of custom tenant structure (outside IAM) and I would like to apply the tenant structure as well into IAM. It might be interesting for us, so the external tooling could disappear.

However, I want to upgrade to 2021.2 including a proper tenant structure, unfortunately there is no task to move a user and user groups to it's own (new) tenant, it's all to “Default”. Is this somehow possible?

Best answer by Mark Jongeling

Hi René,

We have made functionality for moving users and copying user groups to another tenant in the upcoming 2021.3 version of our platform. Since that version is not coming out soon, we have created a script for you to use.

In the script you'll need to input a couple of things, namely:

  • On which IAM it needs to run;
  • To which existing tenant id (Integer) you would like to copy;
  • And which users to copy to the new tenant

The script will automatically copy all user groups that are assigned to the named users to the new tenant. Users will be moved to the new tenant thereafter and automatically be assigned to the new user groups according to their previous user group assignments.

Only thing left you to do is deleting the old user groups in the Default tenant. That way you can make sure everything is copied correctly before the old user groups are deleted.

 

The script is below:

Show content
1/* TARGET IAM */
2use [IAM_NAME]
3go
4
5declare @target_tenant_id int = 1 --Input the tenant ID the following users and their user groups need to be moved to
6
7--Enter here which users to copy to the target_tenant_id. User groups of these users will automatically be copied.
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--Select users to move
23select * from @users_to_move
24
25
26
27
28
29
30/**********/
31/* CODE */
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--User groups to move
47select * from @user_groups_to_move
48
49
50--Copy user groups to target tenant
51declare @include_authorization bit = 1,
52 @include_usr_grp_pref bit = 1
53
54begin try
55 --Copy the main table
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 --Copy authorization
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 --Copy user group preferences
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--Declare temp name for user
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 --Get user info
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() --Refresh temp username
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 --Update email of the user to null to prevent error on duplicate email
343 update usr_general
344 set email = null
345 where tenant_id = @from_tenant_id
346 and usr_id = @usr_id
347
348 --Make a copy of the user, name it GUID
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 --Delete the original user
352 exec task_delete_usr_general @from_tenant_id, @usr_id
353
354 --Rename the GUID user back to original user
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--Final result
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

 

 

View original
Did this topic help you find an answer to your question?
This topic has been closed for replies.

7 replies

Mark Jongeling
Administrator
Forum|alt.badge.img+23

Hi René,

There is no task that moves users to a new Tenant but what you can use is the Mass update functionality of the GUI inside the Users list to update the selected users’ Tenant. Alternatively you can create a SQL script to move users to the correct Tenant based on their email address for example.


Forum|alt.badge.img+15
  • Author
  • Superhero
  • 196 replies
  • June 2, 2021

The mass update feature cannot be used here. Although it looks like it could be used, the tenant_id isn't updated. 

 

 

The tenant (and user id) should not be updateable via this method, as it is in the regular form also isn't editable. Maybe a bug in the mass update feature. The fields cannot be updated because they are part of the primary key. Changing those values need to update a lot of other tables if it's not cascading update.

However then a script should be created. Probably based on the task “Rename user”. Sounds like a lot of work :-(.


Anne Buit
Community Manager
Forum|alt.badge.img+5
  • Community Manager
  • 687 replies
  • June 2, 2021

Hi René,

The tenant is a strong entity within IAM and it indeed isn’t trivial to move a user to another tenant. 

I’ll check with the team to see if we can add this feature to the next version and provide you with a script for version 2021.2 as well.

How would you expect it to deal with user groups? Would it re-add them in the target tenant if they do not yet exists based on the name? And would it assign roles to those user groups?


Forum|alt.badge.img+15
  • Author
  • Superhero
  • 196 replies
  • June 3, 2021
Anne Buit wrote:

Hi René,

The tenant is a strong entity within IAM and it indeed isn’t trivial to move a user to another tenant. 

I’ll check with the team to see if we can add this feature to the next version and provide you with a script for version 2021.2 as well.

How would you expect it to deal with user groups? Would it re-add them in the target tenant if they do not yet exists based on the name? And would it assign roles to those user groups?

Hi,

A script or feature would really help us. We haven't evaluated the added value of tenants yet (if it fits for what we think we need), but what I read about it I almost assume it's targeted for ISV users like us. So we should prepare our IAM environment as early as possible I think. For now we can migrate all users and user groups to the ‘Default’ tenant, as nothing changes for an end user perspective. However when we add new customers we should add the tenant immediately.

The user groups… I see there is a strong relation between usr, usr_grp and usr_grp_usr. Now I understand why moving can be a bit harsh, because the usr_grp can have a different tenant than the usr_grp_usr when migrating. In our situation this isn't a problem because we already have user groups per company (tenant). But for generic purposes, when moving a user to another tenant, I should copy the user group to the new tenant and add the user to that group and remove it from the old one.

I think we should upgrade despite this, but start adding new customers in their own tenant, and wait for the existing customers to be moved after there's a fix available. There is no direct need for tenants, but I do expect so in the near future.

Br, René


Anne Buit
Community Manager
Forum|alt.badge.img+5
  • Community Manager
  • 687 replies
  • June 3, 2021

Hi René,

We’ll keep this use case in mind when developing the script. If I understand you correctly, ideally the assigned roles for a usr_grp should also be duplicated when it is moved to another tenant?


Forum|alt.badge.img+15
  • Author
  • Superhero
  • 196 replies
  • June 3, 2021

 

Anne Buit wrote:

Hi René,

We’ll keep this use case in mind when developing the script. If I understand you correctly, ideally the assigned roles for a usr_grp should also be duplicated when it is moved to another tenant?

Hi, In our case the assigned roles also should be copied. I think the table below should explain a bit more what I would expect:

  Tenant User group Application (gui_appl) Roles
Current Default Company X - Order picking Our App - Company X Read orders, pick order
Current Default Company Y - Order picking Our App - Company Y Read orders, pick order
New Company X Order picking Our app - Company X Read orders, pick orders
New Company Y Order picking Our app - Company Y Read orders, pick orders
New* Default Company X - Order picking Our App - Company X -
New* Default Company Y - Order picking Our App - Company Y -

 

*= these groups could be removed because they're copied to new groups and don't have any users in.


Mark Jongeling
Administrator
Forum|alt.badge.img+23
  • Administrator
  • 4047 replies
  • Answer
  • June 17, 2021

Hi René,

We have made functionality for moving users and copying user groups to another tenant in the upcoming 2021.3 version of our platform. Since that version is not coming out soon, we have created a script for you to use.

In the script you'll need to input a couple of things, namely:

  • On which IAM it needs to run;
  • To which existing tenant id (Integer) you would like to copy;
  • And which users to copy to the new tenant

The script will automatically copy all user groups that are assigned to the named users to the new tenant. Users will be moved to the new tenant thereafter and automatically be assigned to the new user groups according to their previous user group assignments.

Only thing left you to do is deleting the old user groups in the Default tenant. That way you can make sure everything is copied correctly before the old user groups are deleted.

 

The script is below:

Show content
1/* TARGET IAM */
2use [IAM_NAME]
3go
4
5declare @target_tenant_id int = 1 --Input the tenant ID the following users and their user groups need to be moved to
6
7--Enter here which users to copy to the target_tenant_id. User groups of these users will automatically be copied.
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--Select users to move
23select * from @users_to_move
24
25
26
27
28
29
30/**********/
31/* CODE */
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--User groups to move
47select * from @user_groups_to_move
48
49
50--Copy user groups to target tenant
51declare @include_authorization bit = 1,
52 @include_usr_grp_pref bit = 1
53
54begin try
55 --Copy the main table
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 --Copy authorization
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 --Copy user group preferences
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--Declare temp name for user
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 --Get user info
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() --Refresh temp username
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 --Update email of the user to null to prevent error on duplicate email
343 update usr_general
344 set email = null
345 where tenant_id = @from_tenant_id
346 and usr_id = @usr_id
347
348 --Make a copy of the user, name it GUID
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 --Delete the original user
352 exec task_delete_usr_general @from_tenant_id, @usr_id
353
354 --Rename the GUID user back to original user
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--Final result
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

 

 


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings