Files
123123/ZA.CoreService.ESBCertificateManager/Setup/AddCredentialProfileForm.cs
T
2026-07-28 08:03:12 +02:00

450 lines
10 KiB
C#

using System.Drawing;
using System.Windows.Forms;
using ZA.CoreService.ESBCertificateManager.Models;
namespace ZA.CoreService.ESBCertificateManager.Setup;
public sealed class AddCredentialProfileForm : Form
{
private readonly SetupCoordinator _coordinator;
private readonly SonicSystemOption _system;
private readonly TextBox _txtProfileName;
private readonly TextBox _txtUserName;
private readonly TextBox _txtPassword;
private readonly TextBox _txtPasswordRepeat;
private readonly CheckBox _chkDefault;
private readonly Button _btnSave;
private readonly Button _btnCancel;
private bool _operationRunning;
public SonicCredentialProfile? CreatedProfile
{
get;
private set;
}
public AddCredentialProfileForm(
SetupCoordinator coordinator,
SonicSystemOption system)
{
_coordinator =
coordinator
?? throw new ArgumentNullException(
nameof(coordinator));
_system =
system
?? throw new ArgumentNullException(
nameof(system));
Text = "Benutzerprofil hinzufügen";
StartPosition =
FormStartPosition.CenterParent;
ClientSize =
new Size(560, 525);
FormBorderStyle =
FormBorderStyle.FixedDialog;
MaximizeBox = false;
MinimizeBox = false;
ShowInTaskbar = false;
BackColor =
Color.FromArgb(10, 18, 34);
ForeColor =
Color.FromArgb(241, 245, 249);
Font =
new Font("Segoe UI", 10);
Label title = new()
{
Text =
$"Profil für {_system.ConnectionName}",
Location =
new Point(28, 22),
AutoSize = true,
ForeColor =
Color.FromArgb(241, 245, 249),
Font =
new Font(
"Segoe UI",
17,
FontStyle.Bold)
};
Label details = new()
{
Text =
$"Umgebung: {_system.EnvironmentCode}"
+ Environment.NewLine
+ $"Domain: {_system.DomainName}"
+ Environment.NewLine
+ $"Adresse: {_system.ConnectionUrl}"
+ Environment.NewLine
+ $"Prüfcontainer: "
+ $"{_system.ValidationContainerName}",
Location =
new Point(30, 62),
Size =
new Size(495, 90),
ForeColor =
Color.FromArgb(169, 184, 200)
};
Label profileLabel =
CreateLabel(
"Profilname",
160);
_txtProfileName =
CreateTextBox(185);
_txtProfileName.Text =
"Administrator";
Label userLabel =
CreateLabel(
"Sonic-Benutzername",
225);
_txtUserName =
CreateTextBox(250);
_txtUserName.Text =
"Administrator";
Label passwordLabel =
CreateLabel(
"Kennwort",
290);
_txtPassword =
CreateTextBox(315);
_txtPassword.UseSystemPasswordChar =
true;
Label repeatLabel =
CreateLabel(
"Kennwort wiederholen",
355);
_txtPasswordRepeat =
CreateTextBox(380);
_txtPasswordRepeat.UseSystemPasswordChar =
true;
_chkDefault = new CheckBox
{
Text =
"Als Standardprofil verwenden",
Location =
new Point(30, 425),
AutoSize = true,
Checked = true,
ForeColor =
Color.FromArgb(241, 245, 249)
};
_btnCancel = new Button
{
Text = "Abbrechen",
Location =
new Point(125, 465),
Size =
new Size(110, 38),
BackColor =
Color.FromArgb(39, 52, 73),
ForeColor =
Color.FromArgb(241, 245, 249),
FlatStyle =
FlatStyle.Flat,
DialogResult =
DialogResult.Cancel
};
_btnCancel.FlatAppearance.BorderSize = 0;
_btnSave = new Button
{
Text =
"Verbindung prüfen und hinzufügen",
Location =
new Point(245, 465),
Size =
new Size(280, 38),
BackColor =
Color.FromArgb(208, 171, 57),
ForeColor =
Color.FromArgb(30, 25, 10),
FlatStyle =
FlatStyle.Flat,
Font =
new Font(
"Segoe UI",
9,
FontStyle.Bold)
};
_btnSave.FlatAppearance.BorderSize = 0;
_btnSave.Click +=
async (_, _) =>
await TestAndSaveAsync();
Controls.Add(title);
Controls.Add(details);
Controls.Add(profileLabel);
Controls.Add(_txtProfileName);
Controls.Add(userLabel);
Controls.Add(_txtUserName);
Controls.Add(passwordLabel);
Controls.Add(_txtPassword);
Controls.Add(repeatLabel);
Controls.Add(_txtPasswordRepeat);
Controls.Add(_chkDefault);
Controls.Add(_btnCancel);
Controls.Add(_btnSave);
AcceptButton = _btnSave;
CancelButton = _btnCancel;
}
private Label CreateLabel(
string text,
int y)
{
return new Label
{
Text = text,
Location =
new Point(30, y),
AutoSize = true,
ForeColor =
Color.FromArgb(203, 213, 225)
};
}
private TextBox CreateTextBox(int y)
{
return new TextBox
{
Location =
new Point(30, y),
Size =
new Size(495, 27),
BorderStyle =
BorderStyle.FixedSingle,
BackColor =
Color.FromArgb(24, 35, 54),
ForeColor =
Color.FromArgb(241, 245, 249)
};
}
private async Task TestAndSaveAsync()
{
if (_operationRunning)
{
return;
}
string profileName =
_txtProfileName.Text.Trim();
string userName =
_txtUserName.Text.Trim();
string password =
_txtPassword.Text;
string passwordRepeat =
_txtPasswordRepeat.Text;
if (string.IsNullOrWhiteSpace(profileName))
{
ShowWarning(
"Bitte einen Profilnamen eingeben.");
_txtProfileName.Focus();
return;
}
if (profileName.Length > 150)
{
ShowWarning(
"Der Profilname darf maximal 150 Zeichen enthalten.");
_txtProfileName.Focus();
return;
}
if (string.IsNullOrWhiteSpace(userName))
{
ShowWarning(
"Bitte einen Sonic-Benutzernamen eingeben.");
_txtUserName.Focus();
return;
}
if (userName.Length > 256)
{
ShowWarning(
"Der Benutzername darf maximal 256 Zeichen enthalten.");
_txtUserName.Focus();
return;
}
if (string.IsNullOrEmpty(password))
{
ShowWarning(
"Bitte ein Kennwort eingeben.");
_txtPassword.Focus();
return;
}
if (password.Length > 512)
{
ShowWarning(
"Das Kennwort darf maximal 512 Zeichen enthalten.");
_txtPassword.Focus();
return;
}
if (!string.Equals(
password,
passwordRepeat,
StringComparison.Ordinal))
{
ShowWarning(
"Die eingegebenen Kennwörter stimmen nicht überein.");
_txtPasswordRepeat.Clear();
_txtPasswordRepeat.Focus();
return;
}
await RunSaveOperationAsync(
profileName,
userName,
password);
}
private async Task RunSaveOperationAsync(
string profileName,
string userName,
string password)
{
_operationRunning = true;
SetOperationState(isRunning: true);
try
{
CreatedProfile =
await _coordinator.AddCredentialAsync(
_system,
profileName,
userName,
password,
_chkDefault.Checked);
DialogResult = DialogResult.OK;
Close();
}
catch (OperationCanceledException)
{
// Keine Fehlermeldung bei einem regulären Abbruch.
}
catch (Exception exception)
{
MessageBox.Show(
this,
"Das Benutzerprofil konnte nicht angelegt werden."
+ Environment.NewLine
+ Environment.NewLine
+ exception.Message,
"Benutzerprofil",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
finally
{
_operationRunning = false;
if (!IsDisposed)
{
SetOperationState(isRunning: false);
}
}
}
private void SetOperationState(bool isRunning)
{
UseWaitCursor = isRunning;
_btnSave.Enabled = !isRunning;
_btnCancel.Enabled = !isRunning;
_txtProfileName.Enabled = !isRunning;
_txtUserName.Enabled = !isRunning;
_txtPassword.Enabled = !isRunning;
_txtPasswordRepeat.Enabled = !isRunning;
_chkDefault.Enabled = !isRunning;
_btnSave.Text =
isRunning
? "Verbindung wird geprüft ..."
: "Verbindung prüfen und hinzufügen";
}
private void ShowWarning(string message)
{
MessageBox.Show(
this,
message,
"Eingabe prüfen",
MessageBoxButtons.OK,
MessageBoxIcon.Warning);
}
}