Skip to content

Commit

Permalink
Building Edit/Delete AuthUser in Example1
Browse files Browse the repository at this point in the history
  • Loading branch information
JonPSmith committed Aug 21, 2021
1 parent 2d12725 commit 1b2ebfb
Show file tree
Hide file tree
Showing 24 changed files with 2,587 additions and 36 deletions.
12 changes: 12 additions & 0 deletions AuthPermissions/AdminCode/IAuthUsersAdminService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ public interface IAuthUsersAdminService
/// <returns>Status containing the AuthUser with UserRoles and UserTenant, or errors</returns>
Task<IStatusGeneric<AuthUser>> FindAuthUserByEmailAsync(string email);

/// <summary>
/// This returns a list of all the RoleNames
/// </summary>
/// <returns></returns>
Task<List<string>> GetAllRoleNamesAsync();

/// <summary>
/// This returns all the tenant full names
/// </summary>
/// <returns></returns>
Task<List<string>> GetAllTenantNamesAsync();

/// <summary>
/// This adds a new AuthUse to the database
/// </summary>
Expand Down
18 changes: 18 additions & 0 deletions AuthPermissions/AdminCode/Services/AuthUsersAdminService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,24 @@ public async Task<IStatusGeneric<AuthUser>> FindAuthUserByEmailAsync(string emai
return status.SetResult(authUser);
}

/// <summary>
/// This returns a list of all the RoleNames
/// </summary>
/// <returns></returns>
public async Task<List<string>> GetAllRoleNamesAsync()
{
return await _context.RoleToPermissions.Select(x => x.RoleName).ToListAsync();
}

/// <summary>
/// This returns all the tenant full names
/// </summary>
/// <returns></returns>
public async Task<List<string>> GetAllTenantNamesAsync()
{
return await _context.Tenants.Select(x => x.TenantFullName).ToListAsync();
}

/// <summary>
/// This adds a new AuthUse to the database
/// </summary>
Expand Down
65 changes: 65 additions & 0 deletions Example1.RazorPages.IndividualAccounts/Model/CreateUpdateDto.cs
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);
}
}
}
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>
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 Example1.RazorPages.IndividualAccounts/Pages/AuthUsers/Edit.cshtml
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>
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 });
}
}
}
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>
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
@string.Join(", ", item.RoleNames)
</td>
<td>
<a asp-area="" asp-page="AuthUsers\Edit" asp-route-id="@item.UserId">Edit</a>
<a asp-area="" asp-page=".\Edit" asp-route-userId="@item.UserId">Edit</a>
|
<a asp-area="" asp-page="AuthUsers\Delete" asp-route-id="@item.UserId">Delete</a>
<a asp-area="" asp-page=".\Delete" asp-route-userId="@item.UserId">Delete</a>
</td>
</tr>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async Task OnGet(string message)
{
Message = message;
var userQuery = _authUsersAdmin.QueryAuthUsers();
AuthUserList = await AuthUserDisplay.SelectQuery(userQuery.OrderBy(x => x.Email)).ToListAsync();
AuthUserList = await AuthUserDisplay.TurnIntoDisplayFormat(userQuery.OrderBy(x => x.Email)).ToListAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public async Task OnGet()
public async Task<IActionResult> OnPost()
{
var status = await _authUsersAdmin.ApplySyncChangesAsync(Data);
return RedirectToPage("ListUsers", new { message = status.Message });
return status.HasErrors
? RedirectToPage("ErrorPage", new { allErrors = status.GetAllErrors() })
: RedirectToPage("ListUsers", new { message = status.Message });
}
}
}
10 changes: 10 additions & 0 deletions Example1.RazorPages.IndividualAccounts/libman.json
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/"
}
]
}
Loading

0 comments on commit 1b2ebfb

Please sign in to comment.