diff --git a/Config/Config.csproj b/Config/Config.csproj index 1b68a24ce..eb3dc16a0 100644 --- a/Config/Config.csproj +++ b/Config/Config.csproj @@ -94,6 +94,7 @@ + diff --git a/Config/Data.cs b/Config/Data.cs index f58e0dc4f..4b1ad9fc8 100644 --- a/Config/Data.cs +++ b/Config/Data.cs @@ -5,7 +5,7 @@ namespace Config public class Data { /// Current user - public static Entities.User CurrentUser; + public static IUser CurrentUser; public static DateTime LastAuthorization = DateTime.Now; public const string AdminUsername = "admin"; diff --git a/Config/Entities/User.cs b/Config/Entities/User.cs index efa7ea05b..4326f9db0 100644 --- a/Config/Entities/User.cs +++ b/Config/Entities/User.cs @@ -11,14 +11,15 @@ using log4net; namespace Config.Entities { - public class User + public class User : IUser { static readonly ILog log = LogManager.GetLogger(typeof(User)); public virtual int Id { get; protected set; } public virtual string Name { get; set; } public virtual int Number { get; set; } - public virtual string Password { get; set; } + public virtual string Alias { get; set; } + public virtual string Password { get; set; } public virtual string Description { get; set; } public virtual DateTime LastPwChange { get; set; } public virtual IList Groups { get; set; } //User can be a member of a list of groups @@ -42,7 +43,7 @@ namespace Config.Entities Number = number; this.powerUser = powerUser; Groups = new List(); - Description = "Power user"; + Description = powerUser ? "Power user" : string.Empty; } public virtual void AddGroup(Group group) @@ -146,6 +147,66 @@ namespace Config.Entities return false; } + /// + /// 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(). + /// + /// + /// + /// + /// true = authorized + public virtual bool AuthorizeNumber(int number, string password, Grp.GID requiredGroupMembership) + { + if (Number == number) + { + if (IsMemberOf(requiredGroupMembership) && CheckPassword(password)) + { + Data.CurrentUser = this; + Data.LastAuthorization = DateTime.Now; + log.FatalFormat("User ID={0} authorized @level '{1}'", number, requiredGroupMembership); + return true; + } + else + { + return false; + } + } + return false; + } + + /// + /// 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(). + /// + /// + /// + /// + /// true = authorized + public virtual bool AuthorizeAlias(string alias, string password, Grp.GID requiredGroupMembership) + { + if (Alias.ToLower() == alias.ToLower()) + { + if (IsMemberOf(requiredGroupMembership) && CheckPassword(password)) + { + Data.CurrentUser = this; + Data.LastAuthorization = DateTime.Now; + log.FatalFormat("User alias={0} authorized @level '{1}'", alias, requiredGroupMembership); + return true; + } + else + { + return false; + } + } + return false; + } + /// /// The SECOND of TWO possible user authorization method to be used when /// no specific group membership is required. @@ -153,15 +214,44 @@ namespace Config.Entities /// (i.e. the access rights were not risen to a higher level). /// If you require different behavior, use Unauthorize() before calling Authorize(). /// - /// - /// - /// + /// User name + /// Password /// true = authorized public virtual bool Authorize(string userName, string password) { return Authorize(userName, password, Grp.GID.None); } + /// + /// The SECOND of TWO possible user authorization method to be used when + /// no specific group membership is required. + /// 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(). + /// + /// User ID number + /// Password + /// true = authorized + public virtual bool AuthorizeNumber(int number, string password) + { + return AuthorizeNumber(number, password, Grp.GID.None); + } + + /// + /// The SECOND of TWO possible user authorization method to be used when + /// no specific group membership is required. + /// 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(). + /// + /// Alias (abbreviated user name) + /// Password + /// true = authorized + public virtual bool AuthorizeAlias(string alias, string password) + { + return AuthorizeAlias(alias, password, Grp.GID.None); + } + /// /// Returns a 'User' with a given username from a database. /// @@ -183,10 +273,10 @@ namespace Config.Entities public static User LoadUserByName(string username, DBSettings dbSettings) { IList listOfUsers = FluentCommon.CreateSessionFactory(DBKind.Config, dbSettings, false) - .OpenSession() - .CreateQuery("FROM User WHERE LOWER(Name) = :username") - .SetParameter("username", username.ToLower()) - .List(); + .OpenSession() + .QueryOver() + .Where(x => (x.Name.ToLower() == username.ToLower())) + .List(); if (listOfUsers.Count > 0) return listOfUsers[0]; return null; } @@ -199,9 +289,9 @@ namespace Config.Entities public static User LoadUserByName(string username) { IList listOfUsers = FluentCommon.CreateSession(DBKind.Config) - .CreateQuery("FROM User WHERE LOWER(Name) = :username") - .SetParameter("username", username.ToLower()) - .List(); + .QueryOver() + .Where(x => (x.Name.ToLower() == username.ToLower())) + .List(); if (listOfUsers.Count > 0) return listOfUsers[0]; return null; } diff --git a/Config/IUser.cs b/Config/IUser.cs new file mode 100644 index 000000000..2b548e8fd --- /dev/null +++ b/Config/IUser.cs @@ -0,0 +1,26 @@ +using System; + +namespace Config +{ + public interface IUser + { + string Name { get; } + string Alias { get; } + int Number { get; } + + bool IsMemberOf(Grp.GID group); + + bool Authorize(string username, string password); + bool Authorize(string username, string password, Grp.GID requiredGroup); + + bool AuthorizeAlias(string alias, string password); + bool AuthorizeAlias(string alias, string password, Grp.GID requiredGroup); + + bool AuthorizeNumber(int number, string password); + bool AuthorizeNumber(int number, string password, Grp.GID requiredGroup); + + //IUser LoadUserByName(string username, DBSettings dbSettings); + //IUser LoadUserByNumber(int number, DBSettings dbSettings); + //IUser LoadUserByAlias(string alias, DBSettings dbSettings); + } +} diff --git a/TestBenchFramework/Program.cs b/TestBenchFramework/Program.cs index 6f3191c0f..067d5f6ec 100644 --- a/TestBenchFramework/Program.cs +++ b/TestBenchFramework/Program.cs @@ -203,7 +203,7 @@ namespace TBF { log.FatalFormat("Test bench name: {0}", loginDlgBench.BenchName); - User loadedUser = null; + Config.IUser loadedUser = null; try { bool authorized = false; diff --git a/TestBenchFramework/UiControls/SharedButtons.cs b/TestBenchFramework/UiControls/SharedButtons.cs index c3f692cf6..6fb1a08f4 100644 --- a/TestBenchFramework/UiControls/SharedButtons.cs +++ b/TestBenchFramework/UiControls/SharedButtons.cs @@ -62,7 +62,7 @@ namespace TBF.UiControls public Config.Grp.GID RequiredGroupMembership; - Config.Entities.User originalUser; + Config.IUser originalUser; /// /// Default constructor diff --git a/Users/Entities/User.cs b/Users/Entities/User.cs index 746baeca1..611f8026d 100644 --- a/Users/Entities/User.cs +++ b/Users/Entities/User.cs @@ -11,14 +11,15 @@ using log4net; namespace Users.Entities { - public class User + public class User : IUser { static readonly ILog log = LogManager.GetLogger(typeof(User)); public virtual int Id { get; protected set; } public virtual string Name { get; set; } public virtual int Number { get; set; } - public virtual string Password { get; set; } + public virtual string Alias { get; set; } + public virtual string Password { get; set; } public virtual string Description { get; set; } public virtual DateTime LastPwChange { get; set; } public virtual IList Groups { get; set; } //User can be a member of a list of groups @@ -38,7 +39,7 @@ namespace Users.Entities Number = number; this.powerUser = powerUser; Groups = new List(); - Description = "Power user"; + Description = powerUser ? "Power user" : string.Empty; } public virtual void AddGroup(Group group) @@ -48,19 +49,6 @@ namespace Users.Entities /// ------------- Additional stuff not mapped into the database ------------- - /// - /// Authorized user (static) - /// - private static User currentUser = null; /// Updated by instance methods Authorize, Unauthorize - public static User CurrentUser - { - get { return currentUser; } - set { currentUser = value; } - } - - private static DateTime lastAuthorization = DateTime.Now; - public static DateTime LastAuthorization { get { return lastAuthorization; } } - /// /// Check whether user is a member of a group. /// @@ -132,8 +120,8 @@ namespace Users.Entities { if (Entities.User.IsPowerUser(userName, password)) { - currentUser = this; - lastAuthorization = DateTime.Now; + Data.CurrentUser = this; + Data.LastAuthorization = DateTime.Now; log.FatalFormat("Power user '{0}' authorized @level '{1}'", userName, requiredGroupMembership); return true; } @@ -142,8 +130,8 @@ namespace Users.Entities { if (IsMemberOf(requiredGroupMembership) && CheckPassword(password)) { - currentUser = this; - lastAuthorization = DateTime.Now; + Data.CurrentUser = this; + Data.LastAuthorization = DateTime.Now; log.FatalFormat("User '{0}' authorized @level '{1}'", userName, requiredGroupMembership); return true; } @@ -155,6 +143,66 @@ namespace Users.Entities return false; } + /// + /// 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(). + /// + /// + /// + /// + /// true = authorized + public virtual bool AuthorizeNumber(int number, string password, Grp.GID requiredGroupMembership) + { + if (Number == number) + { + if (IsMemberOf(requiredGroupMembership) && CheckPassword(password)) + { + Data.CurrentUser = this; + Data.LastAuthorization = DateTime.Now; + log.FatalFormat("User ID={0} authorized @level '{1}'", number, requiredGroupMembership); + return true; + } + else + { + return false; + } + } + return false; + } + + /// + /// 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(). + /// + /// + /// + /// + /// true = authorized + public virtual bool AuthorizeAlias(string alias, string password, Grp.GID requiredGroupMembership) + { + if (Alias.ToLower() == alias.ToLower()) + { + if (IsMemberOf(requiredGroupMembership) && CheckPassword(password)) + { + Data.CurrentUser = this; + Data.LastAuthorization = DateTime.Now; + log.FatalFormat("User alias={0} authorized @level '{1}'", alias, requiredGroupMembership); + return true; + } + else + { + return false; + } + } + return false; + } + /// /// The SECOND of TWO possible user authorization method to be used when /// no specific group membership is required. @@ -162,15 +210,44 @@ namespace Users.Entities /// (i.e. the access rights were not risen to a higher level). /// If you require different behavior, use Unauthorize() before calling Authorize(). /// - /// - /// - /// - /// true = authorized + /// User name + /// Password + /// true = authorized public virtual bool Authorize(string userName, string password) { return Authorize(userName, password, Grp.GID.None); } + /// + /// The SECOND of TWO possible user authorization method to be used when + /// no specific group membership is required. + /// 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(). + /// + /// User ID number + /// Password + /// true = authorized + public virtual bool AuthorizeNumber(int number, string password) + { + return AuthorizeNumber(number, password, Grp.GID.None); + } + + /// + /// The SECOND of TWO possible user authorization method to be used when + /// no specific group membership is required. + /// 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(). + /// + /// Alias (abbreviated user name) + /// Password + /// true = authorized + public virtual bool AuthorizeAlias(string alias, string password) + { + return AuthorizeAlias(alias, password, Grp.GID.None); + } + /// /// Returns a 'User' with a given username from a database. /// @@ -180,7 +257,7 @@ namespace Users.Entities { User user = new User(); user.Name = username; - currentUser = user; + Data.CurrentUser = user; return user; } @@ -212,7 +289,6 @@ namespace Users.Entities .QueryOver() .Where(x => (x.Name.ToLower() == username.ToLower())) .List(); - if (listOfUsers.Count > 0) return listOfUsers[0]; return null; } @@ -231,8 +307,8 @@ namespace Users.Entities /// public static void Unauthorize() { - currentUser = null; - lastAuthorization = DateTime.Now; + Data.CurrentUser = null; + Data.LastAuthorization = DateTime.Now; } /// diff --git a/Users/Grp.cs b/Users/Grp.cs deleted file mode 100644 index 2ba822d3d..000000000 --- a/Users/Grp.cs +++ /dev/null @@ -1,101 +0,0 @@ -/// -/// Copyright (c) 2016 Sensus Metering Systems -/// -using System; -using System.Collections.Generic; -using System.Text; - -using Users.Entities; -using NHibernate; -using FluentNHibernate; - -namespace Users -{ - /// - /// Enum Grp.GID specifies GID-s of user groups. - /// An instance of this class represenst a group, which contains a list of its members (users). - /// Static private array 'groups' of this class, accesible through indexer, contains all groups. - /// - public class Grp - { - /// - /// GID-s of user groups, each user is member of one or more groups. - /// - public enum GID - { - // The first NrOfGroups items are GroupID-s (0..NrOfGroups-1) - Testers = 0, - TestingSpecialists, - HeadOfLab, - MaintenanceSpecialists, - Metrologists, - CalibrationSpecialists, - Administrators, // application / network / database administrator - Custom1, - Custom2, - Custom3, - NrOfGroups, // Number of groups (this is not a GroupID) - None, // Not a GroupID, can be used to indicate no group membership is required - Invalid = -1, // Not a GroupID, indicates a variable has not been properly set so far - } - - /// - /// Number of groups. - /// - public const int Count = (int)GID.NrOfGroups; - - // Static array of all groups. - static Grp[] groups; - - /// - /// Static constructor, creates all groups with no members. - /// - static Grp() - { - groups = new Grp[Count]; - for (int i = 0; i < Count; i++) - { - groups[i] = new Grp((GID)i); - } - } - - /// - /// Returns a Group from Id. - /// - /// Group Gid - /// Entities.Group object reference - public static Entities.Group FromId(GID gid) - { - int i = (int)gid; - - if (i >= 0 && i < Count) - { - return groups[i].group; - } - else - { - return null; - } - } - - ///////////////////// ^ static ^ ///////////////////// v instance v ///////////////////// - - /// Public properties - public GID Gid { get { return (GID)group.Gid; } } - public string Name { get { return group.Name; } set { group.Name = value; } } - - /// Private fields - Entities.Group group; - - /// - /// Instance constructor. - /// - /// - public Grp(GID gid) - { - group = new Entities.Group(); - this.group.Gid = (int)gid; - this.group.Name = gid.ToString(); - } - } -} diff --git a/Users/Users.csproj b/Users/Users.csproj index 3751836be..07edbbb4e 100644 --- a/Users/Users.csproj +++ b/Users/Users.csproj @@ -58,7 +58,6 @@ -