You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This tutorial uses a scenario to illustrate [!INCLUDE[ssNoVersion](../includes/ssnoversion-md.md)] security concepts involving ownership chains and user context switching.
28
28
29
29
> [!NOTE]
30
-
> To run the code in this tutorial you must have both Mixed Mode security configured and the [!INCLUDE[ssSampleDBobject](../includes/sssampledbobject-md.md)] database installed. For more information about Mixed Mode security, see [Choose an Authentication Mode](../relational-databases/security/choose-an-authentication-mode.md).
30
+
> To run the code in this tutorial you must have both Mixed Mode security configured and the AdventureWorks2017 database installed. For more information about Mixed Mode security, see [Choose an Authentication Mode](../relational-databases/security/choose-an-authentication-mode.md).
31
31
32
32
## Scenario
33
-
In this scenario, two users need accounts to access purchase order data stored in the [!INCLUDE[ssSampleDBobject](../includes/sssampledbobject-md.md)] database. The requirements are as follows:
33
+
In this scenario, two users need accounts to access purchase order data stored in the AdventureWorks2017 database. The requirements are as follows:
34
34
35
35
- The first account (TestManagerUser) must be able to see all details in every purchase order.
36
-
37
36
- The second account (TestEmployeeUser) must be able to see the purchase order number, order date, shipping date, product ID numbers, and the ordered and received items per purchase order, by purchase order number, for items where partial shipments have been received.
38
-
39
-
- All other accounts must retain their current permissions.
40
-
37
+
- All other accounts must retain their current permissions.
41
38
To fulfill the requirements of this scenario, the example is broken into four parts that demonstrate the concepts of ownership chains and context switching:
42
39
43
-
1. Configuring the environment.
44
-
45
-
2. Creating a stored procedure to access data by purchase order.
46
-
40
+
1. Configuring the environment.
41
+
2. Creating a stored procedure to access data by purchase order.
47
42
3. Accessing the data through the stored procedure.
48
-
49
43
4. Resetting the environment.
50
44
51
-
Each code block in this example is explained in line. To copy the complete example, see [Complete Example](#CompleteExample) at the end of this tutorial.
45
+
Each code block in this example is explained in line. To copy the complete example, see [Complete Example](#CompleteExample) at the end of this tutorial.
46
+
47
+
## Prerequisites
48
+
To complete this tutorial, you need SQL Server Management Studio, access to a server that's running SQL Server, and an AdventureWorks database.
49
+
50
+
- Install [SQL Server Management Studio](https://docs.microsoft.com/sql/ssms/download-sql-server-management-studio-ssms).
51
+
- Install [SQL Server 2017 Developer Edition](https://www.microsoft.com/sql-server/sql-server-downloads).
For instructions on restoring a database in SQL Server Management Studio, see [Restore a database](https://docs.microsoft.com/sql/relational-databases/backup-restore/restore-a-database-backup-using-ssms).
52
55
53
56
## 1. Configure the Environment
54
-
Use [!INCLUDE[ssManStudioFull](../includes/ssmanstudiofull-md.md)] and the following code to open the `AdventureWorks2012` database, and use the `CURRENT_USER`[!INCLUDE[tsql](../includes/tsql-md.md)] statement to check that the dbo user is displayed as the context.
57
+
Use [!INCLUDE[ssManStudioFull](../includes/ssmanstudiofull-md.md)] and the following code to open the `AdventureWorks2017` database, and use the `CURRENT_USER`[!INCLUDE[tsql](../includes/tsql-md.md)] statement to check that the dbo user is displayed as the context.
55
58
56
59
```sql
57
-
USE AdventureWorks2012;
60
+
USE AdventureWorks2017;
58
61
GO
59
62
SELECTCURRENT_USERAS'Current User Name';
60
63
GO
61
64
```
62
65
63
66
For more information about the CURRENT_USER statement, see [CURRENT_USER (Transact-SQL)](../t-sql/functions/current-user-transact-sql.md).
64
67
65
-
Use this code as the dbo user to create two users on the server and in the [!INCLUDE[ssSampleDBobject](../includes/sssampledbobject-md.md)] database.
68
+
Use this code as the dbo user to create two users on the server and in the AdventureWorks2017 database.
66
69
67
70
```sql
68
71
CREATE LOGIN TestManagerUser
@@ -174,6 +177,12 @@ SELECT *
174
177
FROMPurchasing.PurchaseOrderDetail;
175
178
GO
176
179
```
180
+
181
+
The error that's returned:
182
+
```
183
+
Msg 229, Level 14, State 5, Line 6
184
+
The SELECT permission was denied on the object 'PurchaseOrderHeader', database 'AdventureWorks2017', schema 'Purchasing'.
185
+
```
177
186
178
187
Because the objects referenced by the stored procedure created in the last section are owned by `TestManagerUser` by virtue of the `Purchasing` schema ownership, `TestEmployeeUser` can access the base tables through the stored procedure. The following code, still using the `TestEmployeeUser` context, passes purchase order 952 as a parameter.
179
188
@@ -217,7 +226,7 @@ Last Updated: Books Online
217
226
Conditions: Execute as DBO or sysadmin in the AdventureWorks database
Instructions for restoring databases in SSMS are here: [Restore a database](https://docs.microsoft.com/sql/relational-databases/backup-restore/restore-a-database-backup-using-ssms).
46
+
For instructions on restoring a database in SQL Server Management Studio, see [Restore a database](https://docs.microsoft.com/sql/relational-databases/backup-restore/restore-a-database-backup-using-ssms).
47
47
48
48
## 1. Configure the Environment
49
49
To set the initial context of the example, in [!INCLUDE[ssManStudioFull](../includes/ssmanstudiofull-md.md)] open a new Query and run the following code to open the Adventureworks2017 database. This code changes the database context to `AdventureWorks2012` and creates a new server login and database user account (`TestCreditRatingUser`), using a password.
0 commit comments