Esempio n. 1
0
        private void syncDatabasePerms(Login sourcelogin, Login destlogin, Server sourceserver, Server destserver)
        {
            // Remove user from destination if it does not exist on source
            if (destlogin.EnumDatabaseMappings() != null)
            {
                foreach (DatabaseMapping dbmap in destlogin.EnumDatabaseMappings())
                {
                    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))
                    {
                        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
            {
                if (sourcelogin.EnumDatabaseMappings() != null)
                {
                    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
                            {
                                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));
                    }
                }
            }
        }