2023-12-19 16:01:13 +01:00
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using TaikoWebUI.Pages.Dialogs;
|
2022-09-12 00:33:58 +08:00
|
|
|
|
|
|
|
|
|
namespace TaikoWebUI.Pages;
|
|
|
|
|
|
2023-11-11 21:04:11 +00:00
|
|
|
|
public partial class Users
|
2022-09-12 00:33:58 +08:00
|
|
|
|
{
|
2023-11-12 17:56:57 +00:00
|
|
|
|
private string inputAccessCode = "";
|
2023-10-16 10:38:27 +01:00
|
|
|
|
private MudForm loginForm = default!;
|
2023-11-12 17:56:57 +00:00
|
|
|
|
private string inputPassword = "";
|
2022-09-12 00:33:58 +08:00
|
|
|
|
private DashboardResponse? response;
|
|
|
|
|
|
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
|
|
|
{
|
|
|
|
|
await base.OnInitializedAsync();
|
|
|
|
|
response = await Client.GetFromJsonAsync<DashboardResponse>("api/Dashboard");
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-11 21:04:11 +00:00
|
|
|
|
private async Task DeleteUser(User user)
|
2022-09-12 00:33:58 +08:00
|
|
|
|
{
|
2023-12-19 16:01:13 +01:00
|
|
|
|
var options = new DialogOptions() { DisableBackdropClick = true };
|
2023-11-12 23:12:54 +00:00
|
|
|
|
if (!LoginService.AllowUserDelete)
|
|
|
|
|
{
|
|
|
|
|
await DialogService.ShowMessageBox(
|
|
|
|
|
"Error",
|
|
|
|
|
"User deletion is disabled by admin.",
|
2023-12-19 16:01:13 +01:00
|
|
|
|
"Ok", null, null, options);
|
2023-11-12 23:12:54 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-09-12 00:33:58 +08:00
|
|
|
|
var parameters = new DialogParameters
|
|
|
|
|
{
|
|
|
|
|
["user"] = user
|
|
|
|
|
};
|
|
|
|
|
|
2023-12-19 16:01:13 +01:00
|
|
|
|
var dialog = DialogService.Show<UserDeleteConfirmDialog>("Delete User", parameters, options);
|
2022-09-12 00:33:58 +08:00
|
|
|
|
var result = await dialog.Result;
|
|
|
|
|
|
2023-10-21 19:33:00 +08:00
|
|
|
|
if (result.Canceled) return;
|
2023-09-09 21:58:20 +09:00
|
|
|
|
|
|
|
|
|
response = await Client.GetFromJsonAsync<DashboardResponse>("api/Dashboard");
|
2023-11-11 21:04:11 +00:00
|
|
|
|
OnLogout();
|
2023-09-09 21:58:20 +09:00
|
|
|
|
}
|
2023-12-19 00:12:48 +00:00
|
|
|
|
|
|
|
|
|
private async Task ResetPassword(User user)
|
|
|
|
|
{
|
2023-12-19 16:01:13 +01:00
|
|
|
|
var options = new DialogOptions() { DisableBackdropClick = true };
|
|
|
|
|
if (LoginService.LoginRequired && !LoginService.IsAdmin)
|
2023-12-19 00:12:48 +00:00
|
|
|
|
{
|
|
|
|
|
await DialogService.ShowMessageBox(
|
|
|
|
|
"Error",
|
|
|
|
|
"Only admin can reset password.",
|
2023-12-19 16:01:13 +01:00
|
|
|
|
"Ok", null , null, options);
|
2023-12-19 00:12:48 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var parameters = new DialogParameters
|
|
|
|
|
{
|
|
|
|
|
["user"] = user
|
|
|
|
|
};
|
|
|
|
|
|
2023-12-19 16:01:13 +01:00
|
|
|
|
var dialog = DialogService.Show<ResetPasswordConfirmDialog>("Reset Password", parameters, options);
|
2023-12-19 00:12:48 +00:00
|
|
|
|
var result = await dialog.Result;
|
|
|
|
|
|
|
|
|
|
if (result.Canceled) return;
|
|
|
|
|
|
|
|
|
|
response = await Client.GetFromJsonAsync<DashboardResponse>("api/Dashboard");
|
|
|
|
|
}
|
2023-10-16 10:38:27 +01:00
|
|
|
|
|
|
|
|
|
private async Task OnLogin()
|
|
|
|
|
{
|
|
|
|
|
if (response != null)
|
2022-09-12 00:33:58 +08:00
|
|
|
|
{
|
2023-11-12 17:56:57 +00:00
|
|
|
|
var result = LoginService.Login(inputAccessCode, inputPassword, response);
|
2023-12-19 16:01:13 +01:00
|
|
|
|
var options = new DialogOptions() { DisableBackdropClick = true };
|
2023-10-16 10:38:27 +01:00
|
|
|
|
switch (result)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
await DialogService.ShowMessageBox(
|
|
|
|
|
"Error",
|
2023-12-19 16:01:13 +01:00
|
|
|
|
"Only admin can log in.",
|
|
|
|
|
"Ok", null, null, options);
|
2023-10-21 19:33:00 +08:00
|
|
|
|
await loginForm.ResetAsync();
|
2023-10-16 10:38:27 +01:00
|
|
|
|
break;
|
|
|
|
|
case 1:
|
2023-11-11 21:04:11 +00:00
|
|
|
|
NavigationManager.NavigateTo("/Users");
|
2023-10-16 10:38:27 +01:00
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
await DialogService.ShowMessageBox(
|
|
|
|
|
"Error",
|
|
|
|
|
"Wrong password!",
|
2023-12-19 16:01:13 +01:00
|
|
|
|
"Ok", null, null, options);
|
2023-10-16 10:38:27 +01:00
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
await DialogService.ShowMessageBox(
|
|
|
|
|
"Error",
|
|
|
|
|
(MarkupString)
|
2023-11-12 17:56:57 +00:00
|
|
|
|
"Access code not found.<br />Please play one game with this access code to register it.",
|
2023-12-19 16:01:13 +01:00
|
|
|
|
"Ok", null, null, options);
|
2023-10-16 10:38:27 +01:00
|
|
|
|
break;
|
|
|
|
|
case 4:
|
|
|
|
|
await DialogService.ShowMessageBox(
|
|
|
|
|
"Error",
|
|
|
|
|
(MarkupString)
|
2023-11-12 17:56:57 +00:00
|
|
|
|
"Access code not registered.<br />Please use register button to create a password first.",
|
2023-12-19 16:01:13 +01:00
|
|
|
|
"Ok", null, null, options);
|
2023-10-16 10:38:27 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
2022-09-12 00:33:58 +08:00
|
|
|
|
}
|
2023-10-16 10:38:27 +01:00
|
|
|
|
}
|
2022-09-12 00:33:58 +08:00
|
|
|
|
|
2023-10-16 10:38:27 +01:00
|
|
|
|
private void OnLogout()
|
|
|
|
|
{
|
|
|
|
|
LoginService.Logout();
|
2023-11-11 21:04:11 +00:00
|
|
|
|
NavigationManager.NavigateTo("/Users");
|
2022-09-12 00:33:58 +08:00
|
|
|
|
}
|
2023-10-16 10:38:27 +01:00
|
|
|
|
|
2023-10-01 08:09:04 -04:00
|
|
|
|
private Task ShowQrCode(User user)
|
|
|
|
|
{
|
|
|
|
|
var parameters = new DialogParameters
|
|
|
|
|
{
|
|
|
|
|
["user"] = user
|
|
|
|
|
};
|
|
|
|
|
|
2023-12-19 16:01:13 +01:00
|
|
|
|
var options = new DialogOptions() { DisableBackdropClick = true };
|
|
|
|
|
DialogService.Show<UserQrCodeDialog>("QR Code", parameters, options);
|
2022-09-12 00:33:58 +08:00
|
|
|
|
|
2023-10-01 08:09:04 -04:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2022-09-12 00:33:58 +08:00
|
|
|
|
}
|