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