2010-02-18 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / CreateUserWizard.cs
index a222c4cf1af49d2dbb12e5f625cb4a562ccffb62..5e1f17f4088a4f6abae1875a45bdc1418ab5cfdd 100644 (file)
@@ -31,8 +31,10 @@ using System.Web;
 using System.Web.UI;
 using System.Web.Security;
 using System.Collections;
+using System.Collections.Specialized;
 using System.ComponentModel;
 using System.Text;
+using System.Net.Mail;
 
 namespace System.Web.UI.WebControls
 {
@@ -40,33 +42,34 @@ namespace System.Web.UI.WebControls
        public class CreateUserWizard : Wizard
        {
                public static readonly string ContinueButtonCommandName = "Continue";
-               private string _password = "";
-               private string _confirmPassword = "";
-               private MembershipProvider _provider = null;
-               private ITextControl _errorMessageLabel = null;
-
-               private Style _textBoxStyle = null;
-               private Style _validatorTextStyle = null;
-
-               private TableItemStyle _completeSuccessTextStyle = null;
-               private TableItemStyle _errorMessageStyle = null;
-               private TableItemStyle _hyperLinkStyle = null;
-               private TableItemStyle _instructionTextStyle = null;
-               private TableItemStyle _labelStyle = null;
-               private TableItemStyle _passwordHintStyle = null;
-               private TableItemStyle _titleTextStyle = null;
+               string _password = String.Empty;
+               string _confirmPassword = String.Empty;
+               MembershipProvider _provider = null;
+               ITextControl _errorMessageLabel = null;
+               MailDefinition _mailDefinition = null;
+
+               Style _textBoxStyle = null;
+               Style _validatorTextStyle = null;
+
+               TableItemStyle _completeSuccessTextStyle = null;
+               TableItemStyle _errorMessageStyle = null;
+               TableItemStyle _hyperLinkStyle = null;
+               TableItemStyle _instructionTextStyle = null;
+               TableItemStyle _labelStyle = null;
+               TableItemStyle _passwordHintStyle = null;
+               TableItemStyle _titleTextStyle = null;
                Style _createUserButtonStyle;
                Style _continueButtonStyle;
 
-               private static readonly object CreatedUserEvent = new object ();
-               private static readonly object CreateUserErrorEvent = new object ();
-               private static readonly object CreatingUserEvent = new object ();
-               private static readonly object ContinueButtonClickEvent = new object ();
-               private static readonly object SendingMailEvent = new object ();
-               private static readonly object SendMailErrorEvent = new object ();
+               static readonly object CreatedUserEvent = new object ();
+               static readonly object CreateUserErrorEvent = new object ();
+               static readonly object CreatingUserEvent = new object ();
+               static readonly object ContinueButtonClickEvent = new object ();
+               static readonly object SendingMailEvent = new object ();
+               static readonly object SendMailErrorEvent = new object ();
 
-               private CompleteWizardStep _completeWizardStep = null;
-               private CreateUserWizardStep _createUserWizardStep = null;
+               CompleteWizardStep _completeWizardStep = null;
+               CreateUserWizardStep _createUserWizardStep = null;
 
                public CreateUserWizard ()
                {
@@ -746,12 +749,17 @@ namespace System.Web.UI.WebControls
                        }
                }
 
-               //[MonoTODO ("Sending mail functionality is not implemented")]
-               //[ThemeableAttribute (false)]
-               //public MailDefinition MailDefinition
-               //{
-               //      get { throw new NotImplementedException (); }
-               //}
+               [ThemeableAttribute (false)]
+               public MailDefinition MailDefinition {
+                       get {
+                               if (this._mailDefinition == null) {
+                                       this._mailDefinition = new MailDefinition();
+                                       if (IsTrackingViewState)
+                                               ((IStateManager) _mailDefinition).TrackViewState ();
+                               }
+                               return _mailDefinition;
+                       }
+               }
 
                [ThemeableAttribute (false)]
                public virtual string MembershipProvider
@@ -1142,7 +1150,7 @@ namespace System.Web.UI.WebControls
                        }
                }
 
