| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.ClientAclModel = void 0;
- const database_1 = require("../config/database");
- class ClientAclModel {
- static async getAll(limit, offset) {
- let query = 'SELECT * FROM client_acl ORDER BY username, priority DESC, created_at DESC';
- const params = [];
- if (limit !== undefined) {
- query += ' LIMIT ?';
- params.push(limit);
- if (offset !== undefined) {
- query += ' OFFSET ?';
- params.push(offset);
- }
- }
- return await (0, database_1.executeQuery)(query, params);
- }
- static async getById(id) {
- const query = 'SELECT * FROM client_acl WHERE id = ?';
- const acls = await (0, database_1.executeQuery)(query, [id]);
- return acls.length > 0 ? acls[0] : null;
- }
- static async getByUsername(username) {
- const query = 'SELECT * FROM client_acl WHERE username = ? ORDER BY priority DESC, created_at DESC';
- return await (0, database_1.executeQuery)(query, [username]);
- }
- static async getByAction(action) {
- const query = 'SELECT * FROM client_acl WHERE action = ? ORDER BY priority DESC, created_at DESC';
- return await (0, database_1.executeQuery)(query, [action]);
- }
- static async getByTopic(topic) {
- const query = 'SELECT * FROM client_acl WHERE topic LIKE ? ORDER BY priority DESC, created_at DESC';
- return await (0, database_1.executeQuery)(query, [`%` + topic + `%`]);
- }
- static async getByPermission(permission) {
- const query = 'SELECT * FROM client_acl WHERE permission = ? ORDER BY priority DESC, created_at DESC';
- return await (0, database_1.executeQuery)(query, [permission]);
- }
- static async getCount() {
- const query = 'SELECT COUNT(*) as count FROM client_acl';
- const result = await (0, database_1.executeQuery)(query);
- return result[0].count;
- }
- static async getActionStats() {
- const query = 'SELECT action, COUNT(*) as count FROM client_acl GROUP BY action';
- return await (0, database_1.executeQuery)(query);
- }
- static async getPermissionStats() {
- const query = 'SELECT permission, COUNT(*) as count FROM client_acl GROUP BY permission';
- return await (0, database_1.executeQuery)(query);
- }
- static async create(aclData) {
- const query = 'INSERT INTO client_acl (clientid, username, topic, action, permission, priority, description) VALUES (?, ?, ?, ?, ?, ?, ?)';
- const values = [
- aclData.clientid || null,
- aclData.username,
- aclData.topic,
- aclData.action,
- aclData.permission,
- aclData.priority || 0,
- aclData.description || null
- ];
- const result = await (0, database_1.executeQuery)(query, values);
- return this.getById(result.insertId);
- }
- static async update(id, updateData) {
- const fields = [];
- const values = [];
- if (updateData.clientid !== undefined) {
- fields.push('clientid = ?');
- values.push(updateData.clientid);
- }
- if (updateData.username !== undefined) {
- fields.push('username = ?');
- values.push(updateData.username);
- }
- if (updateData.topic !== undefined) {
- fields.push('topic = ?');
- values.push(updateData.topic);
- }
- if (updateData.action !== undefined) {
- fields.push('action = ?');
- values.push(updateData.action);
- }
- if (updateData.permission !== undefined) {
- fields.push('permission = ?');
- values.push(updateData.permission);
- }
- if (updateData.priority !== undefined) {
- fields.push('priority = ?');
- values.push(updateData.priority);
- }
- if (updateData.description !== undefined) {
- fields.push('description = ?');
- values.push(updateData.description);
- }
- fields.push('updated_at = NOW()');
- if (fields.length === 0)
- return false;
- values.push(id);
- const query = 'UPDATE client_acl SET ' + fields.join(', ') + ' WHERE id = ?';
- const result = await (0, database_1.executeQuery)(query, values);
- return result.affectedRows > 0;
- }
- static async delete(id) {
- const query = 'DELETE FROM client_acl WHERE id = ?';
- const result = await (0, database_1.executeQuery)(query, [id]);
- return result.affectedRows > 0;
- }
- static async deleteByUsername(username) {
- const query = 'DELETE FROM client_acl WHERE username = ?';
- const result = await (0, database_1.executeQuery)(query, [username]);
- return result.affectedRows > 0;
- }
- static async search(searchTerm, limit, offset) {
- 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';
- const params = [`%` + searchTerm + `%`, `%` + searchTerm + `%`, `%` + searchTerm + `%`, `%` + searchTerm + `%`, `%` + searchTerm + `%`];
- if (limit !== undefined) {
- query += ' LIMIT ?';
- params.push(limit);
- if (offset !== undefined) {
- query += ' OFFSET ?';
- params.push(offset);
- }
- }
- return await (0, database_1.executeQuery)(query, params);
- }
- static async getSearchCount(searchTerm) {
- const query = 'SELECT COUNT(*) as count FROM client_acl WHERE username LIKE ? OR topic LIKE ? OR action LIKE ? OR permission LIKE ? OR description LIKE ?';
- const params = [`%` + searchTerm + `%`, `%` + searchTerm + `%`, `%` + searchTerm + `%`, `%` + searchTerm + `%`, `%` + searchTerm + `%`];
- const result = await (0, database_1.executeQuery)(query, params);
- return result[0].count;
- }
- static async createBatch(aclDataList) {
- if (aclDataList.length === 0)
- return false;
- const values = [];
- const placeholders = [];
- aclDataList.forEach(acl => {
- placeholders.push('(?, ?, ?, ?, ?, ?, ?)');
- values.push(acl.clientid || null, acl.username, acl.topic, acl.action, acl.permission, acl.priority || 0, acl.description || null);
- });
- const query = 'INSERT INTO client_acl (clientid, username, topic, action, permission, priority, description) VALUES ' + placeholders.join(', ');
- const result = await (0, database_1.executeQuery)(query, values);
- return result.affectedRows > 0;
- }
- static async copyToUser(sourceUsername, targetUsername) {
- const sourceAcls = await this.getByUsername(sourceUsername);
- if (sourceAcls.length === 0)
- return false;
- const targetAcls = sourceAcls.map(acl => ({
- clientid: acl.clientid,
- username: targetUsername,
- topic: acl.topic,
- action: acl.action,
- permission: acl.permission,
- priority: acl.priority,
- description: acl.description
- }));
- return await this.createBatch(targetAcls);
- }
- static async deleteMultiple(ids) {
- if (ids.length === 0)
- return false;
- const placeholders = ids.map(() => '?').join(',');
- const query = 'DELETE FROM client_acl WHERE id IN (' + placeholders + ')';
- const result = await (0, database_1.executeQuery)(query, ids);
- return result.affectedRows > 0;
- }
- static async getByUsernameAndAction(username, action) {
- const query = 'SELECT * FROM client_acl WHERE username = ? AND action = ? ORDER BY priority DESC, created_at DESC';
- return await (0, database_1.executeQuery)(query, [username, action]);
- }
- static async checkPermission(username, topic, action) {
- const query = "SELECT * FROM client_acl WHERE username = ? AND action IN (?, 'pubsub') ORDER BY priority DESC";
- const acls = await (0, database_1.executeQuery)(query, [username, action]);
- if (acls.length === 0)
- return false;
- for (const acl of acls) {
- const topicPattern = acl.topic.replace('%', '*');
- const regex = new RegExp(topicPattern.replace(/\*/g, '.*'));
- if (regex.test(topic))
- return acl.permission === 'allow';
- }
- return false;
- }
- }
- exports.ClientAclModel = ClientAclModel;
- //# sourceMappingURL=clientAcl.js.map
|