ProfileMigrationStoreRequest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 ((bool) config_cache('federation.activitypub.enabled') === false ||
  16. (bool) config_cache('federation.migration') === false) {
  17. return false;
  18. }
  19. if (! $this->user() || $this->user()->status) {
  20. return false;
  21. }
  22. return true;
  23. }
  24. /**
  25. * Get the validation rules that apply to the request.
  26. *
  27. * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
  28. */
  29. public function rules(): array
  30. {
  31. return [
  32. 'acct' => 'required|email',
  33. 'password' => 'required|current_password',
  34. ];
  35. }
  36. public function after(): array
  37. {
  38. return [
  39. function (Validator $validator) {
  40. $err = $this->validateNewAccount();
  41. if ($err !== 'noerr') {
  42. $validator->errors()->add(
  43. 'acct',
  44. $err
  45. );
  46. }
  47. },
  48. ];
  49. }
  50. protected function validateNewAccount()
  51. {
  52. if (ProfileMigration::whereProfileId($this->user()->profile_id)->where('created_at', '>', now()->subDays(30))->exists()) {
  53. return 'Error - You have migrated your account in the past 30 days, you can only perform a migration once per 30 days.';
  54. }
  55. $acct = WebfingerService::rawGet($this->acct);
  56. if (! $acct) {
  57. return 'The new account you provided is not responding to our requests.';
  58. }
  59. $pr = FetchCacheService::getJson($acct);
  60. if (! $pr || ! isset($pr['alsoKnownAs'])) {
  61. return 'Invalid account lookup response.';
  62. }
  63. if (! count($pr['alsoKnownAs']) || ! is_array($pr['alsoKnownAs'])) {
  64. return 'The new account does not contain an alias to your current account.';
  65. }
  66. $curAcctUrl = $this->user()->profile->permalink();
  67. if (! in_array($curAcctUrl, $pr['alsoKnownAs'])) {
  68. return 'The new account does not contain an alias to your current account.';
  69. }
  70. return 'noerr';
  71. }
  72. }