public IUser CreateNewUser(string username, string password) { CheckRights(); if (String.IsNullOrEmpty(username)) { AppliactionContext.Log.Warning(this, "Username is empty."); throw new UserManagementException(Resources.UsernameCantBeEmpty); } if (IsMaster(username) || IsGuest(username)) { AppliactionContext.Log.Warning(this, String.Format("User with username '{0}' already exist.", username)); throw new UserManagementException(String.Format(Resources.UserAlreadyExists, username)); } if (String.IsNullOrEmpty(password)) { AppliactionContext.Log.Warning(this, String.Format("Password for user '{0}' is empty.", username)); throw new UserManagementException(Resources.PasswordCantBeEmpty); } using (ISession session = AppliactionContext.SessionFactory.OpenSession()) { IQuery query = session.CreateQuery("from " + typeof (DbUser) + " u where u.Username = :Username"); IList<DbUser> users = query.SetParameter("Username", username).List<DbUser>(); if (users.Count != 0) { AppliactionContext.Log.Warning(this, String.Format("User with username '{0}' already exist.", username)); throw new UserManagementException(String.Format(Resources.UserAlreadyExists, username)); } } IUser newUser = new User(username, password); AppliactionContext.Log.Debug(this, String.Format("User with username '{0}' was created.", username)); newUser.Save(); _users.Add(newUser); return newUser; }
private void ReadAllUsers() { using (AppliactionContext.Log.LogTime(this, "Reading all users")) { using (ISession session = AppliactionContext.SessionFactory.OpenSession()) { IList<DbUser> dbUsers = session.CreateCriteria<DbUser>().List<DbUser>(); foreach (DbUser dbUser in dbUsers) { IUser newUser = new User(dbUser); _users.Add(newUser); AppliactionContext.Log.Debug(this, String.Format("User '{0}' read from DB.", newUser.Username)); } } } }