authority.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * Authority rule controller.
  3. */
  4. angular.module('sentinelDashboardApp').controller('AuthorityRuleController', ['$scope', '$stateParams', 'AuthorityRuleService', 'ngDialog',
  5. 'MachineService',
  6. function ($scope, $stateParams, AuthorityRuleService, ngDialog,
  7. MachineService) {
  8. $scope.app = $stateParams.app;
  9. $scope.rulesPageConfig = {
  10. pageSize: 10,
  11. currentPageIndex: 1,
  12. totalPage: 1,
  13. totalCount: 0,
  14. };
  15. $scope.macsInputConfig = {
  16. searchField: ['text', 'value'],
  17. persist: true,
  18. create: false,
  19. maxItems: 1,
  20. render: {
  21. item: function (data, escape) {
  22. return '<div>' + escape(data.text) + '</div>';
  23. }
  24. },
  25. onChange: function (value, oldValue) {
  26. $scope.macInputModel = value;
  27. }
  28. };
  29. function getMachineRules() {
  30. if (!$scope.macInputModel) {
  31. return;
  32. }
  33. let mac = $scope.macInputModel.split(':');
  34. AuthorityRuleService.queryMachineRules($scope.app, mac[0], mac[1])
  35. .success(function (data) {
  36. if (data.code === 0 && data.data) {
  37. $scope.loadError = undefined;
  38. $scope.rules = data.data;
  39. $scope.rulesPageConfig.totalCount = $scope.rules.length;
  40. } else {
  41. $scope.rules = [];
  42. $scope.rulesPageConfig.totalCount = 0;
  43. $scope.loadError = {message: data.msg};
  44. }
  45. })
  46. .error((data, header, config, status) => {
  47. $scope.loadError = {message: "未知错误"};
  48. });
  49. };
  50. $scope.getMachineRules = getMachineRules;
  51. getMachineRules();
  52. var authorityRuleDialog;
  53. $scope.editRule = function (rule) {
  54. $scope.currentRule = angular.copy(rule);
  55. $scope.authorityRuleDialog = {
  56. title: '编辑授权规则',
  57. type: 'edit',
  58. confirmBtnText: '保存',
  59. };
  60. authorityRuleDialog = ngDialog.open({
  61. template: '/app/views/dialog/authority-rule-dialog.html',
  62. width: 680,
  63. overlay: true,
  64. scope: $scope
  65. });
  66. };
  67. $scope.addNewRule = function () {
  68. var mac = $scope.macInputModel.split(':');
  69. $scope.currentRule = {
  70. app: $scope.app,
  71. ip: mac[0],
  72. port: mac[1],
  73. rule: {
  74. strategy: 0,
  75. limitApp: '',
  76. }
  77. };
  78. $scope.authorityRuleDialog = {
  79. title: '新增授权规则',
  80. type: 'add',
  81. confirmBtnText: '新增',
  82. showAdvanceButton: true,
  83. };
  84. authorityRuleDialog = ngDialog.open({
  85. template: '/app/views/dialog/authority-rule-dialog.html',
  86. width: 680,
  87. overlay: true,
  88. scope: $scope
  89. });
  90. };
  91. $scope.saveRule = function () {
  92. if (!AuthorityRuleService.checkRuleValid($scope.currentRule.rule)) {
  93. return;
  94. }
  95. if ($scope.authorityRuleDialog.type === 'add') {
  96. addNewRuleAndPush($scope.currentRule);
  97. } else if ($scope.authorityRuleDialog.type === 'edit') {
  98. saveRuleAndPush($scope.currentRule, true);
  99. }
  100. };
  101. function addNewRuleAndPush(rule) {
  102. AuthorityRuleService.addNewRule(rule).success((data) => {
  103. if (data.success) {
  104. getMachineRules();
  105. authorityRuleDialog.close();
  106. } else {
  107. alert('添加规则失败:' + data.msg);
  108. }
  109. }).error((data) => {
  110. if (data) {
  111. alert('添加规则失败:' + data.msg);
  112. } else {
  113. alert("添加规则失败:未知错误");
  114. }
  115. });
  116. }
  117. function saveRuleAndPush(rule, edit) {
  118. AuthorityRuleService.saveRule(rule).success(function (data) {
  119. if (data.success) {
  120. alert("修改规则成功");
  121. getMachineRules();
  122. if (edit) {
  123. authorityRuleDialog.close();
  124. } else {
  125. confirmDialog.close();
  126. }
  127. } else {
  128. alert('修改规则失败:' + data.msg);
  129. }
  130. }).error((data) => {
  131. if (data) {
  132. alert('修改规则失败:' + data.msg);
  133. } else {
  134. alert("修改规则失败:未知错误");
  135. }
  136. });
  137. }
  138. function deleteRuleAndPush(entity) {
  139. if (entity.id === undefined || isNaN(entity.id)) {
  140. alert('规则 ID 不合法!');
  141. return;
  142. }
  143. AuthorityRuleService.deleteRule(entity).success((data) => {
  144. if (data.code == 0) {
  145. getMachineRules();
  146. confirmDialog.close();
  147. } else {
  148. alert('删除规则失败:' + data.msg);
  149. }
  150. }).error((data) => {
  151. if (data) {
  152. alert('删除规则失败:' + data.msg);
  153. } else {
  154. alert("删除规则失败:未知错误");
  155. }
  156. });
  157. };
  158. var confirmDialog;
  159. $scope.deleteRule = function (ruleEntity) {
  160. $scope.currentRule = ruleEntity;
  161. $scope.confirmDialog = {
  162. title: '删除授权规则',
  163. type: 'delete_rule',
  164. attentionTitle: '请确认是否删除如下授权限流规则',
  165. attention: '资源名: ' + ruleEntity.rule.resource + ', 流控应用: ' + ruleEntity.rule.limitApp +
  166. ', 类型: ' + (ruleEntity.rule.strategy === 0 ? '白名单' : '黑名单'),
  167. confirmBtnText: '删除',
  168. };
  169. confirmDialog = ngDialog.open({
  170. template: '/app/views/dialog/confirm-dialog.html',
  171. scope: $scope,
  172. overlay: true
  173. });
  174. };
  175. $scope.confirm = function () {
  176. if ($scope.confirmDialog.type === 'delete_rule') {
  177. deleteRuleAndPush($scope.currentRule);
  178. } else {
  179. console.error('error');
  180. }
  181. };
  182. queryAppMachines();
  183. function queryAppMachines() {
  184. MachineService.getAppMachines($scope.app).success(
  185. function (data) {
  186. if (data.code == 0) {
  187. // $scope.machines = data.data;
  188. if (data.data) {
  189. $scope.machines = [];
  190. $scope.macsInputOptions = [];
  191. data.data.forEach(function (item) {
  192. if (item.healthy) {
  193. $scope.macsInputOptions.push({
  194. text: item.ip + ':' + item.port,
  195. value: item.ip + ':' + item.port
  196. });
  197. }
  198. });
  199. }
  200. if ($scope.macsInputOptions.length > 0) {
  201. $scope.macInputModel = $scope.macsInputOptions[0].value;
  202. }
  203. } else {
  204. $scope.macsInputOptions = [];
  205. }
  206. }
  207. );
  208. };
  209. $scope.$watch('macInputModel', function () {
  210. if ($scope.macInputModel) {
  211. getMachineRules();
  212. }
  213. });
  214. }]);