RuleController.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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\Request;
  11. use Redirect;
  12. use Response;
  13. use Validator;
  14. class RuleController extends Controller {
  15. // 审计规则列表
  16. public function ruleList(Request $request) {
  17. $type = $request->input('type');
  18. $query = Rule::query();
  19. if($type){
  20. $query->whereType($type);
  21. }
  22. $view['rules'] = $query->paginate(15)->appends($request->except('page'));
  23. return Response::view('admin.rule.ruleList', $view);
  24. }
  25. // 添加审计规则
  26. public function addRule(Request $request) {
  27. $validator = Validator::make($request->all(), [
  28. 'type' => 'required|between:1,4',
  29. 'name' => 'required',
  30. 'pattern' => 'required',
  31. ]);
  32. if($validator->fails()){
  33. return Response::json(['status' => 'fail', 'message' => $validator->errors()->all()]);
  34. }
  35. $obj = new Rule();
  36. $obj->type = $request->input('type');
  37. $obj->name = $request->input('name');
  38. $obj->pattern = $request->input('pattern');
  39. $obj->save();
  40. if($obj->id){
  41. return Response::json(['status' => 'success', 'data' => '', 'message' => '提交成功']);
  42. }else{
  43. return Response::json(['status' => 'fail', 'data' => '', 'message' => '操作失败']);
  44. }
  45. }
  46. // 编辑审计规则
  47. public function editRule(Request $request) {
  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. }else{
  63. return Response::json(['status' => 'fail', 'message' => '操作失败']);
  64. }
  65. }
  66. // 删除审计规则
  67. public function delRule($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)){
  74. $rules = implode(', ', array_diff($rules, (array) $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) {
  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. }else{
  107. return Redirect::back()->withInput()->withErrors('操作失败');
  108. }
  109. }else{
  110. $view['ruleList'] = Rule::query()->get();
  111. return Response::view('admin.rule.ruleGroupInfo', $view);
  112. }
  113. }
  114. // 编辑审计规则分组
  115. public function editRuleGroup(Request $request) {
  116. $id = $request->input('id');
  117. if($request->isMethod('POST')){
  118. $validator = Validator::make($request->all(), [
  119. 'id' => 'required',
  120. 'name' => 'required',
  121. 'type' => 'required|boolean',
  122. 'rules' => 'required',
  123. ]);
  124. if($validator->fails()){
  125. return Redirect::back()->withInput()->withErrors($validator->errors());
  126. }
  127. $name = $request->input('name');
  128. $type = intval($request->input('type'));
  129. $rules = $request->input('rules');
  130. $ruleGroup = RuleGroup::query()->find($id);
  131. if(!$ruleGroup){
  132. return Redirect::back()->withInput()->withErrors('未找到需要编辑的审计规则分组!');
  133. }
  134. $data = [];
  135. if($ruleGroup->name != $name){
  136. $data['name'] = $name;
  137. }
  138. if($ruleGroup->type != $type){
  139. $data['type'] = $type;
  140. }
  141. if($ruleGroup->rules != $rules){
  142. $data['rules'] = implode(", ", $rules);
  143. }
  144. $ret = RuleGroup::query()->whereId($id)->update($data);
  145. if($ret){
  146. return Redirect::back()->with('successMsg', '操作成功');
  147. }
  148. return Redirect::back()->withInput()->withErrors('操作失败');
  149. }else{
  150. $ruleGroup = RuleGroup::query()->find($id);
  151. if(!$ruleGroup){
  152. return Redirect::back();
  153. }
  154. $view['ruleList'] = Rule::query()->get();
  155. return view('admin.rule.ruleGroupInfo', $view)->with(compact('ruleGroup'));
  156. }
  157. }
  158. // 删除审计规则分组
  159. public function delRuleGroup($id) {
  160. $ruleGroup = RuleGroup::query()->whereId($id)->get();
  161. if(!$ruleGroup){
  162. return Response::json(['status' => 'fail', 'message' => '删除失败,未找到审计规则分组']);
  163. }
  164. try{
  165. RuleGroup::query()->whereId($id)->delete();
  166. RuleGroupNode::query()->whereRuleGroupId($id)->delete();
  167. }catch(Exception $e){
  168. return Response::json(['status' => 'fail', 'message' => '删除失败,'.$e->getMessage()]);
  169. }
  170. return Response::json(['status' => 'success', 'message' => '清理成功']);
  171. }
  172. // 规则分组关联节点
  173. public function assignNode(Request $request) {
  174. $id = $request->input('id');
  175. if($request->isMethod('POST')){
  176. $nodes = $request->input('nodes');
  177. $validator = Validator::make($request->all(), [
  178. 'id' => 'required',
  179. 'nodes' => 'required',
  180. ]);
  181. if($validator->fails()){
  182. return Redirect::back()->withInput()->withErrors($validator->errors());
  183. }
  184. $ruleGroup = RuleGroup::query()->find($id);
  185. if(!$ruleGroup){
  186. return Redirect::back()->withInput()->withErrors('未找到审计规则分组!');
  187. }
  188. try{
  189. if($ruleGroup->nodes != $nodes){
  190. RuleGroup::query()->whereId($id)->update(['nodes' => implode(", ", $nodes)]);
  191. }
  192. RuleGroupNode::query()->whereRuleGroupId($id)->delete();
  193. foreach($nodes as $nodeId){
  194. $obj = new RuleGroupNode();
  195. $obj->rule_group_id = $id;
  196. $obj->node_id = $nodeId;
  197. $obj->save();
  198. }
  199. }catch(Exception $e){
  200. return Redirect::back()->withInput()->withErrors($e->getMessage());
  201. }
  202. return Redirect::back()->with('successMsg', '操作成功');
  203. }else{
  204. $view['ruleGroup'] = RuleGroup::query()->find($id);
  205. $view['nodeList'] = SsNode::query()->get();
  206. return Response::view('admin.rule.assignNode', $view);
  207. }
  208. }
  209. // 用户触发审计规则日志
  210. public function ruleLogList(Request $request) {
  211. $uid = $request->input('uid');
  212. $email = $request->input('email');
  213. $nodeId = $request->input('node_id');
  214. $ruleId = $request->input('rule_id');
  215. $query = RuleLog::query();
  216. if($uid){
  217. $query->whereUserId($uid);
  218. }
  219. if(isset($email)){
  220. $query->whereHas('user', function($q) use ($email) {
  221. $q->where('email', 'like', '%'.$email.'%');
  222. });
  223. }
  224. if($nodeId){
  225. $query->whereNodeId($nodeId);
  226. }
  227. if($ruleId){
  228. $query->whereRuleId($ruleId);
  229. }
  230. $view['nodeList'] = SsNode::query()->get();
  231. $view['ruleList'] = Rule::query()->get();
  232. $view['ruleLogs'] = $query->paginate(15)->appends($request->except('page'));
  233. return Response::view('admin.rule.ruleLogList', $view);
  234. }
  235. // 清除所有审计触发日志
  236. public function clearLog() {
  237. try{
  238. $ret = RuleLog::query()->delete();
  239. }catch(Exception $e){
  240. return Response::json(['status' => 'fail', 'message' => '清理失败, '.$e->getMessage()]);
  241. }
  242. $result = RuleLog::query()->doesntExist();
  243. if($ret || $result){
  244. return Response::json(['status' => 'success', 'message' => '清理成功']);
  245. }else{
  246. return Response::json(['status' => 'fail', 'message' => '清理失败']);
  247. }
  248. }
  249. }