Skip to content

Sync-DbaLoginPermission, Copy-DbaLogin - Sync database permissions by the database user, not by the name of the login - #10863

Open
andreasjordan wants to merge 2 commits into
developmentfrom
sync-dbaloginpermission-database-user
Open

andreasjordan wants to merge 2 commits into
developmentfrom
sync-dbaloginpermission-database-user

Conversation

@andreasjordan

Copy link
Copy Markdown
Collaborator

Fixes #10837

Problem

Update-SqlPermission, the private helper behind Copy-DbaLogin (every created and updated login) and Sync-DbaLoginPermission, synced the database permissions of a login by the name of the login. A database principal is a user, and a user can have another name than its login, on each server another one.

  • Revoke part: EnumDatabasePermissions($newLoginName) on the destination, EnumDatabasePermissions($loginName) on the source, and the revoke went to $newLoginName.
  • Grant part: the permissions came from EnumDatabasePermissions($loginName), and the role memberships were checked with EnumMembers() -contains $loginName.

So a user with another name than its login kept permissions the source user does not have, and did not get the permissions or roles it has there, while an unrelated user that only had the name of the login lost its permissions. A user of another name on each server failed with "The login already has an account under a different user name", because the grant part tried to create a second user for the login.

The revoke also ran without AS <grantor>. As for the server permissions in #10801 (0fc03cb), a REVOKE without AS takes back only the grant of dbo when a user holds the same permission from two grantors.

Change

  • The users of the two logins are paired per database through EnumDatabaseMappings() of both logins, which the grant part already did for the source user and which Export-DbaLogin does too.
  • Revoke part: the permissions of the destination user against those of its paired source user. A source login without a user in that database has no permission there that could stay, so everything is revoked, as before. Each row is revoked as its grantor, with the five-argument overload the server part uses.
  • Grant part: a user the destination login already has in a database is its user there, whatever its name. Role memberships are compared by the user (-ccontains, both names come from the source database), and the permissions are read from the source user.
  • dbo, the user of the owner of a database, is left out of the permission parts, as it already was of the role parts. SQL Server grants and revokes nothing to dbo, and the pairing made it reachable: dbo has its own CONNECT row in every new database (lab 2019, 2022 and 2025), which would have been revoked for a login without a user on the source.
  • The messages name the user.

Not changed: the server part, the creation of users, and the drop of a user that exists on neither server. That drop ($null -eq $sourceDb.Users[$dbUsername] -and $null -eq $destDb.Users[$newDbUsername]) is never reached, because the loop runs over the users the destination login has.

Tests

New context "When the database user of a login has another name than the login (#10837)" in Sync-DbaLoginPermission.Tests.ps1, between InstanceMulti1 and InstanceMulti2, one database and two logins:

  • login A has the user dbatoolsci_user on both servers. Source: VIEW DEFINITION and membership in the role dbatoolsci_userrole. Destination: ALTER ANY SCHEMA, and CREATE TABLE granted by dbo and by the user dbatoolsci_grantor, plus a user without login that has the name of the login, with VIEW DATABASE STATE.
  • login B has dbatoolsci_sourceuser on the source and dbatoolsci_destuser on the destination. Source: CREATE VIEW. Destination: CREATE FUNCTION.

The 7 tests check that both logins sync without a warning, that the users get the permissions and the role of their source users, that ALTER ANY SCHEMA and both grants of CREATE TABLE are revoked while dbatoolsci_grantor keeps its own, and that the user with the name of the login keeps VIEW DATABASE STATE. All 7 fail on development 067b215, login B there with "The login already has an account under a different user name".

Lab set03 (SQL Server 2025 as source, 2022 as destination): Sync-DbaLoginPermission.Tests.ps1 17 passed, Copy-DbaLogin.Tests.ps1 86 passed and 12 skipped, Sync-DbaAvailabilityGroup.Tests.ps1 12 passed and dbatools.Tests.ps1 88 passed and 6 skipped, each on Windows PowerShell 5.1 and on PowerShell 7.6.

🤖 Generated with Claude Code

… the database user, not by the name of the login

Update-SqlPermission read the database permissions of a login, its
database role memberships and the permissions it revoked by the name of
the login. A database principal is a user, and a user can have another
name than its login. Such a user kept permissions the source user does
not have and did not get the ones it has or its roles, while an
unrelated user that had the name of the login lost its permissions. A
user of another name on each server failed with "The login already has
an account under a different user name".

The users of the two logins are now paired per database through the
EnumDatabaseMappings of both logins, as the grant part already did for
the source user and as Export-DbaLogin does. The revoke runs as the
grantor of each row, as the server part does since 0fc03cb: a REVOKE
without AS takes back only the grant of dbo. dbo, the user of the owner
of a database, is left out of the permission parts as it already was of
the role parts: SQL Server grants and revokes nothing to it, and its own
CONNECT row would have been revoked for a login without a user on the
source.

Not changed: the server part, the creation of users, and the drop of a
user that exists on neither server, which is never reached.

The new context of Sync-DbaLoginPermission.Tests.ps1 sets up both cases
in one database. Its 7 tests all fail on development 067b215.

Lab set03 (SQL Server 2025 to 2022): Sync-DbaLoginPermission 17 passed,
Copy-DbaLogin 86 passed and 12 skipped, on Windows PowerShell 5.1 and
on PowerShell 7.6. Sync-DbaAvailabilityGroup 12 passed and dbatools 88
passed and 6 skipped on 5.1.

Fixes #10837

(do Sync-DbaLoginPermission, Copy-DbaLogin)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(do Copy-DbaLogin, Sync-DbaLoginPermission)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sync-DbaLoginPermission and Copy-DbaLogin - Database permissions are synced by the name of the login instead of the database user

2 participants