using System; using System.Data; using System.Web; using System.Web.UI; using CMS.CMSHelper; using CMS.EmailEngine; using CMS.EventLog; using CMS.GlobalHelper; using CMS.SettingsProvider; using CMS.SiteProvider; using CMS.DocumentEngine; using CMS.UIControls; using CMS.WebAnalytics; public partial class CMSModules_Membership_Controls_RegistrationApproval : CMSUserControl { #region "Variables" private string mFromAddress; private string mSuccessfulApprovalText; private string mUnsuccessfulApprovalText; private string mUserDeletedText; private string mAdministratorEmail; private string mWaitingForApprovalText; private bool mNotifyAdministrator = false; #endregion #region "Public properties" /// /// Gets or sets the value that indicates whether administrator should be informed about new user. /// public bool NotifyAdministrator { get { return mNotifyAdministrator; } set { mNotifyAdministrator = value; } } /// /// Gets or sets the administrator e-mail address. /// public string AdministratorEmail { get { return mAdministratorEmail; } set { mAdministratorEmail = value; } } /// /// Gets or sets waiting for approval text. /// public string WaitingForApprovalText { get { if (String.IsNullOrEmpty(mWaitingForApprovalText)) { return SuccessfulApprovalText; } return mWaitingForApprovalText; } set { mWaitingForApprovalText = value; } } /// /// Gets or sets email address of sender. /// public string FromAddress { get { return mFromAddress; } set { mFromAddress = value; } } /// /// Gets or sets Successful Approval Text. /// public string SuccessfulApprovalText { get { return mSuccessfulApprovalText; } set { mSuccessfulApprovalText = value; } } /// /// Gets or sets Unsuccessful Approval text. /// public string UnsuccessfulApprovalText { get { if (!String.IsNullOrEmpty(mUnsuccessfulApprovalText)) { return mUnsuccessfulApprovalText; } else { return GetString("mem.reg.UnsuccessfulApprovalText"); } } set { mUnsuccessfulApprovalText = value; } } /// /// Gets or sets ui deleted text. /// public string UserDeletedText { get { if (!String.IsNullOrEmpty(mUserDeletedText)) { return mUserDeletedText; } else { return GetString("mem.reg.UserDeletedText"); } } set { mUserDeletedText = value; } } #endregion /// /// Page Load. /// protected void Page_Load(object sender, EventArgs e) { // If StopProcessing flag is set, do nothing if (StopProcessing) { Visible = false; return; } // Validate hash if (!QueryHelper.ValidateHash("hash", "aliaspath", false)) { URLHelper.Redirect(ResolveUrl("~/CMSMessages/Error.aspx?title=" + ResHelper.GetString("dialogs.badhashtitle") + "&text=" + ResHelper.GetString("dialogs.badhashtext"))); } Guid userGuid = QueryHelper.GetGuid("userguid", Guid.Empty); if (userGuid != Guid.Empty) { #region "Request validity" UserInfo ui = UserInfoProvider.GetUserInfoByGUID(userGuid); // ui was not found, probably late activation try if (ui == null) { lblInfo.Text = UserDeletedText; return; } // ui has been already activated if (ui.UserSettings.UserWaitingForApproval || ui.UserEnabled) { lblInfo.Text = UnsuccessfulApprovalText; return; } #endregion string siteName = null; bool administrationApproval = SettingsKeyProvider.GetBoolValue(CMSContext.CurrentSiteName + ".CMSRegistrationAdministratorApproval"); lblInfo.Text = SuccessfulApprovalText; // Admin approve is not required, enable ui if (!administrationApproval) { lblInfo.Text = (!String.IsNullOrEmpty(SuccessfulApprovalText)) ? SuccessfulApprovalText : GetString("mem.reg.SuccessfulApprovalText"); // Enable ui ui.UserSettings.UserActivationDate = DateTime.Now; ui.Enabled = true; // ui is confirmed and enabled, could be logged into statistics siteName = CMSContext.CurrentSiteName; AnalyticsHelper.LogRegisteredUser(siteName, ui); } // ui must wait for admin approval else { lblInfo.Text = (!String.IsNullOrEmpty(WaitingForApprovalText)) ? WaitingForApprovalText : ResHelper.GetString("mem.reg.SuccessfulApprovalWaitingForAdministratorApproval"); // If user is already waiting for approval, dont send email again, just show info label if (ui.UserSettings.UserWaitingForApproval) { return; } // Mark for admin approval ui.UserSettings.UserWaitingForApproval = true; } // Save changes UserInfoProvider.SetUserInfo(ui); Activity activity = new ActivityRegistration(ui, CMSContext.CurrentDocument, CMSContext.ActivityEnvironmentVariables); if (activity.Data != null) { int contactId = QueryHelper.GetInteger("contactid", 0); if (contactId <= 0) { contactId = ModuleCommands.OnlineMarketingGetUserLoginContactID(ui); } activity.Data.ContactID = contactId; activity.CheckViewMode = false; activity.Log(); } #region "Administrator notification email" // Check if admin should be notified bool notifyAdministrator = NotifyAdministrator || QueryHelper.GetBoolean("notifyadmin", false); // Notify administrator if enabled and email confirmation is not required if ((!String.IsNullOrEmpty(AdministratorEmail)) && (administrationApproval || notifyAdministrator)) { EmailTemplateInfo template = null; if (administrationApproval) { template = EmailTemplateProvider.GetEmailTemplate("Registration.Approve", CMSContext.CurrentSiteName); } else { template = EmailTemplateProvider.GetEmailTemplate("Registration.New", CMSContext.CurrentSiteName); } EventLogProvider ev = new EventLogProvider(); if (template == null) { ev.LogEvent("E", DateTime.Now, "RegistrationForm", "GetEmailTemplate", HTTPHelper.GetAbsoluteUri()); } // E-mail template ok else { string from = EmailHelper.GetSender(template, (!String.IsNullOrEmpty(FromAddress)) ? FromAddress : SettingsKeyProvider.GetStringValue(CMSContext.CurrentSiteName + ".CMSNoreplyEmailAddress")); if (!String.IsNullOrEmpty(from)) { // Prepare macro replacements string[,] replacements = new string[4, 2]; replacements[0, 0] = "firstname"; replacements[0, 1] = ui.FirstName; replacements[1, 0] = "lastname"; replacements[1, 1] = ui.LastName; replacements[2, 0] = "email"; replacements[2, 1] = ui.Email; replacements[3, 0] = "username"; replacements[3, 1] = ui.UserName; // Set resolver ContextResolver resolver = CMSContext.CurrentResolver; resolver.SourceParameters = replacements; resolver.EncodeResolvedValues = true; // Add user info data resolver.SourceData = new object[1] { ui }; // E-mail message EmailMessage email = new EmailMessage(); email.EmailFormat = EmailFormatEnum.Default; email.Recipients = AdministratorEmail; // Get e-mail sender and subject from template, if used email.From = from; email.Body = resolver.ResolveMacros(template.TemplateText); resolver.EncodeResolvedValues = false; email.PlainTextBody = resolver.ResolveMacros(template.TemplatePlainText); string emailSubject = EmailHelper.GetSubject(template, GetString("RegistrationForm.EmailSubject")); email.Subject = resolver.ResolveMacros(emailSubject); email.CcRecipients = template.TemplateCc; email.BccRecipients = template.TemplateBcc; try { MetaFileInfoProvider.ResolveMetaFileImages(email, template.TemplateID, EmailObjectType.EMAILTEMPLATE, MetaFileInfoProvider.OBJECT_CATEGORY_TEMPLATE); // Send the e-mail immediately EmailSender.SendEmail(CMSContext.CurrentSiteName, email, true); } catch { ev.LogEvent("E", DateTime.Now, "Membership", "RegistrationApprovalEmail", CMSContext.CurrentSite.SiteID); } } else { EventLogProvider eventLog = new EventLogProvider(); eventLog.LogEvent(EventLogProvider.EVENT_TYPE_ERROR, DateTime.Now, "RegistrationApproval", "EmailSenderNotSpecified"); } } } #endregion } else { Visible = false; } } }