ApplicationApiController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\ApplicationApi;
  5. use App\Settings\LocaleSettings;
  6. use Exception;
  7. use Illuminate\Contracts\Foundation\Application;
  8. use Illuminate\Contracts\View\Factory;
  9. use Illuminate\Contracts\View\View;
  10. use Illuminate\Http\JsonResponse;
  11. use Illuminate\Http\RedirectResponse;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Http\Response;
  14. class ApplicationApiController extends Controller
  15. {
  16. /**
  17. * Display a listing of the resource.
  18. *
  19. * @return Application|Factory|View|Response
  20. */
  21. public function index(LocaleSettings $locale_settings)
  22. {
  23. return view('admin.api.index', [
  24. 'locale_datatables' => $locale_settings->datatables
  25. ]);
  26. }
  27. /**
  28. * Show the form for creating a new resource.
  29. *
  30. * @return Application|Factory|View|Response
  31. */
  32. public function create()
  33. {
  34. return view('admin.api.create');
  35. }
  36. /**
  37. * Store a newly created resource in storage.
  38. *
  39. * @param Request $request
  40. * @return RedirectResponse
  41. */
  42. public function store(Request $request)
  43. {
  44. $request->validate([
  45. 'memo' => 'nullable|string|max:60',
  46. ]);
  47. ApplicationApi::create([
  48. 'memo' => $request->input('memo'),
  49. ]);
  50. return redirect()->route('admin.api.index')->with('success', __('api key created!'));
  51. }
  52. /**
  53. * Display the specified resource.
  54. *
  55. * @param ApplicationApi $applicationApi
  56. * @return Response
  57. */
  58. public function show(ApplicationApi $applicationApi)
  59. {
  60. //
  61. }
  62. /**
  63. * Show the form for editing the specified resource.
  64. *
  65. * @param ApplicationApi $applicationApi
  66. * @return Application|Factory|View|Response
  67. */
  68. public function edit(ApplicationApi $applicationApi)
  69. {
  70. return view('admin.api.edit', [
  71. 'applicationApi' => $applicationApi,
  72. ]);
  73. }
  74. /**
  75. * Update the specified resource in storage.
  76. *
  77. * @param Request $request
  78. * @param ApplicationApi $applicationApi
  79. * @return RedirectResponse
  80. */
  81. public function update(Request $request, ApplicationApi $applicationApi)
  82. {
  83. $request->validate([
  84. 'memo' => 'nullable|string|max:60',
  85. ]);
  86. $applicationApi->update($request->all());
  87. return redirect()->route('admin.api.index')->with('success', __('api key updated!'));
  88. }
  89. /**
  90. * Remove the specified resource from storage.
  91. *
  92. * @param ApplicationApi $applicationApi
  93. * @return RedirectResponse
  94. */
  95. public function destroy(ApplicationApi $applicationApi)
  96. {
  97. $applicationApi->delete();
  98. return redirect()->back()->with('success', __('api key has been removed!'));
  99. }
  100. /**
  101. * @param Request $request
  102. * @return JsonResponse|mixed
  103. *
  104. * @throws Exception
  105. */
  106. public function dataTable(Request $request)
  107. {
  108. $query = ApplicationApi::query();
  109. return datatables($query)
  110. ->addColumn('actions', function (ApplicationApi $apiKey) {
  111. return '
  112. <a data-content="'.__('Edit').'" data-toggle="popover" data-trigger="hover" data-placement="top" href="'.route('admin.api.edit', $apiKey->token).'" class="btn btn-sm btn-info mr-1"><i class="fas fa-pen"></i></a>
  113. <form class="d-inline" onsubmit="return submitResult();" method="post" action="'.route('admin.api.destroy', $apiKey->token).'">
  114. '.csrf_field().'
  115. '.method_field('DELETE').'
  116. <button data-content="'.__('Delete').'" data-toggle="popover" data-trigger="hover" data-placement="top" class="btn btn-sm btn-danger mr-1"><i class="fas fa-trash"></i></button>
  117. </form>
  118. ';
  119. })
  120. ->editColumn('token', function (ApplicationApi $apiKey) {
  121. return "<code>{$apiKey->token}</code>";
  122. })
  123. ->editColumn('last_used', function (ApplicationApi $apiKey) {
  124. return $apiKey->last_used ? $apiKey->last_used->diffForHumans() : '';
  125. })
  126. ->rawColumns(['actions', 'token'])
  127. ->make();
  128. }
  129. }