/
sp_SSISClonePermissions.sql
699 lines (596 loc) · 29.1 KB
/
sp_SSISClonePermissions.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
IF NOT EXISTS(SELECT 1 FROM sys.databases where name = 'SSISDB')
BEGIN
RAISERROR(N'SSIS Database Does not Exists', 15, 0)
SET NOEXEC ON;
END
GO
USE [SSISDB]
GO
RAISERROR('Creating procedure [dbo].[sp_SSISClonePermissions]', 0, 0) WITH NOWAIT;
IF NOT EXISTS(SELECT * FROM sys.procedures WHERE object_id = OBJECT_ID('[dbo].[sp_SSISClonePermissions]'))
EXEC (N'CREATE PROCEDURE [dbo].[sp_SSISClonePermissions] AS PRINT ''Placeholder for [dbo].[sp_SSISClonePermissions]''')
GO
/* ****************************************************
sp_SSISClonePermissions v 0.21 (2017-11-14)
Feedback: mailto:pavel.pawlowski@hotmail.cz
MIT License
Copyright (c) 2017 Pavel Pawlowski
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Description:
Clones permissions for FOLDERS, PROJEECTS or ENVIRONMETNS to allow their easy transfer among environments
Parameters:
@folder nvarchar(max) = NULL --Comma separated list of folders to script Permissions. Supports wildcards
,@object nvarchar(max) = '%' --Comma separated list of object names (project or environment). Supports wildcards
,@type nvarchar(MAX) = 'FOLDER,PROJECT,ENVIRONMENT' --Comma separate list of object types to script permissions.
,@targetFolderName nvarchar(128) = '%' --Defines the target folder name. The % wildcard is replaced by the original folder name
,@targetObjectName nvarchar(128) = '%' --Defines the target object name. The % wildcard is replaced by the original object name.
******************************************************* */
ALTER PROCEDURE [dbo].[sp_SSISClonePermissions]
@folder nvarchar(max) = NULL --Comma separated list of folders to script Permissions. Supports wildcards
,@object nvarchar(max) = '%' --Comma separated list of object names (project or environment). Supports wildcards
,@type nvarchar(MAX) = 'FOLDER,PROJECT,ENVIRONMENT' --Comma separate list of object types to script permissions.
,@targetFolderName nvarchar(128) = '%' --Defines the target folder name. The % wildcard is replaced by the original folder name
,@targetObjectName nvarchar(128) = '%' --Defines the target object name. The % wildcard is replaced by the original object name.
WITH EXECUTE AS 'AllSchemaOwner'
AS
BEGIN
SET NOCOUNT ON;
SET XACT_ABORT ON;
DECLARE
@printHelp bit = 0
,@captionBegin nvarchar(50) = N'' --Beginning of the caption for the purpose of the caption printing
,@captionEnd nvarchar(50) = N'' --End of the caption line for the purpose of the caption printing
,@caption nvarchar(max) --sp_SSISClonePermissions caption
,@xml xml
,@object_type smallint
,@ObjectType nvarchar(20)
,@ObjectTypeQuoted nvarchar(20)
,@ObjectFolder nvarchar(128)
,@ObjectFolderQuoted nvarchar(256)
,@ObjectName nvarchar(128)
,@PrincipalName nvarchar(128)
,@GrantorPrincipalName nvarchar(128)
,@PermissionType smallint
,@permissionDesc nvarchar(50)
,@IsDeny bit
,@last_object_type smallint
,@last_object_folder nvarchar(128)
,@isDenyInt int
,@firstInGroup bit = 1
--Table variable for holding ids of folders to process
DECLARE @folders TABLE (
folder_id bigint
)
--contains list of object names to match
DECLARE @objectNames TABLE(
object_name nvarchar(128) NOT NULL PRIMARY KEY CLUSTERED
)
--contains list of object types to script permissions
DECLARE @types TABLE (
TypeName nvarchar(20) NOT NULL PRIMARY KEY CLUSTERED
,[Type] smallint
)
DECLARE @permissions TABLE (
object_type smallint
,ObjectType nvarchar(20)
,ObjectFolder nvarchar(128)
,ObjectName nvarchar(128)
,PrincipalName nvarchar(128)
,GrantorPrincipalName nvarchar(128)
,PermissionType smallint
,IsDeny bit
)
IF @folder IS NULL
SET @printHelp = 1
--Set and print the procedure output caption
IF (@printHelp = 0)
BEGIN
SET @captionBegin = N'RAISERROR(N''';
SET @captionEnd = N''', 0, 0) WITH NOWAIT;';
END
SET @caption = @captionBegin + N'sp_SSISClonePermissions v0.21 (2017-11-14) (C) 2017 Pavel Pawlowski' + @captionEnd + NCHAR(13) + NCHAR(10) +
@captionBegin + N'===================================================================' + @captionEnd + NCHAR(13) + NCHAR(10);
RAISERROR(@caption, 0, 0) WITH NOWAIT;
RAISERROR(N'', 0, 0) WITH NOWAIT;
--get object types to script permissions
SET @xml = N'<i>' + REPLACE(@type, ',', '</i><i>') + N'</i>';
WITH ObjTypes AS (
SELECT DISTINCT
LTRIM(RTRIM(F.value('.', 'nvarchar(20)'))) AS TypeName
FROM @xml.nodes(N'/i') T(F)
)
INSERT INTO @types(TypeName, [Type])
SELECT
TypeName
,CASE TypeName
WHEN N'FOLDER' THEN 1
WHEN N'PROJECT' THEN 2
WHEN N'ENVIRONMENT' THEN 3
WHEN N'OPERATION' THEN 4
ELSE NULL
END AS [Type]
FROM ObjTypes
IF EXISTS(SELECT 1 FROM @types WHERE TypeName NOT IN (N'FOLDER', N'PROJECT', N'ENVIRONMENT'))
BEGIN
SET @printHelp = 1
RAISERROR(N'Only FOLDER,PROJECT,ENVIRONMENT is allowed as @type', 11, 0);
END
IF @printHelp = 1
BEGIN
RAISERROR(N'Clones permissions for FOLDERS, PROJEECTS or ENVIRONMETNS to allow their easy transfer among environments', 0, 0) WITH NOWAIT;
RAISERROR(N'', 0, 0) WITH NOWAIT;
RAISERROR(N'Usage:', 0, 0) WITH NOWAIT;
RAISERROR(N'[sp_SSISClonePermissions] parameters', 0, 0) WITH NOWAIT;
RAISERROR(N'', 0, 0) WITH NOWAIT;
RAISERROR(N'Parameters:
@folder nvarchar(max) = NULL - Comma separated list of folders to script Permissions. Supports wildcards
,@object nvarchar(max) = ''%%'' - Comma separated list of object names (project or environment). Supports wildcards
,@type nvarchar(MAX) = ''FOLDER,PROJECT,ENVIRONMENT'' - Comma separate list of object types to script permissions.
Currently Supports FOLDER, PROJECT and ENVIRONMENT
,@targetFolderName nvarchar(128) = ''%%'' - Defines the target folder name. The %% wildcard is replaced by the original folder name
,@targetObjectName nvarchar(128) = ''%%'' - Defines the target object name. The %% wildcard is replaced by the original object name.
It is shared for both Project and Environment names.
For different names of projects and environments it is necessary
to script permissions for those separately.
', 0, 0) WITH NOWAIT;
RAISERROR(N'Wildcards:
----------
Wildcards are standard wildcards for the LIKE statement
Entries prefixed with [-] (minus) symbol are excluded form results and have priority over the non excluding
Samples:
--------
Clones permission for all folders and projects and environments.
sp_SSISClonePermission
@folder = ''%%''
Clones permission for all folders and projects and environments in that folders which name starts with `DEV_`
sp_SSISClonePermission
@folder = ''DEV_%%''
Clone permission for projects and environments from all folders. Only permissions for projects and environments which name starts with `DEV_` but not ending by `_copy` will be cloned.
sp_SSISClonePermission
@folder = ''%%''
,@object = ''DEV_%%,-%%_copy''
,@type = ''PROJECT,ENVIRONMENT''
', 0, 0) WITH NOWAIT;
RETURN;
END
--Get list of folders
SET @xml = N'<i>' + REPLACE(@folder, ',', '</i><i>') + N'</i>';
WITH FolderNames AS (
SELECT DISTINCT
LTRIM(RTRIM(F.value('.', 'nvarchar(128)'))) AS FolderName
FROM @xml.nodes(N'/i') T(F)
)
INSERT INTO @folders (folder_id)
SELECT DISTINCT
folder_id
FROM internal.folders F
INNER JOIN FolderNames FN ON F.name LIKE FN.FolderName AND LEFT(FN.FolderName, 1) <> '-'
EXCEPT
SELECT
folder_id
FROM internal.folders F
INNER JOIN FolderNames FN ON F.name LIKE RIGHT(FN.FolderName, LEN(FN.FolderName) - 1) AND LEFT(FN.FolderName, 1) = '-'
IF NOT EXISTS(SELECT 1 FROM @folders)
BEGIN
RAISERROR(N'No Folder matching [%s] exists.', 15, 1, @folder) WITH NOWAIT;
RETURN;
END;
--get object names
SET @xml = N'<i>' + REPLACE(@object, ',', '</i><i>') + N'</i>';
INSERT INTO @objectNames(object_name)
SELECT DISTINCT
LTRIM(RTRIM(F.value('.', 'nvarchar(128)'))) AS ObjectName
FROM @xml.nodes(N'/i') T(F);
WITH ObjectPermissions AS (
SELECT
op.object_type
,CASE op.object_type
WHEN 1 THEN 'FOLDER'
WHEN 2 THEN 'PROJECT'
WHEN 3 THEN 'ENVIRONMENT'
WHEN 4 THEN 'OPERATION'
ELSE NULL
END AS ObjectType
,CASE op.object_type
WHEN 1 THEN op.object_id
WHEN 2 THEN p.folder_id
WHEN 3 THEN e.folder_id
ELSE NULL
END AS ObjectFolderID
,CASE op.object_type
WHEN 1 THEN f.name
WHEN 2 THEN pf.name
WHEN 3 THEN ef.name
ELSE NULL
END AS ObjectFolder
,CASE op.object_type
WHEN 1 THEN f.name
WHEN 2 THEN p.name
WHEN 3 THEN e.environment_name
END AS ObjectName
,dp.name AS PrincipalName
,gp.name AS GrantorPrincipalName
,op.permission_type AS PermissionType
,op.is_deny AS IsDeny
FROM internal.object_permissions op
LEFT JOIN internal.folders f ON f.folder_id = op.object_id and op.object_type = 1
LEFT JOIN internal.projects p ON p.project_id = op.object_id AND op.object_type = 2
LEFT JOIN internal.folders pf ON pf.folder_id = p.folder_id
LEFT JOIN internal.environments e ON e.environment_id = op.object_id AND op.object_type = 3
LEFT JOIN internal.folders ef ON ef.folder_id = e.folder_id
INNER JOIN sys.database_principals dp ON dp.sid = op.sid
INNER JOIN sys.database_principals gp ON gp.sid = op.grantor_sid
WHERE
op.object_type IN (SELECT [Type] FROM @types)
)
INSERT INTO @permissions (
object_type
,ObjectType
,ObjectFolder
,ObjectName
,PrincipalName
,GrantorPrincipalName
,PermissionType
,IsDeny
)
SELECT
op.object_type
,op.ObjectType
,op.ObjectFolder
,op.ObjectName
,op.PrincipalName
,op.GrantorPrincipalName
,op.PermissionType
,op.IsDeny
FROM ObjectPermissions op
INNER JOIN @folders f ON f.folder_id = op.ObjectFolderID
INNER JOIN @objectNames o ON op.ObjectName LIKE o.object_name AND LEFT(o.object_name, 1) <> '-'
EXCEPT
SELECT
op.object_type
,op.ObjectType
,op.ObjectFolder
,op.ObjectName
,op.PrincipalName
,op.GrantorPrincipalName
,op.PermissionType
,op.IsDeny
FROM ObjectPermissions op
INNER JOIN @folders f ON f.folder_id = op.ObjectFolderID
INNER JOIN @objectNames o ON op.ObjectName LIKE RIGHT(o.object_name, LEN(o.object_name) - 1) AND LEFT(o.object_name, 1) = '-'
IF NOT EXISTS(SELECT 1 FROM @permissions)
BEGIN
RAISERROR(N'No object matching input criteria @folder = ''%s'', @object = ''%s'' was found', 15, 0, @folder, @object) WITH NOWAIT;
RETURN;
END
RAISERROR(N'
DECLARE
@targetFolderName nvarchar(128) = ''%s'' --Specify name for target folder. %% wildcard is replaced by original folder name
,@targetObjectName nvarchar(128) = ''%s'' --Specify name for target object. %% wildcard is replaced by original object name', 0, 0, @targetFolderName, @targetObjectName) WITH NOWAIT;
RAISERROR(N'
-- Declarations:
-- -------------
DECLARE
@ObjectType nvarchar(20)
,@ObjectFolder nvarchar(128)
,@ObjectName nvarchar(128)
,@PrincipalName nvarchar(128)
,@GrantorPrincipalName nvarchar(128)
,@PermissionType smallint
,@IsDeny bit;
DECLARE @permissions TABLE (
ObjectType nvarchar(20)
,ObjectFolder nvarchar(128)
,ObjectName nvarchar(128)
,PrincipalName nvarchar(128)
,GrantorPrincipalName nvarchar(128)
,PermissionType smallint
,IsDeny bit
)
SET NOCOUNT ON;
', 0, 0) WITH NOWAIT;
DECLARE cr CURSOR FAST_FORWARD FOR
SELECT
object_type
,ObjectType
,ObjectFolder
,ObjectName
,PrincipalName
,GrantorPrincipalName
,PermissionType
,IsDeny
FROM @permissions
ORDER BY object_type, ObjectFolder, ObjectName, PrincipalName
OPEN cr;
FETCH NEXT FROM cr INTO
@object_type
,@ObjectType
,@ObjectFolder
,@ObjectName
,@PrincipalName
,@GrantorPrincipalName
,@PermissionType
,@IsDeny
WHILE @@FETCH_STATUS = 0
BEGIN
--Quote values for printing
SELECT
@ObjectFolderQuoted = N'N''' + REPLACE(@ObjectFolder, '''', '''''') + ''''
,@ObjectName = N'N''' + REPLACE(@ObjectName, '''', '''''') + ''''
,@PrincipalName = N'N''' + REPLACE(@PrincipalName, '''', '''''') + ''''
,@GrantorPrincipalName = N'N''' + REPLACE(@GrantorPrincipalName, '''', '''''') + ''''
,@ObjectTypeQuoted = N'N''' + REPLACE(@ObjectType, '''', '''''') + ''''
,@isDenyInt = @IsDeny
,@permissionDesc =
CASE @PermissionType
WHEN 1 THEN N'READ'
WHEN 2 THEN N'MODIFY'
WHEN 3 THEN N'EXECUTE'
WHEN 4 THEN N'MANAGE_PERMISSIONS'
WHEN 100 THEN N'CREATE_OBJECTS'
WHEN 101 THEN N'READ_OBJECTS'
WHEN 102 THEN N'MODIFY_OBJECTS'
WHEN 103 THEN N'EXECUTE_OBJECTS'
WHEN 104 THEN N'MANAGE_OBJECT_PERMISSIONS'
ELSE N'Unknown'
END
IF @last_object_type IS NULL OR @last_object_type <> @object_type
BEGIN
RAISERROR(N'-- =================================================================================', 0, 0) WITH NOWAIT;
RAISERROR(N'-- Object Types: %s', 0, 0, @ObjectType) WITH NOWAIT;
RAISERROR(N'-- =================================================================================', 0, 0) WITH NOWAIT;
RAISERROR(N'SET @ObjectType = %s', 0, 0, @ObjectTypeQuoted ) WITH NOWAIT;
RAISERROR(N'-- =================================================================================', 0, 0) WITH NOWAIT;
SET @last_object_folder = NULL
SET @firstInGroup = 1
END
IF @object_type <> 1 AND (@last_object_folder IS NULL OR @last_object_folder <> @ObjectFolder)
BEGIN
RAISERROR(N'-- *********************************************************************************', 0, 0) WITH NOWAIT;
RAISERROR(N'-- Folder: %s', 0, 0, @ObjectFolder) WITH NOWAIT;
RAISERROR(N'-- *********************************************************************************', 0, 0) WITH NOWAIT;
RAISERROR(N'SET @ObjectFolder = %s', 0, 0, @ObjectFolderQuoted ) WITH NOWAIT;
RAISERROR(N'-- *********************************************************************************', 0, 0) WITH NOWAIT;
SET @firstInGroup = 1
END
IF @firstInGroup = 0
RAISERROR(N'-- ---------------', 0, 0) WITH NOWAIT;
RAISERROR(N'SET @ObjectName = %s', 0, 0, @ObjectName ) WITH NOWAIT;
RAISERROR(N'SET @PrincipalName = %s', 0, 0, @PrincipalName ) WITH NOWAIT;
RAISERROR(N'SET @GrantorPrincipalName = %s', 0, 0, @GrantorPrincipalName ) WITH NOWAIT;
RAISERROR(N'SET @PermissionType = %d --%s', 0, 0, @PermissionType, @permissionDesc) WITH NOWAIT;
RAISERROR(N'SET @IsDeny = %d', 0, 0, @IsDenyInt ) WITH NOWAIT;
IF @object_type <> 1
RAISERROR(N'INSERT INTO @permissions(ObjectType, ObjectFolder, ObjectName, PrincipalName, GrantorPrincipalName, PermissionType, IsDeny) VALUES(@ObjectType, @ObjectFolder, @ObjectName, @PrincipalName, @GrantorPrincipalName, @PermissionType, @isDeny)', 0, 0) WITH NOWAIT;
ELSE
RAISERROR(N'INSERT INTO @permissions(ObjectType, ObjectFolder, ObjectName, PrincipalName, GrantorPrincipalName, PermissionType, IsDeny) VALUES(@ObjectType, @ObjectName, @ObjectName, @PrincipalName, @GrantorPrincipalName, @PermissionType, @isDeny)', 0, 0) WITH NOWAIT;
SELECT
@last_object_folder = @ObjectFolder
,@last_object_type = @object_type
,@firstInGroup = 0
FETCH NEXT FROM cr INTO
@object_type
,@ObjectType
,@ObjectFolder
,@ObjectName
,@PrincipalName
,@GrantorPrincipalName
,@PermissionType
,@IsDeny
END
CLOSE cr;
DEALLOCATE cr;
--Print Runtime part for the script
RAISERROR(N'', 0, 0) WITH NOWAIT;
RAISERROR(N'-- =================================================================================', 0, 0) WITH NOWAIT;
RAISERROR(N'-- RUNTIME', 0, 0) WITH NOWAIT;
RAISERROR(N'-- =================================================================================', 0, 0) WITH NOWAIT;
RAISERROR(N'
DECLARE
@lastObjectType nvarchar(20)
,@lastObjectFolder nvarchar(128)
,@lastObjectName nvarchar(128)
,@lastPrincipal nvarchar(128)
,@processFld bit
,@processObj bit
,@processPrinc bit
,@object_id bigint
,@principal_id int
,@object_type smallint
,@isDenyInt int
,@permissionDesc nvarchar(50)
,@targetFolder nvarchar(128)
,@targetObject nvarchar(128)
DECLARE cr CURSOR FAST_FORWARD FOR
SELECT
ObjectType
,ObjectFolder
,ObjectName
,PrincipalName
,GrantorPrincipalName
,PermissionType
,IsDeny
FROM @permissions
ORDER BY ObjectType, ObjectFolder, ObjectName, PrincipalName', 0, 0) WITH NOWAIT;
RAISERROR(N'
OPEN cr;
FETCH NEXT FROM cr INTO
@ObjectType
,@ObjectFolder
,@ObjectName
,@PrincipalName
,@GrantorPrincipalName
,@PermissionType
,@isDeny
WHILE @@FETCH_STATUS = 0
BEGIN ', 0, 0) WITH NOWAIT;
RAISERROR(N'
SET @isDenyInt = @IsDeny
SELECT
@targetFolder = REPLACE(@targetFolderName, ''%%'', @ObjectFolder)
,@targetObject = REPLACE(CASE WHEN @ObjectType = ''FOLDER'' THEN @targetFolderName ELSE @targetObjectName END, ''%%'', @ObjectName)
IF @lastObjectType IS NULL OR @lastObjectType <> @ObjectType
BEGIN
SELECT
@lastObjectFolder = NULL
,@lastObjectName = NULL
,@lastPrincipal = NULL
,@processFld = 1
,@processObj = 1
,@processPrinc = 1
,@object_type = CASE @ObjectType
WHEN N''FOLDER'' THEN 1
WHEN N''PROJECT'' THEN 2
WHEN ''ENVIRONMENT'' THEN 3
ELSE NULL
END
IF @lastObjectType IS NOT NULL
RAISERROR(N''==================================================================='', 0, 0) WITH NOWAIT;
RAISERROR(N''Processing ObjectTypes [%%s]'', 0, 0, @ObjectType) WITH NOWAIT;
RAISERROR(N''==================================================================='', 0, 0) WITH NOWAIT;
SET @lastObjectFolder = NULL
END', 0, 0) WITH NOWAIT;
RAISERROR(N'
IF @ObjectType <> N''FOLDER'' AND @lastObjectFolder IS NULL OR @lastObjectFolder <> @ObjectFolder
BEGIN
SELECT
@processFld = 1
,@processObj = 1
,@processPrinc = 1
,@lastObjectName = NULL
,@lastPrincipal = NULL
IF @lastObjectFolder IS NOT NULL
RAISERROR(N''*******************************************************************'', 0, 0) WITH NOWAIT;
IF NOT EXISTS(SELECT 1 FROM [SSISDB].[catalog].[folders] f WHERE f.[name] = @targetFolder)
BEGIN
SET @processFld = 0;
RAISERROR(N''Destination folder [%%s] does not exist. Ignoring objects in folder.'', 11, 0, @targetFolder) WITH NOWAIT;
END
ELSE
BEGIN
RAISERROR(N''Processing folder [%%s]'', 0, 0, @targetFolder) WITH NOWAIT;
END
RAISERROR(N''*******************************************************************'', 0, 0) WITH NOWAIT;
END', 0, 0) WITH NOWAIT;
RAISERROR(N'
IF @processFld = 1
BEGIN
IF @lastObjectName IS NULL OR @lastObjectName <> @ObjectName
BEGIN
SELECT
@processObj = 1
,@processPrinc = 1
,@object_id = NULL
IF @ObjectType = N''FOLDER''
BEGIN
SELECT @object_id = folder_id FROM [SSISDB].[catalog].[folders] f WHERE f.[name] = @targetObject
IF @object_id IS NULL
BEGIN
SET @processFld = 0;
RAISERROR(N''Destination folder [%%s] does not exist. Ignoring Folder Permissions.'', 11, 0, @targetObject) WITH NOWAIT;
END
ELSE
RAISERROR(N''Setting Permissions on Folder [%%s]'', 0, 0, @targetObject) WITH NOWAIT;
END', 0, 0) WITH NOWAIT;
RAISERROR(N' ELSE IF @ObjectType = N''PROJECT''
BEGIN
SELECT @object_id = project_id FROM [SSISDB].[catalog].[projects] p INNER JOIN [SSISDB].[catalog].[folders] f ON f.folder_id = p.folder_id WHERE p.[name] = @targetObject AND f.[name] = @targetFolder
IF @object_id IS NULL
BEGIN
SET @processFld = 0;
RAISERROR(N''Destination Project [%%s]\[%%s] does not exist. Ignoring Project Permissions.'', 11, 0, @targetFolder, @targetObject) WITH NOWAIT;
END
ELSE
RAISERROR(N''Setting Permissions on Project [%%s]\[%%s]'', 0, 0, @targetFolder, @ObjectName) WITH NOWAIT;
END
ELSE IF @objectType = N''ENVIRONMENT''
BEGIN
SELECT @object_id = environment_id FROM [SSISDB].[catalog].[environments] e INNER JOIN [SSISDB].[catalog].[folders] f ON f.folder_id = e.folder_id WHERE e.[name] = @targetObject AND f.[name] = @targetFolder
IF @object_id IS NULL
BEGIN
SET @processFld = 0;
RAISERROR(N''Destination Environment [%%s]\[%%s] does not exist. Ignoring Environment Permissions.'', 11, 0, @targetFolder, @targetObject) WITH NOWAIT;
END
ELSE
RAISERROR(N''Setting Permissions on Environment [%%s]\[%%s]'', 0, 0, @targetFolder, @targetObject) WITH NOWAIT;
END
RAISERROR(N''-------------------------------------------------------------------'', 0, 0) WITH NOWAIT;
END', 0, 0) WITH NOWAIT;
RAISERROR(N'
IF @processFld = 1 AND @lastPrincipal IS NULL OR @lastPrincipal <> @PrincipalName
BEGIN
SET @principal_id = NULL
SELECT @principal_id = principal_id FROM [SSISDB].sys.database_principals WHERE name = @PrincipalName
IF @principal_id IS NULL
BEGIN
RAISERROR(N''Database Principal [%%s] does not exists in SSISDB. Ignoring permissions for database principal'', 11, 0, @PrincipalName) WITH NOWAIT;
SET @processPrinc = 1
END
ELSE
RAISERROR(N''Granting Permissions for Database Principal [%%s]'', 0, 0, @PrincipalName) WITH NOWAIT;
END', 0, 0) WITH NOWAIT;
RAISERROR(N'
IF @processFld = 1 AND @processPrinc = 1
BEGIN
SET @permissionDesc =
CASE @PermissionType
WHEN 1 THEN N''READ''
WHEN 2 THEN N''MODIFY''
WHEN 3 THEN N''EXECUTE''
WHEN 4 THEN N''MANAGE_PERMISSIONS''
WHEN 100 THEN N''CREATE_OBJECTS''
WHEN 101 THEN N''READ_OBJECTS''
WHEN 102 THEN N''MODIFY_OBJECTS''
WHEN 103 THEN N''EXECUTE_OBJECTS''
WHEN 104 THEN N''MANAGE_OBJECT_PERMISSIONS''
ELSE N''Unknown''
END
IF @IsDeny = 1
BEGIN
RAISERROR(N'' - DENYING PermissionType: %%d - %%s'', 0, 0, @PermissionType, @permissionDesc) WITH NOWAIT;
EXEC [SSISDB].[catalog].[deny_permission] @object_type=@object_type, @object_id=@object_id, @principal_id=@principal_id, @permission_type=@PermissionType
END
ELSE
BEGIN
RAISERROR(N'' - GRANTING PermissionType: %%d - %%s'', 0, 0, @PermissionType, @permissionDesc) WITH NOWAIT;
EXEC [SSISDB].[catalog].[grant_permission] @object_type=@object_type, @object_id=@object_id, @principal_id=@principal_id, @permission_type=@PermissionType
END
END
END', 0, 0) WITH NOWAIT;
RAISERROR(N'
SELECT
@lastObjectType = @ObjectType
,@lastObjectFolder = @ObjectFolder
,@lastObjectName = @ObjectName
,@lastPrincipal = @PrincipalName
FETCH NEXT FROM cr INTO
@ObjectType
,@ObjectFolder
,@ObjectName
,@PrincipalName
,@GrantorPrincipalName
,@PermissionType
,@IsDeny
END
CLOSE cr;
DEALLOCATE cr;', 0, 0) WITH NOWAIT;
END
GO
--GRANT EXECUTE permission on the stored procedure to [ssis_admin] role
RAISERROR(N'Granting EXECUTE permission to [ssis_admin]', 0, 0) WITH NOWAIT;
GRANT EXECUTE ON [dbo].[sp_SSISClonePermissions] TO [ssis_admin]
GO