Permalink
Browse files

Names of newly created favorites could be overwritten with the host n…

…ame under specific conditions (#3015)

Also minified some duplicate code along the way
  • Loading branch information...
dmoagx committed Apr 3, 2018
1 parent 75781e0 commit 8ceb5be8c231395cdc72b0646dd494c0c5b4e233
Showing with 23 additions and 60 deletions.
  1. +23 −60 Source/SPConnectionController.m
@@ -1318,6 +1318,14 @@ - (void)_saveCurrentDetailsCreatingNewFavorite:(BOOL)createNewFavorite validateD
}
theFavorite = [self selectedFavorite];
}
void (^_setOrRemoveKey)(NSString *, id) = ^(NSString *key, id value) {
if (value) {
[theFavorite setObject:value forKey:key];
} else {
[theFavorite removeObjectForKey:key];
}
};
// Set the name - either taking the provided name, or generating one.
if ([[self name] length]) {
@@ -1332,75 +1340,27 @@ - (void)_saveCurrentDetailsCreatingNewFavorite:(BOOL)createNewFavorite validateD
// Set standard details for the connection
[theFavorite setObject:[NSNumber numberWithInteger:[self type]] forKey:SPFavoriteTypeKey];
if ([self host]) {
[theFavorite setObject:[self host] forKey:SPFavoriteHostKey];
} else {
[theFavorite removeObjectForKey:SPFavoriteHostKey];
}
if ([self socket]) {
[theFavorite setObject:[self socket] forKey:SPFavoriteSocketKey];
} else {
[theFavorite removeObjectForKey:SPFavoriteSocketKey];
}
if ([self user]) {
[theFavorite setObject:[self user] forKey:SPFavoriteUserKey];
} else {
[theFavorite removeObjectForKey:SPFavoriteUserKey];
}
if ([self port]) {
[theFavorite setObject:[self port] forKey:SPFavoritePortKey];
} else {
[theFavorite removeObjectForKey:SPFavoritePortKey];
}
if ([self database]) {
[theFavorite setObject:[self database] forKey:SPFavoriteDatabaseKey];
} else {
[theFavorite removeObjectForKey:SPFavoriteDatabaseKey];
}
_setOrRemoveKey(SPFavoriteHostKey, [self host]);
_setOrRemoveKey(SPFavoriteSocketKey, [self socket]);
_setOrRemoveKey(SPFavoriteUserKey, [self user]);
_setOrRemoveKey(SPFavoritePortKey, [self port]);
_setOrRemoveKey(SPFavoriteDatabaseKey, [self database]);
[theFavorite setObject:[NSNumber numberWithInteger:[self colorIndex]] forKey:SPFavoriteColorIndexKey];
// SSL details
[theFavorite setObject:[NSNumber numberWithInteger:[self useSSL]] forKey:SPFavoriteUseSSLKey];
[theFavorite setObject:[NSNumber numberWithInteger:[self sslKeyFileLocationEnabled]] forKey:SPFavoriteSSLKeyFileLocationEnabledKey];
if ([self sslKeyFileLocation]) {
[theFavorite setObject:[self sslKeyFileLocation] forKey:SPFavoriteSSLKeyFileLocationKey];
} else {
[theFavorite removeObjectForKey:SPFavoriteSSLKeyFileLocationKey];
}
_setOrRemoveKey(SPFavoriteSSLKeyFileLocationKey, [self sslKeyFileLocation]);
[theFavorite setObject:[NSNumber numberWithInteger:[self sslCertificateFileLocationEnabled]] forKey:SPFavoriteSSLCertificateFileLocationEnabledKey];
if ([self sslCertificateFileLocation]) {
[theFavorite setObject:[self sslCertificateFileLocation] forKey:SPFavoriteSSLCertificateFileLocationKey];
} else {
[theFavorite removeObjectForKey:SPFavoriteSSLCertificateFileLocationKey];
}
_setOrRemoveKey(SPFavoriteSSLCertificateFileLocationKey, [self sslCertificateFileLocation]);
[theFavorite setObject:[NSNumber numberWithInteger:[self sslCACertFileLocationEnabled]] forKey:SPFavoriteSSLCACertFileLocationEnabledKey];
if ([self sslCACertFileLocation]) {
[theFavorite setObject:[self sslCACertFileLocation] forKey:SPFavoriteSSLCACertFileLocationKey];
} else {
[theFavorite removeObjectForKey:SPFavoriteSSLCACertFileLocationKey];
}
_setOrRemoveKey(SPFavoriteSSLCACertFileLocationKey, [self sslCACertFileLocation]);
// SSH details
if ([self sshHost]) {
[theFavorite setObject:[self sshHost] forKey:SPFavoriteSSHHostKey];
} else {
[theFavorite removeObjectForKey:SPFavoriteSSHHostKey];
}
if ([self sshUser]) {
[theFavorite setObject:[self sshUser] forKey:SPFavoriteSSHUserKey];
} else {
[theFavorite removeObjectForKey:SPFavoriteSSHUserKey];
}
if ([self sshPort]) {
[theFavorite setObject:[self sshPort] forKey:SPFavoriteSSHPortKey];
} else {
[theFavorite removeObjectForKey:SPFavoriteSSHPortKey];
}
_setOrRemoveKey(SPFavoriteSSHHostKey, [self sshHost]);
_setOrRemoveKey(SPFavoriteSSHUserKey, [self sshUser]);
_setOrRemoveKey(SPFavoriteSSHPortKey, [self sshPort]);
[theFavorite setObject:[NSNumber numberWithInteger:[self sshKeyLocationEnabled]] forKey:SPFavoriteSSHKeyLocationEnabledKey];
if ([self sshKeyLocation]) {
[theFavorite setObject:[self sshKeyLocation] forKey:SPFavoriteSSHKeyLocationKey];
} else {
[theFavorite removeObjectForKey:SPFavoriteSSHKeyLocationKey];
}
_setOrRemoveKey(SPFavoriteSSHKeyLocationKey, [self sshKeyLocation]);
/**
@@ -1535,6 +1495,9 @@ - (void)_saveCurrentDetailsCreatingNewFavorite:(BOOL)createNewFavorite validateD
[self _sortFavorites];
[self _scrollToSelectedNode];
}
// after saving the favorite, the name is never autogenerated (ie. overridable), regardless of the value (#3015)
favoriteNameFieldWasAutogenerated = NO;
[[NSNotificationCenter defaultCenter] postNotificationName:SPConnectionFavoritesChangedNotification object:self];
#endif

0 comments on commit 8ceb5be

Please sign in to comment.