ApplicationApiController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. const READ_PERMISSION = "admin.api.read";
  17. const WRITE_PERMISSION = "admin.api.write";
  18. /**
  19. * Display a listing of the resource.
  20. *
  21. * @return Application|Factory|View|Response
  22. */
  23. public function index(LocaleSettings $locale_settings)
  24. {
  25. $this->checkPermission(self::READ_PERMISSION);
  26. return view('admin.api.index', [
  27. 'locale_datatables' => $locale_settings->datatables
  28. ]);
  29. }
  30. /**
  31. * Show the form for creating a new resource.
  32. *
  33. * @return Application|Factory|View|Response
  34. */
  35. public function create()
  36. {
  37. $this->checkPermission(self::WRITE_PERMISSION);
  38. return view('admin.api.create');
  39. }
  40. /**
  41. * Store a newly created resource in storage.
  42. *
  43. * @param Request $request
  44. * @return RedirectResponse
  45. */
  46. public function store(Request $request)
  47. {
  48. $request->validate([
  49. 'memo' => 'nullable|string|max:60',
  50. ]);
  51. ApplicationApi::create([
  52. 'memo' => $request->input('memo'),
  53. ]);
  54. return redirect()->route('admin.api.index')->with('success', __('api key created!'));
  55. }
  56. /**
  57. * Display the specified resource.
  58. *
  59. * @param ApplicationApi $applicationApi
  60. * @return Response
  61. */
  62. public function show(ApplicationApi $applicationApi)
  63. {
  64. //
  65. }
  66. /**
  67. * Show the form for editing the specified resource.
  68. *
  69. * @param ApplicationApi $applicationApi
  70. * @return Application|Factory|View|Response
  71. */
  72. public function edit(ApplicationApi $applicationApi)
  73. {
  74. $this->checkPermission(self::WRITE_PERMISSION);
  75. return view('admin.api.edit', [
  76. 'applicationApi' => $applicationApi,
  77. ]);
  78. }
  79. /**
  80. * Update the specified resource in storage.
  81. *
  82. * @param Request $request
  83. * @param ApplicationApi $applicationApi
  84. * @return RedirectResponse
  85. */
  86. public function update(Request $request, ApplicationApi $applicationApi)
  87. {
  88. $request->validate([
  89. 'memo' => 'nullable|string|max:60',
  90. ]);
  91. $applicationApi->update($request->all());
  92. return redirect()->route('admin.api.index')->with('success', __('api key updated!'));
  93. }
  94. /**
  95. * Remove the specified resource from storage.
  96. *
  97. * @param ApplicationApi $applicationApi
  98. * @return RedirectResponse
  99. */
  100. public function destroy(ApplicationApi $applicationApi)
  101. {
  102. $this->checkPermission(self::WRITE_PERMISSION);
  103. $applicationApi->delete();
  104. return redirect()->back()->with('success', __('api key has been removed!'));
  105. }
  106. /**
  107. * @param Request $request
  108. * @return JsonResponse|mixed
  109. *
  110. * @throws Exception
  111. */
  112. public function dataTable(Request $request)
  113. {
  114. $query = ApplicationApi::query();
  115. return datatables($query)
  116. ->addColumn('actions', function (ApplicationApi $apiKey) {
  117. return '
  118. <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>
  119. <form class="d-inline" onsubmit="return submitResult();" method="post" action="'.route('admin.api.destroy', $apiKey->token).'">
  120. '.csrf_field().'
  121. '.method_field('DELETE').'
  122. <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>
  123. </form>
  124. ';
  125. })
  126. ->editColumn('token', function (ApplicationApi $apiKey) {
  127. return "<code>{$apiKey->token}</code>";
  128. })
  129. ->editColumn('last_used', function (ApplicationApi $apiKey) {
  130. return $apiKey->last_used ? $apiKey->last_used->diffForHumans() : '';
  131. })
  132. ->rawColumns(['actions', 'token'])
  133. ->make();
  134. }
  135. }