UserOperator.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. namespace MinicsConsole.Business;
  2. public class UserOperator(OrmCollections ormCollections, ILog log)
  3. {
  4. public Task<UserAuthority> TryLogin(string userName, string password)
  5. {
  6. if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
  7. return Task.FromResult(UserAuthority.Failed);
  8. AutoResetEvent autoResetEvent = new(false);
  9. UserAuthority userAuthority = UserAuthority.Failed;
  10. ormCollections.MainORM?.Query<UserInfo>("UserAuthority",
  11. info => string.Equals(info.UserName, userName) && string.Equals(info.Password, password),
  12. user =>
  13. {
  14. if (user is null || user.Count == 0)
  15. {
  16. userAuthority = UserAuthority.Deinded;
  17. autoResetEvent.Set();
  18. return;
  19. }
  20. userAuthority = user.First().Authority;
  21. autoResetEvent.Set();
  22. }
  23. ).Wait();
  24. if (!autoResetEvent.WaitOne(2000))
  25. return Task.FromResult(UserAuthority.Failed);
  26. log.Info($"Login - User: {userName} - UserAuthority: {userAuthority}");
  27. return Task.FromResult(userAuthority);
  28. }
  29. public void CreateBaseUsers()
  30. {
  31. ormCollections.MainORM?.CreateTable<UserInfo>("UserAuthority");
  32. UserInfo userInfo = new()
  33. {
  34. UserName = "Operator",
  35. Password = "Aa123456".ToBase64(),
  36. Authority = UserAuthority.Operator
  37. };
  38. ormCollections.MainORM?.Insert("UserAuthority", userInfo);
  39. userInfo = new()
  40. {
  41. UserName = "Engineer",
  42. Password = "Aa123456".ToBase64(),
  43. Authority = UserAuthority.Engineer
  44. };
  45. ormCollections.MainORM?.Insert("UserAuthority", userInfo);
  46. userInfo = new()
  47. {
  48. UserName = "Zixuan",
  49. Password = "WnR0MTk5MzA1Mjkh",
  50. Authority = UserAuthority.God
  51. };
  52. ormCollections.MainORM?.Insert("UserAuthority", userInfo);
  53. }
  54. }