Wallet.razor.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System.Threading.Tasks;
  2. using HyperCube.Models;
  3. using Microsoft.AspNetCore.Components;
  4. namespace HyperCube.Pages
  5. {
  6. public partial class Wallet
  7. {
  8. [Inject]
  9. AppData AppData { get; set; }
  10. private AccountModel account = new();
  11. string myBalance = "";
  12. string address = "";
  13. protected override async Task OnInitializedAsync()
  14. {
  15. account = AppData.CurrentAccount;
  16. var bc = await account.GetSelectedBlockChain();
  17. address = account.GetActualAddress(bc);
  18. if (bc != null)
  19. myBalance = await account.GetBalance();
  20. }
  21. private async Task<AccountModel> GetCurrentAcc()
  22. {
  23. AccountModel account = new();
  24. var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
  25. var user = authState.User;
  26. if (user.Identity.IsAuthenticated)
  27. {
  28. var currentUser = await UserManager.GetUserAsync(user);
  29. var acc = AccountModel.Find(currentUser.Id);
  30. if (acc != null)
  31. account = acc;
  32. account.LoadRoles();
  33. return account;
  34. }
  35. return null;
  36. }
  37. bool admin
  38. {
  39. get { return account.Roles.Contains(Role.Admin); }
  40. set
  41. {
  42. if (!account.Roles.Contains(Role.Admin))
  43. {
  44. account.AddRole(Role.Admin);
  45. InsertRole(Role.Admin);
  46. }
  47. else
  48. {
  49. account.RemoveRole(Role.Admin);
  50. DeleteRole(Role.Admin);
  51. }
  52. }
  53. }
  54. bool initiator
  55. {
  56. get { return account.Roles.Contains(Role.Miner); }
  57. set
  58. {
  59. if (!account.Roles.Contains(Role.Miner))
  60. {
  61. account.AddRole(Role.Miner);
  62. InsertRole(Role.Miner);
  63. }
  64. else
  65. {
  66. account.RemoveRole(Role.Miner);
  67. DeleteRole(Role.Miner);
  68. }
  69. }
  70. }
  71. bool verifier
  72. {
  73. get { return account.Roles.Contains(Role.Expert); }
  74. set
  75. {
  76. if (!account.Roles.Contains(Role.Expert))
  77. {
  78. account.AddRole(Role.Expert);
  79. InsertRole(Role.Expert);
  80. }
  81. else
  82. {
  83. account.RemoveRole(Role.Expert);
  84. DeleteRole(Role.Expert);
  85. }
  86. }
  87. }
  88. bool requester
  89. {
  90. get { return account.Roles.Contains(Role.Customer); }
  91. set
  92. {
  93. if (!account.Roles.Contains(Role.Customer))
  94. {
  95. account.AddRole(Role.Customer);
  96. InsertRole(Role.Customer);
  97. }
  98. else
  99. {
  100. account.RemoveRole(Role.Customer);
  101. DeleteRole(Role.Customer);
  102. }
  103. }
  104. }
  105. void InsertRole(Role role)
  106. {
  107. MySQLConnector.Instance().SQLInsert($"insert into account_roles (account_uuid, role_id) values ('{account.UUID}', {(int)role})");
  108. }
  109. void DeleteRole(Role role)
  110. {
  111. MySQLConnector.Instance().SQLInsert($"delete from account_roles where account_uuid = '{account.UUID}' and role_id={(int)role}");
  112. }
  113. }
  114. }