Logging well tested, fixed no 'remote' user DB, way of authorization indicated, copy all remote users, etc., ver. 2.12.498

This commit is contained in:
Milan Hanajik 2017-03-19 10:37:06 +01:00
parent dbfc06960f
commit f12b2c38f7
28 changed files with 347 additions and 528 deletions

View File

@ -66,7 +66,6 @@
<ItemGroup>
<Compile Include="Data.cs" />
<Compile Include="DatabaseSettings.cs" />
<Compile Include="DBSettings.cs" />
<Compile Include="Entities\BenchPath.cs" />
<Compile Include="Entities\Component.cs" />
<Compile Include="Entities\ComponentProcedure.cs" />

View File

@ -1,31 +0,0 @@
///
/// Copyright (c) 2013-2017 Sensus Metering Systems
///
using System;
namespace Config
{
/// <summary>
/// Database settings required to make a connection
/// </summary>
public class DBSettings : ICloneable
{
public Users.Entities.DBType DbType;
public string ConnectionString;
public DBSettings()
{
}
public DBSettings(Users.Entities.DBType dbType, string connectionString)
{
this.DbType = dbType;
this.ConnectionString = connectionString;
}
public object Clone()
{
return new DBSettings(DbType, ConnectionString);
}
}
}

View File

@ -1,5 +1,5 @@
///
/// Copyright (c) 2013-2016 Sensus Metering Systems
/// Copyright (c) 2013-2017 Sensus Metering Systems
///
using System;
using System.Text;
@ -15,18 +15,18 @@ namespace Config
// Public fields
public string BenchName;
public bool IsRealBench;
public DBSettings ProceduresDBSettings; /// Config database settings
public DBSettings WaterMetersDBSettings; /// Results database settings
public DBSettings UsersDBSettings; /// Users database settings
public Users.DBSettings ProceduresDBSettings; /// Config database settings
public Users.DBSettings WaterMetersDBSettings; /// Results database settings
public Users.DBSettings UsersDBSettings; /// Users database settings
// Constructor
public DatabaseSettings()
{
BenchName = String.Empty; /// Empty string (avoid null)
IsRealBench = false;
ProceduresDBSettings = new DBSettings(Users.Entities.DBType.MySql, string.Empty);
WaterMetersDBSettings = new DBSettings(Users.Entities.DBType.MySql, string.Empty);
UsersDBSettings = new DBSettings(Users.Entities.DBType.MySql, string.Empty);
ProceduresDBSettings = new Users.DBSettings(Users.Entities.DBType.MySql, string.Empty);
WaterMetersDBSettings = new Users.DBSettings(Users.Entities.DBType.MySql, string.Empty);
UsersDBSettings = new Users.DBSettings(Users.Entities.DBType.MySql, string.Empty);
}
public object Clone()
@ -35,9 +35,9 @@ namespace Config
result.BenchName = BenchName;
result.IsRealBench = IsRealBench;
result.ProceduresDBSettings = (DBSettings)ProceduresDBSettings.Clone();
result.WaterMetersDBSettings = (DBSettings)WaterMetersDBSettings.Clone();
result.UsersDBSettings = (DBSettings)ProceduresDBSettings.Clone();
result.ProceduresDBSettings = (Users.DBSettings)ProceduresDBSettings.Clone();
result.WaterMetersDBSettings = (Users.DBSettings)WaterMetersDBSettings.Clone();
result.UsersDBSettings = (Users.DBSettings)ProceduresDBSettings.Clone();
return result;
}

View File

