RuleController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Rule;
  5. use App\Models\RuleGroup;
  6. use App\Models\RuleGroupNode;
  7. use App\Models\RuleLog;
  8. use App\Models\SsNode;
  9. use Exception;
  10. use Illuminate\Http\JsonResponse;
  11. use Illuminate\Http\Request;
  12. use Redirect;
  13. use Response;
  14. use Validator;
  15. class RuleController extends Controller {
  16. // 审计规则列表
  17. public function ruleList(Request $request): \Illuminate\Http\Response {
  18. $type = $request->input('type');
  19. $query = Rule::query();
  20. if($type){
  21. $query->whereType($type);
  22. }
  23. $view['rules'] = $query->paginate(15)->appends($request->except('page'));
  24. return Response::view('admin.rule.ruleList', $view);
  25. }
  26. // 添加审计规则
  27. public function addRule(Request $request): ?JsonResponse {
  28. $validator = Validator::make($request->all(), [
  29. 'type' => 'required|between:1,4',
  30. 'name' => 'required',
  31. 'pattern' => 'required',
  32. ]);
  33. if($validator->fails()){
  34. return Response::json(['status' => 'fail', 'message' => $validator->errors()->all()]);
  35. }
  36. $obj = new Rule();
  37. $obj->type = $request->input('type');
  38. $obj->name = $request->input('name');
  39. $obj->pattern = $request->input('pattern');
  40. $obj->save();
  41. if($obj->id){
  42. return Response::json(['status' => 'success', 'data' => '', 'message' => '提交成功']);
  43. }
  44. return Response::json(['status' => 'fail', 'data' => '', 'message' => '操作失败']);
  45. }
  46. // 编辑审计规则
  47. public function editRule(Request $request): ?JsonResponse {
  48. $validator = Validator::make($request->all(), [
  49. 'id' => 'required|exists:rule,id',
  50. 'rule_name' => 'required',
  51. 'rule_pattern' => 'required',
  52. ]);
  53. if($validator->fails()){
  54. return Response::json(['status' => 'fail', 'message' => $validator->errors()->all()]);
  55. }
  56. $ret = Rule::query()->whereId($request->input('id'))->update([
  57. 'name' => $request->input('rule_name'),
  58. 'pattern' => $request->input('rule_pattern')
  59. ]);
  60. if($ret){
  61. return Response::json(['status' => 'success', 'message' => '操作成功']);
  62. }
  63. return Response::json(['status' => 'fail', 'message' => '操作失败']);
  64. }
  65. // 删除审计规则
  66. public function delRule(Request $request): JsonResponse {
  67. $id = $request->input('id');
  68. try{
  69. Rule::query()->whereId($id)->delete();
  70. $RuleGroupList = RuleGroup::query()->get();
  71. foreach($RuleGroupList as $RuleGroup){
  72. $rules = explode(',', $RuleGroup->rules);
  73. if(in_array($id, $rules, true)){
  74. $rules = implode(',', array_diff($rules, [$id]));
  75. RuleGroup::query()->whereId($RuleGroup->id)->update(['rules' => $rules]);
  76. }
  77. }
  78. }catch(Exception $e){
  79. return Response::json(['status' => 'fail', 'message' => '操作失败, '.$e->getMessage()]);
  80. }
  81. return Response::json(['status' => 'success', 'message' => '操作成功']);
  82. }
  83. // 审计规则分组列表
  84. public function ruleGroupList(Request $request): \Illuminate\Http\Response {
  85. $view['ruleGroupList'] = RuleGroup::query()->paginate(15)->appends($request->except('page'));
  86. return Response::view('admin.rule.ruleGroupList', $view);
  87. }
  88. // 添加审计规则分组
  89. public function addRuleGroup(Request $request) {
  90. if($request->isMethod('POST')){
  91. $validator = Validator::make($request->all(), [
  92. 'name' => 'required',
  93. 'type' => 'required|boolean',
  94. 'rules' => 'required',
  95. ]);
  96. if($validator->fails()){
  97. return Redirect::back()->withInput()->withErrors($validator->errors());
  98. }
  99. $obj = new RuleGroup();
  100. $obj->name = $request->input('name');
  101. $obj->type = intval($request->input('type'));
  102. $obj->rules = implode(',', $request->input('rules'));
  103. $obj->save();
  104. if($obj->id){
  105. return Redirect::back()->with('successMsg', '操作成功');
  106. }
  107. return Redirect::back()->withInput()->withErrors('操作失败');
  108. }
  109. $view['ruleList'] = Rule::query()->get();
  110. return Response::view('admin.rule.ruleGroupInfo', $view);
  111. }
  112. // 编辑审计规则分组
  113. public function editRuleGroup(Request $request) {
  114. $id = $request->input('id');
  115. if($request->isMethod('POST')){
  116. $validator = Validator::make($request->all(), [
  117. 'id' => 'required',
  118. 'name' => 'required',
  119. 'type' => 'required|boolean'
  120. ]);
  121. if($validator->fails()){
  122. return Redirect::back()->withInput()->withErrors($validator->errors());
  123. }
  124. $name = $request->input('name');
  125. $type = intval($request->input('type'));
  126. $rules = $request->input('rules');
  127. $ruleGroup = RuleGroup::query()->find($id);
  128. if(!$ruleGroup){
  129. return Redirect::back()->withInput()->withErrors('未找到需要编辑的审计规则分组!');
  130. }
  131. $data = [];
  132. if($ruleGroup->name != $name){
  133. $data['name'] = $name;
  134. }
  135. if($ruleGroup->type != $type){
  136. $data['type'] = $type;
  137. }
  138. if($rules){
  139. $ruleStr = implode(',', $rules);
  140. if($ruleGroup->rules != $ruleStr){
  141. $data['rules'] = $ruleStr;
  142. }else{
  143. return Redirect::back()->with('successMsg', '检测为未修改,无变动!');
  144. }
  145. }elseif(isset($ruleGroup->rules)){
  146. $data['rules'] = $rules;
  147. }
  148. $ret = RuleGroup::query()->whereId($id)->update($data);
  149. if($ret){
  150. return Redirect::back()->with('successMsg', '操作成功');
  151. }
  152. return Redirect::back()->withInput()->withErrors('操作失败');
  153. }
  154. $ruleGroup = RuleGroup::query()->find($id);
  155. if(!$ruleGroup){
  156. return Redirect::back();
  157. }
  158. $view['ruleList'] = Rule::query()->get();
  159. return view('admin.rule.ruleGroupInfo', $view)->with(compact('ruleGroup'));
  160. }
  161. // 删除审计规则分组
  162. public function delRuleGroup(Request $request): JsonResponse {
  163. $id = $request->input('id');
  164. $ruleGroup = RuleGroup::query()->whereId($id)->get();
  165. if(!$ruleGroup){
  166. return Response::json(['status' => 'fail', 'message' => '删除失败,未找到审计规则分组']);
  167. }
  168. try{
  169. RuleGroup::query()->whereId($id)->delete();
  170. RuleGroupNode::query()->whereRuleGroupId($id)->delete();
  171. }catch(Exception $e){
  172. return Response::json(['status' => 'fail', 'message' => '删除失败,'.$e->getMessage()]);
  173. }
  174. return Response::json(['status' => 'success', 'message' => '清理成功']);
  175. }
  176. // 规则分组关联节点
  177. public function assignNode(Request $request) {
  178. $id = $request->input('id');
  179. if($request->isMethod('POST')){
  180. $nodes = $request->input('nodes');
  181. $validator = Validator::make($request->all(), [
  182. 'id' => 'required',
  183. ]);
  184. if($validator->fails()){
  185. return Redirect::back()->withInput()->withErrors($validator->errors());
  186. }
  187. $ruleGroup = RuleGroup::query()->find($id);
  188. if(!$ruleGroup){
  189. return Redirect::back()->withInput()->withErrors('未找到审计规则分组!');
  190. }
  191. try{
  192. if($nodes){
  193. $nodeStr = implode(',', $nodes);
  194. // 无变动 不改动
  195. if($ruleGroup->nodes == $nodeStr){
  196. return Redirect::back()->with('successMsg', '检测为未修改,无变动!');
  197. }
  198. RuleGroup::query()->whereId($id)->update(['nodes' => $nodeStr]);
  199. RuleGroupNode::query()->whereRuleGroupId($id)->delete();
  200. foreach($nodes as $nodeId){
  201. $obj = new RuleGroupNode();
  202. $obj->rule_group_id = $id;
  203. $obj->node_id = $nodeId;
  204. $obj->save();
  205. }
  206. }else{
  207. RuleGroup::query()->whereId($id)->update(['nodes' => $nodes]);
  208. RuleGroupNode::query()->whereRuleGroupId($id)->delete();
  209. }
  210. }catch(Exception $e){
  211. return Redirect::back()->withInput()->withErrors($e->getMessage());
  212. }
  213. return Redirect::back()->with('successMsg', '操作成功');
  214. }
  215. $view['ruleGroup'] = RuleGroup::query()->find($id);
  216. $view['nodeList'] = SsNode::query()->get();
  217. return Response::view('admin.rule.assignNode', $view);
  218. }
  219. // 用户触发审计规则日志
  220. public function ruleLogList(Request $request): \Illuminate\Http\Response {
  221. $uid = $request->input('uid');
  222. $email = $request->input('email');
  223. $nodeId = $request->input('node_id');
  224. $ruleId = $request->input('rule_id');
  225. $query = RuleLog::query();
  226. if($uid){
  227. $query->whereUserId($uid);
  228. }
  229. if(isset($email)){
  230. $query->whereHas('user', static function($q) use ($email) {
  231. $q->where('email', 'like', '%'.$email.'%');
  232. });
  233. }
  234. if($nodeId){
  235. $query->whereNodeId($nodeId);
  236. }
  237. if($ruleId){
  238. $query->whereRuleId($ruleId);
  239. }
  240. $view['nodeList'] = SsNode::query()->get();
  241. $view['ruleList'] = Rule::query()->get();
  242. $view['ruleLogs'] = $query->paginate(15)->appends($request->except('page'));
  243. return Response::view('admin.rule.ruleLogList', $view);
  244. }
  245. // 清除所有审计触发日志
  246. public function clearLog(): ?JsonResponse {
  247. try{
  248. $ret = RuleLog::query()->delete();
  249. }catch(Exception $e){
  250. return Response::json(['status' => 'fail', 'message' => '清理失败, '.$e->getMessage()]);
  251. }
  252. $result = RuleLog::query()->doesntExist();
  253. if($ret || $result){
  254. return Response::json(['status' => 'success', 'message' => '清理成功']);
  255. }
  256. return Response::json(['status' => 'fail', 'message' => '清理失败']);
  257. }
  258. }