SQL Server Performance

  • Home
  • Articles
  • Forums
  • Tips
  • Training
  • FAQ's
  • Blogs
  • Software
  • Books
  • About Us
RSS Feeds
Sign in | Join


Article Topics

All Articles
Performance Tuning
Audit
Business Intelligence
Clustering
Reporting Services
SQL Azure
Developer
General DBA
ASP.NET / ADO.NET
SQL Azure

USEFUL SITES :

ASP.NET Tutorials
Windows and SQL Azure Tutorials
Cloud Hosting Magazine
SharePoint Tutorials
Windows Server Help

Write for Us

Share your SQL Server knowledge with others and raise your profile in the community More...
Latest Articles

A High Level Comparison Between Oracle and SQL Server - Part ...
A High Level Comparison Between Oracle and SQL Server - Part ...
A High Level Comparison Between Oracle and SQL Server
SQL Server 2008 R2 Multi-server Administration - A First Look ...

More     
 
Latest FAQ's

Add Node to A SQL Server failover Cluster failed with invalid ...
SQL Server Destination remote server error
Setting Up Data And Log Files For SQL Server
Will Check Constraints Improve Database Performance?

More     
   
Latest Software Reviews

dbForge Review
Spotlight on ApexSQL Diff - Server-based database comparison tool ...
Spotlight on ApexSQL Data Diff - Server-based database comparison tool ...
Spotlight on ApexSQL Doc 2008

More     

articles >> general dba >> Context Switching or Database Impersonation using EXEC ...

Context Switching or Database Impersonation using EXEC AS

By : Arshad Ali
Jul 06, 2009

Page 2 / 3

//

//

The switching of execution context of the module is valid only for the duration of the module execution or as long as no  REVERT statement is used or   switching the context to another login/user or session is disconnected.

Example: - In this example, we  create a stored procedure which will be called by a database user to log an error in a log table and will be executed in the execution context of the caller. This stored procedure will first check if the log table already exists, if not it will impersonate the database owner (which has privileges to create a table) to create the log table and grant permission to INSERT and SELECT from the table to the database user and finally revert the execution context back to caller.

USE AdventureWorks;

GO

-- Create two temporary principals for demonstration.

CREATELOGIN DBUserLogin WITHPASSWORD='ABCD1234!@#$';

CREATELOGIN DBOwnerLogin WITHPASSWORD='ZXCV9876(*&';

GO

-- Create two users and map them with created logins

CREATEUSER DBUser FORLOGIN DBUserLogin;

CREATEUSER DBOwner FORLOGIN DBOwnerLogin;

GO

-- Give IMPERSONATE permissions on DBOwner to DBUser

-- so that DBUser can successfully set the execution context to DBOwner.

GRANT IMPERSONATE ONUSER:: DBOwner TO DBUser;

GO

-- Add DBOwner user to the db_owner role 

EXECsp_addrolemember'db_owner','DBOwner'

GO

-- Create a procedure which logs error message into log table if the table

-- already exists or else create the table first and logs the error message

CREATEPROCEDURE dbo.LogError(@ErrorMessage VARCHAR(200))

WITHEXECUTEASCALLER

AS

BEGIN

       -- Display the execution context (Login and User) of the user executing this section of the code for demonstration 

       SELECTSUSER_NAME()AS [LoginName],USER_NAME()AS [UserName];

       IFOBJECT_ID('GlobalLog')ISNULL

       BEGIN

              -- Switching execution context to DBOwner, a more privileged user to create the GlobalLog table

              EXECUTEASUSER='DBOwner';

              -- Display the execution context (Login and User) of the user executing this section of the code for demonstration

              SELECTSUSER_NAME()AS [LoginName],USER_NAME()AS [UserName];

              CREATETABLE GlobalLog

              (

                     [ID]                 INTPRIMARYKEYIDENTITY,

                     ErrorMessage  VARCHAR(200)

              );

              -- Grant user permission to insert and select from the newly created GlobalLog table

              GRANTSELECT, INSERTON GlobalLog TO DBUser;

              -- Reverting back the execution context to the original caller to limit the permission

              REVERT;

              -- Display the execution context (Login and User) of the user executing this section of the code for demonstration

              SELECTSUSER_NAME()AS [LoginName],USER_NAME()AS [UserName];

       END

       -- Display the execution context (Login and User) of the user executing this section of the code for demonstration

       SELECTSUSER_NAME()AS [LoginName],USER_NAME()AS [UserName];

       -- Insert the log record

       INSERTINTO GlobalLog( ErrorMessage )VALUES ( @ErrorMessage )

END

GO

-- Grant user permission to execute the stored procedure 

GRANTEXECUTEON dbo.LogError TO DBUser



<< Prev Page     Next Page>>    








C# Help and Tutorials | PHP MySQL Tutorial | Sharepoint Tutorial | Azure Tutorial | Cloud Hosting Magazine | ASP.NET Tutorials | Windows Server Help | Windows Phone Pro | Silverlight Ace | Visual Studio Tutorials | Home | Peformance Articles | Audit Articles | Business Intelligence Articles | Clustering Articles | Developer Articles | Reporting Services Articles | DBA Articles | ASP.NET / ADO.NET Articles | SQL Server Training Videos | DBA FAQ's | Developer Peformance FAQ's | DBA Peformance FAQ's | Developer FAQ's | Clustering FAQ's | Error Messages | Audit Tool Reviews | Backup Tool Reviews | Coding Tool Reviews | Compare Tool Reviews | Documentation Tool Reviews | Design Tool Reviews | Monitoring Tool Reviews | Log Tool Reviews | Reporting Tool Reviews | Clustering Tool Reviews | Security Tool Reviews | Change Management Tool Reviews | Remote Access Tool Reviews | Book Reviews | Security Tool Reviews | ADO.NET / ASP.NET | Administration | Analysis/OLAP Services | Application Development | Configuration | Components | ETL | Hardware | High Availability | Hints | Index | Misc | Operating Systems | Performance Tuning | Replication | T-SQL | Views


              © 2010 Jude O'Kelly. All rights reserved