clientAcl.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ClientAclModel = void 0;
  4. const database_1 = require("../config/database");
  5. class ClientAclModel {
  6. static async getAll(limit, offset) {
  7. let query = 'SELECT * FROM client_acl ORDER BY username, priority DESC, created_at DESC';
  8. const params = [];
  9. if (limit !== undefined) {
  10. query += ' LIMIT ?';
  11. params.push(limit);
  12. if (offset !== undefined) {
  13. query += ' OFFSET ?';
  14. params.push(offset);
  15. }
  16. }
  17. return await (0, database_1.executeQuery)(query, params);
  18. }
  19. static async getById(id) {
  20. const query = 'SELECT * FROM client_acl WHERE id = ?';
  21. const acls = await (0, database_1.executeQuery)(query, [id]);
  22. return acls.length > 0 ? acls[0] : null;
  23. }
  24. static async getByUsername(username) {
  25. const query = 'SELECT * FROM client_acl WHERE username = ? ORDER BY priority DESC, created_at DESC';
  26. return await (0, database_1.executeQuery)(query, [username]);
  27. }
  28. static async getByAction(action) {
  29. const query = 'SELECT * FROM client_acl WHERE action = ? ORDER BY priority DESC, created_at DESC';
  30. return await (0, database_1.executeQuery)(query, [action]);
  31. }
  32. static async getByTopic(topic) {
  33. const query = 'SELECT * FROM client_acl WHERE topic LIKE ? ORDER BY priority DESC, created_at DESC';
  34. return await (0, database_1.executeQuery)(query, [`%` + topic + `%`]);
  35. }
  36. static async getByPermission(permission) {
  37. const query = 'SELECT * FROM client_acl WHERE permission = ? ORDER BY priority DESC, created_at DESC';
  38. return await (0, database_1.executeQuery)(query, [permission]);
  39. }
  40. static async getCount() {
  41. const query = 'SELECT COUNT(*) as count FROM client_acl';
  42. const result = await (0, database_1.executeQuery)(query);
  43. return result[0].count;
  44. }
  45. static async getActionStats() {
  46. const query = 'SELECT action, COUNT(*) as count FROM client_acl GROUP BY action';
  47. return await (0, database_1.executeQuery)(query);
  48. }
  49. static async getPermissionStats() {
  50. const query = 'SELECT permission, COUNT(*) as count FROM client_acl GROUP BY permission';
  51. return await (0, database_1.executeQuery)(query);
  52. }
  53. static async create(aclData) {
  54. const query = 'INSERT INTO client_acl (clientid, username, topic, action, permission, priority, description) VALUES (?, ?, ?, ?, ?, ?, ?)';
  55. const values = [
  56. aclData.clientid || null,
  57. aclData.username,
  58. aclData.topic,
  59. aclData.action,
  60. aclData.permission,
  61. aclData.priority || 0,
  62. aclData.description || null
  63. ];
  64. const result = await (0, database_1.executeQuery)(query, values);
  65. return this.getById(result.insertId);
  66. }
  67. static async update(id, updateData) {
  68. const fields = [];
  69. const values = [];
  70. if (updateData.clientid !== undefined) {
  71. fields.push('clientid = ?');
  72. values.push(updateData.clientid);
  73. }
  74. if (updateData.username !== undefined) {
  75. fields.push('username = ?');
  76. values.push(updateData.username);
  77. }
  78. if (updateData.topic !== undefined) {
  79. fields.push('topic = ?');
  80. values.push(updateData.topic);
  81. }
  82. if (updateData.action !== undefined) {
  83. fields.push('action = ?');
  84. values.push(updateData.action);
  85. }
  86. if (updateData.permission !== undefined) {
  87. fields.push('permission = ?');
  88. values.push(updateData.permission);
  89. }
  90. if (updateData.priority !== undefined) {
  91. fields.push('priority = ?');
  92. values.push(updateData.priority);
  93. }
  94. if (updateData.description !== undefined) {
  95. fields.push('description = ?');
  96. values.push(updateData.description);
  97. }
  98. fields.push('updated_at = NOW()');
  99. if (fields.length === 0)
  100. return false;
  101. values.push(id);
  102. const query = 'UPDATE client_acl SET ' + fields.join(', ') + ' WHERE id = ?';
  103. const result = await (0, database_1.executeQuery)(query, values);
  104. return result.affectedRows > 0;
  105. }
  106. static async delete(id) {
  107. const query = 'DELETE FROM client_acl WHERE id = ?';
  108. const result = await (0, database_1.executeQuery)(query, [id]);
  109. return result.affectedRows > 0;
  110. }
  111. static async deleteByUsername(username) {
  112. const query = 'DELETE FROM client_acl WHERE username = ?';
  113. const result = await (0, database_1.executeQuery)(query, [username]);
  114. return result.affectedRows > 0;
  115. }
  116. static async search(searchTerm, limit, offset) {
  117. let query = 'SELECT * FROM client_acl WHERE username LIKE ? OR topic LIKE ? OR action LIKE ? OR permission LIKE ? OR description LIKE ? ORDER BY username, priority DESC, created_at DESC';
  118. const params = [`%` + searchTerm + `%`, `%` + searchTerm + `%`, `%` + searchTerm + `%`, `%` + searchTerm + `%`, `%` + searchTerm + `%`];
  119. if (limit !== undefined) {
  120. query += ' LIMIT ?';
  121. params.push(limit);
  122. if (offset !== undefined) {
  123. query += ' OFFSET ?';
  124. params.push(offset);
  125. }
  126. }
  127. return await (0, database_1.executeQuery)(query, params);
  128. }
  129. static async getSearchCount(searchTerm) {
  130. const query = 'SELECT COUNT(*) as count FROM client_acl WHERE username LIKE ? OR topic LIKE ? OR action LIKE ? OR permission LIKE ? OR description LIKE ?';
  131. const params = [`%` + searchTerm + `%`, `%` + searchTerm + `%`, `%` + searchTerm + `%`, `%` + searchTerm + `%`, `%` + searchTerm + `%`];
  132. const result = await (0, database_1.executeQuery)(query, params);
  133. return result[0].count;
  134. }
  135. static async createBatch(aclDataList) {
  136. if (aclDataList.length === 0)
  137. return false;
  138. const values = [];
  139. const placeholders = [];
  140. aclDataList.forEach(acl => {
  141. placeholders.push('(?, ?, ?, ?, ?, ?, ?)');
  142. values.push(acl.clientid || null, acl.username, acl.topic, acl.action, acl.permission, acl.priority || 0, acl.description || null);
  143. });
  144. const query = 'INSERT INTO client_acl (clientid, username, topic, action, permission, priority, description) VALUES ' + placeholders.join(', ');
  145. const result = await (0, database_1.executeQuery)(query, values);
  146. return result.affectedRows > 0;
  147. }
  148. static async copyToUser(sourceUsername, targetUsername) {
  149. const sourceAcls = await this.getByUsername(sourceUsername);
  150. if (sourceAcls.length === 0)
  151. return false;
  152. const targetAcls = sourceAcls.map(acl => ({
  153. clientid: acl.clientid,
  154. username: targetUsername,
  155. topic: acl.topic,
  156. action: acl.action,
  157. permission: acl.permission,
  158. priority: acl.priority,
  159. description: acl.description
  160. }));
  161. return await this.createBatch(targetAcls);
  162. }
  163. static async deleteMultiple(ids) {
  164. if (ids.length === 0)
  165. return false;
  166. const placeholders = ids.map(() => '?').join(',');
  167. const query = 'DELETE FROM client_acl WHERE id IN (' + placeholders + ')';
  168. const result = await (0, database_1.executeQuery)(query, ids);
  169. return result.affectedRows > 0;
  170. }
  171. static async getByUsernameAndAction(username, action) {
  172. const query = 'SELECT * FROM client_acl WHERE username = ? AND action = ? ORDER BY priority DESC, created_at DESC';
  173. return await (0, database_1.executeQuery)(query, [username, action]);
  174. }
  175. static async checkPermission(username, topic, action) {
  176. const query = "SELECT * FROM client_acl WHERE username = ? AND action IN (?, 'pubsub') ORDER BY priority DESC";
  177. const acls = await (0, database_1.executeQuery)(query, [username, action]);
  178. if (acls.length === 0)
  179. return false;
  180. for (const acl of acls) {
  181. const topicPattern = acl.topic.replace('%', '*');
  182. const regex = new RegExp(topicPattern.replace(/\*/g, '.*'));
  183. if (regex.test(topic))
  184. return acl.permission === 'allow';
  185. }
  186. return false;
  187. }
  188. }
  189. exports.ClientAclModel = ClientAclModel;
  190. //# sourceMappingURL=clientAcl.js.map