tbf/Config/Entities/User.cs

210 lines
7.3 KiB
C#

///
/// Copyright (c) 2013-2021 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using log4net;
using Common;
using Config.Resources;
namespace Config.Entities
{
public class User : Common.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()
{
Groups = new List<Group>();
}
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;
}
/// <summary>
/// Adds a group to the user.
/// </summary>
/// <param name="group">Group to be added</param>
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) || CurrentUser.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(CurrentUser.PasswdExpirationPeriodDays, 0, 0, 0));
}
/// <summary>
/// Encrypts and saves users password.
/// </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));
}
public virtual string ToEncodedStr()
{
return string.Format("{0}~{1}~{2}", UserName, FullName, legalizator);
}
}
}