Sidebar.razor.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.Pages;
  8. using HyperCube.Models;
  9. using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
  10. namespace HyperCube.Shared
  11. {
  12. public partial class Sidebar : ComponentBase
  13. {
  14. [Inject]
  15. ProtectedSessionStorage storage { get; set; }
  16. [Inject]
  17. NavigationManager NavigationManager { get; set; }
  18. [Inject]
  19. AuthenticationStateProvider AuthenticationStateProvider { get; set; }
  20. [Inject]
  21. UserManager<IdentityUser> UserManager { get; set; }
  22. ModalProfile modalProfile { get; set; }
  23. ModalFiles modalFiles { get; set; }
  24. ModalCompetency modalCompetency { get; set; }
  25. ModalRating modalRating { get; set; }
  26. ModalRules modalRules { get; set; }
  27. ModalAssets modalAssets { get; set; }
  28. ModalError modalError404 { get; set; }
  29. ModalLoading modalLoading { get; set; }
  30. AccountModel _currentAccount;
  31. protected override async Task OnInitializedAsync()
  32. {
  33. var acc = await GetCurrentAcc();
  34. Console.WriteLine($"sidebar myFlag set1 {acc.Name}");
  35. //AppData.CurrentAccount = acc;
  36. await storage.SetAsync("acc", acc.Email);
  37. var myFlag = await storage.GetAsync<string>("acc");
  38. Console.WriteLine($"sidebar myFlag set {myFlag.Value}");
  39. await storage.SetAsync("completed", true);
  40. }
  41. void LogoClick()
  42. {
  43. NavigationManager.NavigateTo("");
  44. }
  45. async Task ProfileClick()
  46. {
  47. //AccountModel account = new() { Name = "[SomeUserName]", UUID = "[SomeUserID]" };
  48. AccountModel account = await GetCurrentAcc();
  49. account.LoadRoles();
  50. modalProfile.Open(account);
  51. }
  52. void FilesClick()
  53. {
  54. modalFiles.Open();
  55. }
  56. void CompetencyClick()
  57. {
  58. modalCompetency.Open();
  59. }
  60. void RatingClick()
  61. {
  62. modalRating.Open();
  63. }
  64. void RulesClick()
  65. {
  66. modalRules.Open();
  67. }
  68. void AssetsClick()
  69. {
  70. modalAssets.Open();
  71. }
  72. void ExitClick()
  73. {
  74. NavigationManager.NavigateTo("Identity/Account/Logout", true);
  75. }
  76. void ErrorClick()
  77. {
  78. modalError404.Open();
  79. }
  80. async Task<AccountModel> GetCurrentAcc()
  81. {
  82. if (_currentAccount == null)
  83. {
  84. var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
  85. var user = authState.User;
  86. if (user.Identity.IsAuthenticated)
  87. {
  88. var currentUser = await UserManager.GetUserAsync(user);
  89. var acc = AccountModel.Find(currentUser.Id);
  90. if (acc != null)
  91. {
  92. _currentAccount = AccountModel.Loaded[currentUser.Id];
  93. }
  94. else
  95. {
  96. Dictionary<string, AccountModel> accounts = await MySQLConnector.Instance().SQLSelectASPUsers();
  97. if (accounts.ContainsKey(currentUser.Id))
  98. _currentAccount = accounts[currentUser.Id];
  99. }
  100. }
  101. }
  102. return _currentAccount;
  103. }
  104. }
  105. }