forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegistrationService.cs
More file actions
194 lines (164 loc) · 6.85 KB
/
RegistrationService.cs
File metadata and controls
194 lines (164 loc) · 6.85 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System;
using System.Configuration;
using System.Globalization;
using ServiceStack.Common;
using ServiceStack.Common.Web;
using ServiceStack.FluentValidation;
using ServiceStack.ServiceHost;
using ServiceStack.ServiceInterface.ServiceModel;
using ServiceStack.ServiceInterface.Validation;
using ServiceStack.WebHost.Endpoints;
namespace ServiceStack.ServiceInterface.Auth
{
public class Registration
{
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string DisplayName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public bool? AutoLogin { get; set; }
public string Continue { get; set; }
}
public class RegistrationResponse
{
public RegistrationResponse()
{
this.ResponseStatus = new ResponseStatus();
}
public string UserId { get; set; }
public string SessionId { get; set; }
public string UserName { get; set; }
public string ReferrerUrl { get; set; }
public ResponseStatus ResponseStatus { get; set; }
}
public class FullRegistrationValidator : RegistrationValidator
{
public FullRegistrationValidator()
{
RuleSet(ApplyTo.Post, () => {
RuleFor(x => x.DisplayName).NotEmpty();
});
}
}
public class RegistrationValidator : AbstractValidator<Registration>
{
public IUserAuthRepository UserAuthRepo { get; set; }
public RegistrationValidator()
{
RuleSet(ApplyTo.Post, () => {
RuleFor(x => x.Password).NotEmpty();
RuleFor(x => x.UserName).NotEmpty().When(x => x.Email.IsNullOrEmpty());
RuleFor(x => x.Email).NotEmpty().EmailAddress().When(x => x.UserName.IsNullOrEmpty());
RuleFor(x => x.UserName)
.Must(x => UserAuthRepo.GetUserAuthByUserName(x) == null)
.WithErrorCode("AlreadyExists")
.WithMessage("UserName already exists")
.When(x => !x.UserName.IsNullOrEmpty());
RuleFor(x => x.Email)
.Must(x => x.IsNullOrEmpty() || UserAuthRepo.GetUserAuthByUserName(x) == null)
.WithErrorCode("AlreadyExists")
.WithMessage("Email already exists")
.When(x => !x.Email.IsNullOrEmpty());
});
RuleSet(ApplyTo.Put, () => {
RuleFor(x => x.UserName).NotEmpty();
RuleFor(x => x.Email).NotEmpty();
});
}
}
public class RegistrationService : RestServiceBase<Registration>
{
public IUserAuthRepository UserAuthRepo { get; set; }
public static ValidateFn ValidateFn { get; set; }
public IValidator<Registration> RegistrationValidator { get; set; }
private void AssertUserAuthRepo()
{
if (UserAuthRepo == null)
throw new ConfigurationException("No IUserAuthRepository has been registered in your AppHost.");
}
/// <summary>
/// Create new Registration
/// </summary>
public override object OnPost(Registration request)
{
if (!ValidationFeature.Enabled)
RegistrationValidator.ValidateAndThrow(request, ApplyTo.Post);
AssertUserAuthRepo();
if (ValidateFn != null)
{
var validateResponse = ValidateFn(this, HttpMethods.Post, request);
if (validateResponse != null) return validateResponse;
}
RegistrationResponse response = null;
var session = this.GetSession();
var newUserAuth = ToUserAuth(request);
var existingUser = UserAuthRepo.GetUserAuth(session, null);
var user = existingUser != null
? this.UserAuthRepo.UpdateUserAuth(existingUser, newUserAuth, request.Password)
: this.UserAuthRepo.CreateUserAuth(newUserAuth, request.Password);
if (request.AutoLogin.GetValueOrDefault())
{
var authService = base.ResolveService<AuthService>();
var authResponse = authService.Post(new Auth {
UserName = request.UserName ?? request.Email,
Password = request.Password
});
if (authResponse is IHttpError)
throw (Exception)authResponse;
var typedResponse = authResponse as AuthResponse;
if (typedResponse != null)
{
response = new RegistrationResponse {
SessionId = typedResponse.SessionId,
UserName = typedResponse.UserName,
ReferrerUrl = typedResponse.ReferrerUrl,
UserId = user.Id.ToString(CultureInfo.InvariantCulture),
};
}
}
if (response == null)
{
response = new RegistrationResponse {
UserId = user.Id.ToString(CultureInfo.InvariantCulture),
};
}
if (request.Continue == null)
return response;
return new HttpResult(response) {
Location = request.Continue
};
}
public UserAuth ToUserAuth(Registration request)
{
var to = request.TranslateTo<UserAuth>();
to.PrimaryEmail = request.Email;
return to;
}
/// <summary>
/// Logic to update UserAuth from Registration info, not enabled on OnPut because of security.
/// </summary>
public object UpdateUserAuth(Registration request)
{
if (!ValidationFeature.Enabled)
RegistrationValidator.ValidateAndThrow(request, ApplyTo.Put);
if (ValidateFn != null)
{
var response = ValidateFn(this, HttpMethods.Put, request);
if (response != null) return response;
}
var session = this.GetSession();
var existingUser = UserAuthRepo.GetUserAuth(session, null);
if (existingUser == null)
{
throw HttpError.NotFound("User does not exist");
}
var newUserAuth = ToUserAuth(request);
UserAuthRepo.UpdateUserAuth(newUserAuth, existingUser, request.Password);
return new RegistrationResponse {
UserId = existingUser.Id.ToString(CultureInfo.InvariantCulture),
};
}
}
}