-
Notifications
You must be signed in to change notification settings - Fork 1
/
KvpUserInfoAdminHandler.cs
336 lines (303 loc) · 12.5 KB
/
KvpUserInfoAdminHandler.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Copyright (c) Source Tree Solutions, LLC. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Author: Joe Audette
// Created: 2017-07-10
// Last Modified: 2018-01-24
//
using cloudscribe.Core.Models;
using cloudscribe.Core.Web.ExtensionPoints;
using cloudscribe.Core.Web.ViewModels.Account;
using cloudscribe.Core.Web.ViewModels.UserAdmin;
using cloudscribe.Kvp.Models;
using cloudscribe.Pagination.Models;
using cloudscribe.UserProperties.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace cloudscribe.UserProperties.Kvp
{
public class KvpUserInfoAdminHandler : IHandleCustomUserInfoAdmin
{
public KvpUserInfoAdminHandler(
IProfileOptionsResolver customPropsResolver,
IUserPropertyService userPropertyService,
IUserPropertyValidator userPropertyValidator,
IOptions<MultiTenantOptions> multiTenantOptionsAccessor,
IKvpUserSearchQueries kvpUserSearchQueries,
ILogger<KvpUserInfoAdminHandler> logger
)
{
_customPropsResolver = customPropsResolver;
_log = logger;
_userPropertyValidator = userPropertyValidator;
_kvpUserSearchQueries = kvpUserSearchQueries;
_multiTenantOptions = multiTenantOptionsAccessor.Value;
_userPropertyService = userPropertyService;
}
protected IProfileOptionsResolver _customPropsResolver;
protected ILogger _log;
protected UserPropertySet _props = null;
protected IUserPropertyService _userPropertyService;
protected IUserPropertyValidator _userPropertyValidator;
private readonly IKvpUserSearchQueries _kvpUserSearchQueries;
private MultiTenantOptions _multiTenantOptions;
private async Task EnsureProps()
{
if(_props == null)
{
_props = await _customPropsResolver.GetProfileProps();
}
}
public Task<string> GetNewUserViewName(
ISiteContext site,
HttpContext httpContext)
{
return Task.FromResult("NewUser"); // this is just returning the default view name.
}
public async Task HandleNewUserGet(
ISiteContext site,
NewUserViewModel viewModel,
HttpContext httpContext,
ViewDataDictionary viewData,
CancellationToken cancellationToken = default(CancellationToken)
)
{
await EnsureProps();
// if any configured properties have default values add them to viewData
// to pass them to the view
foreach (var p in _props.Properties)
{
if (p.VisibleOnRegistration)
{
if (!string.IsNullOrWhiteSpace(p.DefaultValue))
{
viewData[p.Key] = p.DefaultValue;
}
}
}
}
public async Task<bool> HandleNewUserValidation(
ISiteContext site,
NewUserViewModel viewModel,
HttpContext httpContext,
ViewDataDictionary viewData,
ModelStateDictionary modelState,
CancellationToken cancellationToken = default(CancellationToken)
)
{
await EnsureProps();
var result = true;
foreach (var p in _props.Properties)
{
if (p.VisibleOnRegistration)
{
var postedValue = httpContext.Request.Form[p.Key];
if (_userPropertyValidator.IsValid(p, postedValue, modelState))
{
// if valid keep the field populated in case some other model validation failed and the form is re-displayed
viewData[p.Key] = postedValue;
}
else
{
result = false;
}
}
}
return result;
}
public Task ProcessUserBeforeCreate(ISiteUser siteUser, HttpContext httpContext)
{
if (siteUser != null)
{
foreach (var p in _props.Properties)
{
if (p.EditableOnAdminUserEdit)
{
if (_userPropertyService.IsNativeUserProperty(p.Key))
{
var postedValue = httpContext.Request.Form[p.Key];
_userPropertyService.UpdateNativeUserProperty(siteUser, p.Key, postedValue);
}
}
}
// we don't need to save the user here it is saved after this method
}
return Task.FromResult(0);
}
public async Task HandleNewUserPostSuccess(
ISiteContext site,
ISiteUser siteUser,
NewUserViewModel viewModel,
HttpContext httpContext,
CancellationToken cancellationToken = default(CancellationToken)
)
{
await EnsureProps();
// we "could" re-validate here but
// the method above gets called just before this in the same postback
// so we know there were no validation errors or this method would not be invoked
//SiteUser siteUser = null;
//if (_userPropertyService.HasAnyNativeProps(_props.Properties))
//{
// siteUser = await _userPropertyService.GetUser(loginResult.User.Id.ToString());
//}
if (siteUser != null)
{
foreach (var p in _props.Properties)
{
if (p.EditableOnAdminUserEdit)
{
if (!_userPropertyService.IsNativeUserProperty(p.Key))
{
var postedValue = httpContext.Request.Form[p.Key];
// persist to kvp storage
await _userPropertyService.CreateOrUpdate(
site.Id.ToString(),
siteUser.Id.ToString(),
p.Key,
postedValue);
}
}
}
}
else
{
_log.LogError("user was null in HandleNewUserPostSuccess, unable to update user with custom data");
}
}
public virtual Task<string> GetUserEditViewName(
ISiteContext site,
HttpContext httpContext)
{
return Task.FromResult("UserEdit"); // this is just returning the default view name.
}
public virtual async Task HandleUserEditGet(
ISiteContext site,
EditUserViewModel viewModel,
HttpContext httpContext,
ViewDataDictionary viewData,
CancellationToken cancellationToken = default(CancellationToken)
)
{
await EnsureProps();
// add user props to viewData to pass them to the view
var userProps = await _userPropertyService.FetchByUser(site.Id.ToString(), viewModel.UserId.ToString());
SiteUser siteUser = null;
if (_userPropertyService.HasAnyNativeProps(_props.Properties))
{
siteUser = await _userPropertyService.GetUser(viewModel.UserId.ToString());
}
foreach (var p in _props.Properties)
{
if(p.VisibleOnAdminUserEdit)
{
if (_userPropertyService.IsNativeUserProperty(p.Key))
{
viewData[p.Key] = _userPropertyService.GetNativeUserProperty(siteUser, p.Key);
}
else
{
var found = userProps.Where(x => x.Key == p.Key).FirstOrDefault();
if (found != null)
{
viewData[p.Key] = found.Value;
}
}
}
}
}
public virtual async Task<bool> HandleUserEditValidation(
ISiteContext site,
EditUserViewModel viewModel,
HttpContext httpContext,
ViewDataDictionary viewData,
ModelStateDictionary modelState,
CancellationToken cancellationToken = default(CancellationToken)
)
{
await EnsureProps();
var result = true;
foreach (var p in _props.Properties)
{
if (p.EditableOnAdminUserEdit)
{
var postedValue = httpContext.Request.Form[p.Key];
if (_userPropertyValidator.IsValid(p, postedValue, modelState))
{
// if valid keep the field populated in case some other model validation failed and the form is re-displayed
viewData[p.Key] = postedValue;
}
else
{
result = false;
}
}
}
return result;
}
public virtual async Task HandleUserEditPostSuccess(
ISiteContext site,
ISiteUser siteUser,
EditUserViewModel viewModel,
HttpContext httpContext,
CancellationToken cancellationToken = default(CancellationToken)
)
{
await EnsureProps();
// we "could" re-validate here but
// the method above gets called just before this in the same postback
// so we know there were no validation errors or this method would not be invoked
if (siteUser != null)
{
foreach (var p in _props.Properties)
{
if(p.EditableOnAdminUserEdit)
{
var postedValue = httpContext.Request.Form[p.Key];
if (_userPropertyService.IsNativeUserProperty(p.Key))
{
_userPropertyService.UpdateNativeUserProperty(siteUser, p.Key, postedValue);
}
else
{
// persist to kvp storage
await _userPropertyService.CreateOrUpdate(
site.Id.ToString(),
siteUser.Id.ToString(),
p.Key,
postedValue);
}
}
}
}
else
{
_log.LogError("user was null in HandleUserInfoPostSuccess, unable to update user with custom data");
}
}
public virtual async Task<PagedResult<IUserInfo>> GetCustomUserAdminSearchPage(
Guid siteId,
int pageNumber,
int pageSize,
string searchInput,
int sortMode,
CancellationToken cancellationToken = default)
{
if (_multiTenantOptions.UseRelatedSitesMode) { siteId = _multiTenantOptions.RelatedSiteId; }
// list of searchable KVPs comes in from json config
var searchableProps = await _customPropsResolver.GetSearchableProfileProps();
var searchableKvpKeys = searchableProps.Select(definition => definition.Key.ToUpper()).Distinct().ToList();
return await _kvpUserSearchQueries.GetUserAdminSearchPage(siteId, pageNumber, pageSize, searchInput, sortMode, searchableKvpKeys);
}
public virtual async Task<string> GetUserListViewName(ISiteContext site, HttpContext httpContext)
{
return await _customPropsResolver.GetUserListingViewName(); // this is NO LONGER just returning the default index view name.
}
}
}