-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Building Edit/Delete AuthUser in Example1
- Loading branch information
Showing
24 changed files
with
2,587 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
Example1.RazorPages.IndividualAccounts/Model/CreateUpdateDto.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright (c) 2021 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/ | ||
// Licensed under MIT license. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using AuthPermissions.AdminCode; | ||
using AuthPermissions.DataLayer.Classes.SupportTypes; | ||
using Microsoft.AspNetCore.Mvc.Rendering; | ||
using StatusGeneric; | ||
|
||
namespace Example1.RazorPages.IndividualAccounts.Model | ||
{ | ||
public class CreateUpdateDto | ||
{ | ||
/// <summary> | ||
/// The userId of the user (NOTE: this is not show) | ||
/// </summary> | ||
[Required(AllowEmptyStrings = false)] | ||
[MaxLength(AuthDbConstants.UserIdSize)] | ||
public string UserId { get; set; } | ||
/// <summary> | ||
/// The user's main email (used as one way to find the user) | ||
/// </summary> | ||
[Required(AllowEmptyStrings = false)] | ||
[MaxLength(AuthDbConstants.EmailSize)] | ||
public string Email { get; set; } | ||
/// <summary> | ||
/// The user's name | ||
/// </summary> | ||
[MaxLength(AuthDbConstants.UserNameSize)] | ||
public string UserName { get; set; } | ||
|
||
/// <summary> | ||
/// The AuthRoles for this AuthUser | ||
/// </summary> | ||
public SelectList RolesSelectList { set; get; } | ||
|
||
public List<string> SelectedRoleNames { get; set; } | ||
|
||
public static async Task<IStatusGeneric<CreateUpdateDto>> PrepareForUpdateAsync(string userId, | ||
IAuthUsersAdminService authUsersAdmin) | ||
{ | ||
var status = new StatusGenericHandler<CreateUpdateDto>(); | ||
var authUserStatus = await authUsersAdmin.FindAuthUserByUserIdAsync(userId); | ||
if (status.CombineStatuses(authUserStatus).HasErrors) | ||
return status; | ||
|
||
var authUser = authUserStatus.Result; | ||
var allRoleNames = await authUsersAdmin.GetAllRoleNamesAsync(); | ||
|
||
var result = new CreateUpdateDto | ||
{ | ||
UserId = authUser.UserId, | ||
UserName = authUser.UserName, | ||
Email = authUser.Email, | ||
SelectedRoleNames = authUser.UserRoles.Select(x => x.RoleName).ToList(), | ||
RolesSelectList = new SelectList(allRoleNames, authUser.UserRoles.Select(x => x.RoleName)) | ||
}; | ||
|
||
return status.SetResult(result); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
Example1.RazorPages.IndividualAccounts/Pages/AuthUsers/Delete.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
@page | ||
@model Example1.RazorPages.IndividualAccounts.Pages.AuthUsers.DeleteModel | ||
|
||
<h1>Delete AuthUser</h1> | ||
|
||
<h3>Are you sure you want to delete this user?</h3> | ||
<div> | ||
<dl class="row"> | ||
|
||
<dt class="col-sm-2"> | ||
@Html.DisplayNameFor(model => model.UserName) | ||
</dt> | ||
<dd class="col-sm-10"> | ||
@Html.DisplayFor(model => model.UserName) | ||
</dd> | ||
<dt class="col-sm-2"> | ||
@Html.DisplayNameFor(model => model.Email) | ||
</dt> | ||
<dd class="col-sm-10"> | ||
@Html.DisplayFor(model => model.Email) | ||
</dd> | ||
</dl> | ||
<form method="post"> | ||
@Html.HiddenFor(model => model.UserId) | ||
<input type="submit" value="Delete" class="btn btn-danger" /> | ||
<a class="btn btn-secondary" asp-area="" asp-page=".\ListUsers">Cancel</a> | ||
</form> | ||
</div> |
43 changes: 43 additions & 0 deletions
43
Example1.RazorPages.IndividualAccounts/Pages/AuthUsers/Delete.cshtml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System.Threading.Tasks; | ||
using AuthPermissions.AdminCode; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
||
namespace Example1.RazorPages.IndividualAccounts.Pages.AuthUsers | ||
{ | ||
public class DeleteModel : PageModel | ||
{ | ||
|
||
private readonly IAuthUsersAdminService _authUsersAdmin; | ||
|
||
public DeleteModel(IAuthUsersAdminService authUsersAdmin) | ||
{ | ||
_authUsersAdmin = authUsersAdmin; | ||
} | ||
|
||
[BindProperty] public string UserId { get; set; } | ||
[BindProperty] public string UserName { get; set; } | ||
[BindProperty] public string Email { get; set; } | ||
|
||
public async Task<IActionResult> OnGet(string userId) | ||
{ | ||
var status = await _authUsersAdmin.FindAuthUserByUserIdAsync(userId); | ||
if (status.HasErrors) | ||
RedirectToPage("ErrorPage", new { allErrors = status.GetAllErrors() }); | ||
|
||
UserId = userId; | ||
UserName = status.Result.UserName; | ||
Email = status.Result.Email; | ||
|
||
return Page(); | ||
} | ||
|
||
public async Task<IActionResult> OnPost() | ||
{ | ||
var status = await _authUsersAdmin.DeleteUserAsync(UserId); | ||
return status.HasErrors | ||
? RedirectToPage("ErrorPage", new { allErrors = status.GetAllErrors() }) | ||
: RedirectToPage("ListUsers", new { message = status.Message }); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
Example1.RazorPages.IndividualAccounts/Pages/AuthUsers/Edit.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
@page | ||
@model Example1.RazorPages.IndividualAccounts.Pages.AuthUsers.EditModel | ||
|
||
<h1>Edit</h1> | ||
|
||
<h4>Edit the user's information</h4> | ||
<hr /> | ||
<div class="row"> | ||
<div class="col-md-8"> | ||
<form method="post"> | ||
<div asp-validation-summary="ModelOnly" class="text-danger"></div> | ||
@Html.Hidden("Data.UserId", Model.Data.UserId) | ||
<div class="form-group"> | ||
<label asp-for="Data.Email" class="control-label"></label> | ||
<input asp-for="Data.Email" class="form-control" /> | ||
<span asp-validation-for="Data.Email" class="text-danger"></span> | ||
</div> | ||
<div class="form-group"> | ||
<label asp-for="Data.UserName" class="control-label"></label> | ||
<input asp-for="Data.UserName" class="form-control" /> | ||
<span asp-validation-for="Data.UserName" class="text-danger"></span> | ||
</div> | ||
<div class="form-group row"> | ||
@Html.LabelFor(model => model.Data.RolesSelectList, new { @class = "col-sm-2 col-form-label" }) | ||
<div class="col-sm-10"> | ||
<select asp-for="Data.SelectedRoleNames" asp-items="Model.Data.RolesSelectList"></select> | ||
</div> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<input class="btn btn-primary" type="submit" value="Update" /> | ||
<a class="btn btn-secondary" asp-area="" asp-page=".\ListUsers">Cancel</a> | ||
</div> | ||
</form> | ||
</div> | ||
</div> |
42 changes: 42 additions & 0 deletions
42
Example1.RazorPages.IndividualAccounts/Pages/AuthUsers/Edit.cshtml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.Threading.Tasks; | ||
using AuthPermissions.AdminCode; | ||
using AuthPermissions.DataLayer.EfCode; | ||
using Example1.RazorPages.IndividualAccounts.Model; | ||
using ExamplesCommonCode.CommonAdmin; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using Microsoft.AspNetCore.Mvc.Rendering; | ||
|
||
namespace Example1.RazorPages.IndividualAccounts.Pages.AuthUsers | ||
{ | ||
public class EditModel : PageModel | ||
{ | ||
private readonly IAuthUsersAdminService _authUsersAdmin; | ||
|
||
public EditModel(IAuthUsersAdminService authUsersAdmin) | ||
{ | ||
_authUsersAdmin = authUsersAdmin; | ||
} | ||
|
||
[BindProperty] | ||
public CreateUpdateDto Data { get; set; } | ||
|
||
public async Task<IActionResult> OnGet(string userId) | ||
{ | ||
var status = await CreateUpdateDto.PrepareForUpdateAsync(userId, _authUsersAdmin); | ||
if (status.HasErrors) | ||
return RedirectToPage("ErrorPage", new { allErrors = status.GetAllErrors() }); | ||
|
||
Data = status.Result; | ||
return Page(); | ||
} | ||
|
||
public async Task<IActionResult> OnPost() | ||
{ | ||
var status = await _authUsersAdmin.UpdateUserAsync(Data.UserId, Data.Email, Data.UserName, Data.SelectedRoleNames); | ||
return status.HasErrors | ||
? RedirectToPage("ErrorPage", new { allErrors = status.GetAllErrors() }) | ||
: RedirectToPage("ListUsers", new { message = status.Message }); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
Example1.RazorPages.IndividualAccounts/Pages/AuthUsers/ErrorPage.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
@page | ||
@model Example1.RazorPages.IndividualAccounts.Pages.AuthUsers.ErrorPageModel | ||
|
||
@foreach (var error in @Model.Data) | ||
{ | ||
<h3 class="text-danger">@error</h3> | ||
} | ||
|
||
<a asp-area="" asp-page=".\ListUsers">Back to List Users</a> |
18 changes: 18 additions & 0 deletions
18
Example1.RazorPages.IndividualAccounts/Pages/AuthUsers/ErrorPage.cshtml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
||
namespace Example1.RazorPages.IndividualAccounts.Pages.AuthUsers | ||
{ | ||
public class ErrorPageModel : PageModel | ||
{ | ||
|
||
public IEnumerable<string> Data { get; private set; } | ||
|
||
public void OnGet(string allErrors) | ||
{ | ||
Data = allErrors.Split(Environment.NewLine); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"version": "1.0", | ||
"defaultProvider": "cdnjs", | ||
"libraries": [ | ||
{ | ||
"library": "[email protected]", | ||
"destination": "wwwroot/lib/bootstrap-multiselect/" | ||
} | ||
] | ||
} |
Oops, something went wrong.