NodeAuthController.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Node;
  5. use App\Models\NodeAuth;
  6. use Illuminate\Contracts\View\View;
  7. use Illuminate\Http\JsonResponse;
  8. use Str;
  9. class NodeAuthController extends Controller
  10. {
  11. public function index(): View
  12. { // 节点授权列表
  13. $authorizations = NodeAuth::with(['node:id,name,type,server,ip,ipv6'])
  14. ->orderBy('node_id')
  15. ->paginate()
  16. ->appends(request('page'));
  17. return view('admin.node.auth', compact('authorizations'));
  18. }
  19. public function store(): JsonResponse
  20. { // 添加节点授权
  21. $nodes = Node::whereStatus(1)->doesntHave('auth')->pluck('id');
  22. if ($nodes->isEmpty()) {
  23. return response()->json(['status' => 'success', 'message' => trans('admin.node.auth.empty')]);
  24. }
  25. $authData = [];
  26. foreach ($nodes as $node_id) {
  27. $authData[] = [
  28. 'node_id' => $node_id,
  29. 'key' => Str::random(),
  30. 'secret' => Str::random(8),
  31. ];
  32. }
  33. NodeAuth::insert($authData);
  34. return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.generate')])]);
  35. }
  36. public function update(NodeAuth $auth): JsonResponse
  37. { // 重置节点授权
  38. if ($auth->update(['key' => Str::random(), 'secret' => Str::random(8)])) {
  39. return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.reset')])]);
  40. }
  41. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.reset')])]);
  42. }
  43. public function destroy(NodeAuth $auth): JsonResponse
  44. { // 删除节点授权
  45. if ($auth->delete()) {
  46. return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.delete')])]);
  47. }
  48. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.delete')])]);
  49. }
  50. }