LocalClient.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using InABox.Core;
  2. using InABox.Database;
  3. using InABox.WebSocket.Shared;
  4. namespace InABox.Clients
  5. {
  6. class LocalPusher : IPusher
  7. {
  8. public IEnumerable<Guid> GetUserSessions(Guid userID)
  9. {
  10. if(userID == ClientFactory.UserGuid)
  11. {
  12. return new Guid[] { ClientFactory.SessionID };
  13. }
  14. return Array.Empty<Guid>();
  15. }
  16. public IEnumerable<Guid> GetSessions(Platform platform)
  17. {
  18. if (platform == Platform.Wpf)
  19. {
  20. return new Guid[] { ClientFactory.SessionID };
  21. }
  22. return Array.Empty<Guid>();
  23. }
  24. public void PushToAll<TPush>(TPush push) where TPush : BaseObject
  25. {
  26. ClientFactory.PushHandlers.Push(typeof(TPush), push);
  27. }
  28. public void PushToSession<TPush>(Guid session, TPush push) where TPush : BaseObject
  29. => PushToSession(session, typeof(TPush), push);
  30. public void PushToSession(Guid session, Type TPush, BaseObject push)
  31. {
  32. if (session == ClientFactory.SessionID)
  33. {
  34. ClientFactory.PushHandlers.Push(TPush, push);
  35. }
  36. }
  37. }
  38. public class LocalClient<TEntity> : BaseClient<TEntity> where TEntity : Entity, new()
  39. {
  40. public LocalClient()
  41. {
  42. var pusher = new LocalPusher();
  43. PushManager.AddPusher(pusher);
  44. PushManager.Poll(ClientFactory.SessionID);
  45. }
  46. public override bool IsConnected() => true;
  47. public override IEnumerable<string> SupportedTypes()
  48. {
  49. return DbFactory.SupportedTypes();
  50. }
  51. protected override IValidationData DoValidate(Guid session)
  52. {
  53. return DoValidate(new Filter<User>().None(), session);
  54. }
  55. protected override IValidationData DoValidate(string pin, Guid session)
  56. {
  57. return DoValidate(
  58. new Filter<User>(x => x.PIN).IsEqualTo(pin), session);
  59. }
  60. protected override IValidationData DoValidate(string userid, string password, Guid session)
  61. {
  62. return DoValidate(
  63. new Filter<User>(x => x.UserID).IsEqualTo(userid)
  64. .And(x => x.Password).IsEqualTo(password), session);
  65. }
  66. private IValidationData DoValidate(Filter<User> filter, Guid session = default)
  67. {
  68. var row = DbFactory.FindStore<User>(Guid.Empty, "", ClientFactory.Platform, ClientFactory.Version, Logger.New()).Query(
  69. filter,
  70. Columns.None<User>().Add(x => x.ID, x => x.UserID, x => x.SecurityGroup.ID, x => x.Recipient2FA)
  71. ).Rows.FirstOrDefault();
  72. if (row != null)
  73. {
  74. return new ValidationData(
  75. ValidationStatus.VALID,
  76. row.Get<User, string>(x => x.UserID),
  77. row.Get<User, Guid>(x => x.ID),
  78. row.Get<User, Guid>(x => x.SecurityGroup.ID),
  79. Guid.Empty,
  80. row.Get<User, string?>(x => x.Recipient2FA),
  81. DateTime.MinValue
  82. );
  83. }
  84. return new ValidationData(
  85. ValidationStatus.INVALID,
  86. "",
  87. Guid.Empty,
  88. Guid.Empty,
  89. Guid.Empty,
  90. null,
  91. DateTime.MinValue
  92. );
  93. }
  94. #region Query
  95. protected override CoreTable DoQuery(Filter<TEntity>? filter, Columns<TEntity>? columns, SortOrder<TEntity>? sort = null, CoreRange? range = null )
  96. {
  97. var store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version, Logger.New());
  98. var result = store.Query(filter, columns, sort, range);
  99. return result;
  100. }
  101. #endregion
  102. #region Load
  103. protected override TEntity[] DoLoad(Filter<TEntity>? filter = null, SortOrder<TEntity>? sort = null, CoreRange? range = null)
  104. {
  105. var store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version, Logger.New());
  106. var result = store.Query(filter, null, sort, range).ToObjects<TEntity>().ToArray();
  107. return result;
  108. }
  109. #endregion
  110. #region MultipleTables
  111. protected override Dictionary<string, CoreTable> DoQueryMultiple(Dictionary<string, IQueryDef> queries)
  112. {
  113. return DbFactory.QueryMultiple(queries, ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version, Logger.New());
  114. }
  115. #endregion
  116. #region Save
  117. protected override void DoSave(TEntity entity, string auditnote)
  118. {
  119. var store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version, Logger.New());
  120. store.Save(entity, auditnote);
  121. entity.CommitChanges();
  122. }
  123. protected override void DoSave(IEnumerable<TEntity> entities, string auditnote)
  124. {
  125. var store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version, Logger.New());
  126. store.Save(entities, auditnote);
  127. foreach(var entity in entities)
  128. {
  129. entity.CommitChanges();
  130. }
  131. }
  132. #endregion
  133. #region Delete
  134. protected override void DoDelete(TEntity entity, string auditnote)
  135. {
  136. var store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version, Logger.New());
  137. store.Delete(entity, auditnote);
  138. }
  139. protected override void DoDelete(IEnumerable<TEntity> entities, string auditnote)
  140. {
  141. var store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version, Logger.New());
  142. store.Delete(entities, auditnote);
  143. }
  144. #endregion
  145. #region 2FA
  146. protected override bool DoCheck2FA(string code, Guid? session)
  147. {
  148. return true;
  149. }
  150. #endregion
  151. #region Ping
  152. protected override bool DoPing()
  153. {
  154. return true;
  155. }
  156. public override DatabaseInfo Info()
  157. {
  158. return new DatabaseInfo()
  159. {
  160. Version = CoreUtils.GetVersion(),
  161. ColorScheme = DbFactory.ColorScheme,
  162. Logo = DbFactory.Logo,
  163. DatabaseID = DbFactory.ID
  164. };
  165. }
  166. #endregion
  167. #region Update
  168. public override string Version()
  169. {
  170. return File.ReadAllText(Path.Combine(CoreUtils.GetCommonAppData("PRSServer"), "update/version.txt"));
  171. }
  172. public override string ReleaseNotes()
  173. {
  174. return File.ReadAllText(Path.Combine(CoreUtils.GetCommonAppData("PRSServer"), "update/Release Notes.txt"));
  175. }
  176. public override byte[]? Installer()
  177. {
  178. return File.ReadAllBytes(Path.Combine(CoreUtils.GetCommonAppData("PRSServer"), "update/PRSDesktopSetup.exe"));
  179. }
  180. #endregion
  181. }
  182. }