InitialCode Commit
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
@page
|
||||
@model MTG_CollectionVerwaltung.Areas.Identity.Pages.Account.Admin.IndexModel
|
||||
@{}
|
||||
<h2>Users</h2>
|
||||
<table class="table table-striped">
|
||||
<thead class="table-dark">
|
||||
<tr>
|
||||
<th>UserId</th>
|
||||
<th>Username</th>
|
||||
<th>Usermail</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var user in Model.Users)
|
||||
{
|
||||
<tr>
|
||||
<th>@user.Id</th>
|
||||
<th>@user.UserName</th>
|
||||
<th>@user.Email</th>
|
||||
<th>
|
||||
<div class="btn-group">
|
||||
@if (user.Id == Model.CurrentUserID)
|
||||
{
|
||||
<a class="btn btn-outline-success disabled">Modify</a>
|
||||
}
|
||||
else
|
||||
{
|
||||
<a asp-page="/Account/Admin/ModifyUser" asp-route-id="@user.Id" class="btn btn-outline-success">Modify</a>
|
||||
}
|
||||
@if (user.Id == Model.CurrentUserID)
|
||||
{
|
||||
<a class="btn btn-outline-danger disabled">Delete</a>
|
||||
}
|
||||
else
|
||||
{
|
||||
<button data-bs-toggle="modal" data-bs-target="#deleteUserModal" data-user-id="@user.Id" data-user-name="@user.UserName" type="button" class="btn btn-outline-danger">Delete</button>
|
||||
}
|
||||
|
||||
</div>
|
||||
</th>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<div class="modal fade" id="deleteUserModal" tabindex="-1" aria-hidden="true">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<form method="post" asp-page-handler="Delete">
|
||||
<div class="modal-header">
|
||||
<h1 class="modal-title fs-5">Benutzer Löschen</h1>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<p>Soll der Benutzer <strong id="modalUserName"></strong> wirklich gelöscht werden?</p>
|
||||
<input type="hidden" id="modalUserId" name="UserId" />
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Abbrechen</button>
|
||||
<button type="submit" class="btn btn-danger">Löschen</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
var deleteUserModal = document.getElementById('deleteUserModal')
|
||||
deleteUserModal.addEventListener('show.bs.modal', function (event) {
|
||||
var button = event.relatedTarget
|
||||
var userId = button.getAttribute('data-user-id')
|
||||
var userName = button.getAttribute('data-user-name')
|
||||
|
||||
// Fülle Modal Felder
|
||||
var modalUserName = deleteUserModal.querySelector('#modalUserName')
|
||||
var modalUserId = deleteUserModal.querySelector('#modalUserId')
|
||||
|
||||
modalUserName.textContent = userName
|
||||
modalUserId.value = userId
|
||||
})
|
||||
</script>
|
||||
@@ -0,0 +1,46 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using MTG_CollectionVerwaltung.Data;
|
||||
using MTG_CollectionVerwaltung.Data.MTGCollection;
|
||||
|
||||
namespace MTG_CollectionVerwaltung.Areas.Identity.Pages.Account.Admin
|
||||
{
|
||||
[Authorize(Roles = "Admin")]
|
||||
public class IndexModel : PageModel
|
||||
{
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
private readonly SignInManager<ApplicationUser> _signInManager;
|
||||
|
||||
public IndexModel(
|
||||
UserManager<ApplicationUser> userManager,
|
||||
SignInManager<ApplicationUser> signInManager)
|
||||
{
|
||||
_userManager = userManager;
|
||||
_signInManager = signInManager;
|
||||
}
|
||||
|
||||
public List<ApplicationUser> Users { get; set; } = new List<ApplicationUser>();
|
||||
|
||||
public string? CurrentUserID { get; set; }
|
||||
|
||||
public void OnGet()
|
||||
{
|
||||
Users = _userManager.Users.ToList();
|
||||
CurrentUserID = _userManager.GetUserId(User);
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostDeleteAsync(string userId)
|
||||
{
|
||||
var user = await _userManager.FindByIdAsync(userId);
|
||||
if (user != null)
|
||||
{
|
||||
if(userId != CurrentUserID)
|
||||
await _userManager.DeleteAsync(user);
|
||||
}
|
||||
|
||||
return RedirectToPage();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
@page
|
||||
@model MTG_CollectionVerwaltung.Areas.Identity.Pages.Account.Admin.ModifyUserModel
|
||||
@{
|
||||
}
|
||||
<h2>@Model.userToModyfy?.UserName Bearbeiten</h2>
|
||||
<partial name="_StatusMessage" for="StatusMessage" />
|
||||
<button type="button" data-bs-toggle="modal" data-bs-target="#resetPasswordModal" class="btn btn-secondary">Reset Passwort</button>
|
||||
|
||||
<div class="modal fade" id="resetPasswordModal" tabindex="-1" aria-hidden="true">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<form method="post" asp-page-handler="ResetPassword">
|
||||
<div class="modal-header">
|
||||
<h1 class="modal-title fs-5">Passwort Zurücksetzen</h1>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<p>Soll das Passwort für den User wirklich geändert werden?</p>
|
||||
<input type="hidden" id="modalUserId" name="UserId" value="@Model.userToModyfy?.Id" />
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Abbrechen</button>
|
||||
<button type="submit" class="btn btn-danger">Ändern</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,71 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using MTG_CollectionVerwaltung.Data;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MTG_CollectionVerwaltung.Areas.Identity.Pages.Account.Admin
|
||||
{
|
||||
[Authorize(Roles = "Admin")]
|
||||
public class ModifyUserModel : PageModel
|
||||
{
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
|
||||
public ModifyUserModel(UserManager<ApplicationUser> userManger)
|
||||
{
|
||||
_userManager = userManger;
|
||||
}
|
||||
|
||||
[TempData]
|
||||
public string StatusMessage { get; set; }
|
||||
|
||||
public ApplicationUser? userToModyfy { get; set; }
|
||||
|
||||
public async Task<IActionResult> OnGet(string id)
|
||||
{
|
||||
var CurrentUserID = _userManager.GetUserId(User);
|
||||
userToModyfy = await _userManager.FindByIdAsync(id);
|
||||
if (id == CurrentUserID)
|
||||
{
|
||||
return RedirectToPage("/Account/Admin/Index");
|
||||
}
|
||||
return Page();
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostResetPasswordAsync(string userId)
|
||||
{
|
||||
var userToModyfy = await _userManager.FindByIdAsync(userId);
|
||||
|
||||
if (userToModyfy == null) {
|
||||
StatusMessage = "Error Benutzer konnte nicht gefunden werden.";
|
||||
return Page();
|
||||
}
|
||||
|
||||
var currentUserId = _userManager.GetUserId(User);
|
||||
if (userId == currentUserId)
|
||||
{
|
||||
StatusMessage = "Error Passwort des eigenen user kann nicht zurückgesetzt werden.";
|
||||
return Page();
|
||||
}
|
||||
|
||||
|
||||
var standardPassword = "Password123!";
|
||||
|
||||
var token = await _userManager.GeneratePasswordResetTokenAsync(userToModyfy);
|
||||
|
||||
var result = await _userManager.ResetPasswordAsync(userToModyfy, token, standardPassword);
|
||||
|
||||
if (result.Succeeded)
|
||||
{
|
||||
StatusMessage = $"Passwort von {userToModyfy.UserName} zurückgesetzt.";
|
||||
}
|
||||
else
|
||||
{
|
||||
StatusMessage = "Error " + string.Join(", ", result.Errors.Select(e => e.Description));
|
||||
}
|
||||
|
||||
return Page();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user