LocalClient.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. protected override IValidationData DoValidate(Guid session)
  48. {
  49. return DoValidate(new Filter<User>().None(), session);
  50. }
  51. protected override IValidationData DoValidate(string pin, Guid session)
  52. {
  53. return DoValidate(
  54. new Filter<User>(x => x.PIN).IsEqualTo(pin), session);
  55. }
  56. protected override IValidationData DoValidate(string userid, string password, Guid session)
  57. {
  58. return DoValidate(
  59. new Filter<User>(x => x.UserID).IsEqualTo(userid)
  60. .And(x => x.Password).IsEqualTo(password), session);
  61. }
  62. private IValidationData DoValidate(Filter<User> filter, Guid session = default)
  63. {
  64. var row = DbFactory.FindStore<User>(Guid.Empty, "", ClientFactory.Platform, ClientFactory.Version).Query(
  65. filter,
  66. new Columns<User>(x => x.ID, x => x.UserID, x => x.SecurityGroup.ID, x => x.Recipient2FA)
  67. ).Rows.FirstOrDefault();
  68. if (row != null)
  69. {
  70. return new ValidationData(
  71. ValidationStatus.VALID,
  72. row.Get<User, string>(x => x.UserID),
  73. row.Get<User, Guid>(x => x.ID),
  74. row.Get<User, Guid>(x => x.SecurityGroup.ID),
  75. Guid.Empty,
  76. row.Get<User, string?>(x => x.Recipient2FA),
  77. DateTime.MinValue
  78. );
  79. }
  80. return new ValidationData(
  81. ValidationStatus.INVALID,
  82. "",
  83. Guid.Empty,
  84. Guid.Empty,
  85. Guid.Empty,
  86. null,
  87. DateTime.MinValue
  88. );
  89. }
  90. #region Query
  91. protected override CoreTable DoQuery(Filter<TEntity> filter, Columns<TEntity> columns, SortOrder<TEntity> sort = null)
  92. {
  93. var store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version);
  94. var result = store.Query(filter, columns, sort);
  95. return result;
  96. }
  97. #endregion
  98. #region Load
  99. protected override TEntity[] DoLoad(Filter<TEntity>? filter = null, SortOrder<TEntity>? sort = null)
  100. {
  101. var store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version);
  102. var result = store.Query(filter, null, sort).ToObjects<TEntity>().ToArray();
  103. return result;
  104. }
  105. #endregion
  106. #region MultipleTables
  107. protected override Dictionary<string, CoreTable> DoQueryMultiple(Dictionary<string, IQueryDef> queries)
  108. {
  109. return DbFactory.QueryMultiple(queries, ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version);
  110. }
  111. #endregion
  112. #region List
  113. //public override IEnumerable<object[]> List(Filter<TEntity> filter = null, Columns<TEntity> columns = null, SortOrder<TEntity> sort = null)
  114. //{
  115. // store = DbFactory.FindStore<TEntity>(UserID, Password, Platform, Version);
  116. // return store.List(filter, columns, sort);
  117. // //DataTable result = LoadDataTable(columns, data);
  118. // //return result;
  119. //}
  120. //public override IEnumerable<object[]> List(object filter = null, object columns = null, object sort = null)
  121. //{
  122. // store = DbFactory.FindStore<TEntity>(UserID, Password, Platform, Version);
  123. // return List((Filter<TEntity>)filter, (Columns<TEntity>)columns, (SortOrder<TEntity>)sort);
  124. // //return result;
  125. //}
  126. //public override void List(Filter<TEntity> filter, Columns<TEntity> columns, SortOrder<TEntity> sort, Action<IEnumerable<object[]>, Exception> callback)
  127. //{
  128. // Task.Run(() =>
  129. // {
  130. // try
  131. // {
  132. // IEnumerable<object[]> result = List(filter, columns, sort);
  133. // callback.Invoke(result, null);
  134. // }
  135. // catch (Exception e)
  136. // {
  137. // callback.Invoke(null, e);
  138. // }
  139. // }
  140. // );
  141. //}
  142. #endregion
  143. #region Save
  144. protected override void DoSave(TEntity entity, string auditnote)
  145. {
  146. var store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version);
  147. store.Save(entity, auditnote);
  148. entity.CommitChanges();
  149. }
  150. protected override void DoSave(IEnumerable<TEntity> entities, string auditnote)
  151. {
  152. var store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version);
  153. store.Save(entities, auditnote);
  154. foreach(var entity in entities)
  155. {
  156. entity.CommitChanges();
  157. }
  158. }
  159. #endregion
  160. #region Delete
  161. protected override void DoDelete(TEntity entity, string auditnote)
  162. {
  163. var store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version);
  164. store.Delete(entity, auditnote);
  165. }
  166. protected override void DoDelete(IEnumerable<TEntity> entities, string auditnote)
  167. {
  168. var store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version);
  169. store.Delete(entities, auditnote);
  170. }
  171. #endregion
  172. #region 2FA
  173. protected override bool DoCheck2FA(string code, Guid? session)
  174. {
  175. return true;
  176. }
  177. #endregion
  178. #region Ping
  179. protected override bool DoPing()
  180. {
  181. return true;
  182. }
  183. public override DatabaseInfo Info()
  184. {
  185. return new DatabaseInfo()
  186. {
  187. Version = CoreUtils.GetVersion(),
  188. ColorScheme = DbFactory.ColorScheme,
  189. Logo = DbFactory.Logo,
  190. DatabaseID = DbFactory.ID
  191. };
  192. }
  193. #endregion
  194. #region Update
  195. public override string Version()
  196. {
  197. return File.ReadAllText(Path.Combine(CoreUtils.GetCommonAppData("PRSServer"), "update/version.txt"));
  198. }
  199. public override string ReleaseNotes()
  200. {
  201. return File.ReadAllText(Path.Combine(CoreUtils.GetCommonAppData("PRSServer"), "update/Release Notes.txt"));
  202. }
  203. public override byte[]? Installer()
  204. {
  205. return File.ReadAllBytes(Path.Combine(CoreUtils.GetCommonAppData("PRSServer"), "update/PRSDesktopSetup.exe"));
  206. }
  207. #endregion
  208. }
  209. }