286 lines
9.9 KiB
C#
286 lines
9.9 KiB
C#
///
|
|
/// Copyright (c) 2017 Sensus Metering Systems
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Windows.Forms;
|
|
using Users.Entities;
|
|
using Users.Forms;
|
|
using NHibernate;
|
|
using FluentNHibernate;
|
|
|
|
namespace UserManagement
|
|
{
|
|
static class Program
|
|
{
|
|
public const string HomeDir = "C:\\UserManagement\\"; /// Contains subdirectories Results, Logs, Images, ...
|
|
|
|
/// Program information
|
|
public static string Version; /// version string
|
|
public static DateTime BuildDateTime; /// date and time of program build
|
|
|
|
/// Local settings
|
|
public static LocalSettings LocalSettings;
|
|
public const string LocalSettingsFileName = "config.xml";
|
|
public const string LocalSettingsBackupName = "config.backup.xml";
|
|
|
|
|
|
/// <summary>
|
|
/// The main entry point for the application.
|
|
/// </summary>
|
|
[STAThread]
|
|
static void Main()
|
|
{
|
|
/// Program version string
|
|
System.Reflection.Assembly thisAssembly = System.Reflection.Assembly.GetExecutingAssembly();
|
|
Version ver = thisAssembly.GetName().Version;
|
|
Version = string.Format("{0}.{1}.{2}", ver.Major, ver.Minor, ver.Build);
|
|
BuildDateTime = new System.IO.FileInfo(thisAssembly.Location).LastWriteTime;
|
|
|
|
/// Local program configuration directory including the trailing backslash
|
|
string locAppDataDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) +
|
|
Path.DirectorySeparatorChar + "UserManagement" + Path.DirectorySeparatorChar;
|
|
|
|
/// Check whether local application data directory exists.
|
|
/// If not, create it and copy the initial configuration to this directory.
|
|
/// This is done only once after a clean program installation on the first program start-up
|
|
try
|
|
{
|
|
if (!Directory.Exists(locAppDataDir))
|
|
{
|
|
Directory.CreateDirectory(locAppDataDir);
|
|
Environment.CurrentDirectory = locAppDataDir;
|
|
|
|
string sampleConfig = Application.StartupPath + Path.DirectorySeparatorChar + "SampleConfig" + Path.DirectorySeparatorChar;
|
|
if (File.Exists(sampleConfig + LocalSettingsFileName)) { File.Copy(sampleConfig + LocalSettingsFileName, LocalSettingsFileName); }
|
|
|
|
File.SetAttributes(LocalSettingsFileName, FileAttributes.Normal);
|
|
}
|
|
else
|
|
{
|
|
Environment.CurrentDirectory = locAppDataDir;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return;
|
|
}
|
|
|
|
///
|
|
/// Load the local settings
|
|
///
|
|
Program.LocalSettings = LocalSettings.Load(Program.LocalSettingsFileName);
|
|
if (Program.LocalSettings == null || Program.LocalSettings.ConnectionString == null)
|
|
{
|
|
/// Loading local seetings from regular config file failed. Use the backup
|
|
Program.LocalSettings = LocalSettings.Load(Program.LocalSettingsBackupName);
|
|
if (Program.LocalSettings == null || Program.LocalSettings.ConnectionString == null)
|
|
{
|
|
return; /// Fatal error
|
|
}
|
|
else
|
|
{
|
|
LocalSettings.Save(); /// Save the settings to overwrite the wrong file
|
|
}
|
|
}
|
|
else
|
|
{
|
|
/// Loading local seetings from the regular config file was successful. Update the backup
|
|
File.Copy(Program.LocalSettingsFileName, Program.LocalSettingsBackupName, true);
|
|
}
|
|
|
|
|
|
try
|
|
{
|
|
string culture = LocalSettings.Language.Replace('_', '-');
|
|
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(culture);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
string msg = e.Message;
|
|
MessageBox.Show("Selected language not supported.\nUsing English.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
|
|
System.Threading.Thread.CurrentThread.CurrentUICulture =
|
|
new System.Globalization.CultureInfo("en");
|
|
}
|
|
|
|
|
|
/// This belongs to Application.Run(new MainWnd()) ...
|
|
/// and should be executed before opening 'loginDlg'
|
|
Application.EnableVisualStyles();
|
|
Application.SetCompatibleTextRenderingDefault(false);
|
|
|
|
|
|
/// User login
|
|
bool retryLogin = true; /// true = stay in a login loop
|
|
do
|
|
{
|
|
LoginDlg loginDlg;
|
|
if (LocalSettings.LastBenchName != null)
|
|
{
|
|
loginDlg = new LoginDlg();
|
|
}
|
|
else
|
|
{
|
|
loginDlg = new LoginDlg();
|
|
}
|
|
loginDlg.Method = LocalSettings.LoginMethod;
|
|
DialogResult dr = loginDlg.ShowDialog();
|
|
|
|
if (dr == DialogResult.Cancel)
|
|
{
|
|
return; /// Login canceled --> Exit application
|
|
}
|
|
else
|
|
{
|
|
for (int i = 0; i < LocalSettings.BenchesCount; i++)
|
|
{
|
|
if (LocalSettings.TestBenches[i].BenchName == loginDlg.BenchName)
|
|
{
|
|
log.FatalFormat("Test bench name: {0}", loginDlg.BenchName);
|
|
|
|
Config.IUser loadedUser = null;
|
|
try
|
|
{
|
|
bool authorized = false;
|
|
|
|
if (User.IsPowerUser(loginDlg.Alias, loginDlg.Password))
|
|
{
|
|
loadedUser = new User(loginDlg.Alias, 6, true);
|
|
authorized = loadedUser.Authorize(loginDlg.Alias, loginDlg.Password);
|
|
}
|
|
|
|
///
|
|
/// Try authorisation with Users DB first
|
|
///
|
|
if (!authorized)
|
|
{
|
|
bool settingsChanged = false;
|
|
|
|
if (string.IsNullOrEmpty(LocalSettings.TestBenches[i].UsersDBSettings.ConnectionString))
|
|
{
|
|
LocalSettings.TestBenches[i].UsersDBSettings.ConnectionString = LocalSettings.TestBenches[i].ProceduresDBSettings.ConnectionString;
|
|
LocalSettings.TestBenches[i].UsersDBSettings.DbType = LocalSettings.TestBenches[i].ProceduresDBSettings.DbType;
|
|
settingsChanged = true;
|
|
}
|
|
|
|
/// Load and authorise user from the UsersDB database
|
|
try
|
|
{
|
|
switch (loginDlg.Method)
|
|
{
|
|
default:
|
|
case Config.Entities.LoginMethod.UserName:
|
|
loadedUser = Users.Entities.User.LoadUserByName(loginDlg.Alias, LocalSettings.TestBenches[i].UsersDBSettings);
|
|
if (loadedUser != null) authorized = loadedUser.Authorize(loginDlg.Alias, loginDlg.Password);
|
|
break;
|
|
case Config.Entities.LoginMethod.FullName:
|
|
loadedUser = Users.Entities.User.LoadUserByFullName(loginDlg.Alias, LocalSettings.TestBenches[i].UsersDBSettings);
|
|
if (loadedUser != null) authorized = loadedUser.AuthorizeFullName(loginDlg.Alias, loginDlg.Password);
|
|
break;
|
|
case Config.Entities.LoginMethod.Number:
|
|
int number;
|
|
if (!int.TryParse(loginDlg.Alias, out number)) break;
|
|
loadedUser = Users.Entities.User.LoadUserByNumber(number, LocalSettings.TestBenches[i].UsersDBSettings);
|
|
if (loadedUser != null) authorized = loadedUser.AuthorizeNumber(number, loginDlg.Password);
|
|
break;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
authorized = false;
|
|
}
|
|
|
|
if (settingsChanged && authorized) LocalSettings.Save();
|
|
}
|
|
|
|
///
|
|
/// One more attempt: authorisation with Config DB
|
|
///
|
|
if (!authorized)
|
|
{
|
|
/// Load and authorise user from the Config database
|
|
try
|
|
{
|
|
switch (loginDlg.Method)
|
|
{
|
|
default:
|
|
case Config.Entities.LoginMethod.UserName:
|
|
loadedUser = Config.Entities.User.LoadUserByName(loginDlg.Alias, LocalSettings.TestBenches[i].ProceduresDBSettings);
|
|
if (loadedUser != null) authorized = loadedUser.Authorize(loginDlg.Alias, loginDlg.Password);
|
|
break;
|
|
case Config.Entities.LoginMethod.FullName:
|
|
loadedUser = Config.Entities.User.LoadUserByFullName(loginDlg.Alias, LocalSettings.TestBenches[i].ProceduresDBSettings);
|
|
if (loadedUser != null) authorized = loadedUser.AuthorizeFullName(loginDlg.Alias, loginDlg.Password);
|
|
break;
|
|
case Config.Entities.LoginMethod.Number:
|
|
int number;
|
|
if (!int.TryParse(loginDlg.Alias, out number)) break;
|
|
loadedUser = Config.Entities.User.LoadUserByNumber(number, LocalSettings.TestBenches[i].ProceduresDBSettings);
|
|
if (loadedUser != null) authorized = loadedUser.AuthorizeNumber(number, loginDlg.Password);
|
|
break;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
authorized = false;
|
|
}
|
|
}
|
|
|
|
if (authorized)
|
|
{
|
|
LocalSettings.LoginMethod = loginDlg.Method; /// Save the login method actually used
|
|
|
|
Users.DB.ConnectionString = Config.Data.CurrentBench.WaterMetersDBSettings.ConnectionString;
|
|
Users.DB.LoadSharedData();
|
|
|
|
/// This is to trigger an exception in case of a power user and no Config database
|
|
IList<User> listOfProcedures = Config.FluentCommon.CreateSession(Config.Entities.DBKind.Config)
|
|
.QueryOver<User>()
|
|
.List();
|
|
|
|
retryLogin = false;
|
|
break;
|
|
}
|
|
|
|
if (retryLogin)
|
|
{
|
|
/// Close Fluent NHibernate
|
|
MessageBox.Show("Invalid user, password or bench", "Error", MessageBoxButtons.OK);
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
/// Database connect failed, ask what to do
|
|
switch ((new DatabaseSettingsDlg().ShowDialog())
|
|
{
|
|
case DialogResult.Abort: return; /// Exit program
|
|
case DialogResult.Retry: break; /// Retry the loop
|
|
default:
|
|
/// Open 'DatabaseSetingsDlg' and retry DB connect on OK
|
|
if (new Users.Forms.LoginDlg(true).ShowDialog() == DialogResult.Cancel ||
|
|
new BenchesDlg().ShowDialog() == DialogResult.Cancel)
|
|
{
|
|
return; /// Exit program
|
|
}
|
|
Program.LocalSettings = LocalSettings.Load(Program.LocalSettingsFileName);
|
|
if (Program.LocalSettings == null) return; /// Fatal error
|
|
break; /// Retry the loop
|
|
}
|
|
}
|
|
} // if benchname is OK
|
|
} // for all benches in local settings loop
|
|
} // not Canceled
|
|
} //do
|
|
while (retryLogin);
|
|
|
|
IList<User> listOfUsers = Config.FluentCommon.CreateSession(Config.Entities.DBKind.Config)
|
|
.QueryOver<User>()
|
|
.List();
|
|
|
|
Application.Run(new Users.Forms.UserManagementDlg());
|
|
}
|
|
}
|
|
}
|