/// <summary>
        /// Saves a list of <see cref="IUser"/> objects
        /// </summary>
        /// <param name="entities"><see cref="IEnumerable{IUser}"/> to save</param>
        /// <param name="raiseEvents">Optional parameter to raise events.
        /// Default is <c>True</c> otherwise set to <c>False</c> to not raise events</param>
        public void Save(IEnumerable <IUser> entities, bool raiseEvents = true)
        {
            if (raiseEvents)
            {
                if (SavingUser.IsRaisedEventCancelled(new SaveEventArgs <IUser>(entities), this))
                {
                    return;
                }
            }

            var uow = UowProvider.GetUnitOfWork();

            using (var repository = RepositoryFactory.CreateUserRepository(uow))
            {
                foreach (var member in entities)
                {
                    repository.AddOrUpdate(member);
                }
                //commit the whole lot in one go
                uow.Commit();
            }

            if (raiseEvents)
            {
                SavedUser.RaiseEvent(new SaveEventArgs <IUser>(entities, false), this);
            }
        }
        /// <summary>
        /// Creates and persists a Member
        /// </summary>
        /// <remarks>Using this method will persist the Member object before its returned
        /// meaning that it will have an Id available (unlike the CreateMember method)</remarks>
        /// <param name="username">Username of the Member to create</param>
        /// <param name="email">Email of the Member to create</param>
        /// <param name="passwordValue">This value should be the encoded/encrypted/hashed value for the password that will be stored in the database</param>
        /// <param name="userType">MemberType the Member should be based on</param>
        /// <returns><see cref="IUser"/></returns>
        private IUser CreateUserWithIdentity(string username, string email, string passwordValue, IUserType userType)
        {
            if (userType == null)
            {
                throw new ArgumentNullException("userType");
            }

            //TODO: PUT lock here!!

            var uow = UowProvider.GetUnitOfWork();

            using (var repository = RepositoryFactory.CreateUserRepository(uow))
            {
                var loginExists = uow.Database.ExecuteScalar <int>("SELECT COUNT(id) FROM umbracoUser WHERE userLogin = @Login", new { Login = username }) != 0;
                if (loginExists)
                {
                    throw new ArgumentException("Login already exists");
                }

                var user = new User(userType)
                {
                    DefaultToLiveEditing = false,
                    Email            = email,
                    Language         = Configuration.GlobalSettings.DefaultUILanguage,
                    Name             = username,
                    RawPasswordValue = passwordValue,
                    Username         = username,
                    StartContentId   = -1,
                    StartMediaId     = -1,
                    IsLockedOut      = false,
                    IsApproved       = true
                };
                //adding default sections content and media
                user.AddAllowedSection("content");
                user.AddAllowedSection("media");

                if (SavingUser.IsRaisedEventCancelled(new SaveEventArgs <IUser>(user), this))
                {
                    return(user);
                }

                repository.AddOrUpdate(user);
                uow.Commit();

                SavedUser.RaiseEvent(new SaveEventArgs <IUser>(user, false), this);

                return(user);
            }
        }
        /// <summary>
        /// Saves an <see cref="IUser"/>
        /// </summary>
        /// <param name="entity"><see cref="IUser"/> to Save</param>
        /// <param name="raiseEvents">Optional parameter to raise events.
        /// Default is <c>True</c> otherwise set to <c>False</c> to not raise events</param>
        public void Save(IUser entity, bool raiseEvents = true)
        {
            if (raiseEvents)
            {
                if (SavingUser.IsRaisedEventCancelled(new SaveEventArgs <IUser>(entity), this))
                {
                    return;
                }
            }

            var uow = UowProvider.GetUnitOfWork();

            using (var repository = RepositoryFactory.CreateUserRepository(uow))
            {
                repository.AddOrUpdate(entity);
                try
                {
                    uow.Commit();
                }
                catch (DbException ex)
                {
                    //Special case, if we are upgrading and an exception occurs, just continue
                    if (IsUpgrading == false)
                    {
                        throw;
                    }

                    Logger.WarnWithException <UserService>("An error occurred attempting to save a user instance during upgrade, normally this warning can be ignored", ex);
                    return;
                }
            }

            if (raiseEvents)
            {
                SavedUser.RaiseEvent(new SaveEventArgs <IUser>(entity, false), this);
            }
        }