Skip to content

Commit

Permalink
Fix sync database permissions in copy users
Browse files Browse the repository at this point in the history
  • Loading branch information
crimdon committed Jan 25, 2017
1 parent 85fb628 commit c8d44d4
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 84 deletions.
5 changes: 0 additions & 5 deletions DBAToolKit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DBAToolKit", "DBAToolKit\DB
EndProject
Project("{54435603-DBB4-11D2-8724-00A0C9A8B90C}") = "Setup", "Setup\Setup.vdproj", "{F53805A6-4A60-46A2-AEDE-82D1B500533A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{106EB47D-FEFB-496D-AC81-4BD61EBCB057}"
ProjectSection(SolutionItems) = preProject
README.md = README.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
163 changes: 84 additions & 79 deletions DBAToolKit/Tools/Copy-SqlLogin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ private void processLogins(Server destserver, string action)
foreach (Login sourcelogin in sourceserver.Logins)
{
string username = sourcelogin.Name;
Login destlogin = destserver.Logins[username];
string currentlogin = sourceserver.ConnectionContext.TrueLogin;
string servername = sourceserver.NetName.ToLower();
ItemToCopy item = new ItemToCopy();
Expand All @@ -91,8 +92,6 @@ private void processLogins(Server destserver, string action)

if (item.IsChecked)
{
Login destlogin = destserver.Logins[username];

if (username.StartsWith("##") || username == "sa" || username == "distributor_admin")
{
showOutput.displayOutput(string.Format("Skipping {0}.", username));
Expand Down Expand Up @@ -311,95 +310,101 @@ private void syncPermissions(Server sourceserver, Server destserver, string user
private void syncDatabasePerms(Login sourcelogin, Login destlogin, Server sourceserver, Server destserver)
{
// Remove user from destination if it does not exist on source
foreach (DatabaseMapping dbmap in destlogin.EnumDatabaseMappings())
if (destlogin.EnumDatabaseMappings() != null)
{
string dbname = dbmap.DBName;
Database destdb = destserver.Databases[dbname];
Database sourcedb = sourceserver.Databases[dbname];
string dbusername = dbmap.UserName;
string dbloginname = dbmap.LoginName;

if (DBChecks.DatabaseExists(sourceserver, destdb.Name) &&
!DBChecks.DatabaseUserExists(sourcedb, dbusername) && DBChecks.DatabaseUserExists(destdb, dbusername))
foreach (DatabaseMapping dbmap in destlogin.EnumDatabaseMappings())
{

try
{
DBFunctions.DropDBUser(sourcedb, destdb, dbusername);
}
catch (Exception ex)
string dbname = dbmap.DBName;
Database destdb = destserver.Databases[dbname];
Database sourcedb = sourceserver.Databases[dbname];
string dbusername = dbmap.UserName;
string dbloginname = dbmap.LoginName;

if (DBChecks.DatabaseExists(sourceserver, destdb.Name) &&
!DBChecks.DatabaseUserExists(sourcedb, dbusername) && DBChecks.DatabaseUserExists(destdb, dbusername))
{
showOutput.displayOutput(string.Format("Failed to drop user {0} From {1} on destination.", dbusername, dbname),true);
showOutput.displayOutput(ex.Message);
}

try
{
DBFunctions.RevokeDBPerms(sourcedb, destdb, dbusername);
}
catch (Exception ex)
{
showOutput.displayOutput(string.Format("Failed to revoke permission for user {0} on {1}.", dbusername, dbname),true);
showOutput.displayOutput(ex.Message, true);
try
{
DBFunctions.DropDBUser(sourcedb, destdb, dbusername);
}
catch (Exception ex)
{
showOutput.displayOutput(string.Format("Failed to drop user {0} From {1} on destination.", dbusername, dbname), true);
showOutput.displayOutput(ex.Message);
}

try
{
DBFunctions.RevokeDBPerms(sourcedb, destdb, dbusername);
}
catch (Exception ex)
{
showOutput.displayOutput(string.Format("Failed to revoke permission for user {0} on {1}.", dbusername, dbname), true);
showOutput.displayOutput(ex.Message, true);
}
}
}
}

// Add the database mappings and permissions
foreach (DatabaseMapping dbmap in sourcelogin.EnumDatabaseMappings())
{
string dbname = dbmap.DBName;
Database destdb = destserver.Databases[dbname];
Database sourcedb = sourceserver.Databases[dbname];
string dbusername = dbmap.UserName;
string dbloginname = dbmap.LoginName;

// Only if database exists on destination and its status is normal
if (DBChecks.DatabaseExists(destserver, sourcedb.Name) &&
DBChecks.LoginExists(destserver, dbloginname) && !DBChecks.DatabaseUserExists(destdb, dbusername)
&& destdb.Status == DatabaseStatus.Normal)
{
// Add DB User
try
{
DBFunctions.AddDBUser(destdb, dbusername);
}
catch (Exception ex)
{
showOutput.displayOutput(string.Format("Failed to add user {0} to database {1}", dbusername, dbname),true);
showOutput.displayOutput(ex.Message, true);
}

//Change the owner
if (sourcedb.Owner == dbusername)
{
DBFunctions.ChangeDbOwner(destserver, null, dbusername, dbname);
}

//Map the roles
try
if (sourcelogin.EnumDatabaseMappings() != null)
foreach (DatabaseMapping dbmap in sourcelogin.EnumDatabaseMappings())
{
DBFunctions.AddUserToDBRoles(sourcedb, destdb, dbusername);
string dbname = dbmap.DBName;
Database destdb = destserver.Databases[dbname];
Database sourcedb = sourceserver.Databases[dbname];
string dbusername = dbmap.UserName;
string dbloginname = dbmap.LoginName;

// Only if database exists on destination and its status is normal
if (DBChecks.DatabaseExists(destserver, sourcedb.Name) &&
DBChecks.LoginExists(destserver, dbloginname) && !DBChecks.DatabaseUserExists(destdb, dbusername)
&& destdb.Status == DatabaseStatus.Normal)
{
// Add DB User
try
{
DBFunctions.AddDBUser(destdb, dbusername);
}
catch (Exception ex)
{
showOutput.displayOutput(string.Format("Failed to add user {0} to database {1}", dbusername, dbname), true);
showOutput.displayOutput(ex.Message, true);
}

//Change the owner
if (sourcedb.Owner == dbusername)
{
DBFunctions.ChangeDbOwner(destserver, null, dbusername, dbname);
}

//Map the roles
try
{
DBFunctions.AddUserToDBRoles(sourcedb, destdb, dbusername);
}
catch (Exception ex)
{
showOutput.displayOutput(string.Format("Error adding user {0} to role on database {1}", dbusername, dbname), true);
showOutput.displayOutput(ex.Message, true);
}

//Map permissions

try
{
DBFunctions.GrantDBPerms(sourcedb, destdb, dbusername);
}
catch (Exception ex)
{
showOutput.displayOutput(string.Format("Error granting permission for user {0} on database {1}", dbusername, dbname), true);
showOutput.displayOutput(ex.Message, true);
}
}
showOutput.displayOutput(string.Format("Database permissions synced for user {0} on database {1}", dbusername, dbname));
}
catch (Exception ex)
{
showOutput.displayOutput(string.Format("Error adding user {0} to role on database {1}", dbusername, dbname),true);
showOutput.displayOutput(ex.Message, true);
}

//Map permissions

try
{
DBFunctions.GrantDBPerms(sourcedb, destdb, dbusername);
}
catch (Exception ex)
{
showOutput.displayOutput(string.Format("Error granting permission for user {0} on database {1}", dbusername, dbname),true);
showOutput.displayOutput(ex.Message, true);
}
}
showOutput.displayOutput(string.Format("Database permissions synced for user {0} on database {1}", dbusername, dbname));
}
}
private void setupJobList()
Expand Down

0 comments on commit c8d44d4

Please sign in to comment.