@ -2,7 +2,6 @@
/// Copyright (c) 2013-2015 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
namespace Config.Entities
{

View File

@ -3,14 +3,11 @@
///
using System;
using System.Collections.Generic;
using log4net;
namespace Config.Entities
{
public class User : Users.IUser
public class User //: Users.IUser
{
static readonly ILog log = LogManager.GetLogger(typeof(User));
public virtual int Id { get; protected set; }
public virtual string UserName { get; set; } /// = name, alias, abbreviation
public virtual string FullName { get; set; } /// = description
@ -19,351 +16,9 @@ namespace Config.Entities
public virtual DateTime LastPwChange { get; set; }
public virtual IList<Group> Groups { get; set; } //User can be a member of a list of groups
private bool powerUser; /// true for built-in users, power users have full access even with empty Groups list
public User()
{
this.powerUser = false;
Groups = new List<Group>();
#if ORACLE_DB
Number = 6;
#else
Number = 0;
#endif
}
public User(string userName, int number, bool powerUser)
{
UserName = userName;
Number = number;
this.powerUser = powerUser;
Groups = new List<Group>();
FullName = powerUser ? "Power user" : string.Empty;
}
public virtual void AddGroup(Group group)
{
Groups.Add(group);
}
/// ------------- Additional stuff not mapped into the database -------------
/// <summary>
/// Check whether user is a member of a group.
/// </summary>
/// <param name="groupId">Group GID</param>
/// <returns>true is user is a member of the specified group</returns>
public virtual bool IsMemberOf(Users.Grp.GID groupId)
{
if (groupId == Users.Grp.GID.None || powerUser)
{
return true;
}
else if (groupId >= 0 && (int)groupId < Users.Grp.Count)
{
// for all group elements in User.groups
for (int i = 0; i < Groups.Count; ++i)
{
// element GID = parameter GroupId ?
if (((Group)Groups[i]).Gid == (int)groupId)
{
return true;
}
}
return false;
}
else
{
return false;
}
}
/// <summary>
/// Sets users password. It then will be encrypted.
/// </summary>
/// <param name="password">Password</param>
public virtual void SetPassword(string password)
{
this.Password = EncryptedPassword(password);
LastPwChange = DateTime.Now;
}
public virtual string EncryptedPassword(string password)
{
return getHash(password);
}
/// <summary>
/// Verifies users password. Used by static bool Authorisation(...)
/// </summary>
/// <param name="password">Password</param>
/// <returns>true if password is correct</returns>
public virtual bool CheckPassword(string password)
{
return (Password == EncryptedPassword(password));
}
/// <summary>
/// The FIRST of TWO possible user authorization method to be used
/// when a specific group membership is required (you can use Grp.GID.None).
/// If the user is not authorized, the current user remains to be a current user
/// (i.e. the access rights were not risen to a higher level).
/// If you require different behavior, use Unauthorize() before calling Authorize().
/// </summary>
/// <param name="userName">User name</param>
/// <param name="password">Password</param>
/// <param name="requiredGrupMembership"></param>
/// <returns>true = authorized</returns>
public virtual bool Authorize(string userName, string password, Users.Grp.GID requiredGroupMembership)
{
if (Entities.User.IsPowerUser(userName, password))
{
Users.GlobalData.CurrentUser = this;
Users.GlobalData.LastAuthorization = DateTime.Now;
log.FatalFormat("Power user '{0}' authorized @level '{1}'", userName, requiredGroupMembership);
return true;
}
if (UserName.ToLower() == userName.ToLower())
{
if (IsMemberOf(requiredGroupMembership) && CheckPassword(password))
{
Users.GlobalData.CurrentUser = this;
Users.GlobalData.LastAuthorization = DateTime.Now;
log.FatalFormat("User '{0}' authorized @level '{1}'", userName, requiredGroupMembership);
return true;
}
else
{
return false;
}
}
return false;
}
/// <summary>
/// The SECOND of TWO possible user authorization method to be used when
/// no specific group membership is required.
/// </summary>
public virtual bool Authorize(string userName, string password)
{
return Authorize(userName, password, Users.Grp.GID.None);
}
/// <summary>
/// The FIRST of TWO possible user authorization method to be used
/// when a specific group membership is required (you can use Grp.GID.None).
/// If the user is not authorized, the current user remains to be a current user
/// (i.e. the access rights were not risen to a higher level).
/// If you require different behavior, use Unauthorize() before calling Authorize().
/// </summary>
/// <param name="number">User ID number</param>
/// <param name="password">Password</param>
/// <param name="requiredGrupMembership"></param>
/// <returns>true = authorized</returns>
public virtual bool AuthorizeNumber(int number, string password, Users.Grp.GID requiredGroupMembership)
{
if (Number == number)
{
if (IsMemberOf(requiredGroupMembership) && CheckPassword(password))
{
Users.GlobalData.CurrentUser = this;
Users.GlobalData.LastAuthorization = DateTime.Now;
log.FatalFormat("User ID={0} authorized @level '{1}'", number, requiredGroupMembership);
return true;
}
else
{
return false;
}
}
return false;
}
/// <summary>
/// The SECOND of TWO possible user authorization method to be used when
/// no specific group membership is required.
/// </summary>
public virtual bool AuthorizeNumber(int number, string password)
{
return AuthorizeNumber(number, password, Users.Grp.GID.None);
}
/// <summary>
/// The FIRST of TWO possible user authorization method to be used
/// when a specific group membership is required (you can use Grp.GID.None).
/// If the user is not authorized, the current user remains to be a current user
/// (i.e. the access rights were not risen to a higher level).
/// If you require different behavior, use Unauthorize() before calling Authorize().
/// </summary>
/// <param name="fullName"></param>
/// <param name="password"></param>
/// <param name="requiredGrupMembership"></param>
/// <returns>true = authorized</returns>
public virtual bool AuthorizeFullName(string fullName, string password, Users.Grp.GID requiredGroupMembership)
{
if (FullName.ToLower() == fullName.ToLower())
{
if (IsMemberOf(requiredGroupMembership) && CheckPassword(password))
{
Users.GlobalData.CurrentUser = this;
Users.GlobalData.LastAuthorization = DateTime.Now;
log.FatalFormat("User alias={0} authorized @level '{1}'", fullName, requiredGroupMembership);
return true;
}
else
{
return false;
}
}
return false;
}
/// <summary>
/// The SECOND of TWO possible user authorization method to be used when
/// no specific group membership is required.
/// </summary>
public virtual bool AuthorizeFullName(string fullName, string password)
{
return AuthorizeFullName(fullName, password, Users.Grp.GID.None);
}
/// <summary>
/// Returns a 'User' with a given username from a database.
/// </summary>
/// <param name="username">User name for the query</param>
/// <returns>reference to a 'User' (if it exists) or null</returns>
public static User AuthorizeDummyUser(string username)
{
User user = new User();
user.UserName = username;
Users.GlobalData.CurrentUser = user;
return user;
}
/// <summary>
/// Returns a 'User' with a given username from an ARBITRARY database.
/// </summary>
/// <param name="username">User name for the query</param>
/// <returns>reference to a 'User' (if it exists) or null</returns>
public static User LoadUserByName(string userName, DBSettings dbSettings)
{
IList<User> listOfUsers = FluentCommon.CreateSessionFactory(Users.Entities.DBKind.Config, dbSettings.DbType, dbSettings.ConnectionString, false)
.OpenSession()
.CreateQuery("FROM User WHERE LOWER(UserName) = :username") /// Note: QueryOver<User>().Where(x => x.UserName.ToLower() == ... does not work
.SetParameter("username", userName.ToLower())
.List<User>();
if (listOfUsers.Count > 0) return listOfUsers[0];
return null;
}
/// <summary>
/// Returns a 'User' with a given username from the users database.
/// </summary>
/// <param name="username">User name for the query</param>
/// <returns>reference to a 'User' (if it exists) or null</returns>
public static User LoadUserByName(string userName)
{
IList<User> listOfUsers = FluentCommon.CreateSession(Users.Entities.DBKind.Config)
.CreateQuery("FROM User WHERE LOWER(UserName) = :username") /// Note: QueryOver<User>().Where(x => x.UserName.ToLower() == ... does not work
.SetParameter("username", userName.ToLower())
.List<User>();
if (listOfUsers.Count > 0) return listOfUsers[0];
return null;
}
/// <summary>
/// Returns a 'User' with a given full name from an ARBITRARY database.
/// </summary>
/// <param name="fullName">Full name for the query</param>
/// <returns>reference to a 'User' (if it exists) or null</returns>
public static User LoadUserByFullName(string fullName, Config.DBSettings dbSettings)
{
IList<User> listOfUsers = FluentCommon.CreateSessionFactory(Users.Entities.DBKind.Config, dbSettings.DbType, dbSettings.ConnectionString, false)
.OpenSession()
.CreateQuery("FROM User WHERE LOWER(Description) = :fullname") /// Note: QueryOver<User>().Where(x => x.FullName.ToLower() == ... does not work
.SetParameter("fullname", fullName.ToLower())
.List<User>();
if (listOfUsers.Count > 0) return listOfUsers[0];
return null;
}
/// <summary>
/// Returns a 'User' with a given full name from the users database.
/// </summary>
/// <param name="fullName">Full name for the query</param>
/// <returns>reference to a 'User' (if it exists) or null</returns>
public static User LoadUserByFullName(string fullName)
{
IList<User> listOfUsers = FluentCommon.CreateSession(Users.Entities.DBKind.Config)
.CreateQuery("FROM User WHERE LOWER(Description) = :fullname") /// Note: QueryOver<User>().Where(x => x.FullName.ToLower() == ... does not work
.SetParameter("fullname", fullName.ToLower())
.List<User>();
if (listOfUsers.Count > 0) return listOfUsers[0];
return null;
}
/// <summary>
/// Returns a 'User' with a given username from an ARBITRARY database.
/// </summary>
/// <param name="number">User ID number for the query</param>
/// <returns>reference to a 'User' (if it exists) or null</returns>
public static User LoadUserByNumber(int number, Config.DBSettings dbSettings)
{
IList<User> listOfUsers = FluentCommon.CreateSessionFactory(Users.Entities.DBKind.Config, dbSettings.DbType, dbSettings.ConnectionString, false)
.OpenSession()
.QueryOver<User>()
.Where(x => (x.Number == number))
.List();
if (listOfUsers.Count > 0) return listOfUsers[0];
return null;
}
/// <summary>
/// Returns a 'User' with a given username from the users database.
/// </summary>
/// <param name="number">User ID number for the query</param>
/// <returns>reference to a 'User' (if it exists) or null</returns>
public static User LoadUserByNumber(int number)
{
IList<User> listOfUsers = FluentCommon.CreateSession(Users.Entities.DBKind.Config)
.QueryOver<User>()
.Where(x => (x.Number == number))
.List();
if (listOfUsers.Count > 0) return listOfUsers[0];
return null;
}
/// <summary>
/// returns an IList of all Users
/// </summary>
public static IList<User> GetAllUsers()
{
return FluentCommon.CreateSession(Users.Entities.DBKind.Config)
.CreateQuery("FROM User")
.List<User>();
}
public static void Unauthorize()
{
Users.Entities.User.Unauthorize();
}
public static string getHash(string text)
{
return Users.Entities.User.getHash(text);
}
public static bool IsPowerUser(string userName, string password)
{
return Users.Entities.User.IsPowerUser(userName, password);
}
}
}

View File

@ -171,16 +171,16 @@ namespace Config
///
/// Prepare all groups
///
var testers = new Entities.Group { Gid = (int)Users.Grp.GID.Testers, Name = Strings.Tester };
var testingSpecialists = new Entities.Group { Gid = (int)Users.Grp.GID.TestingSpecialists, Name = Strings.Testing_Specialist };
var headOfLab = new Entities.Group { Gid = (int)Users.Grp.GID.HeadOfLab, Name = Strings.Head_of_Lab };
var maintenanceSpecialists = new Entities.Group { Gid = (int)Users.Grp.GID.MaintenanceSpecialists, Name = Strings.Maintenance_Specialist };
var metrologists = new Entities.Group { Gid = (int)Users.Grp.GID.Metrologists, Name = Strings.Metrologist };
var calibrationSpecialists = new Entities.Group { Gid = (int)Users.Grp.GID.CalibrationSpecialists, Name = Strings.Calibration_Specialist };
var administrators = new Entities.Group { Gid = (int)Users.Grp.GID.Administrators, Name = Strings.Administrator };
var testers = new Users.Entities.Group((int)Users.Grp.GID.Testers, Strings.Tester);
var testingSpecialists = new Users.Entities.Group((int)Users.Grp.GID.TestingSpecialists, Strings.Testing_Specialist);
var headOfLab = new Users.Entities.Group((int)Users.Grp.GID.HeadOfLab, Strings.Head_of_Lab);
var maintenanceSpecialists = new Users.Entities.Group((int)Users.Grp.GID.MaintenanceSpecialists, Strings.Maintenance_Specialist);
var metrologists = new Users.Entities.Group((int)Users.Grp.GID.Metrologists, Strings.Metrologist);
var calibrationSpecialists = new Users.Entities.Group((int)Users.Grp.GID.CalibrationSpecialists, Strings.Calibration_Specialist);
var administrators = new Users.Entities.Group((int)Users.Grp.GID.Administrators, Strings.Administrator);
/// Create the user 'admin' add his groups
var admin = new Entities.User
var admin = new Users.Entities.User
{
UserName = Data.AdminUsername,
FullName = Strings.Administrator,

View File

@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.12.486.0")]
[assembly: AssemblyFileVersion("2.12.486.0")]
[assembly: AssemblyVersion("2.12.498.0")]
[assembly: AssemblyFileVersion("2.12.498.0")]

View File

@ -58,7 +58,7 @@ namespace TBF.Forms
realBenchCheckBox.Checked = bench.IsRealBench;
/// Initialize 'Database type' combo-boxes
for (int i = 0; i < (int)Users.Entities.DBType.DbTypesCount; i++)
for (int i = 0; i < (int)Users.Entities.DBType.Count; i++)
{
Users.Entities.DBType dbType = (Users.Entities.DBType)i;
configDbTypeComboBox.Items.Add(dbType);

View File

@ -1,12 +1,7 @@
///
/// Copyright (c) 2013-2016 Sensus Metering Systems
/// Copyright (c) 2013-2017 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TBF.Resources;

View File

@ -348,7 +348,9 @@ namespace TBF
public void UpdateUser()
{
userInfoStatusLabel.Text = string.Format("{0}: {1} , ", Strings.User, Users.GlobalData.CurrentUser.UserName);
userInfoStatusLabel.Text = string.Format("{0}: {1}{2}, ", Strings.User, Users.GlobalData.CurrentUser.UserName,
(Users.GlobalData.AuthorizedAs == Users.Entities.AuthorizedAs.PowerUser) ? "(S)" :
((Users.GlobalData.AuthorizedAs == Users.Entities.AuthorizedAs.LocalUser) ? "(L)" : "(R)"));
}
public void UpdateStatusP(string statusPStr)

View File

@ -7,6 +7,7 @@ using System.Collections.Generic;
using System.Windows.Forms;
using log4net;
using Config.Entities;
using Users;
using TBF.Resources;
using TBF.Forms;
@ -203,52 +204,52 @@ namespace TBF
{
log.FatalFormat("Test bench name: {0}", loginDlgBench.BenchName);
GlobalData.RemoteUsersDB = LocalSettings.TestBenches[i].UsersDBSettings;
GlobalData.LocalUsersDB = LocalSettings.TestBenches[i].ProceduresDBSettings;
Users.IUser loadedUser = null;
try
{
bool authorized = false;
Users.Entities.AuthorizedAs authorizedAs = Users.Entities.AuthorizedAs.PowerUser;
if (User.IsPowerUser(loginDlgBench.Alias, loginDlgBench.Password))
if (Users.Entities.User.IsPowerUser(loginDlgBench.Alias, loginDlgBench.Password))
{
loadedUser = new User(loginDlgBench.Alias, 6, true);
loadedUser = new Users.Entities.User(loginDlgBench.Alias, 6, true);
authorized = loadedUser.Authorize(loginDlgBench.Alias, loginDlgBench.Password);
if (authorized)
GlobalData.AuthorizedAs = Users.Entities.AuthorizedAs.PowerUser;
}
///
/// Try authorisation with Users DB first
/// Authorisation using databases
///
if (!authorized)
{
bool settingsChanged = false;
DBSettings[] dbs = new DBSettings[] { GlobalData.RemoteUsersDB, GlobalData.LocalUsersDB };
if (string.IsNullOrEmpty(LocalSettings.TestBenches[i].UsersDBSettings.ConnectionString))
authorizedAs = Users.Entities.AuthorizedAs.RemoteUser; /// Try remote DB first
foreach (var db in dbs)
{
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
{
Users.Entities.DBType dbType = LocalSettings.TestBenches[i].UsersDBSettings.DbType;
string connStr = LocalSettings.TestBenches[i].UsersDBSettings.ConnectionString;
switch (loginDlgBench.Method)
{
default:
case Users.Entities.LoginMethod.UserName:
loadedUser = Users.Entities.User.LoadUserByName(loginDlgBench.Alias, dbType, connStr);
loadedUser = Users.Entities.User.LoadUserByName(loginDlgBench.Alias, db);
if (loadedUser != null) authorized = loadedUser.Authorize(loginDlgBench.Alias, loginDlgBench.Password);
break;
case Users.Entities.LoginMethod.FullName:
loadedUser = Users.Entities.User.LoadUserByFullName(loginDlgBench.Alias, dbType, connStr);
loadedUser = Users.Entities.User.LoadUserByFullName(loginDlgBench.Alias, db);
if (loadedUser != null) authorized = loadedUser.AuthorizeFullName(loginDlgBench.Alias, loginDlgBench.Password);
break;
case Users.Entities.LoginMethod.Number:
int number;
if (!int.TryParse(loginDlgBench.Alias, out number)) break;
loadedUser = Users.Entities.User.LoadUserByNumber(number, dbType, connStr);
loadedUser = Users.Entities.User.LoadUserByNumber(number, db);
if (loadedUser != null) authorized = loadedUser.AuthorizeNumber(number, loginDlgBench.Password);
break;
}
@ -258,44 +259,16 @@ namespace TBF
authorized = false;
}
if (settingsChanged && authorized) LocalSettings.Save();
}
if (authorized) break;
///
/// One more attempt: authorisation with Config DB
///
if (!authorized)
{
/// Load and authorise user from the Config database
try
{
switch (loginDlgBench.Method)
{
default:
case Users.Entities.LoginMethod.UserName:
loadedUser = Config.Entities.User.LoadUserByName(loginDlgBench.Alias, LocalSettings.TestBenches[i].ProceduresDBSettings);
if (loadedUser != null) authorized = loadedUser.Authorize(loginDlgBench.Alias, loginDlgBench.Password);
break;
case Users.Entities.LoginMethod.FullName:
loadedUser = Config.Entities.User.LoadUserByFullName(loginDlgBench.Alias, LocalSettings.TestBenches[i].ProceduresDBSettings);
if (loadedUser != null) authorized = loadedUser.AuthorizeFullName(loginDlgBench.Alias, loginDlgBench.Password);
break;
case Users.Entities.LoginMethod.Number:
int number;
if (!int.TryParse(loginDlgBench.Alias, out number)) break;
loadedUser = Config.Entities.User.LoadUserByNumber(number, LocalSettings.TestBenches[i].ProceduresDBSettings);
if (loadedUser != null) authorized = loadedUser.AuthorizeNumber(number, loginDlgBench.Password);
break;
}
}
catch
{
authorized = false;
authorizedAs = Users.Entities.AuthorizedAs.LocalUser; /// Try local DB afterwards
}
}
if (authorized)
{
Users.GlobalData.AuthorizedAs = authorizedAs;
LocalSettings.LoginMethod = loginDlgBench.Method; /// Save the login method actually used
if (LocalSettings.TestBenches[i].IsRealBench)

View File

@ -29,5 +29,5 @@ using System.Runtime.InteropServices;
// Build Number
// Revision
//
[assembly: AssemblyVersion("2.12.497.1")]
[assembly: AssemblyFileVersion("2.12.497.1")]
[assembly: AssemblyVersion("2.12.498.1")]
[assembly: AssemblyFileVersion("2.12.498.1")]

View File

@ -96,11 +96,8 @@ namespace UserManagement
try
{
Users.Entities.User user = Users.Entities.User.LoadUserByName("SAD", Users.Entities.DBType.MySql, Program.LocalSettings.ConnectionString);
//Users.DB.ConnectionString = Program.LocalSettings.ConnectionString;
//Users.DB.LoadSharedData();
Users.DB.ConnectionString = Program.LocalSettings.ConnectionString;
Users.DB.LoadSharedData();
authorized = true; /// No LoginDlg, authorized when reading users from the DB is successfull
}

View File

@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.12.497.0")]
[assembly: AssemblyFileVersion("2.12.497.0")]
[assembly: AssemblyVersion("2.12.498.0")]
[assembly: AssemblyFileVersion("2.12.498.0")]

31
Users/DBSettings.cs Normal file
View File

@ -0,0 +1,31 @@
///
/// Copyright (c) 2013-2017 Sensus Metering Systems
///
using System;
namespace Users
{
/// <summary>
/// Database settings required to make a connection
/// </summary>
public class DBSettings : ICloneable
{
public Users.Entities.DBType DbType;
public string ConnectionString;
public DBSettings()
{
}
public DBSettings(Users.Entities.DBType dbType, string connectionString)
{
this.DbType = dbType;
this.ConnectionString = connectionString;
}
public object Clone()
{
return new DBSettings(DbType, ConnectionString);
}
}
}

View File

@ -10,7 +10,7 @@ namespace Users.Entities
{
MySql, /// MySQL database
SQLite, /// SQLite
DbTypesCount,
Count
}
/// <summary> Identifies the database based on the content </summary>
@ -18,7 +18,7 @@ namespace Users.Entities
{
Config,
Results,
Count,
Count
}
public enum LoginMethod
@ -28,4 +28,12 @@ namespace Users.Entities
Number,
Count
}
public enum AuthorizedAs
{
PowerUser,
LocalUser,
RemoteUser,
Count
}
}

View File

@ -1,5 +1,5 @@
///
/// Copyright (c) 2016 Sensus Metering Systems
/// Copyright (c) 2016-2017 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
@ -11,5 +11,15 @@ namespace Users.Entities
public virtual int Id { get; protected set; }
public virtual int Gid { get; set; }
public virtual string Name { get; set; }
public Group()
{
}
public Group(int gid, string name)
{
Gid = gid;
Name = name;
}
}
}

View File

@ -44,6 +44,22 @@ namespace Users.Entities
Groups.Add(group);
}
/// <summary>
/// All members are copied except of ID (so that NHibernate works properly).
/// List of groups is empty.
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public virtual object Clone()
{
User newUser = new User(UserName, Number, false);
newUser.FullName = FullName;
newUser.Password = Password;
newUser.LastPwChange = LastPwChange;
return newUser;
}
/// ------------- Additional stuff not mapped into the database -------------
/// <summary>
@ -248,10 +264,12 @@ namespace Users.Entities
/// </summary>
/// <param name="username">User name for the query</param>
/// <returns>reference to a 'User' (if it exists) or null</returns>
public static User LoadUserByName(string userName, DBType dbType, string connectionString)
public static User LoadUserByName(string userName, DBSettings dbSettings)
{
DB.DbType = dbType;
DB.ConnectionString = connectionString;
if (dbSettings == null || string.IsNullOrEmpty(dbSettings.ConnectionString)) return null;
DB.DbType = dbSettings.DbType;
DB.ConnectionString = dbSettings.ConnectionString;
IList<User> listOfUsers = DB.CreateSession()
.CreateQuery("FROM User WHERE LOWER(UserName) = :username") /// Note: QueryOver<User>().Where(x => x.UserName.ToLower() == ... does not work
.SetParameter("username", userName.ToLower())
@ -282,10 +300,12 @@ namespace Users.Entities
/// </summary>
/// <param name="fullName">Full name for the query</param>
/// <returns>reference to a 'User' (if it exists) or null</returns>
public static User LoadUserByFullName(string fullName, DBType dbType, string connectionString)
public static User LoadUserByFullName(string fullName, DBSettings dbSettings)
{
DB.DbType = dbType;
DB.ConnectionString = connectionString;
if (dbSettings == null || string.IsNullOrEmpty(dbSettings.ConnectionString)) return null;
DB.DbType = dbSettings.DbType;
DB.ConnectionString = dbSettings.ConnectionString;
IList<User> listOfUsers = DB.CreateSession()
.CreateQuery("FROM User WHERE LOWER(Description) = :fullname") /// Note: QueryOver<User>().Where(x => x.FullName.ToLower() == ... does not work
.SetParameter("fullname", fullName.ToLower())
@ -315,10 +335,12 @@ namespace Users.Entities
/// </summary>
/// <param name="number">User ID number for the query</param>
/// <returns>reference to a 'User' (if it exists) or null</returns>
public static User LoadUserByNumber(int number, DBType dbType, string connectionString)
public static User LoadUserByNumber(int number, DBSettings dbSettings)
{
DB.DbType = dbType;
DB.ConnectionString = connectionString;
if (dbSettings == null || string.IsNullOrEmpty(dbSettings.ConnectionString)) return null;
DB.DbType = dbSettings.DbType;
DB.ConnectionString = dbSettings.ConnectionString;
IList<User> listOfUsers = DB.CreateSession()
.QueryOver<User>()
.Where(x => (x.Number == number))

View File

@ -95,7 +95,8 @@ namespace Users.Forms
CurrentEditUser.Groups.Clear();
for (int i = 0; (int)i < checkedListBoxGroups.Items.Count ; i++)
{
if (checkedListBoxGroups.GetItemChecked(i)) {
if (checkedListBoxGroups.GetItemChecked(i))
{
Group newGroup = Grp.FromId((Grp.GID)i);
CurrentEditUser.Groups.Add(newGroup );
}

View File

@ -2,11 +2,6 @@
/// Copyright (c) 2017 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Users.Resources;
@ -114,11 +109,22 @@ namespace Users.Forms
user = userNameTextBox.Text;
password = passwordTextBox.Text;
Entities.AuthorizedAs authorizedAs = Entities.AuthorizedAs.PowerUser;
bool authorized = Entities.User.IsPowerUser(user, password);
if (authorized)
{
Users.GlobalData.CurrentUser = new Users.Entities.User(user, 6, true);
}
if (!authorized && !noDatabase)
{
Entities.User loadedUser = Entities.User.LoadUserByName(user);
DBSettings[] dbs = new DBSettings[] { GlobalData.RemoteUsersDB, GlobalData.LocalUsersDB };
authorizedAs = Entities.AuthorizedAs.RemoteUser;
foreach (var db in dbs)
{
Users.Entities.User loadedUser = Users.Entities.User.LoadUserByName(user, db);
if (loadedUser != null)
{
switch (Method)
@ -137,10 +143,16 @@ namespace Users.Forms
break;
}
}
if (authorized) break;
authorizedAs = Entities.AuthorizedAs.LocalUser;
}
}
if (authorized)
{
Users.GlobalData.AuthorizedAs = authorizedAs;
DialogResult = DialogResult.OK;
Close();
return;

View File

@ -127,7 +127,70 @@ namespace Users.Forms
private void copyFromRemoteButton_Click(object sender, EventArgs e)
{
if (GlobalData.RemoteUsersDB == null || string.IsNullOrEmpty(GlobalData.RemoteUsersDB.ConnectionString) ||
GlobalData.LocalUsersDB == null || string.IsNullOrEmpty(GlobalData.LocalUsersDB.ConnectionString))
{
MessageBox.Show(Strings.No_remote_database_of_users,
Strings.Error,
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
return;
}
if (MessageBox.Show(Strings.Are_you_sure,
Strings.Warning,
MessageBoxButtons.YesNo,
MessageBoxIcon.Exclamation) != DialogResult.Yes)
{
return;
}
///
/// Read remote users
///
DB.DbType = GlobalData.RemoteUsersDB.DbType;
DB.ConnectionString = GlobalData.RemoteUsersDB.ConnectionString;
ISession session = DB.CreateSession();
IList<User> remoteUsers = session.QueryOver<User>().List();
IList<Group> remoteGroups = session.QueryOver<Group>().List();
///
/// Delete all local users
///
DB.DbType = GlobalData.LocalUsersDB.DbType;
DB.ConnectionString = GlobalData.LocalUsersDB.ConnectionString;
session = DB.CreateSession();
IList<User> users = session.QueryOver<User>().List();
foreach (var user in users) DB.DeleteObject(session, user);
IList<Group> groups = session.QueryOver<Group>().List();
foreach (var group in groups) DB.DeleteObject(session, group);
session.Flush();
///
/// Save all remote users to the local DB
///
DB.DbType = GlobalData.LocalUsersDB.DbType;
DB.ConnectionString = GlobalData.LocalUsersDB.ConnectionString;
session = DB.CreateSession();
IList<User> newUsers = session.QueryOver<User>().List();
IList<Group> newGroups = session.QueryOver<Group>().List();
foreach (var user in remoteUsers)
{
User newUser = (User)user.Clone();
newUsers.Add(newUser);
foreach (var group in user.Groups)
{
Group newGroup = new Group(group.Gid, group.Name);
newUser.AddGroup(newGroup);
}
DB.SaveObject(session, newUser);
}
session.Flush();
MessageBox.Show(string.Format(Strings.N_users_copied, remoteUsers.Count),
Strings.Confirmation,
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
}

View File

@ -4,7 +4,11 @@ namespace Users
{
public class GlobalData
{
public static DBSettings RemoteUsersDB = null;
public static DBSettings LocalUsersDB = null;
public static IUser CurrentUser;
public static Entities.AuthorizedAs AuthorizedAs;
public static DateTime LastAuthorization = DateTime.Now;
}
}

View File

@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.12.475.0")]
[assembly: AssemblyFileVersion("2.12.475.0")]
[assembly: AssemblyVersion("2.12.498.0")]
[assembly: AssemblyFileVersion("2.12.498.0")]

View File

@ -69,6 +69,15 @@ namespace Users.Resources {
}
}
/// <summary>
/// Looks up a localized string similar to Are you sure?.
/// </summary>
internal static string Are_you_sure {
get {
return ResourceManager.GetString("Are_you_sure", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Cancel.
/// </summary>
@ -87,6 +96,15 @@ namespace Users.Resources {
}
}
/// <summary>
/// Looks up a localized string similar to Confirmation.
/// </summary>
internal static string Confirmation {
get {
return ResourceManager.GetString("Confirmation", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Description.
/// </summary>
@ -168,6 +186,15 @@ namespace Users.Resources {
}
}
/// <summary>
/// Looks up a localized string similar to {0} users copied.
/// </summary>
internal static string N_users_copied {
get {
return ResourceManager.GetString("N_users_copied", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Name.
/// </summary>
@ -177,6 +204,15 @@ namespace Users.Resources {
}
}
/// <summary>
/// Looks up a localized string similar to No remote database of users.
/// </summary>
internal static string No_remote_database_of_users {
get {
return ResourceManager.GetString("No_remote_database_of_users", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to OK.
/// </summary>
@ -257,5 +293,14 @@ namespace Users.Resources {
return ResourceManager.GetString("Username_taken", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Warning.
/// </summary>
internal static string Warning {
get {
return ResourceManager.GetString("Warning", resourceCulture);
}
}
}
}

View File

@ -183,4 +183,13 @@
<data name="CloseBtnText" xml:space="preserve">
<value>Zavřít</value>
</data>
<data name="Confirmation" xml:space="preserve">
<value>Potvrzení</value>
</data>
<data name="N_users_copied" xml:space="preserve">
<value>Vytvořených {0} uživatelů v lokální databáze.</value>
</data>
<data name="Warning" xml:space="preserve">
<value>Varování</value>
</data>
</root>

View File

@ -183,4 +183,13 @@
<data name="CloseBtnText" xml:space="preserve">
<value>Beenden</value>
</data>
<data name="Confirmation" xml:space="preserve">
<value>Bestätigung</value>
</data>
<data name="N_users_copied" xml:space="preserve">
<value>{0} Benutzer gemacht.</value>
</data>
<data name="Warning" xml:space="preserve">
<value>Warnung</value>
</data>
</root>

View File

@ -183,4 +183,19 @@
<data name="CloseBtnText" xml:space="preserve">
<value>Close</value>
</data>
<data name="No_remote_database_of_users" xml:space="preserve">
<value>No remote database of users</value>
</data>
<data name="Are_you_sure" xml:space="preserve">
<value>Are you sure?</value>
</data>
<data name="Warning" xml:space="preserve">
<value>Warning</value>
</data>
<data name="Confirmation" xml:space="preserve">
<value>Confirmation</value>
</data>
<data name="N_users_copied" xml:space="preserve">
<value>{0} users copied</value>
</data>
</root>

View File

@ -57,6 +57,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="DBSettings.cs" />
<Compile Include="GlobalData.cs" />
<Compile Include="DB.cs" />
<Compile Include="Entities\Enums.cs" />