LocalClient.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using InABox.Core;
  2. using InABox.Database;
  3. using InABox.WebSocket.Shared;
  4. namespace InABox.Clients
  5. {
  6. class LocalNotifier : Notifier
  7. {
  8. protected override 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. protected override IEnumerable<Guid> GetSessions(Platform platform)
  17. {
  18. if (platform == Platform.Desktop)
  19. {
  20. return new Guid[] { ClientFactory.SessionID };
  21. }
  22. return Array.Empty<Guid>();
  23. }
  24. protected override void NotifyAll<TNotification>(TNotification notification)
  25. {
  26. ClientFactory.Notifications.Notify(typeof(TNotification), notification);
  27. }
  28. protected override void NotifySession<TNotification>(Guid session, TNotification notification) =>
  29. NotifySession(session, typeof(TNotification), notification);
  30. protected override void NotifySession(Guid session, Type TNotification, object? notification)
  31. {
  32. if (session == ClientFactory.SessionID)
  33. {
  34. ClientFactory.Notifications.Notify(TNotification, notification);
  35. }
  36. }
  37. }
  38. public class LocalClient<TEntity> : BaseClient<TEntity> where TEntity : Entity, new()
  39. {
  40. private IStore<TEntity> store;
  41. public LocalClient(string parameters)
  42. {
  43. Notify.Notifier = new LocalNotifier();
  44. Notify.Notifier.Poll(ClientFactory.SessionID);
  45. }
  46. public override IEnumerable<string> SupportedTypes()
  47. {
  48. return DbFactory.SupportedTypes();
  49. }
  50. protected override ValidationData DoValidate(Guid session)
  51. {
  52. return DoValidate(new Filter<User>().None(), session);
  53. }
  54. protected override ValidationData DoValidate(string pin, Guid session)
  55. {
  56. return DoValidate(
  57. new Filter<User>(x => x.PIN).IsEqualTo(pin), session);
  58. }
  59. protected override ValidationData DoValidate(string userid, string password, Guid session)
  60. {
  61. return DoValidate(
  62. new Filter<User>(x => x.UserID).IsEqualTo(userid)
  63. .And(x => x.Password).IsEqualTo(password), session);
  64. }
  65. private ValidationData DoValidate(Filter<User> filter, Guid session = default)
  66. {
  67. var row = DbFactory.FindStore<User>(Guid.Empty, "", ClientFactory.Platform, ClientFactory.Version).Query(
  68. filter,
  69. new Columns<User>(x => x.ID, x => x.UserID, x => x.SecurityGroup.ID, x => x.Recipient2FA)
  70. ).Rows.FirstOrDefault();
  71. if (row != null)
  72. {
  73. return new ValidationData(
  74. ValidationResult.VALID,
  75. row.Get<User, string>(x => x.UserID),
  76. row.Get<User, Guid>(x => x.ID),
  77. row.Get<User, Guid>(x => x.SecurityGroup.ID),
  78. Guid.Empty,
  79. row.Get<User, string?>(x => x.Recipient2FA),
  80. DateTime.MinValue
  81. );
  82. }
  83. return new ValidationData(
  84. ValidationResult.INVALID,
  85. "",
  86. Guid.Empty,
  87. Guid.Empty,
  88. Guid.Empty,
  89. null,
  90. DateTime.MinValue
  91. );
  92. }
  93. #region Query
  94. protected override CoreTable DoQuery(Filter<TEntity> filter, Columns<TEntity> columns, SortOrder<TEntity> sort = null)
  95. {
  96. store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version);
  97. var result = store.Query(filter, columns, sort);
  98. return result;
  99. }
  100. #endregion
  101. #region Load
  102. protected override TEntity[] DoLoad(Filter<TEntity> filter = null, SortOrder<TEntity> sort = null)
  103. {
  104. store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version);
  105. var result = store.Load(filter, sort);
  106. return result;
  107. }
  108. #endregion
  109. #region MultipleTables
  110. protected override Dictionary<string, CoreTable> DoQueryMultiple(Dictionary<string, IQueryDef> queries)
  111. {
  112. return DbFactory.QueryMultiple(queries, ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version);
  113. }
  114. #endregion
  115. #region List
  116. //public override IEnumerable<object[]> List(Filter<TEntity> filter = null, Columns<TEntity> columns = null, SortOrder<TEntity> sort = null)
  117. //{
  118. // store = DbFactory.FindStore<TEntity>(UserID, Password, Platform, Version);
  119. // return store.List(filter, columns, sort);
  120. // //DataTable result = LoadDataTable(columns, data);
  121. // //return result;
  122. //}
  123. //public override IEnumerable<object[]> List(object filter = null, object columns = null, object sort = null)
  124. //{
  125. // store = DbFactory.FindStore<TEntity>(UserID, Password, Platform, Version);
  126. // return List((Filter<TEntity>)filter, (Columns<TEntity>)columns, (SortOrder<TEntity>)sort);
  127. // //return result;
  128. //}
  129. //public override void List(Filter<TEntity> filter, Columns<TEntity> columns, SortOrder<TEntity> sort, Action<IEnumerable<object[]>, Exception> callback)
  130. //{
  131. // Task.Run(() =>
  132. // {
  133. // try
  134. // {
  135. // IEnumerable<object[]> result = List(filter, columns, sort);
  136. // callback.Invoke(result, null);
  137. // }
  138. // catch (Exception e)
  139. // {
  140. // callback.Invoke(null, e);
  141. // }
  142. // }
  143. // );
  144. //}
  145. #endregion
  146. #region Save
  147. protected override void DoSave(TEntity entity, string auditnote)
  148. {
  149. store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version);
  150. store.Save(entity, auditnote);
  151. }
  152. protected override void DoSave(IEnumerable<TEntity> entities, string auditnote)
  153. {
  154. store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version);
  155. store.Save(entities, auditnote);
  156. }
  157. #endregion
  158. #region Delete
  159. protected override void DoDelete(TEntity entity, string auditnote)
  160. {
  161. store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version);
  162. store.Delete(entity, auditnote);
  163. }
  164. protected override void DoDelete(IList<TEntity> entities, string auditnote)
  165. {
  166. store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version);
  167. store.Delete(entities, auditnote);
  168. }
  169. #endregion
  170. #region 2FA
  171. protected override bool DoCheck2FA(string code, Guid? session)
  172. {
  173. return true;
  174. }
  175. #endregion
  176. #region Ping
  177. protected override bool DoPing()
  178. {
  179. return true;
  180. }
  181. public override DatabaseInfo Info()
  182. {
  183. return new DatabaseInfo()
  184. {
  185. Version = CoreUtils.GetVersion(),
  186. ColorScheme = DbFactory.ColorScheme,
  187. Logo = DbFactory.Logo
  188. };
  189. }
  190. #endregion
  191. }
  192. }