2008-11-06 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.Security / MembershipUser.cs
index f53bf87954b6ffb69302b771aab05c8fa4905d16..598ecfeab4daf82590aa2c2635e9ecdaf812dd27 100644 (file)
 // (C) 2003 Ben Maurer
 //
 
-#if NET_1_2
-namespace System.Web.Security {
-       public class MembershipUser {
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+#if NET_2_0
+namespace System.Web.Security
+{
+       [Serializable]
+       public class MembershipUser
+       {
+               string providerName;
+               string name;
+               object providerUserKey;
+               string email;
+               string passwordQuestion;
+               string comment;
+               bool isApproved;
+               bool isLockedOut;
+               DateTime creationDate;
+               DateTime lastLoginDate;
+               DateTime lastActivityDate;
+               DateTime lastPasswordChangedDate;
+               DateTime lastLockoutDate;
+               
                protected MembershipUser ()
                {
                }
                
-               public MembershipUser (IMembershipProvider provider, string name, string email,
-                       string passwordQuestion, string comment, bool isApproved,
+               public MembershipUser (string providerName, string name, object providerUserKey, string email,
+                       string passwordQuestion, string comment, bool isApproved, bool isLockedOut,
                        DateTime creationDate, DateTime lastLoginDate, DateTime lastActivityDate,
-                       DateTime lastPasswordChangedDate)
+                       DateTime lastPasswordChangedDate, DateTime lastLockoutDate)
                {
-                       this.provider = provider;
+                       this.providerName = providerName;
                        this.name = name;
+                       this.providerUserKey = providerUserKey;
                        this.email = email;
                        this.passwordQuestion = passwordQuestion;
                        this.comment = comment;
                        this.isApproved = isApproved;
-                       this.creationDate = creationDate;
-                       this.lastLoginDate = lastLoginDate;
-                       this.lastActivityDate = lastActivityDate;
-                       this.lastPasswordChangedDate = lastPasswordChangedDate;
+                       this.isLockedOut = isLockedOut;
+                       this.creationDate = creationDate.ToUniversalTime ();
+                       this.lastLoginDate = lastLoginDate.ToUniversalTime ();
+                       this.lastActivityDate = lastActivityDate.ToUniversalTime ();
+                       this.lastPasswordChangedDate = lastPasswordChangedDate.ToUniversalTime ();
+                       this.lastLockoutDate = lastLockoutDate.ToUniversalTime ();
                }
                
+               void UpdateSelf (MembershipUser fromUser)
+               {
+                       try { Comment = fromUser.Comment; } catch (NotSupportedException) {}
+                       try { creationDate = fromUser.CreationDate; } catch (NotSupportedException) {}
+                       try { Email = fromUser.Email; } catch (NotSupportedException) {}
+                       try { IsApproved = fromUser.IsApproved; } catch (NotSupportedException) {}
+                       try { isLockedOut = fromUser.IsLockedOut; } catch (NotSupportedException) {}
+                       try { LastActivityDate = fromUser.LastActivityDate; } catch (NotSupportedException) {}
+                       try { lastLockoutDate = fromUser.LastLockoutDate; } catch (NotSupportedException) {}
+                       try { LastLoginDate = fromUser.LastLoginDate; } catch (NotSupportedException) {}
+                       try { lastPasswordChangedDate = fromUser.LastPasswordChangedDate; } catch (NotSupportedException) {}
+                       try { passwordQuestion = fromUser.PasswordQuestion; } catch (NotSupportedException) {}
+                       try { providerUserKey = fromUser.ProviderUserKey; } catch (NotSupportedException) {}
+               }
+
+               internal void UpdateUser ()
+               {
+                       MembershipUser newUser = Provider.GetUser (name, false);
+                       UpdateSelf (newUser);
+               }
+
                public virtual bool ChangePassword (string oldPassword, string newPassword)
                {
-                       bool success = Provider.ChangePassword (Username, oldPassword, newPassword);
-                       if (success)
-                               LastPasswordChangedDate = DateTime.Now;
+                       bool success = Provider.ChangePassword (UserName, oldPassword, newPassword);
+
+                       UpdateUser ();
                        
                        return success;
                }
                
                public virtual bool ChangePasswordQuestionAndAnswer (string password, string newPasswordQuestion, string newPasswordAnswer)
                {
-                       bool success = Provider.ChangePasswordQuestionAndAnswer (Username, password, newPasswordQuestion, newPasswordAnswer);
-                       if (success)
-                               passwordQuestion = newPasswordQuestion;
+                       bool success = Provider.ChangePasswordQuestionAndAnswer (UserName, password, newPasswordQuestion, newPasswordAnswer);
+
+                       UpdateUser ();
                        
                        return success;
                }
@@ -56,7 +118,7 @@ namespace System.Web.Security {
                
                public virtual string GetPassword (string answer)
                {
-                       return Provider.GetPassword (Username, answer);
+                       return Provider.GetPassword (UserName, answer);
                }
                
                public virtual string ResetPassword ()
@@ -66,9 +128,9 @@ namespace System.Web.Security {
                
                public virtual string ResetPassword (string answer)
                {
-                       string newPass = Provider.ResetPassword (Username, answer);
-                       if (newPass != null)
-                               LastPasswordChangedDate = DateTime.Now;
+                       string newPass = Provider.ResetPassword (UserName, answer);
+
+                       UpdateUser ();
                        
                        return newPass;
                }
@@ -79,8 +141,7 @@ namespace System.Web.Security {
                }
                
                public virtual DateTime CreationDate {
-                       get { return creationDate; }
-                       set { creationDate = value; }
+                       get { return creationDate.ToLocalTime (); }
                }
                
                public virtual string Email {
@@ -93,48 +154,71 @@ namespace System.Web.Security {
                        set { isApproved = value; }
                }
                
-               [MonoTODO]
+               public virtual bool IsLockedOut {
+                       get { return isLockedOut; }
+               }
+               
                public bool IsOnline {
-                       get { throw new NotImplementedException (); }
+                       get {
+                               return LastActivityDate > DateTime.Now - TimeSpan.FromMinutes (Membership.UserIsOnlineTimeWindow);  
+                       }
                }
                
                public virtual DateTime LastActivityDate {
-                       get { return lastActivityDate; }
-                       set { lastActivityDate = value; }
+                       get { return lastActivityDate.ToLocalTime (); }
+                       set { lastActivityDate = value.ToUniversalTime (); }
                }
                
                public virtual DateTime LastLoginDate {
-                       get { return lastLoginDate; }
-                       set { lastLoginDate = value; }
+                       get { return lastLoginDate.ToLocalTime (); }
+                       set { lastLoginDate = value.ToUniversalTime (); }
                }
                
                public virtual DateTime LastPasswordChangedDate {
-                       get { return lastPasswordChangedDate; }
-                       set { lastPasswordChangedDate = value; }
+                       get { return lastPasswordChangedDate.ToLocalTime (); }
+               }
+               
+               public virtual DateTime LastLockoutDate {
+                       get { return lastLockoutDate.ToLocalTime (); }
                }
                
                public virtual string PasswordQuestion {
                        get { return passwordQuestion; }
                }
                
-               public virtual IMembershipProvider Provider {
-                       get { return provider; }
+               public virtual string ProviderName {
+                       get { return providerName; }
                }
                
-               public virtual string Username {
+               public virtual string UserName {
                        get { return name; }
                }
                
-               IMembershipProvider provider;
-               string name;
-               string email;
-               string passwordQuestion;
-               string comment;
-               bool isApproved;
-               DateTime creationDate;
-               DateTime lastLoginDate;
-               DateTime lastActivityDate;
-               DateTime lastPasswordChangedDate;
+               public virtual object ProviderUserKey {
+                       get { return providerUserKey; }
+               }
+               
+               public override string ToString ()
+               {
+                       return UserName;
+               }
+               
+               public virtual bool UnlockUser ()
+               {
+                       bool retval = Provider.UnlockUser (UserName);
+
+                       UpdateUser ();
+
+                       return retval;
+               }
+               
+               MembershipProvider Provider {
+                       get {
+                               MembershipProvider p = Membership.Providers [ProviderName];
+                               if (p == null) throw new InvalidOperationException ("Membership provider '" + ProviderName + "' not found.");
+                               return p;
+                       }
+               }
        }
 }
 #endif