NodeAuthController.php 2.4 KB

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