using System; using System.Collections.Generic; using System.Security.Cryptography; using System.Text; using System.Diagnostics; namespace TBF.Entities { public class User { public virtual int Id { get; protected set; } public virtual string Name { get; set; } public virtual string Password { get; set; } public virtual string Description { get; set; } public virtual DateTime LastPwChange { get; set; } public virtual IList Groups { get; set; } //User can be a member of a list of groups public User() { Groups = new List(); } public virtual void AddGroup(Group group) { Groups.Add(group); } /// ------------- 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; } } /// /// 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(Grp.GID groupId) { if (groupId == Grp.GID.None) { 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; } } /// /// Sets users password. It then will be encrypted. /// /// Password public virtual void SetPassword(string password) { this.Password = EncryptedPassword(password); LastPwChange = DateTime.Now; } public virtual string EncryptedPassword(string password) { return getHash(password); } /// /// Verifies users password. Used by static bool Authorisation(...) /// /// Password /// true if password is correct public virtual bool CheckPassword(string password) { return (Password == EncryptedPassword(password)); } /// /// 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(). /// /// /// /// /// true = authorized public virtual bool Authorize(string userName, string password, TBF.Grp.GID requiredGroupMembership) { if (Name.ToLower() == userName.ToLower()) { if (IsMemberOf(requiredGroupMembership) && CheckPassword(password)) { currentUser = this; lastAuthorization = DateTime.Now; return true; } else { return false; } } return false; } /// /// 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(). /// /// /// /// /// true = authorized public virtual bool Authorize(string userName, string password) { return Authorize(userName, password, Grp.GID.None); } /// /// Returns a 'User' with a given username from a database. /// /// User name for the query /// reference to a 'User' (if it exists) or null public static User AuthorizeDummyUser(string username) { User user = new User(); user.Name = username; currentUser = user; return user; } /// /// Returns a 'User' with a given username from an ARBITRARY database. /// /// User name for the query /// reference to a 'User' (if it exists) or null public static User LoadUserByName(string username, DBSettings dbSettings) { IList listOfUsers = FluentCommon.CreateSessionFactory(Database.Users, dbSettings, false) .OpenSession() .CreateQuery("FROM User WHERE LOWER(Name) = :username") .SetParameter("username", username.ToLower()) .List(); if (listOfUsers.Count > 0) return listOfUsers[0]; return null; } /// /// Returns a 'User' with a given username from the users database. /// /// User name for the query /// reference to a 'User' (if it exists) or null public static User LoadUserByName(string username) { IList listOfUsers = FluentCommon.CreateSession(Database.Users) .CreateQuery("FROM User WHERE LOWER(Name) = :username") .SetParameter("username", username.ToLower()) .List(); if (listOfUsers.Count > 0) return listOfUsers[0]; return null; } /// /// returns an IList of all Users /// public static IList GetAllUsers() { return FluentCommon.CreateSession(Database.Users) .CreateQuery("FROM User") .List(); } /// /// Unauthorize, abandon current users authorization. /// public static void Unauthorize() { currentUser = null; lastAuthorization = DateTime.Now; } /// /// getHash encrypts a string /// /// the string to encrypt /// 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; } } }