clientAclController.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ClientAclController = void 0;
  4. const clientAcl_1 = require("../models/clientAcl");
  5. const helpers_1 = require("../utils/helpers");
  6. class ClientAclController {
  7. static async getAllClientAcl(req, res) {
  8. try {
  9. const page = Number(req.query.page) || 1;
  10. const limit = Number(req.query.limit) || 20;
  11. const offset = (page - 1) * limit;
  12. const clientAcls = await clientAcl_1.ClientAclModel.getAll(limit, offset);
  13. const total = await clientAcl_1.ClientAclModel.getCount();
  14. res.status(200).json({
  15. success: true,
  16. data: clientAcls,
  17. pagination: {
  18. page,
  19. limit,
  20. total,
  21. pages: Math.ceil(total / limit)
  22. }
  23. });
  24. }
  25. catch (error) {
  26. console.error('获取客户端授权规则列表失败:', error);
  27. res.status(500).json({
  28. success: false,
  29. message: '获取客户端授权规则列表失败',
  30. error: error instanceof Error ? error.message : '未知错误'
  31. });
  32. }
  33. }
  34. static async getClientAclById(req, res) {
  35. try {
  36. const { id } = req.params;
  37. if (!id || isNaN(Number(id))) {
  38. res.status(400).json({
  39. success: false,
  40. message: '无效的ID'
  41. });
  42. return;
  43. }
  44. const clientAcl = await clientAcl_1.ClientAclModel.getById(Number(id));
  45. if (!clientAcl) {
  46. res.status(404).json({
  47. success: false,
  48. message: '客户端授权规则不存在'
  49. });
  50. return;
  51. }
  52. res.status(200).json({
  53. success: true,
  54. data: clientAcl
  55. });
  56. }
  57. catch (error) {
  58. console.error('获取客户端授权规则失败:', error);
  59. res.status(500).json({
  60. success: false,
  61. message: '获取客户端授权规则失败',
  62. error: error instanceof Error ? error.message : '未知错误'
  63. });
  64. }
  65. }
  66. static async getClientAclByUsername(req, res) {
  67. try {
  68. const { username } = req.params;
  69. const usernameStr = (0, helpers_1.toString)(username);
  70. if (!usernameStr) {
  71. res.status(400).json({
  72. success: false,
  73. message: '用户名不能为空'
  74. });
  75. return;
  76. }
  77. const clientAcls = await clientAcl_1.ClientAclModel.getByUsername(usernameStr);
  78. res.status(200).json({
  79. success: true,
  80. data: clientAcls
  81. });
  82. }
  83. catch (error) {
  84. console.error('根据用户名获取授权规则失败:', error);
  85. res.status(500).json({
  86. success: false,
  87. message: '根据用户名获取授权规则失败',
  88. error: error instanceof Error ? error.message : '未知错误'
  89. });
  90. }
  91. }
  92. static async getClientAclByTopic(req, res) {
  93. try {
  94. const { topic } = req.params;
  95. const topicStr = (0, helpers_1.toString)(topic);
  96. if (!topicStr) {
  97. res.status(400).json({
  98. success: false,
  99. message: '主题不能为空'
  100. });
  101. return;
  102. }
  103. const clientAcls = await clientAcl_1.ClientAclModel.getByTopic(topicStr);
  104. res.status(200).json({
  105. success: true,
  106. data: clientAcls
  107. });
  108. }
  109. catch (error) {
  110. console.error('根据主题获取授权规则失败:', error);
  111. res.status(500).json({
  112. success: false,
  113. message: '根据主题获取授权规则失败',
  114. error: error instanceof Error ? error.message : '未知错误'
  115. });
  116. }
  117. }
  118. static async createClientAcl(req, res) {
  119. try {
  120. const { clientid, username, topic, action, permission, priority, description } = req.body;
  121. if (!username || !topic || !action || !permission) {
  122. res.status(400).json({
  123. success: false,
  124. message: '用户名、主题、操作和权限不能为空'
  125. });
  126. return;
  127. }
  128. if (!['publish', 'subscribe', 'pubsub'].includes(action)) {
  129. res.status(400).json({
  130. success: false,
  131. message: '操作必须是publish、subscribe或pubsub之一'
  132. });
  133. return;
  134. }
  135. if (!['allow', 'deny'].includes(permission)) {
  136. res.status(400).json({
  137. success: false,
  138. message: '权限必须是allow或deny之一'
  139. });
  140. return;
  141. }
  142. const newClientAcl = await clientAcl_1.ClientAclModel.create({
  143. clientid: clientid || null,
  144. username,
  145. topic,
  146. action,
  147. permission,
  148. priority: priority || 0,
  149. description: description || null
  150. });
  151. res.status(201).json({
  152. success: true,
  153. data: newClientAcl,
  154. message: '客户端授权规则创建成功'
  155. });
  156. }
  157. catch (error) {
  158. console.error('创建客户端授权规则失败:', error);
  159. res.status(500).json({
  160. success: false,
  161. message: '创建客户端授权规则失败',
  162. error: error instanceof Error ? error.message : '未知错误'
  163. });
  164. }
  165. }
  166. static async updateClientAcl(req, res) {
  167. try {
  168. const { id } = req.params;
  169. const { username, topic, action, permission, priority, description } = req.body;
  170. if (!id || isNaN(Number(id))) {
  171. res.status(400).json({
  172. success: false,
  173. message: '无效的ID'
  174. });
  175. return;
  176. }
  177. const existingClientAcl = await clientAcl_1.ClientAclModel.getById(Number(id));
  178. if (!existingClientAcl) {
  179. res.status(404).json({
  180. success: false,
  181. message: '客户端授权规则不存在'
  182. });
  183. return;
  184. }
  185. if (action && !['publish', 'subscribe', 'pubsub'].includes(action)) {
  186. res.status(400).json({
  187. success: false,
  188. message: '操作必须是publish、subscribe或pubsub之一'
  189. });
  190. return;
  191. }
  192. if (permission && !['allow', 'deny'].includes(permission)) {
  193. res.status(400).json({
  194. success: false,
  195. message: '权限必须是allow或deny之一'
  196. });
  197. return;
  198. }
  199. const updatedClientAcl = await clientAcl_1.ClientAclModel.update(Number(id), {
  200. username,
  201. topic,
  202. action,
  203. permission,
  204. priority,
  205. description
  206. });
  207. res.status(200).json({
  208. success: true,
  209. data: updatedClientAcl,
  210. message: '客户端授权规则更新成功'
  211. });
  212. }
  213. catch (error) {
  214. console.error('更新客户端授权规则失败:', error);
  215. res.status(500).json({
  216. success: false,
  217. message: '更新客户端授权规则失败',
  218. error: error instanceof Error ? error.message : '未知错误'
  219. });
  220. }
  221. }
  222. static async deleteClientAcl(req, res) {
  223. try {
  224. const { id } = req.params;
  225. if (!id || isNaN(Number(id))) {
  226. res.status(400).json({
  227. success: false,
  228. message: '无效的ID'
  229. });
  230. return;
  231. }
  232. const existingClientAcl = await clientAcl_1.ClientAclModel.getById(Number(id));
  233. if (!existingClientAcl) {
  234. res.status(404).json({
  235. success: false,
  236. message: '客户端授权规则不存在'
  237. });
  238. return;
  239. }
  240. await clientAcl_1.ClientAclModel.delete(Number(id));
  241. res.status(200).json({
  242. success: true,
  243. message: '客户端授权规则删除成功'
  244. });
  245. }
  246. catch (error) {
  247. console.error('删除客户端授权规则失败:', error);
  248. res.status(500).json({
  249. success: false,
  250. message: '删除客户端授权规则失败',
  251. error: error instanceof Error ? error.message : '未知错误'
  252. });
  253. }
  254. }
  255. static async deleteMultipleClientAcl(req, res) {
  256. try {
  257. const { ids } = req.body;
  258. if (!ids || !Array.isArray(ids) || ids.length === 0) {
  259. res.status(400).json({
  260. success: false,
  261. message: '请提供有效的ID列表'
  262. });
  263. return;
  264. }
  265. const validIds = ids.filter(id => !isNaN(Number(id)));
  266. if (validIds.length !== ids.length) {
  267. res.status(400).json({
  268. success: false,
  269. message: 'ID列表包含无效的ID'
  270. });
  271. return;
  272. }
  273. await clientAcl_1.ClientAclModel.deleteMultiple(validIds.map(id => Number(id)));
  274. res.status(200).json({
  275. success: true,
  276. message: `成功删除${validIds.length}条客户端授权规则`
  277. });
  278. }
  279. catch (error) {
  280. console.error('批量删除客户端授权规则失败:', error);
  281. res.status(500).json({
  282. success: false,
  283. message: '批量删除客户端授权规则失败',
  284. error: error instanceof Error ? error.message : '未知错误'
  285. });
  286. }
  287. }
  288. static async getClientAclByUsernameAndAction(req, res) {
  289. try {
  290. const { username, action } = req.params;
  291. const usernameStr = (0, helpers_1.toString)(username);
  292. const actionStr = (0, helpers_1.toString)(action);
  293. if (!usernameStr || !actionStr) {
  294. res.status(400).json({
  295. success: false,
  296. message: '用户名和操作类型不能为空'
  297. });
  298. return;
  299. }
  300. if (!['publish', 'subscribe', 'pubsub'].includes(actionStr)) {
  301. res.status(400).json({
  302. success: false,
  303. message: '操作必须是publish、subscribe或pubsub之一'
  304. });
  305. return;
  306. }
  307. const clientAcls = await clientAcl_1.ClientAclModel.getByUsernameAndAction(usernameStr, actionStr);
  308. res.status(200).json({
  309. success: true,
  310. data: clientAcls
  311. });
  312. }
  313. catch (error) {
  314. console.error('根据用户名和操作类型获取授权规则失败:', error);
  315. res.status(500).json({
  316. success: false,
  317. message: '根据用户名和操作类型获取授权规则失败',
  318. error: error instanceof Error ? error.message : '未知错误'
  319. });
  320. }
  321. }
  322. static async checkUserPermission(req, res) {
  323. try {
  324. const { username, topic, action } = req.body;
  325. if (!username || !topic || !action) {
  326. res.status(400).json({
  327. success: false,
  328. message: '用户名、主题和操作类型不能为空'
  329. });
  330. return;
  331. }
  332. if (!['publish', 'subscribe'].includes(action)) {
  333. res.status(400).json({
  334. success: false,
  335. message: '操作必须是publish或subscribe之一'
  336. });
  337. return;
  338. }
  339. const hasPermission = await clientAcl_1.ClientAclModel.checkPermission(username, topic, action);
  340. res.status(200).json({
  341. success: true,
  342. data: {
  343. username,
  344. topic,
  345. action,
  346. hasPermission
  347. },
  348. message: `用户${hasPermission ? '有' : '没有'}权限${action === 'publish' ? '发布到' : '订阅'}主题${topic}`
  349. });
  350. }
  351. catch (error) {
  352. console.error('检查用户权限失败:', error);
  353. res.status(500).json({
  354. success: false,
  355. message: '检查用户权限失败',
  356. error: error instanceof Error ? error.message : '未知错误'
  357. });
  358. }
  359. }
  360. static async getClientAclStats(req, res) {
  361. try {
  362. const stats = await clientAcl_1.ClientAclModel.getPermissionStats();
  363. res.status(200).json({
  364. success: true,
  365. data: stats,
  366. message: '获取客户端授权统计信息成功'
  367. });
  368. }
  369. catch (error) {
  370. console.error('获取客户端授权统计信息失败:', error);
  371. res.status(500).json({
  372. success: false,
  373. message: '获取客户端授权统计信息失败',
  374. error: error instanceof Error ? error.message : '未知错误'
  375. });
  376. }
  377. }
  378. }
  379. exports.ClientAclController = ClientAclController;
  380. //# sourceMappingURL=clientAclController.js.map