-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample_CleanSlate.sql
203 lines (151 loc) · 4.74 KB
/
Example_CleanSlate.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
-- ===========================
-- ==== Setup for example ====
-- ===========================
CREATE DATABASE [SourceDB]
GO
CREATE DATABASE [TargetDB1]
GO
CREATE DATABASE [TargetDB2]
GO
USE [SourceDB]
GO
CREATE PROCEDURE [dbo].[TestProc1]
AS
BEGIN
SELECT 1
END
GO
CREATE PROCEDURE [dbo].[TestProc2]
AS
BEGIN
SELECT 2
END
GO
USE [TargetDB1]
GO
CREATE ROLE [TestRole1]
GO
CREATE ROLE [TestRole2]
GO
CREATE TABLE [TestTable1] (
[id] INT NOT NULL IDENTITY (1,1)
)
GO
CREATE TABLE [TestTable2] (
[id] INT NOT NULL IDENTITY (1,1)
)
GO
USE [TargetDB2]
GO
CREATE ROLE [TestRole1]
GO
CREATE ROLE [TestRole2]
GO
CREATE TABLE [TestTable1] (
[id] INT NOT NULL IDENTITY (1,1)
)
GO
CREATE TABLE [TestTable2] (
[id] INT NOT NULL IDENTITY (1,1)
)
GO
-- =======================
-- ==== Begin Example ====
-- =======================
USE [Certificate_Administration]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET XACT_ABORT, NOCOUNT ON
BEGIN TRY
BEGIN TRANSACTION
-- Display old certificates and permission requests
SELECT * FROM [Permission].[Certificates]
SELECT * FROM [Permission].[PermissionRequests]
-- Clear out all old permissions and groups
EXEC [Permission].[DeleteAllSourceGroups]
EXEC [Permission].[RevokeAllObjects] 1
DECLARE
@src_db SYSNAME = 'SourceDB'
, @tar_db1 SYSNAME = 'TargetDB1'
, @tar_db2 SYSNAME = 'TargetDB2'
, @Group1 SYSNAME = 'Group1'
, @dbo SYSNAME = 'dbo'
, @TestProc1 SYSNAME = 'TestProc1'
, @TestProc2 SYSNAME = 'TestProc2'
, @TestTable1 SYSNAME = 'TestTable1'
, @TestTable2 SYSNAME = 'TestTable2'
, @TestRole1 SYSNAME = 'TestRole1'
, @TestRole2 SYSNAME = 'TestRole2'
-- Request permissions
-- ===================
-- ===== Group 1 =====
-- ===================
-- Group TestProc1 and TestProc2
EXEC [Permission].[AddSourceObjectToSourceGroupFromName] @src_db, @dbo, @TestProc1, @Group1
EXEC [Permission].[AddSourceObjectToSourceGroupFromName] @src_db, @dbo, @TestProc2, @Group1
-- Request permissions for the group
EXEC [Permission].[AddGroupPermissionRequestFromName] @src_db, @Group1, @tar_db1, @dbo, @TestTable1, 'SELECT'
EXEC [Permission].[AddGroupPermissionRequestFromName] @src_db, @Group1, @tar_db1, @dbo, @TestTable1, 'UPDATE'
-- Request roles for the group
EXEC [Permission].[AddGroupRoleRequestFromName] @src_db, @Group1, @tar_db1, @TestRole1
EXEC [Permission].[AddGroupRoleRequestFromName] @src_db, @Group1, @tar_db2, @TestRole2
-- =====================
-- ===== TestProc1 =====
-- =====================
-- Request permissions for TestProc1
EXEC [Permission].[AddObjectPermissionRequestFromName] @src_db, @dbo, @TestProc1, @tar_db2, @dbo, @TestTable1, 'ALTER'
-- Request roles for TestProc1
EXEC [Permission].[AddObjectRoleRequestFromName] @src_db, @dbo, @TestProc1, @tar_db1, @TestRole2
-- =====================
-- ===== TestProc2 =====
-- =====================
-- Request permissions for TestProc2
EXEC [Permission].[AddObjectPermissionRequestFromName] @src_db, @dbo, @TestProc2, @tar_db2, @dbo, @TestTable2, 'INSERT'
-- Request roles for TestProc2
EXEC [Permission].[AddObjectRoleRequestFromName] @src_db, @dbo, @TestProc2, @tar_db2, @TestRole1
-- ==========================
-- ===== Grant Requests =====
-- ==========================
-- View certificates and requests before they're granted
SELECT * FROM [Permission].[Certificates]
SELECT * FROM [Permission].[PermissionRequests]
-- Clear out items with no associated requests
EXEC [Permission].[CleanOrphanedItems]
-- Create certs and grant perms
EXEC [Permission].[CreateAllCertificatesAndUsers]
EXEC [Permission].[GrantAllSourceGroupRequests] 1
EXEC [Permission].[GrantAllSourceObjectRequests] 1
-- Display new certificates and permission requests
SELECT * FROM [Permission].[Certificates]
SELECT * FROM [Permission].[PermissionRequests]
COMMIT TRANSACTION
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION
; THROW
END CATCH
GO
-- =====================
-- ==== End Example ====
-- =====================
-- ==========================
-- ==== Clean Up Example ====
-- ==========================
-- Clear out all permissions and groups
EXEC [Permission].[DeleteAllSourceGroups]
EXEC [Permission].[RevokeAllObjects] 1
GO
USE master
GO
ALTER DATABASE [SourceDB] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
DROP DATABASE [SourceDB]
GO
ALTER DATABASE [TargetDB1] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
DROP DATABASE [TargetDB1]
GO
ALTER DATABASE [TargetDB2] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
DROP DATABASE [TargetDB2]
GO