Repository navigation
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
Open
andreasjordan wants to merge 2 commits into
andreasjordan wants to merge 2 commits into
Conversation
… 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>
andreasjordan
marked this pull request as ready for review
November 22, 2026 17:43
(do Copy-DbaLogin, Sync-DbaLoginPermission)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.EnumDatabasePermissions($newLoginName)on the destination,EnumDatabasePermissions($loginName)on the source, and the revoke went to$newLoginName.EnumDatabasePermissions($loginName), and the role memberships were checked withEnumMembers() -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), aREVOKEwithoutAStakes back only the grant of dbo when a user holds the same permission from two grantors.Change
EnumDatabaseMappings()of both logins, which the grant part already did for the source user and which Export-DbaLogin does too.-ccontains, both names come from the source database), and the permissions are read from the source 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, betweenInstanceMulti1andInstanceMulti2, one database and two logins:dbatoolsci_useron both servers. Source: VIEW DEFINITION and membership in the roledbatoolsci_userrole. Destination: ALTER ANY SCHEMA, and CREATE TABLE granted by dbo and by the userdbatoolsci_grantor, plus a user without login that has the name of the login, with VIEW DATABASE STATE.dbatoolsci_sourceuseron the source anddbatoolsci_destuseron 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_grantorkeeps its own, and that the user with the name of the login keeps VIEW DATABASE STATE. All 7 fail ondevelopment067b215, 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.ps117 passed,Copy-DbaLogin.Tests.ps186 passed and 12 skipped,Sync-DbaAvailabilityGroup.Tests.ps112 passed anddbatools.Tests.ps188 passed and 6 skipped, each on Windows PowerShell 5.1 and on PowerShell 7.6.🤖 Generated with Claude Code