ProfileMigrationStoreRequest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Http\Requests;
  3. use App\Models\ProfileMigration;
  4. use App\Services\FetchCacheService;
  5. use App\Services\WebfingerService;
  6. use Illuminate\Foundation\Http\FormRequest;
  7. use Illuminate\Validation\Validator;
  8. class ProfileMigrationStoreRequest extends FormRequest
  9. {
  10. /**
  11. * Determine if the user is authorized to make this request.
  12. */
  13. public function authorize(): bool
  14. {
  15. if (! $this->user() || $this->user()->status) {
  16. return false;
  17. }
  18. return true;
  19. }
  20. /**
  21. * Get the validation rules that apply to the request.
  22. *
  23. * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
  24. */
  25. public function rules(): array
  26. {
  27. return [
  28. 'acct' => 'required|email',
  29. 'password' => 'required|current_password',
  30. ];
  31. }
  32. public function after(): array
  33. {
  34. return [
  35. function (Validator $validator) {
  36. $err = $this->validateNewAccount();
  37. if ($err !== 'noerr') {
  38. $validator->errors()->add(
  39. 'acct',
  40. $err
  41. );
  42. }
  43. },
  44. ];
  45. }
  46. protected function validateNewAccount()
  47. {
  48. if (ProfileMigration::whereProfileId($this->user()->profile_id)->where('created_at', '>', now()->subDays(30))->exists()) {
  49. return 'Error - You have migrated your account in the past 30 days, you can only perform a migration once per 30 days.';
  50. }
  51. $acct = WebfingerService::rawGet($this->acct);
  52. if (! $acct) {
  53. return 'The new account you provided is not responding to our requests.';
  54. }
  55. $pr = FetchCacheService::getJson($acct);
  56. if (! $pr || ! isset($pr['alsoKnownAs'])) {
  57. return 'Invalid account lookup response.';
  58. }
  59. if (! count($pr['alsoKnownAs']) || ! is_array($pr['alsoKnownAs'])) {
  60. return 'The new account does not contain an alias to your current account.';
  61. }
  62. $curAcctUrl = $this->user()->profile->permalink();
  63. if (! in_array($curAcctUrl, $pr['alsoKnownAs'])) {
  64. return 'The new account does not contain an alias to your current account.';
  65. }
  66. return 'noerr';
  67. }
  68. }