SQL Server - 根据登录限制数据库视图

发布于 2024-08-20 22:42:26 字数 541 浏览 2 评论 0原文

我有一种情况,我想限制每个用户登录的数据库视图。

例如: 我的 SQL Server 实例中有 3 个数据库,分别为 MyDB、UserDB、RestrictedDB。 然后我创建了 2 个登录名;用户1和用户2。 然后,我执行了拒绝查看任何数据库到 [User1],然后拒绝查看任何数据库到 [User2]。 然后,我通过执行 sp_changedbowner 'User1' 使 User1 成为 UserDB 的所有者。

当我以 User1 身份登录时,我只能看到 UserDB,这正是我想要的。但是当然,当我以 User2 身份登录时,他无法查看任何数据库。

我也通过执行 sp_addrolemember 'db_owner','User2' 将 User2 设置为 db_owner 并再次登录,但我仍然看不到 UserDB。我没有尝试在 User2 上执行 sp_changedbowner,因为我认为结果是 User2 将能够看到 UserDB,但看不到 User1。

我需要的是 User1 和 User1 User2 只能看到 UserDB。我该怎么做呢?

提前致谢!

I have a situation where I want to limit the database view per user login.

For example:
I have 3 databases in my SQL Server instance called MyDB, UserDB, RestrictedDB.
I then create a 2 logins; User1 and User2.
I then did a DENY VIEW ANY DATABASE TO [User1] and then DENY VIEW ANY DATABASE TO [User2].
I then made User1 the owner of UserDB by executing sp_changedbowner 'User1'

When I login as User1, I can only see UserDB which is exactly what I want. But of course when I login as User2, there is no database that he can view.

I made User2 as a db_owner as well by executing sp_addrolemember 'db_owner','User2' and logged in again but I still can't see UserDB. I did not attempt to execute sp_changedbowner on User2 as I think the result would be that User2 will be able to see UserDB but not User1.

What I need is for both User1 & User2 to be able to see just UserDB. How would I go about doing this?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

单身情人 2024-08-27 22:42:26

您可以设置一个安全角色,授予对表、视图及其所有访问权限的权限。将所需的用户与您创建的角色相关联,您将获得所需的结果。

以下内容应创建角色、登录名、用户,并为相关视图提供仅 SELECT 权限。相同的方法可以应用于您想要的任何权限。

CREATE ROLE [db_views_role] AUTHORIZATION [dbo]
GO
GRANT SELECT ON [dbo].[vMyView] TO [db_views_role]
GO
CREATE LOGIN [my_login] WITH PASSWORD=N'PASSWORD', DEFAULT_DATABASE=[MyDatabase], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
GO
CREATE USER [my_login] FOR LOGIN [my_login]
GO
EXEC sp_addrolemember N'db_views_role', N'my_login'
GO

You can setup a security role granting privleges on tables, views, and their access all of that. Associate the user desired to the role you have created and you will get the desired result.

The following should create a role, login, user and provide SELECT only permission to the view in question. The same methods can be applied for whatever permissions you wish.

CREATE ROLE [db_views_role] AUTHORIZATION [dbo]
GO
GRANT SELECT ON [dbo].[vMyView] TO [db_views_role]
GO
CREATE LOGIN [my_login] WITH PASSWORD=N'PASSWORD', DEFAULT_DATABASE=[MyDatabase], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
GO
CREATE USER [my_login] FOR LOGIN [my_login]
GO
EXEC sp_addrolemember N'db_views_role', N'my_login'
GO
煞人兵器 2024-08-27 22:42:26

需要通过创建映射到登录名的数据库用户来将登录名与数据库关联起来。

The logins need to be associated with the databases by creating database users mapping to the logins.

若言繁花未落 2024-08-27 22:42:26

在 SQL Server 中,有服务器登录数据库用户。尝试通过以下方式将服务器登录关联到特定数据库:

Database >安全>用户>新用户...

In SQL Server, there are Server Logins and Database users. Try associated the Server Login to the specific database by:

Database > Security > Users > New User...

偏爱自由 2024-08-27 22:42:26

在 Oracle 中,这是通过“GRANT”命令完成的。我不知道SQL Server是否有GRANT

GRANT被使用:

GRANT [permissions] ON TABLE to [user]

所以

GRANT select, insert, update, delete ON TABLE to User2

In Oracle, this is done via the 'GRANT' command. I don't know is SQL Server has GRANT

GRANT is used:

GRANT [permissions] ON TABLE to [user]

so

GRANT select, insert, update, delete ON TABLE to User2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文