/// /// 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 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(); } public User(string userName, int number, bool powerUser) { UserName = userName; Number = number; this.powerUser = powerUser; Groups = new List(); FullName = powerUser ? "Power User" : string.Empty; } /// /// Adds a group to the user. /// /// Group to be added public virtual void AddGroup(Group group) { Groups.Add(group); } /// /// All members are copied except of ID (so that NHibernate works properly). /// List of groups is empty. /// /// /// 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 ------------- /// /// Check whether user is a member of a group. /// /// Group GID /// true is user is a member of the specified group 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; } /// /// Check whether user is a member of any of specified groups. /// Returns true also when no groups are defined (groupIds = null). /// /// An array of group GID-s or null (no membership required) /// true if user is a member of any of specified groups 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; } /// /// Checks requirements on the password regardless of the password history /// /// Password /// true when the password is OK public static bool IsPasswordMeetsRequirements(string password, out string explanation) { return Common.Utils.IsPasswordMeetsRequirements(password, out explanation); } /// /// Returns true when password was already used in the past /// /// Password /// false when password is new, true when it was used in the past public virtual bool IsPasswordUsedInPast(string passwordCandidate) { string encryptedPW = EncryptedPassword(passwordCandidate); return (encryptedPW == Password) || (encryptedPW == Password2) || (encryptedPW == Password3); } /// /// Checks 'LastPwChange' and returns true when password expired /// /// 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)); } /// /// Encrypts and saves users password. /// /// Password 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); } /// /// Verifies users password. Used by static bool Authorisation(...) /// /// Password /// true if password is correct 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); } } }