New test.
[mono.git] / mcs / class / System.Web / System.Web.Security / MembershipUser.cs
1 //
2 // System.Web.Security.MembershipUser
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //
7 // (C) 2003 Ben Maurer
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 #if NET_2_0
32 namespace System.Web.Security
33 {
34         [Serializable]
35         public class MembershipUser
36         {
37                 string providerName;
38                 string name;
39                 object providerUserKey;
40                 string email;
41                 string passwordQuestion;
42                 string comment;
43                 bool isApproved;
44                 bool isLockedOut;
45                 DateTime creationDate;
46                 DateTime lastLoginDate;
47                 DateTime lastActivityDate;
48                 DateTime lastPasswordChangedDate;
49                 DateTime lastLockoutDate;
50                 
51                 protected MembershipUser ()
52                 {
53                 }
54                 
55                 public MembershipUser (string providerName, string name, object providerUserKey, string email,
56                         string passwordQuestion, string comment, bool isApproved, bool isLockedOut,
57                         DateTime creationDate, DateTime lastLoginDate, DateTime lastActivityDate,
58                         DateTime lastPasswordChangedDate, DateTime lastLockoutDate)
59                 {
60                         this.providerName = providerName;
61                         this.name = name;
62                         this.providerUserKey = providerUserKey;
63                         this.email = email;
64                         this.passwordQuestion = passwordQuestion;
65                         this.comment = comment;
66                         this.isApproved = isApproved;
67                         this.isLockedOut = isLockedOut;
68                         this.creationDate = creationDate.ToUniversalTime ();
69                         this.lastLoginDate = lastLoginDate.ToUniversalTime ();
70                         this.lastActivityDate = lastActivityDate.ToUniversalTime ();
71                         this.lastPasswordChangedDate = lastPasswordChangedDate.ToUniversalTime ();
72                         this.lastLockoutDate = lastLockoutDate.ToUniversalTime ();
73                 }
74                 
75                 void UpdateSelf (MembershipUser fromUser)
76                 {
77                         try { Comment = fromUser.Comment; } catch (NotSupportedException) {}
78                         try { creationDate = fromUser.CreationDate; } catch (NotSupportedException) {}
79                         try { Email = fromUser.Email; } catch (NotSupportedException) {}
80                         try { IsApproved = fromUser.IsApproved; } catch (NotSupportedException) {}
81                         try { isLockedOut = fromUser.IsLockedOut; } catch (NotSupportedException) {}
82                         try { LastActivityDate = fromUser.LastActivityDate; } catch (NotSupportedException) {}
83                         try { lastLockoutDate = fromUser.LastLockoutDate; } catch (NotSupportedException) {}
84                         try { LastLoginDate = fromUser.LastLoginDate; } catch (NotSupportedException) {}
85                         try { lastPasswordChangedDate = fromUser.LastPasswordChangedDate; } catch (NotSupportedException) {}
86                         try { passwordQuestion = fromUser.PasswordQuestion; } catch (NotSupportedException) {}
87                         try { providerUserKey = fromUser.ProviderUserKey; } catch (NotSupportedException) {}
88                 }
89
90                 internal void UpdateUser ()
91                 {
92                         MembershipUser newUser = Provider.GetUser (name, false);
93                         UpdateSelf (newUser);
94                 }
95
96                 public virtual bool ChangePassword (string oldPassword, string newPassword)
97                 {
98                         bool success = Provider.ChangePassword (UserName, oldPassword, newPassword);
99
100                         UpdateUser ();
101                         
102                         return success;
103                 }
104                 
105                 public virtual bool ChangePasswordQuestionAndAnswer (string password, string newPasswordQuestion, string newPasswordAnswer)
106                 {
107                         bool success = Provider.ChangePasswordQuestionAndAnswer (UserName, password, newPasswordQuestion, newPasswordAnswer);
108
109                         UpdateUser ();
110                         
111                         return success;
112                 }
113                 
114                 public virtual string GetPassword ()
115                 {
116                         return GetPassword (null);
117                 }
118                 
119                 public virtual string GetPassword (string answer)
120                 {
121                         return Provider.GetPassword (UserName, answer);
122                 }
123                 
124                 public virtual string ResetPassword ()
125                 {
126                         return ResetPassword (null);
127                 }
128                 
129                 public virtual string ResetPassword (string answer)
130                 {
131                         string newPass = Provider.ResetPassword (UserName, answer);
132
133                         UpdateUser ();
134                         
135                         return newPass;
136                 }
137                 
138                 public virtual string Comment {
139                         get { return comment; }
140                         set { comment = value; }
141                 }
142                 
143                 public virtual DateTime CreationDate {
144                         get { return creationDate.ToLocalTime (); }
145                 }
146                 
147                 public virtual string Email {
148                         get { return email; }
149                         set { email = value; }
150                 }
151                 
152                 public virtual bool IsApproved {
153                         get { return isApproved; }
154                         set { isApproved = value; }
155                 }
156                 
157                 public virtual bool IsLockedOut {
158                         get { return isLockedOut; }
159                 }
160                 
161                 public bool IsOnline {
162                         get {
163                                 return LastActivityDate > DateTime.Now - TimeSpan.FromMinutes (Membership.UserIsOnlineTimeWindow);  
164                         }
165                 }
166                 
167                 public virtual DateTime LastActivityDate {
168                         get { return lastActivityDate.ToLocalTime (); }
169                         set { lastActivityDate = value.ToUniversalTime (); }
170                 }
171                 
172                 public virtual DateTime LastLoginDate {
173                         get { return lastLoginDate.ToLocalTime (); }
174                         set { lastLoginDate = value.ToUniversalTime (); }
175                 }
176                 
177                 public virtual DateTime LastPasswordChangedDate {
178                         get { return lastPasswordChangedDate.ToLocalTime (); }
179                 }
180                 
181                 public virtual DateTime LastLockoutDate {
182                         get { return lastLockoutDate.ToLocalTime (); }
183                 }
184                 
185                 public virtual string PasswordQuestion {
186                         get { return passwordQuestion; }
187                 }
188                 
189                 public virtual string ProviderName {
190                         get { return providerName; }
191                 }
192                 
193                 public virtual string UserName {
194                         get { return name; }
195                 }
196                 
197                 public virtual object ProviderUserKey {
198                         get { return providerUserKey; }
199                 }
200                 
201                 public override string ToString ()
202                 {
203                         return UserName;
204                 }
205                 
206                 public virtual bool UnlockUser ()
207                 {
208                         bool retval = Provider.UnlockUser (UserName);
209
210                         UpdateUser ();
211
212                         return retval;
213                 }
214                 
215                 MembershipProvider Provider {
216                         get {
217                                 MembershipProvider p = Membership.Providers [ProviderName];
218                                 if (p == null) throw new InvalidOperationException ("Membership provider '" + ProviderName + "' not found.");
219                                 return p;
220                         }
221                 }
222         }
223 }
224 #endif
225