Example #1
0
        private ToolStripMenuItem createChangePasswordMenuItem(HostPwEntry hostPwEntry)
        {
            IPasswordChanger pwChanger = null;
            var hostTypeMapper         = new HostTypeMapper(new HostTypeSafeConverter());
            var hostType = hostTypeMapper.Get(hostPwEntry);

            if (hostType == HostType.ESXi && QuickConnectUtils.IsVSpherePowerCLIInstalled())
            {
                pwChanger = new ESXiPasswordChanger();
            }
            else if (hostType == HostType.Windows &&
                     !String.IsNullOrEmpty(this.Settings.PsPasswdPath) &&
                     File.Exists(this.Settings.PsPasswdPath) &&
                     PsPasswdWrapper.IsPsPasswdUtility(this.Settings.PsPasswdPath) &&
                     PsPasswdWrapper.IsSupportedVersion(this.Settings.PsPasswdPath)
                     )
            {
                pwChanger = new WindowsPasswordChanger(new PsPasswdWrapper(this.Settings.PsPasswdPath));
            }
            else if (hostType == HostType.Linux)
            {
                PuttyOptions puttyOptions = null;
                bool         success      = PuttyOptions.TryParse(hostPwEntry.AdditionalOptions, out puttyOptions);
                // Disable change password menu item if authentication is done using SSH key file.
                if (!success || (success && !puttyOptions.HasKeyFile()))
                {
                    int?sshPort = null;
                    if (success)
                    {
                        sshPort = puttyOptions.Port;
                    }
                    pwChanger = new LinuxPasswordChanger()
                    {
                        SshPort = sshPort
                    };
                }
            }
            var menuItem = new ToolStripMenuItem()
            {
                Text    = ChangePasswordMenuItemText,
                Enabled = hostPwEntry.HasIPAddress && pwChanger != null
            };

            menuItem.Click += new EventHandler(
                delegate(object obj, EventArgs ev) {
                try {
                    var pwDatabase       = new PasswordDatabase(this.pluginHost.Database);
                    var pwChangerService = new PasswordChangerServiceWrapper(pwDatabase, pwChanger, new SystemClock());
                    using (var form = new FormPasswordChanger(hostPwEntry, pwChangerService)) {
                        form.ShowDialog();
                        if (form.Changed)
                        {
                            refreshUI();
                        }
                    }
                }
                catch (Exception ex) {
                    log(ex);
                }
            }
                );
            return(menuItem);
        }