Config.IUser interface added, etc.

This commit is contained in:
Milan Hanajik
2016-12-21 07:56:26 +01:00
parent 9bd409e672
commit 13b80de703
9 changed files with 237 additions and 146 deletions
+104 -28
View File
@@ -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>