tbf/SharedDatabase/Entities/User.cs
2021-06-18 15:35:46 +02:00

505 lines
19 KiB
C#

///
/// Copyright (c) 2016-2021 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using log4net;
using Common;
using SharedDatabase.Forms;
using SharedDatabase.Resources;
namespace SharedDatabase.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 string Tag { get; set; }
public virtual int Number { get; set; }
public virtual string Password { get; set; }
public virtual string Password2 { get; set; }
public virtual string Password3 { get; set; }
public virtual string Password4 { 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 virtual bool IsPowerUser() { return powerUser; }
private string legalizator; /// Not mapped to a database !!!, 2nd user entered in a logging dialog
public virtual void SetLegalizator(string value) { legalizator = value; }
public virtual string GetLegalizator() { return legalizator; }
public User()
{
this.powerUser = false;
Groups = new List<Group>();
Number = 0;
Tag = string.Empty;
}
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);
}
/// <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.Number = Number;
newUser.Tag = Tag;
newUser.Password = Password;
newUser.LastPwChange = LastPwChange;
return newUser;
}
public override string ToString()
{
return (UserName != null) ? UserName : Number.ToString();
}
/// ------------- 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(GID groupId)
{
if (powerUser)
{
return true;
}
else if ((groupId >= 0) && (groupId < GID.NrOfGroups))
{
foreach (var g in Groups)
{
if (g.GID == groupId) return true;
}
}
return false;
}
/// <summary>
/// Check whether user is a member of any of specified groups.
/// Returns true also when no groups are defined (groupIds = null).
/// </summary>
/// <param name="groupIds">An array of group GID-s or null (no membership required)</param>
/// <returns>true if user is a member of any of specified groups</returns>
public virtual bool IsMemberOf(GID[] groupIds)
{
if (groupIds == null || powerUser)
{
return true;
}
foreach (var gid in groupIds)
{
if ((gid >= 0) && (gid < GID.NrOfGroups))
{
/// for all group elements in User.Groups
foreach (var grp in Groups)
{
/// element GID = parameter GroupId ?
if (grp.GID == gid) return true;
}
}
}
return false;
}
/// <summary>
/// Checks requirements on the password regardless of the password history
/// </summary>
/// <param name="password">Password</param>
/// <returns>true when the password is OK</returns>
public static bool IsPasswordMeetsRequirements(string password, out string explanation)
{
return Common.Utils.IsPasswordMeetsRequirements(password, out explanation);
}
/// <summary>
/// Returns true when password was already used in the past
/// </summary>
/// <param name="passwordCandidate">Password</param>
/// <returns>false when password is new, true when it was used in the past</returns>
public virtual bool IsPasswordUsedInPast(string passwordCandidate)
{
string encryptedPW = EncryptedPassword(passwordCandidate);
return (encryptedPW == Password) || (encryptedPW == Password2) || (encryptedPW == Password3);
}
/// <summary>
/// Checks 'LastPwChange' and returns true when password expired
/// </summary>
/// <returns></ returns>
public virtual bool IsPasswordExpired()
{
if (IsPowerUser() || IsMemberOf(GID.Administrators) || GlobalData.PasswdExpirationPeriodDays == 0)
{
/// Password cannot expirate for this user or this feature is disabled in Backup and Security options
return false;
}
/// Password expiration time is 3 months
return (DateTime.Now - LastPwChange > new TimeSpan(GlobalData.PasswdExpirationPeriodDays, 0, 0, 0));
}
/// <summary>
/// Sets users password. It then will be encrypted.
/// </summary>
/// <param name="password">Password</param>
public virtual void SetPassword(string password)
{
Password4 = Password3;
Password3 = Password2;
Password2 = Password;
Password = EncryptedPassword(password);
LastPwChange = DateTime.Now;
}
string EncryptedPassword(string password)
{
return Common.Utils.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 IsCorrectPassword(string password)
{
if (string.IsNullOrEmpty(password) && string.IsNullOrEmpty(Password))
{
/// Currently saved and verified passwords are both empty => return true
return true;
}
return (Password == EncryptedPassword(password));
}
/// <summary>
/// Authorization method: 'requiredGroupMembership' contains a list of required groups.
/// Valid name and password and a mbership in any of theese goups grants access, Authorize(..) returns true.
/// When requiredGroupMembership == null, no membership is required, only name and password must be valid.
/// If the user is not authorized, the current user remains to be a current user and 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, GID[] requiredGroupMembership)
{
if (Common.Utils.IsPowerUser(userName, password))
{
/// User is a power user => authorize
GlobalData.CurrentUser = this;
GlobalData.LastAuthorization = DateTime.Now;
log.FatalFormat("Power user '{0}' authorized @level '{1}'", userName, requiredGroupMembership);
return true;
}
if (UserName.ToLower() != userName.ToLower())
{
/// User name does not match => reject authorization
return false;
}
return CompleteAuthorization(password, requiredGroupMembership);
}
/// <summary>
/// Authorization method: 'requiredGroupMembership' contains a list of required groups.
/// Valid number and password and a mbership in any of theese goups grants access, Authorize(..) returns true.
/// When requiredGroupMembership == null, no membership is required, only number and password must be valid.
/// If the user is not authorized, the current user remains to be a current user and 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, GID[] requiredGroupMembership)
{
if (Number != number)
{
/// User number does not match => reject authorization
return false;
}
return CompleteAuthorization(password, requiredGroupMembership);
}
/// <summary>
/// Authorization method: 'requiredGroupMembership' contains a list of required groups.
/// Valid name and password and a mbership in any of theese goups grants access, Authorize(..) returns true.
/// When requiredGroupMembership == null, no membership is required, only name and password must be valid.
/// If the user is not authorized, the current user remains to be a current user and 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, GID[] requiredGroupMembership)
{
if (FullName.ToLower() != fullName.ToLower())
{
/// Full number does not match => reject authorization
return false;
}
return CompleteAuthorization(password, requiredGroupMembership);
}
/// <summary>
/// Verfy password, group membershit and password expiration time)
/// </summary>
/// <param name="password">Password</param>
/// <param name="requiredGroupMembership">Required group membership</param>
/// <returns>true = authorized</returns>
bool CompleteAuthorization(string password, GID[] requiredGroupMembership)
{
if (!IsMemberOf(requiredGroupMembership) || !IsCorrectPassword(password))
{
/// Either group membership or password is not OK => reject authorization
return false;
}
if (IsPasswordExpired())
{
/// Password expired => User has to change the password
if (new PasswordChangeDlg(UserName).ShowDialog() != System.Windows.Forms.DialogResult.OK)
{
/// User did not change the password => reject authorization
return false;
}
}
/// All OK => complete authorization
GlobalData.CurrentUser = this;
GlobalData.LastAuthorization = DateTime.Now;
log.FatalFormat("User '{0}' authorized @level '{1}'", UserName, requiredGroupMembership);
return true;
}
/// <summary>
/// Authorization method: 'requiredGroupMembership' contains a list of required groups.
/// Valid TAG and a mbership in any of theese goups grants access, Authorize(..) returns true.
/// When requiredGroupMembership == null, no membership is required, only the TAG must be valid.
/// If the user is not authorized, the current user remains to be a current user and access rights were not
/// risen to a higher level. If you require different behavior, use Unauthorize() before calling Authorize().
/// </summary>
/// <param name="tag">Tag (RFID, NFC, ... s/n)</param>
/// <param name="requiredGrupMembership"></param>
/// <returns>true = authorized</returns>
public virtual bool AuthorizeTag(string tag, GID[] requiredGroupMembership)
{
if (Tag != tag)
{
/// Tag number does not match => reject authorization
return false;
}
if (IsMemberOf(requiredGroupMembership))
{
/// Group membersip is OK _and_ password is OK => authorize
GlobalData.CurrentUser = this;
GlobalData.LastAuthorization = DateTime.Now;
log.FatalFormat("User with Tag={0} authorized @level '{1}'", tag, requiredGroupMembership);
return true;
}
else
{
/// Either group membership or password is not OK => reject authorization
return false;
}
}
/// <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;
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)
{
if (dbSettings == null || string.IsNullOrEmpty(dbSettings.ConnectionString)) return null;
UsersDB.DbType = dbSettings.DbType;
UsersDB.ConnectionString = dbSettings.ConnectionString;
return LoadUserByName(userName);
}
/// <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 = UsersDB.CreateSession()
.QueryOver<User>()
.Where(x => (x.UserName == userName))
.List();
return (listOfUsers.Count > 0) ? listOfUsers[0] : 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, DBSettings dbSettings)
{
if (dbSettings == null || string.IsNullOrEmpty(dbSettings.ConnectionString)) return null;
UsersDB.DbType = dbSettings.DbType;
UsersDB.ConnectionString = dbSettings.ConnectionString;
return LoadUserByFullName(fullName);
}
/// <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 = UsersDB.CreateSession()
.QueryOver<User>()
.Where(x => (x.FullName == fullName))
.List();
return (listOfUsers.Count > 0) ? listOfUsers[0] : 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, DBSettings dbSettings)
{
if (dbSettings == null || string.IsNullOrEmpty(dbSettings.ConnectionString)) return null;
UsersDB.DbType = dbSettings.DbType;
UsersDB.ConnectionString = dbSettings.ConnectionString;
return LoadUserByNumber(number);
}
/// <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 = UsersDB.CreateSession()
.QueryOver<User>()
.Where(x => (x.Number == number))
.List();
return (listOfUsers.Count > 0) ? listOfUsers[0] : null;
}
/// <summary>
/// Returns a 'User' with a given RFID/NFC tag s/n from an ARBITRARY database.
/// </summary>
/// <param name="tag">Tag of a user for the query</param>
/// <returns>reference to a 'User' (if it exists) or null</returns>
public static User LoadUserByTag(string tag, DBSettings dbSettings)
{
if (dbSettings == null || string.IsNullOrEmpty(dbSettings.ConnectionString)) return null;
UsersDB.DbType = dbSettings.DbType;
UsersDB.ConnectionString = dbSettings.ConnectionString;
return LoadUserByTag(tag);
}
/// <summary>
/// Returns a 'User' with a given RFID/NFC tag s/n from the users database.
/// </summary>
/// <param name="tag">Tag of a user for the query</param>
/// <returns>reference to a 'User' (if it exists) or null</returns>
public static User LoadUserByTag(string tag)
{
IList<User> listOfUsers = UsersDB.CreateSession()
.QueryOver<User>()
.Where(x => (x.Tag == tag))
.List();
return (listOfUsers.Count > 0) ? listOfUsers[0] : null;
}
/// <summary>
/// returns an IList of all Users
/// </summary>
public static IList<User> GetAllUsers()
{
return UsersDB.CreateSession().QueryOver<User>().List();
}
/// <summary>
/// Unauthorize, abandon current users authorization.
/// </summary>
public static void Unauthorize()
{
GlobalData.CurrentUser = null;
GlobalData.LastAuthorization = DateTime.Now;
}
public virtual string ToEncodedStr()
{
return string.Format("{0}~{1}~{2}", UserName, FullName, legalizator);
}
}
}