12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Components;
- using Microsoft.AspNetCore.Components.Authorization;
- using Microsoft.AspNetCore.Identity;
- using HyperCube.Models;
- using Console = HyperCube.Utils.AdvConsole;
- namespace HyperCube.Shared
- {
- public partial class Sidebar : ComponentBase
- {
- [Inject]
- public AuthenticationStateProvider AuthenticationStateProvider { get; set; }
- [Inject]
- public UserManager<IdentityUser> UserManager { get; set; }
- AccountModel account;
- List<Role> roles;
- //string rr = "";
- //roles = account.roles;
- protected override async Task OnInitializedAsync()
- {
- account = await GetCurrentAcc();
- if (account != null)
- account.RolesChanged += Change;
- }
- public async Task InitializeAccount()
- {
- AccountModel.Current = await GetCurrentAcc();
- Console.WriteLine("InitializeAccount in Sidebar " + AccountModel.Current.Name);
- await Rerender();
- }
- private async Task<AccountModel> GetCurrentAcc()
- {
- AccountModel account = new();
- var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
- var user = authState.User;
- if (user.Identity.IsAuthenticated)
- {
- var currentUser = await UserManager.GetUserAsync(user);
- account.UUID = currentUser.Id;
- //account.Name = currentUser.UserName;
- //account.Email = currentUser.Email;
- var acc = AccountModel.Find(account.UUID);
- if (acc != null)
- account = acc;
- ///tmp
- //account.AccRole = Role.User;
- //account.Name = "test";
- return account;
- }
- return null;
- }
- private void Change(int count)
- {
- Console.WriteLine($"Sidebar role changed, count: {count}");
- Rerender();
- }
- private async Task Rerender() { StateHasChanged(); }
- }
- }
|