Mercurial > altnet-hispano
annotate Agendas/trunk/src/Agendas.Web/Models/AccountModels.cs @ 50:3ebe89c88caa
Agregando propiedad al Evento sobre el Usuario que lo crea o que realizó la
última modificación.
author | nelopauselli |
---|---|
date | Fri, 13 May 2011 23:29:05 -0300 |
parents | 475be11edf56 |
children | 65f0b3d70c18 |
rev | line source |
---|---|
10 | 1 using System; |
2 using System.Collections.Generic; | |
3 using System.ComponentModel.DataAnnotations; | |
4 using System.Globalization; | |
5 using System.Web.Mvc; | |
6 using System.Web.Security; | |
7 | |
34
475be11edf56
Ajuste en los nombre de los assemblies y namespaces
nelo@MTEySS.neluz.int
parents:
10
diff
changeset
|
8 namespace AltNetHispano.Agendas.Web.Models |
10 | 9 { |
10 | |
11 #region Models | |
12 | |
13 public class ChangePasswordModel | |
14 { | |
15 [Required] | |
16 [DataType(DataType.Password)] | |
17 [Display(Name = "Current password")] | |
18 public string OldPassword { get; set; } | |
19 | |
20 [Required] | |
21 [ValidatePasswordLength] | |
22 [DataType(DataType.Password)] | |
23 [Display(Name = "New password")] | |
24 public string NewPassword { get; set; } | |
25 | |
26 [DataType(DataType.Password)] | |
27 [Display(Name = "Confirm new password")] | |
28 [Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")] | |
29 public string ConfirmPassword { get; set; } | |
30 } | |
31 | |
32 public class LogOnModel | |
33 { | |
34 [Required] | |
35 [Display(Name = "User name")] | |
36 public string UserName { get; set; } | |
37 | |
38 [Required] | |
39 [DataType(DataType.Password)] | |
40 [Display(Name = "Password")] | |
41 public string Password { get; set; } | |
42 | |
43 [Display(Name = "Remember me?")] | |
44 public bool RememberMe { get; set; } | |
45 } | |
46 | |
47 | |
48 public class RegisterModel | |
49 { | |
50 [Required] | |
51 [Display(Name = "User name")] | |
52 public string UserName { get; set; } | |
53 | |
54 [Required] | |
55 [DataType(DataType.EmailAddress)] | |
56 [Display(Name = "Email address")] | |
57 public string Email { get; set; } | |
58 | |
59 [Required] | |
60 [ValidatePasswordLength] | |
61 [DataType(DataType.Password)] | |
62 [Display(Name = "Password")] | |
63 public string Password { get; set; } | |
64 | |
65 [DataType(DataType.Password)] | |
66 [Display(Name = "Confirm password")] | |
67 [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] | |
68 public string ConfirmPassword { get; set; } | |
69 } | |
70 #endregion | |
71 | |
72 #region Services | |
73 // The FormsAuthentication type is sealed and contains static members, so it is difficult to | |
74 // unit test code that calls its members. The interface and helper class below demonstrate | |
75 // how to create an abstract wrapper around such a type in order to make the AccountController | |
76 // code unit testable. | |
77 | |
78 public interface IMembershipService | |
79 { | |
80 int MinPasswordLength { get; } | |
81 | |
82 bool ValidateUser(string userName, string password); | |
83 MembershipCreateStatus CreateUser(string userName, string password, string email); | |
84 bool ChangePassword(string userName, string oldPassword, string newPassword); | |
85 } | |
86 | |
87 public class AccountMembershipService : IMembershipService | |
88 { | |
89 private readonly MembershipProvider _provider; | |
90 | |
91 public AccountMembershipService() | |
92 : this(null) | |
93 { | |
94 } | |
95 | |
96 public AccountMembershipService(MembershipProvider provider) | |
97 { | |
98 _provider = provider ?? Membership.Provider; | |
99 } | |
100 | |
101 public int MinPasswordLength | |
102 { | |
103 get | |
104 { | |
105 return _provider.MinRequiredPasswordLength; | |
106 } | |
107 } | |
108 | |
109 public bool ValidateUser(string userName, string password) | |
110 { | |
111 if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName"); | |
112 if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", "password"); | |
113 | |
114 return _provider.ValidateUser(userName, password); | |
115 } | |
116 | |
117 public MembershipCreateStatus CreateUser(string userName, string password, string email) | |
118 { | |
119 if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName"); | |
120 if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", "password"); | |
121 if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email"); | |
122 | |
123 MembershipCreateStatus status; | |
124 _provider.CreateUser(userName, password, email, null, null, true, null, out status); | |
125 return status; | |
126 } | |
127 | |
128 public bool ChangePassword(string userName, string oldPassword, string newPassword) | |
129 { | |
130 if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName"); | |
131 if (String.IsNullOrEmpty(oldPassword)) throw new ArgumentException("Value cannot be null or empty.", "oldPassword"); | |
132 if (String.IsNullOrEmpty(newPassword)) throw new ArgumentException("Value cannot be null or empty.", "newPassword"); | |
133 | |
134 // The underlying ChangePassword() will throw an exception rather | |
135 // than return false in certain failure scenarios. | |
136 try | |
137 { | |
138 MembershipUser currentUser = _provider.GetUser(userName, true /* userIsOnline */); | |
139 return currentUser.ChangePassword(oldPassword, newPassword); | |
140 } | |
141 catch (ArgumentException) | |
142 { | |
143 return false; | |
144 } | |
145 catch (MembershipPasswordException) | |
146 { | |
147 return false; | |
148 } | |
149 } | |
150 } | |
151 | |
152 public interface IFormsAuthenticationService | |
153 { | |
154 void SignIn(string userName, bool createPersistentCookie); | |
155 void SignOut(); | |
156 } | |
157 | |
158 public class FormsAuthenticationService : IFormsAuthenticationService | |
159 { | |
160 public void SignIn(string userName, bool createPersistentCookie) | |
161 { | |
162 if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName"); | |
163 | |
164 FormsAuthentication.SetAuthCookie(userName, createPersistentCookie); | |
165 } | |
166 | |
167 public void SignOut() | |
168 { | |
169 FormsAuthentication.SignOut(); | |
170 } | |
171 } | |
172 #endregion | |
173 | |
174 #region Validation | |
175 public static class AccountValidation | |
176 { | |
177 public static string ErrorCodeToString(MembershipCreateStatus createStatus) | |
178 { | |
179 // See http://go.microsoft.com/fwlink/?LinkID=177550 for | |
180 // a full list of status codes. | |
181 switch (createStatus) | |
182 { | |
183 case MembershipCreateStatus.DuplicateUserName: | |
184 return "Username already exists. Please enter a different user name."; | |
185 | |
186 case MembershipCreateStatus.DuplicateEmail: | |
187 return "A username for that e-mail address already exists. Please enter a different e-mail address."; | |
188 | |
189 case MembershipCreateStatus.InvalidPassword: | |
190 return "The password provided is invalid. Please enter a valid password value."; | |
191 | |
192 case MembershipCreateStatus.InvalidEmail: | |
193 return "The e-mail address provided is invalid. Please check the value and try again."; | |
194 | |
195 case MembershipCreateStatus.InvalidAnswer: | |
196 return "The password retrieval answer provided is invalid. Please check the value and try again."; | |
197 | |
198 case MembershipCreateStatus.InvalidQuestion: | |
199 return "The password retrieval question provided is invalid. Please check the value and try again."; | |
200 | |
201 case MembershipCreateStatus.InvalidUserName: | |
202 return "The user name provided is invalid. Please check the value and try again."; | |
203 | |
204 case MembershipCreateStatus.ProviderError: | |
205 return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator."; | |
206 | |
207 case MembershipCreateStatus.UserRejected: | |
208 return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator."; | |
209 | |
210 default: | |
211 return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator."; | |
212 } | |
213 } | |
214 } | |
215 | |
216 [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)] | |
217 public sealed class ValidatePasswordLengthAttribute : ValidationAttribute, IClientValidatable | |
218 { | |
219 private const string _defaultErrorMessage = "'{0}' must be at least {1} characters long."; | |
220 private readonly int _minCharacters = Membership.Provider.MinRequiredPasswordLength; | |
221 | |
222 public ValidatePasswordLengthAttribute() | |
223 : base(_defaultErrorMessage) | |
224 { | |
225 } | |
226 | |
227 public override string FormatErrorMessage(string name) | |
228 { | |
229 return String.Format(CultureInfo.CurrentCulture, ErrorMessageString, | |
230 name, _minCharacters); | |
231 } | |
232 | |
233 public override bool IsValid(object value) | |
234 { | |
235 string valueAsString = value as string; | |
236 return (valueAsString != null && valueAsString.Length >= _minCharacters); | |
237 } | |
238 | |
239 public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) | |
240 { | |
241 return new[]{ | |
242 new ModelClientValidationStringLengthRule(FormatErrorMessage(metadata.GetDisplayName()), _minCharacters, int.MaxValue) | |
243 }; | |
244 } | |
245 } | |
246 #endregion | |
247 } |