2023-04-13 11:51:03 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
using Aitex.Core.RT.Log;
|
|
|
|
|
using MECF.Framework.UI.Client.ClientBase;
|
|
|
|
|
using OpenSEMI.ClientBase;
|
|
|
|
|
using OpenSEMI.ClientBase.Command;
|
|
|
|
|
|
|
|
|
|
namespace MECF.Framework.UI.Client.CenterViews.Configs.Accounts
|
|
|
|
|
{
|
2023-09-20 17:25:51 +08:00
|
|
|
|
public sealed class AccountViewModel : BaseModel
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
2023-09-20 17:25:51 +08:00
|
|
|
|
#region Variables
|
2023-04-13 11:51:03 +08:00
|
|
|
|
|
2023-09-20 17:25:51 +08:00
|
|
|
|
private ObservableCollection<AccountItem> _AccountsList = new();
|
2023-04-13 11:51:03 +08:00
|
|
|
|
private AccountItem _treeSelectedAccount = null;
|
2023-09-20 17:25:51 +08:00
|
|
|
|
private CtrlMode _controlMode = CtrlMode.VIEW;
|
|
|
|
|
private PasswordBox _newPasswordBox;
|
|
|
|
|
private PasswordBox _confirmPasswordBox;
|
|
|
|
|
|
|
|
|
|
private ICommand _btnSaveAccountCommand;
|
|
|
|
|
private ICommand _btnAddAccountCommand;
|
|
|
|
|
private ICommand _btnCloneAccountCommand;
|
|
|
|
|
private ICommand _btnDeleteAccountCommand;
|
|
|
|
|
private ICommand _btnCancelAccountCommand;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Property
|
|
|
|
|
|
2023-04-13 11:51:03 +08:00
|
|
|
|
public AccountItem TreeSelectedAccount
|
|
|
|
|
{
|
2023-09-20 17:25:51 +08:00
|
|
|
|
get => _treeSelectedAccount;
|
|
|
|
|
set { _treeSelectedAccount = value; NotifyOfPropertyChange(); }
|
2023-04-13 11:51:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 17:25:51 +08:00
|
|
|
|
|
2023-04-13 11:51:03 +08:00
|
|
|
|
public CtrlMode ControlMode
|
|
|
|
|
{
|
2023-09-20 17:25:51 +08:00
|
|
|
|
get => _controlMode;
|
|
|
|
|
set { _controlMode = value; NotifyOfPropertyChange(); }
|
2023-04-13 11:51:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 17:25:51 +08:00
|
|
|
|
|
|
|
|
|
public ObservableCollection<AccountItem> AccountList => _AccountsList;
|
|
|
|
|
|
2023-04-13 11:51:03 +08:00
|
|
|
|
|
|
|
|
|
#region command define
|
2023-09-20 17:25:51 +08:00
|
|
|
|
|
|
|
|
|
|
2023-04-13 11:51:03 +08:00
|
|
|
|
public ICommand BtnSaveAccountCommand
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2023-09-20 17:25:51 +08:00
|
|
|
|
if (_btnSaveAccountCommand == null)
|
|
|
|
|
_btnSaveAccountCommand = new BaseCommand<object>(OnBtnSaveAccountCommand);
|
|
|
|
|
return _btnSaveAccountCommand;
|
2023-04-13 11:51:03 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 17:25:51 +08:00
|
|
|
|
|
2023-04-13 11:51:03 +08:00
|
|
|
|
public ICommand BtnAddAccountCommand
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2023-09-20 17:25:51 +08:00
|
|
|
|
if (_btnAddAccountCommand == null)
|
|
|
|
|
_btnAddAccountCommand = new BaseCommand<object>(OnBtnAddAccountCommand);
|
|
|
|
|
return _btnAddAccountCommand;
|
2023-04-13 11:51:03 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 17:25:51 +08:00
|
|
|
|
|
2023-04-13 11:51:03 +08:00
|
|
|
|
public ICommand BtnCloneAccountCommand
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2023-09-20 17:25:51 +08:00
|
|
|
|
if (_btnCloneAccountCommand == null)
|
|
|
|
|
_btnCloneAccountCommand = new BaseCommand<object>(OnBtnCloneAccountCommand);
|
|
|
|
|
return _btnCloneAccountCommand;
|
2023-04-13 11:51:03 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 17:25:51 +08:00
|
|
|
|
|
2023-04-13 11:51:03 +08:00
|
|
|
|
public ICommand BtnDeleteAccountCommand
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2023-09-20 17:25:51 +08:00
|
|
|
|
if (_btnDeleteAccountCommand == null)
|
|
|
|
|
_btnDeleteAccountCommand = new BaseCommand<object>(OnBtnDeleteAccountCommand);
|
|
|
|
|
return _btnDeleteAccountCommand;
|
2023-04-13 11:51:03 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 17:25:51 +08:00
|
|
|
|
|
2023-04-13 11:51:03 +08:00
|
|
|
|
public ICommand BtnCancelAccountCommand
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2023-09-20 17:25:51 +08:00
|
|
|
|
if (_btnCancelAccountCommand == null)
|
|
|
|
|
_btnCancelAccountCommand = new BaseCommand<object>(OnBtnCancelAccountCommand);
|
|
|
|
|
return _btnCancelAccountCommand;
|
2023-04-13 11:51:03 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
#endregion
|
|
|
|
|
|
2023-09-20 17:25:51 +08:00
|
|
|
|
#region Constructors
|
|
|
|
|
|
2023-04-13 11:51:03 +08:00
|
|
|
|
public AccountViewModel()
|
|
|
|
|
{
|
2023-09-20 17:25:51 +08:00
|
|
|
|
DisplayName = "Account";
|
2023-04-13 11:51:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 17:25:51 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ViewModel Operations
|
|
|
|
|
|
2023-04-13 11:51:03 +08:00
|
|
|
|
protected override void OnViewLoaded(object view)
|
|
|
|
|
{
|
|
|
|
|
base.OnViewLoaded(view);
|
2023-09-20 17:25:51 +08:00
|
|
|
|
var av = view as AccountView;
|
|
|
|
|
_newPasswordBox = av.pwNewPassword;
|
|
|
|
|
_confirmPasswordBox = av.pwConfirmPassword;
|
2023-04-13 11:51:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnActivate()
|
|
|
|
|
{
|
2023-09-20 17:25:51 +08:00
|
|
|
|
AccountManagerClient.Instance.Initialize();
|
2023-04-13 11:51:03 +08:00
|
|
|
|
RefreshAccountList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnDeactivate(bool close)
|
|
|
|
|
{
|
|
|
|
|
if (ControlMode == CtrlMode.EDIT && IsPermission)
|
|
|
|
|
{
|
|
|
|
|
if (DialogBox.Confirm("The data has been modified. Do you want to save the change(s)?"))
|
|
|
|
|
{
|
|
|
|
|
if (SaveChanged())
|
|
|
|
|
{
|
|
|
|
|
ControlMode = CtrlMode.VIEW;
|
|
|
|
|
DialogBox.ShowInfo("Operated successfully.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
base.OnDeactivate(close);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 17:25:51 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Methods
|
|
|
|
|
|
2023-04-13 11:51:03 +08:00
|
|
|
|
private void RefreshAccountList()
|
|
|
|
|
{
|
|
|
|
|
_AccountsList.Clear();
|
|
|
|
|
_treeSelectedAccount = null;
|
|
|
|
|
|
2023-09-20 17:25:51 +08:00
|
|
|
|
var accounts = AccountManagerClient.Instance.GetAllAccounts();
|
|
|
|
|
if (accounts == null || accounts.Count == 0) return;
|
2023-04-13 11:51:03 +08:00
|
|
|
|
|
2023-09-20 17:25:51 +08:00
|
|
|
|
foreach (var acc in accounts)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
2023-09-20 17:25:51 +08:00
|
|
|
|
var treeAccount = AccountManagerClient.Instance.CloneAccount(acc);
|
2023-04-13 11:51:03 +08:00
|
|
|
|
if (treeAccount != null)
|
|
|
|
|
{
|
|
|
|
|
if (treeAccount.AccountName == BaseApp.Instance.UserContext.LoginName)
|
|
|
|
|
{
|
|
|
|
|
treeAccount.IsEnableChangeAccountName = false;
|
|
|
|
|
}
|
|
|
|
|
_AccountsList.Add(treeAccount);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TreeSelectedAccount = _AccountsList.FirstOrDefault();
|
|
|
|
|
TreeSelectedAccount.IsSelected = true;
|
|
|
|
|
ControlMode = CtrlMode.VIEW;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnAccountChanged()
|
|
|
|
|
{
|
|
|
|
|
if (ControlMode == CtrlMode.EDIT)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
//check account to set the mode from view to edit
|
|
|
|
|
if (_treeSelectedAccount != null && _treeSelectedAccount.IsAccountChanged())
|
|
|
|
|
ControlMode = CtrlMode.EDIT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool SaveChanged()
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(TreeSelectedAccount.DisplayAccountName))
|
|
|
|
|
{
|
|
|
|
|
DialogBox.ShowWarning("{0} cannot be empty.", "Account name");
|
|
|
|
|
//TreeSelectedAccount.DisplayAccountName = "NewUser";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (IsAccountExists(TreeSelectedAccount))
|
|
|
|
|
{
|
|
|
|
|
DialogBox.ShowWarning("{0} already exists.", "Account");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 17:25:51 +08:00
|
|
|
|
TreeSelectedAccount.NewPassword = _newPasswordBox.Password;
|
|
|
|
|
TreeSelectedAccount.ConfirmPassword = _confirmPasswordBox.Password;
|
2023-04-13 11:51:03 +08:00
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(TreeSelectedAccount.NewPassword) ||
|
|
|
|
|
string.IsNullOrWhiteSpace(TreeSelectedAccount.ConfirmPassword))
|
|
|
|
|
{
|
|
|
|
|
DialogBox.ShowWarning("{0} cannot be empty.", "The password");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (TreeSelectedAccount.NewPassword != TreeSelectedAccount.ConfirmPassword)
|
|
|
|
|
{
|
|
|
|
|
DialogBox.ShowWarning("The password does not match.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(TreeSelectedAccount.DisplayEmail))
|
|
|
|
|
{
|
2023-09-20 17:25:51 +08:00
|
|
|
|
var reg = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
|
|
|
|
|
var r = new Regex(reg);
|
2023-04-13 11:51:03 +08:00
|
|
|
|
if (!r.IsMatch(TreeSelectedAccount.DisplayEmail))
|
|
|
|
|
{
|
|
|
|
|
DialogBox.ShowWarning("The email is invalid.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TreeSelectedAccount.AccountName = TreeSelectedAccount.DisplayAccountName;
|
|
|
|
|
TreeSelectedAccount.FirstName = TreeSelectedAccount.DisplayFirstName;
|
|
|
|
|
TreeSelectedAccount.LastName = TreeSelectedAccount.DisplayLastName;
|
|
|
|
|
TreeSelectedAccount.Email = TreeSelectedAccount.DisplayEmail;
|
|
|
|
|
TreeSelectedAccount.Description = TreeSelectedAccount.DisplayDescription;
|
|
|
|
|
TreeSelectedAccount.AccountTextSaved = TreeSelectedAccount.FirstNameTextSaved =
|
|
|
|
|
TreeSelectedAccount.LastNameTextSaved = TreeSelectedAccount.EmailTextSaved = true;
|
|
|
|
|
|
2023-09-20 17:25:51 +08:00
|
|
|
|
var isRoleSelected = false;
|
|
|
|
|
foreach (var entity in TreeSelectedAccount.RoleColleciton)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
|
|
|
|
if (entity.DisplayRoleStatus)
|
|
|
|
|
{
|
|
|
|
|
isRoleSelected = true;
|
|
|
|
|
}
|
|
|
|
|
entity.RoleStatus = entity.DisplayRoleStatus;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isRoleSelected)
|
|
|
|
|
{
|
|
|
|
|
DialogBox.ShowWarning("Please set role information for this account.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-09-20 17:25:51 +08:00
|
|
|
|
AccountManagerClient.Instance.SaveAccount(TreeSelectedAccount);
|
2023-04-13 11:51:03 +08:00
|
|
|
|
}
|
2023-09-20 17:25:51 +08:00
|
|
|
|
catch (Exception ex)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
|
|
|
|
LOG.Write(ex);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 17:25:51 +08:00
|
|
|
|
_newPasswordBox.Clear();
|
|
|
|
|
_confirmPasswordBox.Clear();
|
2023-04-13 11:51:03 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool IsAccountExists(AccountItem account)
|
|
|
|
|
{
|
|
|
|
|
if (AccountList == null || AccountList.Count == 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var sameNameList = AccountList.Where(t => t.DisplayAccountName == account.DisplayAccountName);
|
|
|
|
|
if (sameNameList == null || sameNameList.Count() <= 1)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 17:25:51 +08:00
|
|
|
|
private void OnBtnAddAccountCommand(object arg)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
2023-09-20 17:25:51 +08:00
|
|
|
|
var newAccount = AccountManagerClient.Instance.CreateAccount();
|
2023-04-13 11:51:03 +08:00
|
|
|
|
if (newAccount != null)
|
|
|
|
|
{
|
|
|
|
|
_AccountsList.Add(newAccount);
|
|
|
|
|
TreeSelectedAccount = newAccount;
|
|
|
|
|
TreeSelectedAccount.IsSelected = true;
|
|
|
|
|
}
|
|
|
|
|
ControlMode = CtrlMode.EDIT;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 17:25:51 +08:00
|
|
|
|
private void OnBtnDeleteAccountCommand(object arg)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
|
|
|
|
if (_treeSelectedAccount == null) return;
|
|
|
|
|
|
|
|
|
|
if (!DialogBox.Confirm("Are you sure that you want to delete this account?"))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (BaseApp.Instance.UserContext.LoginName == _treeSelectedAccount.AccountName)
|
|
|
|
|
{
|
|
|
|
|
DialogBox.ShowWarning("The action cannot be completed because {0} is currently in use.", "the account");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 17:25:51 +08:00
|
|
|
|
AccountManagerClient.Instance.DeleteAccount(TreeSelectedAccount.AccountID);
|
2023-04-13 11:51:03 +08:00
|
|
|
|
RefreshAccountList();
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 17:25:51 +08:00
|
|
|
|
private void OnBtnCloneAccountCommand(object arg)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
|
|
|
|
if (_treeSelectedAccount != null)
|
|
|
|
|
{
|
2023-09-20 17:25:51 +08:00
|
|
|
|
var newAccount = AccountManagerClient.Instance.CreateAccount(_treeSelectedAccount);
|
2023-04-13 11:51:03 +08:00
|
|
|
|
if (newAccount != null)
|
|
|
|
|
{
|
|
|
|
|
newAccount.DisplayAccountName = newAccount.AccountName = "Copy of " + newAccount.DisplayAccountName;
|
|
|
|
|
_AccountsList.Add(newAccount);
|
|
|
|
|
TreeSelectedAccount = newAccount;
|
|
|
|
|
TreeSelectedAccount.IsSelected = true;
|
|
|
|
|
ControlMode = CtrlMode.EDIT;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 17:25:51 +08:00
|
|
|
|
private void OnBtnSaveAccountCommand(object arg)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
|
|
|
|
if (!TreeSelectedAccount.IsValid)
|
|
|
|
|
{
|
|
|
|
|
DialogBox.ShowWarning("Input error.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (SaveChanged())
|
|
|
|
|
{
|
|
|
|
|
ControlMode = CtrlMode.VIEW;
|
|
|
|
|
DialogBox.ShowInfo("Operated successfully.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
DialogBox.ShowInfo("Operation failed.");
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 17:25:51 +08:00
|
|
|
|
private void OnBtnCancelAccountCommand(object arg)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
|
|
|
|
RefreshAccountList();
|
2023-09-20 17:25:51 +08:00
|
|
|
|
_newPasswordBox.Clear();
|
|
|
|
|
_confirmPasswordBox.Clear();
|
2023-04-13 11:51:03 +08:00
|
|
|
|
ControlMode = CtrlMode.VIEW;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|