LocalClient.cs 7.9 KB

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