[bcl] Enable tests for the monodroid profile.
[mono.git] / mcs / class / System.Web.ApplicationServices / 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 using System;
31 using System.Reflection;
32 using System.Runtime.CompilerServices;
33
34 namespace System.Web.Security
35 {
36         [TypeForwardedFrom ("System.Web, Version=2.0.0.0, Culture=Neutral, PublicKeyToken=b03f5f7f11d50a3a")]
37         [Serializable]
38         public class MembershipUser
39         {
40                 string providerName;
41                 string name;
42                 object providerUserKey;
43                 string email;
44                 string passwordQuestion;
45                 string comment;
46                 bool isApproved;
47                 bool isLockedOut;
48                 DateTime creationDate;
49                 DateTime lastLoginDate;
50                 DateTime lastActivityDate;
51                 DateTime lastPasswordChangedDate;
52                 DateTime lastLockoutDate;
53
54                 protected MembershipUser ()
55                 {
56                 }
57                 
58                 public MembershipUser (string providerName, string name, object providerUserKey, string email,
59                         string passwordQuestion, string comment, bool isApproved, bool isLockedOut,
60                         DateTime creationDate, DateTime lastLoginDate, DateTime lastActivityDate,
61                         DateTime lastPasswordChangedDate, DateTime lastLockoutDate)
62                 {
63                         this.providerName = providerName;
64                         this.name = name;
65                         this.providerUserKey = providerUserKey;
66                         this.email = email;
67                         this.passwordQuestion = passwordQuestion;
68                         this.comment = comment;
69                         this.isApproved = isApproved;
70                         this.isLockedOut = isLockedOut;
71                         this.creationDate = creationDate.ToUniversalTime ();
72                         this.lastLoginDate = lastLoginDate.ToUniversalTime ();
73                         this.lastActivityDate = lastActivityDate.ToUniversalTime ();
74                         this.lastPasswordChangedDate = lastPasswordChangedDate.ToUniversalTime ();
75                         this.lastLockoutDate = lastLockoutDate.ToUniversalTime ();
76                 }
77                 
78                 void UpdateSelf (MembershipUser fromUser)
79                 {
80                         try { Comment = fromUser.Comment; } catch (NotSupportedException) {}
81                         try { creationDate = fromUser.CreationDate; } catch (NotSupportedException) {}
82                         try { Email = fromUser.Email; } catch (NotSupportedException) {}
83                         try { IsApproved = fromUser.IsApproved; } catch (NotSupportedException) {}
84                         try { isLockedOut = fromUser.IsLockedOut; } catch (NotSupportedException) {}
85                         try { LastActivityDate = fromUser.LastActivityDate; } catch (NotSupportedException) {}
86                         try { lastLockoutDate = fromUser.LastLockoutDate; } catch (NotSupportedException) {}
87                         try { LastLoginDate = fromUser.LastLoginDate; } catch (NotSupportedException) {}
88                         try { lastPasswordChangedDate = fromUser.LastPasswordChangedDate; } catch (NotSupportedException) {}
89                         try { passwordQuestion = fromUser.PasswordQuestion; } catch (NotSupportedException) {}
90                         try { providerUserKey = fromUser.ProviderUserKey; } catch (NotSupportedException) {}
91                 }
92
93                 internal void UpdateUser ()
94                 {
95                         MembershipUser newUser = Provider.GetUser (UserName, false);
96                         UpdateSelf (newUser);
97                 }
98
99                 public virtual bool ChangePassword (string oldPassword, string newPassword)
100                 {
101                         bool success = Provider.ChangePassword (UserName, oldPassword, newPassword);
102
103                         UpdateUser ();
104                         
105                         return success;
106                 }
107                 
108                 public virtual bool ChangePasswordQuestionAndAnswer (string password, string newPasswordQuestion, string newPasswordAnswer)
109                 {
110                         bool success = Provider.ChangePasswordQuestionAndAnswer (UserName, password, newPasswordQuestion, newPasswordAnswer);
111
112                         UpdateUser ();
113                         
114                         return success;
115                 }
116                 
117                 public virtual string GetPassword ()
118                 {
119                         return GetPassword (null);
120                 }
121                 
122                 public virtual string GetPassword (string answer)
123                 {
124                         return Provider.GetPassword (UserName, answer);
125                 }
126                 
127                 public virtual string ResetPassword ()
128                 {
129                         return ResetPassword (null);
130                 }
131                 
132                 public virtual string ResetPassword (string answer)
133                 {
134                         string newPass = Provider.ResetPassword (UserName, answer);
135
136                         UpdateUser ();
137                         
138                         return newPass;
139                 }
140                 
141                 public virtual string Comment {
142                         get { return comment; }
143                         set { comment = value; }
144                 }
145                 
146                 public virtual DateTime CreationDate {
147                         get { return creationDate.ToLocalTime (); }
148                 }
149                 
150                 public virtual string Email {
151                         get { return email; }
152                         set { email = value; }
153                 }
154                 
155                 public virtual bool IsApproved {
156                         get { return isApproved; }
157                         set { isApproved = value; }
158                 }
159                 
160                 public virtual bool IsLockedOut {
161                         get { return isLockedOut; }
162                 }
163
164                 public virtual
165                 bool IsOnline {
166                         get {
167                                 int minutes;
168                                 IMembershipHelper helper = MembershipProvider.Helper;
169                                 if (helper == null)
170                                         throw new PlatformNotSupportedException ("The method is not available.");
171                                 minutes = helper.UserIsOnlineTimeWindow;
172                                 return LastActivityDate > DateTime.Now - TimeSpan.FromMinutes (minutes);
173                         }
174                 }
175                 
176                 public virtual DateTime LastActivityDate {
177                         get { return lastActivityDate.ToLocalTime (); }
178                         set { lastActivityDate = value.ToUniversalTime (); }
179                 }
180                 
181                 public virtual DateTime LastLoginDate {
182                         get { return lastLoginDate.ToLocalTime (); }
183                         set { lastLoginDate = value.ToUniversalTime (); }
184                 }
185                 
186                 public virtual DateTime LastPasswordChangedDate {
187                         get { return lastPasswordChangedDate.ToLocalTime (); }
188                 }
189                 
190                 public virtual DateTime LastLockoutDate {
191                         get { return lastLockoutDate.ToLocalTime (); }
192                 }
193                 
194                 public virtual string PasswordQuestion {
195                         get { return passwordQuestion; }
196                 }
197                 
198                 public virtual string ProviderName {
199                         get { return providerName; }
200                 }
201                 
202                 public virtual string UserName {
203                         get { return name; }
204                 }
205                 
206                 public virtual object ProviderUserKey {
207                         get { return providerUserKey; }
208                 }
209                 
210                 public override string ToString ()
211                 {
212                         return UserName;
213                 }
214                 
215                 public virtual bool UnlockUser ()
216                 {
217                         bool retval = Provider.UnlockUser (UserName);
218
219                         UpdateUser ();
220
221                         return retval;
222                 }
223                 
224                 MembershipProvider Provider {
225                         get {
226                                 MembershipProvider p;                           
227                                 IMembershipHelper helper = MembershipProvider.Helper;
228                                 if (helper == null)
229                                         throw new PlatformNotSupportedException ("The method is not available.");
230                                 p = helper.Providers [ProviderName];
231                                 if (p == null)
232                                         throw new InvalidOperationException ("Membership provider '" + ProviderName + "' not found.");
233                                 return p;
234                         }
235                 }
236         }
237 }
238
239