RecipientKeyController.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\UpdateRecipientKeyRequest;
  5. use App\Http\Resources\RecipientResource;
  6. class RecipientKeyController extends Controller
  7. {
  8. protected $gnupg;
  9. public function __construct()
  10. {
  11. $this->gnupg = new \gnupg();
  12. }
  13. public function update(UpdateRecipientKeyRequest $request, $id)
  14. {
  15. $recipient = user()->recipients()->findOrFail($id);
  16. $info = $this->gnupg->import($request->key_data);
  17. if (!$info || !$info['fingerprint']) {
  18. return response('Key could not be imported', 404);
  19. }
  20. $recipient->update([
  21. 'should_encrypt' => true,
  22. 'fingerprint' => $info['fingerprint']
  23. ]);
  24. return new RecipientResource($recipient->fresh());
  25. }
  26. public function destroy($id)
  27. {
  28. $recipient = user()->recipients()->findOrFail($id);
  29. if (!$this->gnupg->deletekey($recipient->fingerprint)) {
  30. return response('Key could not be deleted', 404);
  31. }
  32. // Remove the key from all recipients using that same fingerprint.
  33. Recipient::all()
  34. ->where('fingerprint', $recipient->fingerprint)
  35. ->each(function ($recipient) {
  36. $recipient->update([
  37. 'should_encrypt' => false,
  38. 'fingerprint' => null
  39. ]);
  40. });
  41. return response('', 204);
  42. }
  43. }