Config.IUser interface added, etc.
This commit is contained in:
parent
9bd409e672
commit
13b80de703
@ -94,6 +94,7 @@
|
||||
<Compile Include="Entities\WMType.cs" />
|
||||
<Compile Include="FluentCommon.cs" />
|
||||
<Compile Include="Grp.cs" />
|
||||
<Compile Include="IUser.cs" />
|
||||
<Compile Include="Mappings\BenchPathMap.cs" />
|
||||
<Compile Include="Mappings\ComponentMap.cs" />
|
||||
<Compile Include="Mappings\ComponentProcedureMap.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";
|
||||
|
||||
@ -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<Group> 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<Group>();
|
||||
Description = "Power user";
|
||||
Description = powerUser ? "Power user" : string.Empty;
|
||||
}
|
||||
|
||||
public virtual void AddGroup(Group group)
|
||||
@ -146,6 +147,66 @@ namespace Config.Entities
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <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"></param>
|
||||
/// <param name="password"></param>
|
||||
/// <param name="requiredGrupMembership"></param>
|
||||
/// <returns>true = authorized</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <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="alias"></param>
|
||||
/// <param name="password"></param>
|
||||
/// <param name="requiredGrupMembership"></param>
|
||||
/// <returns>true = authorized</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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().
|
||||
/// </summary>
|
||||
/// <param name="userName"></param>
|
||||
/// <param name="password"></param>
|
||||
/// <param name="requiredGroupMembership"></param>
|
||||
/// <param name="userName">User name</param>
|
||||
/// <param name="password">Password</param>
|
||||
/// <returns>true = authorized</returns>
|
||||
public virtual bool Authorize(string userName, string password)
|
||||
{
|
||||
return Authorize(userName, password, Grp.GID.None);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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().
|
||||
/// </summary>
|
||||
/// <param name="number">User ID number</param>
|
||||
/// <param name="password">Password</param>
|
||||
/// <returns>true = authorized</returns>
|
||||
public virtual bool AuthorizeNumber(int number, string password)
|
||||
{
|
||||
return AuthorizeNumber(number, password, Grp.GID.None);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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().
|
||||
/// </summary>
|
||||
/// <param name="userName">Alias (abbreviated user name)</param>
|
||||
/// <param name="password">Password</param>
|
||||
/// <returns>true = authorized</returns>
|
||||
public virtual bool AuthorizeAlias(string alias, string password)
|
||||
{
|
||||
return AuthorizeAlias(alias, password, Grp.GID.None);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a 'User' with a given username from a database.
|
||||
/// </summary>
|
||||
@ -183,10 +273,10 @@ namespace Config.Entities
|
||||
public static User LoadUserByName(string username, DBSettings dbSettings)
|
||||
{
|
||||
IList<User> listOfUsers = FluentCommon.CreateSessionFactory(DBKind.Config, dbSettings, false)
|
||||
.OpenSession()
|
||||
.CreateQuery("FROM User WHERE LOWER(Name) = :username")
|
||||
.SetParameter("username", username.ToLower())
|
||||
.List<User>();
|
||||
.OpenSession()
|
||||
.QueryOver<User>()
|
||||
.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<User> listOfUsers = FluentCommon.CreateSession(DBKind.Config)
|
||||
.CreateQuery("FROM User WHERE LOWER(Name) = :username")
|
||||
.SetParameter("username", username.ToLower())
|
||||
.List<User>();
|
||||
.QueryOver<User>()
|
||||
.Where(x => (x.Name.ToLower() == username.ToLower()))
|
||||
.List();
|
||||
if (listOfUsers.Count > 0) return listOfUsers[0];
|
||||
return null;
|
||||
}
|
||||
|
||||
26
Config/IUser.cs
Normal file
26
Config/IUser.cs
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
|
||||
@ -62,7 +62,7 @@ namespace TBF.UiControls
|
||||
|
||||
public Config.Grp.GID RequiredGroupMembership;
|
||||
|
||||
Config.Entities.User originalUser;
|
||||
Config.IUser originalUser;
|
||||
|
||||
/// <summary>
|
||||
/// Default constructor
|
||||
|
||||
@ -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<Group> 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<Group>();
|
||||
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; } }
|
||||
|
||||
/// <summary>
|
||||
/// Check whether user is a member of a group.
|
||||
/// </summary>
|
||||
@ -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;
|
||||
}
|
||||
|
||||
/// <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"></param>
|
||||
/// <param name="password"></param>
|
||||
/// <param name="requiredGrupMembership"></param>
|
||||
/// <returns>true = authorized</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <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="alias"></param>
|
||||
/// <param name="password"></param>
|
||||
/// <param name="requiredGrupMembership"></param>
|
||||
/// <returns>true = authorized</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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().
|
||||
/// </summary>
|
||||
/// <param name="userName"></param>
|
||||
/// <param name="password"></param>
|
||||
/// <param name="requiredGroupMembership"></param>
|
||||
/// <returns>true = authorized</returns>
|
||||
/// <param name="userName">User name</param>
|
||||
/// <param name="password">Password</param>
|
||||
/// <returns>true = authorized</returns>
|
||||
public virtual bool Authorize(string userName, string password)
|
||||
{
|
||||
return Authorize(userName, password, Grp.GID.None);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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().
|
||||
/// </summary>
|
||||
/// <param name="number">User ID number</param>
|
||||
/// <param name="password">Password</param>
|
||||
/// <returns>true = authorized</returns>
|
||||
public virtual bool AuthorizeNumber(int number, string password)
|
||||
{
|
||||
return AuthorizeNumber(number, password, Grp.GID.None);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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().
|
||||
/// </summary>
|
||||
/// <param name="userName">Alias (abbreviated user name)</param>
|
||||
/// <param name="password">Password</param>
|
||||
/// <returns>true = authorized</returns>
|
||||
public virtual bool AuthorizeAlias(string alias, string password)
|
||||
{
|
||||
return AuthorizeAlias(alias, password, Grp.GID.None);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a 'User' with a given username from a database.
|
||||
/// </summary>
|
||||
@ -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<User>()
|
||||
.Where(x => (x.Name.ToLower() == username.ToLower()))
|
||||
.List();
|
||||
|
||||
if (listOfUsers.Count > 0) return listOfUsers[0];
|
||||
return null;
|
||||
}
|
||||
@ -231,8 +307,8 @@ namespace Users.Entities
|
||||
/// </summary>
|
||||
public static void Unauthorize()
|
||||
{
|
||||
currentUser = null;
|
||||
lastAuthorization = DateTime.Now;
|
||||
Data.CurrentUser = null;
|
||||
Data.LastAuthorization = DateTime.Now;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
101
Users/Grp.cs
101
Users/Grp.cs
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public class Grp
|
||||
{
|
||||
/// <summary>
|
||||
/// GID-s of user groups, each user is member of one or more groups.
|
||||
/// </summary>
|
||||
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
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Number of groups.
|
||||
/// </summary>
|
||||
public const int Count = (int)GID.NrOfGroups;
|
||||
|
||||
// Static array of all groups.
|
||||
static Grp[] groups;
|
||||
|
||||
/// <summary>
|
||||
/// Static constructor, creates all groups with no members.
|
||||
/// </summary>
|
||||
static Grp()
|
||||
{
|
||||
groups = new Grp[Count];
|
||||
for (int i = 0; i < Count; i++)
|
||||
{
|
||||
groups[i] = new Grp((GID)i);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a Group from Id.
|
||||
/// </summary>
|
||||
/// <param name="id">Group Gid</param>
|
||||
/// <returns>Entities.Group object reference</returns>
|
||||
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;
|
||||
|
||||
/// <summary>
|
||||
/// Instance constructor.
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
public Grp(GID gid)
|
||||
{
|
||||
group = new Entities.Group();
|
||||
this.group.Gid = (int)gid;
|
||||
this.group.Name = gid.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -58,7 +58,6 @@
|
||||
<Compile Include="DB.cs" />
|
||||
<Compile Include="Entities\Group.cs" />
|
||||
<Compile Include="Entities\User.cs" />
|
||||
<Compile Include="Grp.cs" />
|
||||
<Compile Include="Mappings\GroupMap.cs" />
|
||||
<Compile Include="Mappings\UserMap.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
|
||||
Loading…
Reference in New Issue
Block a user