-               private void InstantiateCompleteWizardStep (CompleteWizardStep step)
+               void InstantiateCompleteWizardStep (CompleteWizardStep step)
                {
                        CompleteStepContainer contentTemplateContainer = new CompleteStepContainer (this);
                        if (step.ContentTemplate != null) {
@@ -1278,7 +1286,7 @@ namespace System.Web.UI.WebControls
                        return base.OnBubbleEvent (source, e);
                }
 
-               private void ProcessContinueEvent () {
+               void ProcessContinueEvent () {
                        OnContinueButtonClick (EventArgs.Empty);
 
                        if (ContinueDestinationPageUrl.Length > 0) {
@@ -1384,13 +1392,15 @@ namespace System.Web.UI.WebControls
                                ((IStateManager) CreateUserButtonStyle).LoadViewState (states [10]);
                        if (states [11] != null)
                                ((IStateManager) ContinueButtonStyle).LoadViewState (states [11]);
+                       if (states [12] != null)
+                               ((IStateManager) MailDefinition).LoadViewState (states [12]);
 
                        ((CreateUserStepContainer) CreateUserStep.ContentTemplateContainer).EnsureValidatorsState ();
                }
 
                protected override object SaveViewState ()
                {
-                       object [] state = new object [12];
+                       object [] state = new object [13];
                        state [0] = base.SaveViewState ();
 
                        if (_textBoxStyle != null)
@@ -1415,6 +1425,8 @@ namespace System.Web.UI.WebControls
                                state [10] = ((IStateManager) _createUserButtonStyle).SaveViewState ();
                        if (_continueButtonStyle != null)
                                state [11] = ((IStateManager) _continueButtonStyle).SaveViewState ();
+                       if (_mailDefinition != null)
+                               state [12] = ((IStateManager) _mailDefinition).SaveViewState ();
 
                        for (int n = 0; n < state.Length; n++)
                                if (state [n] != null)
@@ -1454,38 +1466,40 @@ namespace System.Web.UI.WebControls
                                ((IStateManager) _createUserButtonStyle).TrackViewState ();
                        if (_continueButtonStyle != null)
                                ((IStateManager) _continueButtonStyle).TrackViewState ();
+                       if (_mailDefinition != null)
+                               ((IStateManager) _mailDefinition).TrackViewState ();
                }
 
                #endregion
 
                #region Private event handlers
 
-               private void UserName_TextChanged (object sender, EventArgs e)
+               void UserName_TextChanged (object sender, EventArgs e)
                {
                        UserName = ((ITextControl) sender).Text;
                }
 
-               private void Password_TextChanged (object sender, EventArgs e)
+               void Password_TextChanged (object sender, EventArgs e)
                {
                        _password = ((ITextControl) sender).Text;
                }
 
-               private void ConfirmPassword_TextChanged (object sender, EventArgs e)
+               void ConfirmPassword_TextChanged (object sender, EventArgs e)
                {
                        _confirmPassword = ((ITextControl) sender).Text;
                }
 
-               private void Email_TextChanged (object sender, EventArgs e)
+               void Email_TextChanged (object sender, EventArgs e)
                {
                        Email = ((ITextControl) sender).Text;
                }
 
-               private void Question_TextChanged (object sender, EventArgs e)
+               void Question_TextChanged (object sender, EventArgs e)
                {
                        Question = ((ITextControl) sender).Text;
                }
 
-               private void Answer_TextChanged (object sender, EventArgs e)
+               void Answer_TextChanged (object sender, EventArgs e)
                {
                        Answer = ((ITextControl) sender).Text;
                }
@@ -1494,7 +1508,7 @@ namespace System.Web.UI.WebControls
 
                #region Private Methods
 
-               private void InitMemberShipProvider ()
+               void InitMemberShipProvider ()
                {
                        string mp = MembershipProvider;
                        _provider = (mp.Length == 0) ? _provider = Membership.Provider : Membership.Providers [mp];
@@ -1502,7 +1516,7 @@ namespace System.Web.UI.WebControls
                                throw new HttpException (Locale.GetText ("No provider named '{0}' could be found.", mp));
                }
 
-               private bool CreateUser ()
+               bool CreateUser ()
                {
                        if (!Page.IsValid)
                                return false;
@@ -1518,6 +1532,7 @@ namespace System.Web.UI.WebControls
 
                        if ((newUser != null) && (status == MembershipCreateStatus.Success)) {
                                OnCreatedUser (new EventArgs ());
+                               SendPasswordByMail (newUser, Password);
                                return true;
                        }
 
@@ -1561,20 +1576,58 @@ namespace System.Web.UI.WebControls
                        return false;
                }
 
-               private void Login ()
+               void SendPasswordByMail (MembershipUser user, string password)
+               {
+                       if (user == null)
+                               return;
+                       
+                       if (_mailDefinition == null)
+                               return;
+                       
+                       string messageText = "A new account has been created for you. Please go to the site and log in using the following information.\nUser Name: <%USERNAME%>\nPassword: <%PASSWORD%>";
+
+                       ListDictionary dictionary = new ListDictionary ();
+                       dictionary.Add ("<%USERNAME%>", user.UserName);
+                       dictionary.Add ("<%PASSWORD%>", password);
+
+                       MailMessage message = null;
+                       
+                       if (MailDefinition.BodyFileName.Length == 0)
+                               message = MailDefinition.CreateMailMessage (user.Email, dictionary, messageText, this);
+                       else
+                               message = MailDefinition.CreateMailMessage (user.Email, dictionary, this);
+
+                       if (string.IsNullOrEmpty (message.Subject))
+                               message.Subject = "Account information";
+
+                       MailMessageEventArgs args = new MailMessageEventArgs (message);
+                       OnSendingMail (args);
+
+                       SmtpClient smtpClient = new SmtpClient ();
+                       try {
+                               smtpClient.Send (message);
+                       } catch (Exception e) {
+                               SendMailErrorEventArgs mailArgs = new SendMailErrorEventArgs (e);
+                               OnSendMailError (mailArgs);
+                               if (!mailArgs.Handled)
+                                       throw e;
+                       }
+               }
+
+               void Login ()
                {
                        bool userValidated = MembershipProviderInternal.ValidateUser (UserName, Password);
                        if (userValidated)
                                FormsAuthentication.SetAuthCookie (UserName, false);
                }
 
-               private void ShowErrorMessage (string errorMessage)
+               void ShowErrorMessage (string errorMessage)
                {
                        if (_errorMessageLabel != null)
                                _errorMessageLabel.Text = errorMessage;
                }
 
-               private string GeneratePassword ()
+               string GeneratePassword ()
                {
                        return Membership.GeneratePassword (8, 3);
                }
@@ -1665,9 +1718,9 @@ namespace System.Web.UI.WebControls
                                t.Height = Unit.Percentage (100);
                                TableRow row = new TableRow ();
 
-                               AddButtonCell (row, _createUserWizard.CreateButtonSet (Wizard.StepPreviousButtonID, Wizard.MovePreviousCommandName, false));
-                               AddButtonCell (row, _createUserWizard.CreateButtonSet (Wizard.StepNextButtonID, Wizard.MoveNextCommandName));
-                               AddButtonCell (row, _createUserWizard.CreateButtonSet (Wizard.CancelButtonID, Wizard.CancelCommandName, false));
+                               AddButtonCell (row, _createUserWizard.CreateButtonSet (Wizard.StepPreviousButtonID, Wizard.MovePreviousCommandName, false, _createUserWizard.ID));
+                               AddButtonCell (row, _createUserWizard.CreateButtonSet (Wizard.StepNextButtonID, Wizard.MoveNextCommandName, true, _createUserWizard.ID));
+                               AddButtonCell (row, _createUserWizard.CreateButtonSet (Wizard.CancelButtonID, Wizard.CancelCommandName, false, _createUserWizard.ID));
                                
                                t.Rows.Add (row);
                                container.Controls.Add (t);
@@ -2262,7 +2315,7 @@ namespace System.Web.UI.WebControls
                                cell20.ColumnSpan = 2;
                                row2.Cells.Add (cell20);
 
-                               Control[] b = _createUserWizard.CreateButtonSet ("ContinueButton", CreateUserWizard.ContinueButtonCommandName);
+                               Control [] b = _createUserWizard.CreateButtonSet ("ContinueButton", CreateUserWizard.ContinueButtonCommandName, false, _createUserWizard.ID);
                                for (int i = 0; i < b.Length; i++)
                                        cell20.Controls.Add (b [i]);