-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
AccountValidationService.cs
65 lines (50 loc) · 2.44 KB
/
AccountValidationService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using Orchard.Localization;
using Orchard.Security;
using Orchard.Users.Models;
namespace Orchard.Users.Services {
public class AccountValidationService : IAccountValidationService {
private readonly IUserService _userService;
public AccountValidationService(
IUserService userService) {
_userService = userService;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public bool ValidatePassword(AccountValidationContext context) {
IDictionary<string, LocalizedString> validationErrors;
_userService.PasswordMeetsPolicies(context.Password, context.User, out validationErrors);
if (validationErrors != null && validationErrors.Any()) {
foreach (var err in validationErrors) {
if (!context.ValidationErrors.ContainsKey(err.Key)) {
context.ValidationErrors.Add(err);
}
}
}
return context.ValidationSuccessful;
}
public bool ValidateUserName(AccountValidationContext context) {
if (string.IsNullOrWhiteSpace(context.UserName)) {
context.ValidationErrors.Add("username", T("You must specify a username."));
} else if (context.UserName.Length >= UserPart.MaxUserNameLength) {
context.ValidationErrors.Add("username", T("The username you provided is too long."));
}
return context.ValidationSuccessful;
}
public bool ValidateEmail(AccountValidationContext context) {
if (string.IsNullOrWhiteSpace(context.Email)) {
context.ValidationErrors.Add("email", T("You must specify an email address."));
} else if (context.Email.Length >= UserPart.MaxEmailLength) {
context.ValidationErrors.Add("email", T("The email address you provided is too long."));
} else if (!Regex.IsMatch(context.Email, UserPart.EmailPattern, RegexOptions.IgnoreCase)) {
// http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx
context.ValidationErrors.Add("email", T("You must specify a valid email address."));
}
return context.ValidationSuccessful;
}
}
}