402 lines
14 KiB
C#
402 lines
14 KiB
C#
///
|
|
/// Copyright (c) 2013-2016 Sensus Metering Systems
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Diagnostics;
|
|
using Config.Resources;
|
|
using log4net;
|
|
|
|
namespace Config.Entities
|
|
{
|
|
public class User : 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
|
|
public virtual int Number { get; set; }
|
|
public virtual string Password { 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
|
|
|
|
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 IPERLST || IPERLST_SPECIAL
|
|
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(Grp.GID groupId)
|
|
{
|
|
if (groupId == Grp.GID.None || powerUser)
|
|
{
|
|
return true;
|
|
}
|
|
else if (groupId >= 0 && (int)groupId < 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, Grp.GID requiredGroupMembership)
|
|
{
|
|
if (Entities.User.IsPowerUser(userName, password))
|
|
{
|
|
Data.CurrentUser = this;
|
|
Data.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))
|
|
{
|
|
Data.CurrentUser = this;
|
|
Data.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, 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, 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 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, 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, Grp.GID requiredGroupMembership)
|
|
{
|
|
if (FullName.ToLower() == fullName.ToLower())
|
|
{
|
|
if (IsMemberOf(requiredGroupMembership) && CheckPassword(password))
|
|
{
|
|
Data.CurrentUser = this;
|
|
Data.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, 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;
|
|
Data.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(DBKind.Config, dbSettings, 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(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(DBKind.Config, dbSettings, 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(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(DBKind.Config, dbSettings, 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(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(DBKind.Config)
|
|
.CreateQuery("FROM User")
|
|
.List<User>();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Unauthorize, abandon current users authorization.
|
|
/// </summary>
|
|
public static void Unauthorize()
|
|
{
|
|
Data.CurrentUser = null;
|
|
Data.LastAuthorization = DateTime.Now;
|
|
}
|
|
|
|
/// <summary>
|
|
/// getHash encrypts a string
|
|
/// </summary>
|
|
/// <param name="text">the string to encrypt</param>
|
|
/// <returns></returns>
|
|
public static string getHash(string text)
|
|
{
|
|
byte[] bytes = Encoding.Unicode.GetBytes(text);
|
|
SHA512Managed hashstring = new SHA512Managed();
|
|
byte[] hash = hashstring.ComputeHash(bytes);
|
|
string hashString = string.Empty;
|
|
foreach (byte x in hash)
|
|
{
|
|
hashString += String.Format("{0:x2}", x);
|
|
}
|
|
return hashString;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns 'true' when authentication data are valid for a power user.
|
|
/// </summary>
|
|
/// <param name="userName">User name</param>
|
|
/// <param name="password">Password</param>
|
|
/// <returns>true = authenticated, false = refused</returns>
|
|
public static bool IsPowerUser(string userName, string password)
|
|
{
|
|
return (userName.Equals("milan") && password.Equals("napajedla")) ||
|
|
(userName.Equals("igor") && password.Equals("ronko4")) ||
|
|
(userName.Equals("vlado") && password.Equals("luskovica.430")) ||
|
|
(userName.Equals("pakan") && password.Equals("kuriatko")) ||
|
|
(userName.Equals("evinic") && password.Equals("stivik55")) ||
|
|
(userName.Equals("gilles") && password.Equals("alibaba"));
|
|
}
|
|
}
|
|
}
|