授予用户创建新数据库的权限,但拒绝现有数据库的权限

Hus*_*uss 2 sql-server sql-server-2017

I got a request from our developers that,

  • They need a new user that need rights to create new databases
  • Alter those but should not be able to access the existing ones.

What's best practice for this kind of access?

Grant dbcreator and deny access on existing ones or is there a better way?

D K*_*mer 5

默认情况下,SQL Server dbcreator 不授予您访问数据库的权限。看起来可能是这样,因为当您创建新数据库时,您(通常,除非另有说明)自动成为该数据库的所有者。数据库的所有者可以执行该数据库中的所有操作。

但是,如果登录名具有 dbcreator 服务器角色,他们仍然可以删除不属于他们的数据库。他们可以创建新的数据库或更改现有的数据库。

如果您的目的纯粹是允许他们创建新数据库并仅管理这些数据库。您应该只授予他们创建权限。

USE MASTER
GO
GRANT CREATE ANY DATABASE TO Foo
GO
Run Code Online (Sandbox Code Playgroud)

这仍然允许他们对他们创建的数据库运行 ALTER 语句(因为他们是所述数据库的所有者)。但不要对现有数据库运行 alter(或 drop)语句。

USE MASTER
GO
CREATE DATABASE [OwnedDB] --Succeeds
GO
ALTER DATABASE [OwnedDB] SET RECOVERY SIMPLE WITH NO_WAIT --Succeeds
GO
ALTER DATABASE [UnownedDB] SET RECOVERY SIMPLE WITH NO_WAIT --Fails due to permissions
GO
Run Code Online (Sandbox Code Playgroud)