MSDN Documentation

SQL Server Developer: Security - Authorization

Understanding Authorization in SQL Server

Authorization is the process of granting or denying specific permissions to users and roles on database objects. While authentication verifies who a user is, authorization determines what that user is allowed to do.

Core Concepts of Authorization

Permissions Hierarchy

Permissions are often organized in a hierarchy. Granting a permission at a higher level in the hierarchy implicitly grants it to lower-level objects, unless explicitly denied. For example, granting CONTROL permission on a database typically grants control over all objects within that database.

Types of Permissions

SQL Server supports two main types of permissions:

Common Authorization Scenarios

Granting Permissions to Users

You can grant specific permissions to database users using the GRANT statement:

-- Grant SELECT and INSERT permissions on the 'Customers' table to the 'SalesUser'
GRANT SELECT, INSERT ON dbo.Customers TO SalesUser;

-- Grant EXECUTE permission on a stored procedure
GRANT EXECUTE ON dbo.usp_UpdateOrderStatus TO OrderProcessor;

Denying Permissions

The DENY statement explicitly prevents a principal from performing an action, even if they might otherwise have permission through membership in a role.

-- Explicitly deny DELETE permission on the 'Products' table to 'GuestUser'
DENY DELETE ON dbo.Products TO GuestUser;

It's generally recommended to use roles for managing permissions rather than granting/denying directly to individual users, as it simplifies management.

Revoking Permissions

The REVOKE statement removes previously granted or denied permissions.

-- Revoke SELECT permission on the 'Orders' table from 'ReadOnlyUser'
REVOKE SELECT ON dbo.Orders TO ReadOnlyUser;

Roles for Authorization Management

Roles are securable objects that act as containers for permissions. You can add database users to roles, and then grant permissions to the roles. This is a best practice for managing large numbers of users and complex permission structures.

-- Create a new database role
CREATE ROLE ReadOnlyRole;

-- Grant SELECT permission on all tables in the current database to the role
GRANT SELECT ON SCHEMA::dbo TO ReadOnlyRole;

-- Add a user to the role
ALTER ROLE ReadOnlyRole ADD MEMBER UserAlice;

Fixed Database Roles

SQL Server provides several built-in fixed database roles that offer common sets of permissions:

Fixed Server Roles

Similarly, there are fixed server roles for managing server-level permissions:

Granting membership to fixed roles like sysadmin or db_owner should be done with extreme caution due to the broad access they provide.

Best Practices for Authorization

Effective authorization is a cornerstone of SQL Server security, protecting your data from unauthorized access and modification.