Sidebar.razor.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using Microsoft.AspNetCore.Components;
  5. using Microsoft.AspNetCore.Components.Authorization;
  6. using Microsoft.AspNetCore.Identity;
  7. using HyperCube.Models;
  8. using Console = HyperCube.Utils.AdvConsole;
  9. namespace HyperCube.Shared
  10. {
  11. public partial class Sidebar : ComponentBase
  12. {
  13. [Inject]
  14. public AuthenticationStateProvider AuthenticationStateProvider { get; set; }
  15. [Inject]
  16. public UserManager<IdentityUser> UserManager { get; set; }
  17. AccountModel account;
  18. List<Role> roles;
  19. //string rr = "";
  20. //roles = account.roles;
  21. protected override async Task OnInitializedAsync()
  22. {
  23. account = await GetCurrentAcc();
  24. if (account != null)
  25. account.RolesChanged += Change;
  26. }
  27. public async Task InitializeAccount()
  28. {
  29. AccountModel.Current = await GetCurrentAcc();
  30. Console.WriteLine("InitializeAccount in Sidebar " + AccountModel.Current.Name);
  31. await Rerender();
  32. }
  33. private async Task<AccountModel> GetCurrentAcc()
  34. {
  35. AccountModel account = new();
  36. var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
  37. var user = authState.User;
  38. if (user.Identity.IsAuthenticated)
  39. {
  40. var currentUser = await UserManager.GetUserAsync(user);
  41. account.UUID = currentUser.Id;
  42. //account.Name = currentUser.UserName;
  43. //account.Email = currentUser.Email;
  44. var acc = AccountModel.Find(account.UUID);
  45. if (acc != null)
  46. account = acc;
  47. ///tmp
  48. //account.AccRole = Role.User;
  49. //account.Name = "test";
  50. return account;
  51. }
  52. return null;
  53. }
  54. private void Change(int count)
  55. {
  56. Console.WriteLine($"Sidebar role changed, count: {count}");
  57. Rerender();
  58. }
  59. private async Task Rerender() { StateHasChanged(); }
  60. }
  61. }