Wallet.razor.cs 3.7 KB

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