/// <summary>
        /// Activates the user with given activation key
        /// </summary>
        /// <param name="activationKey">The randomly generated activation key for the user.</param>
        /// <returns>True if the activation was successful</returns>
        public override bool ActivateUser(string activationKey)
        {
            try
            {
                User user = Repository.GetUserByActivationKey(activationKey);
                if (user != null)
                {
                    user.IsActivated = true;
                    Repository.SaveOrUpdateUser(user);

                    #region MailChimp
                    var SiteSettings = StructureMap.ObjectFactory.GetInstance <SiteSettings>();

                    if (SiteSettings.EnableMailChimp)
                    {
                        var mc = new MailChimp.MailChimpManager(SiteSettings.MailChimpApiKey);

                        if (user.EmailSubscriber)
                        {
                            // Subscribe user
                            var MergeVars = new MailChimp.Lists.MergeVar();
                            MergeVars.Add(new KeyValuePair <string, object>("FNAME", user.Firstname));
                            MergeVars.Add(new KeyValuePair <string, object>("LNAME", user.Lastname));

                            mc.Subscribe(SiteSettings.MailChimpListId, new MailChimp.Helper.EmailParameter {
                                Email = user.Email
                            }, MergeVars, doubleOptIn: false, updateExisting: true);
                        }
                    }
                    #endregion

                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (DatabaseException ex)
            {
                throw new SecurityException(ex, "An error occurred while activating the user with key {0}", activationKey);
            }
        }
		/// <summary>
		/// Activates the user with given activation key
		/// </summary>
		/// <param name="activationKey">The randomly generated activation key for the user.</param>
		/// <returns>True if the activation was successful</returns>
		public override bool ActivateUser(string activationKey)
		{
			try
			{
				User user = Repository.GetUserByActivationKey(activationKey);
				if (user != null)
				{
					user.IsActivated = true;
					Repository.SaveOrUpdateUser(user);

                    #region MailChimp
                    var SiteSettings = StructureMap.ObjectFactory.GetInstance<SiteSettings>();

                    if (SiteSettings.EnableMailChimp)
                    {
                        var mc = new MailChimp.MailChimpManager(SiteSettings.MailChimpApiKey);

                        if (user.EmailSubscriber)
                        {
                            // Subscribe user
                            var MergeVars = new MailChimp.Lists.MergeVar();
                            MergeVars.Add(new KeyValuePair<string, object>("FNAME", user.Firstname));
                            MergeVars.Add(new KeyValuePair<string, object>("LNAME", user.Lastname));

                            mc.Subscribe(SiteSettings.MailChimpListId, new MailChimp.Helper.EmailParameter { Email = user.Email }, MergeVars, doubleOptIn: false, updateExisting: true);
                        }
                    }
                    #endregion

                    return true;
				}
				else
				{
					return false;
				}
			}
			catch (DatabaseException ex)
			{
				throw new SecurityException(ex, "An error occurred while activating the user with key {0}", activationKey);
			}
		}
        /// <summary>
        /// Changes the username of a user to a new username.
        /// </summary>
        /// <param name="model">The user details to change. The password property is ignored for this object - use ChangePassword instead.</param>
        /// <returns>
        /// true if the change was successful;false if the new username already exists in the system.
        /// </returns>
        /// <exception cref="SecurityException">An databaseerror occurred while changing the email/username.</exception>
        public override bool UpdateUser(UserViewModel model)
        {
            try
            {
                User user;

                // These checks are run in the UserViewModel object by MVC - but doubled up in here for _when_ the API is used without MVC.
                if (model.ExistingEmail != model.NewEmail)
                {
                    user = Repository.GetUserByEmail(model.NewEmail);
                    if (user != null)
                    {
                        throw new SecurityException(null, "The email provided already exists.");
                    }
                }

                if (model.ExistingUsername != model.NewUsername)
                {
                    user = Repository.GetUserByUsername(model.NewUsername);
                    if (user != null)
                    {
                        throw new SecurityException(null, "The username provided already exists.");
                    }
                }

                user = Repository.GetUserById(model.Id.Value);
                if (user == null)
                {
                    throw new SecurityException(null, "The user does not exist.");
                }

                // Update the profile details
                user.Firstname = model.Firstname;
                user.Lastname  = model.Lastname;
                user.orgID     = model.orgID;

                #region MailChimp
                if (user.EmailSubscriber != model.EmailSubscriber)
                {
                    var SiteSettings = StructureMap.ObjectFactory.GetInstance <SiteSettings>();

                    if (SiteSettings.EnableMailChimp)
                    {
                        var mc = new MailChimp.MailChimpManager(SiteSettings.MailChimpApiKey);

                        if (model.EmailSubscriber)
                        {
                            // Subscribe user
                            var MergeVars = new MailChimp.Lists.MergeVar();
                            MergeVars.Add(new KeyValuePair <string, object>("FNAME", user.Firstname));
                            MergeVars.Add(new KeyValuePair <string, object>("LNAME", user.Lastname));

                            mc.Subscribe(SiteSettings.MailChimpListId, new MailChimp.Helper.EmailParameter {
                                Email = user.Email
                            }, MergeVars, doubleOptIn: false, updateExisting: true);
                        }
                        else
                        {
                            // Unsubscribe user
                            mc.Unsubscribe(SiteSettings.MailChimpListId, new MailChimp.Helper.EmailParameter {
                                Email = user.Email
                            }, sendGoodbye: false, sendNotify: false);
                        }
                    }

                    user.EmailSubscriber = model.EmailSubscriber;
                }
                #endregion

                Repository.SaveOrUpdateUser(user);

                // Save the email
                if (model.ExistingEmail != model.NewEmail)
                {
                    user = Repository.GetUserByEmail(model.ExistingEmail);
                    if (user != null)
                    {
                        user.Email = model.NewEmail;
                        Repository.SaveOrUpdateUser(user);

                        #region MailChimp
                        var SiteSettings = StructureMap.ObjectFactory.GetInstance <SiteSettings>();
                        if (user.EmailSubscriber && SiteSettings.EnableMailChimp)
                        {
                            var mc = new MailChimp.MailChimpManager(SiteSettings.MailChimpApiKey);

                            // Update user
                            var MergeVars = new MailChimp.Lists.MergeVar();
                            MergeVars.Add(new KeyValuePair <string, object>("new-email", model.NewEmail));

                            mc.UpdateMember(SiteSettings.MailChimpListId, new MailChimp.Helper.EmailParameter {
                                Email = user.Email
                            }, MergeVars);
                        }
                        #endregion
                    }
                    else
                    {
                        return(false);
                    }
                }

                // Save the username
                if (model.ExistingUsername != model.NewUsername)
                {
                    user = Repository.GetUserByUsername(model.ExistingUsername);
                    if (user != null)
                    {
                        user.Username = model.NewUsername;
                        Repository.SaveOrUpdateUser(user);

                        //
                        // Update the PageContent.EditedBy history
                        //
                        IList <PageContent> pageContents = Repository.FindPageContentsEditedBy(model.ExistingUsername).ToList();
                        for (int i = 0; i < pageContents.Count; i++)
                        {
                            pageContents[i].EditedBy = model.NewUsername;
                            Repository.UpdatePageContent(pageContents[i]);
                        }

                        //
                        // Update all Page.CreatedBy and Page.ModifiedBy
                        //
                        IList <Page> pages = Repository.FindPagesCreatedBy(model.ExistingUsername).ToList();
                        for (int i = 0; i < pages.Count; i++)
                        {
                            pages[i].CreatedBy = model.NewUsername;
                            Repository.SaveOrUpdatePage(pages[i]);
                        }

                        pages = Repository.FindPagesModifiedBy(model.ExistingUsername).ToList();
                        for (int i = 0; i < pages.Count; i++)
                        {
                            pages[i].ModifiedBy = model.NewUsername;
                            Repository.SaveOrUpdatePage(pages[i]);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }

                return(true);
            }
            catch (DatabaseException ex)
            {
                throw new SecurityException(ex, "An error occurred updating the user {0} ", model.ExistingEmail);
            }
        }
		/// <summary>
		/// Changes the username of a user to a new username.
		/// </summary>
		/// <param name="model">The user details to change. The password property is ignored for this object - use ChangePassword instead.</param>
		/// <returns>
		/// true if the change was successful;false if the new username already exists in the system.
		/// </returns>
		/// <exception cref="SecurityException">An databaseerror occurred while changing the email/username.</exception>
		public override bool UpdateUser(UserViewModel model)
		{
			try
			{
				User user;

				// These checks are run in the UserViewModel object by MVC - but doubled up in here for _when_ the API is used without MVC.
				if (model.ExistingEmail != model.NewEmail)
				{
					user = Repository.GetUserByEmail(model.NewEmail);
					if (user != null)
						throw new SecurityException(null, "The email provided already exists.");
				}

				if (model.ExistingUsername != model.NewUsername)
				{
					user = Repository.GetUserByUsername(model.NewUsername);
					if (user != null)
						throw new SecurityException(null, "The username provided already exists.");
				}

				user = Repository.GetUserById(model.Id.Value);
				if (user == null)
					throw new SecurityException(null, "The user does not exist.");

				// Update the profile details
				user.Firstname = model.Firstname;
				user.Lastname = model.Lastname;
                user.orgID = model.orgID;

                #region MailChimp
                if (user.EmailSubscriber != model.EmailSubscriber)
                {
                    var SiteSettings = StructureMap.ObjectFactory.GetInstance<SiteSettings>();

                    if (SiteSettings.EnableMailChimp)
                    {
                        var mc = new MailChimp.MailChimpManager(SiteSettings.MailChimpApiKey);

                        if (model.EmailSubscriber)
                        {
                            // Subscribe user
                            var MergeVars = new MailChimp.Lists.MergeVar();
                            MergeVars.Add(new KeyValuePair<string, object>("FNAME", user.Firstname));
                            MergeVars.Add(new KeyValuePair<string, object>("LNAME", user.Lastname));

                            mc.Subscribe(SiteSettings.MailChimpListId, new MailChimp.Helper.EmailParameter { Email = user.Email }, MergeVars, doubleOptIn: false, updateExisting: true);
                        }
                        else
                        {
                            // Unsubscribe user
                            mc.Unsubscribe(SiteSettings.MailChimpListId, new MailChimp.Helper.EmailParameter { Email = user.Email }, sendGoodbye: false, sendNotify: false);
                        }
                    }

                    user.EmailSubscriber = model.EmailSubscriber;
                }
                #endregion

                Repository.SaveOrUpdateUser(user);

				// Save the email
				if (model.ExistingEmail != model.NewEmail)
				{
					user = Repository.GetUserByEmail(model.ExistingEmail);
					if (user != null)
					{
						user.Email = model.NewEmail;
						Repository.SaveOrUpdateUser(user);

                        #region MailChimp
                        var SiteSettings = StructureMap.ObjectFactory.GetInstance<SiteSettings>();
                        if (user.EmailSubscriber && SiteSettings.EnableMailChimp)
                        {
                            var mc = new MailChimp.MailChimpManager(SiteSettings.MailChimpApiKey);
                             
                            // Update user
                            var MergeVars = new MailChimp.Lists.MergeVar();
                            MergeVars.Add(new KeyValuePair<string, object>("new-email", model.NewEmail));

                            mc.UpdateMember(SiteSettings.MailChimpListId, new MailChimp.Helper.EmailParameter { Email = user.Email }, MergeVars);
                        }
                        #endregion
					}
					else
					{
						return false;
					}
				}

				// Save the username
				if (model.ExistingUsername != model.NewUsername)
				{
					user = Repository.GetUserByUsername(model.ExistingUsername);
					if (user != null)
					{
						user.Username = model.NewUsername;
						Repository.SaveOrUpdateUser(user);

						//
						// Update the PageContent.EditedBy history
						//
						IList<PageContent> pageContents = Repository.FindPageContentsEditedBy(model.ExistingUsername).ToList();
						for (int i = 0; i < pageContents.Count; i++)
						{
							pageContents[i].EditedBy = model.NewUsername;
							Repository.UpdatePageContent(pageContents[i]);
						}

						//
						// Update all Page.CreatedBy and Page.ModifiedBy
						//
						IList<Page> pages = Repository.FindPagesCreatedBy(model.ExistingUsername).ToList();
						for (int i = 0; i < pages.Count; i++)
						{
							pages[i].CreatedBy = model.NewUsername;
							Repository.SaveOrUpdatePage(pages[i]);
						}

						pages = Repository.FindPagesModifiedBy(model.ExistingUsername).ToList();
						for (int i = 0; i < pages.Count; i++)
						{
							pages[i].ModifiedBy = model.NewUsername;
							Repository.SaveOrUpdatePage(pages[i]);
						}
					}
					else
					{
						return false;
					}
				}

				return true;
			}
			catch (DatabaseException ex)
			{
				throw new SecurityException(ex, "An error occurred updating the user {0} ", model.ExistingEmail);
			}
		}