mirror of https://github.com/pixelfed/pixelfed
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
2.7 KiB
PHP
90 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use App\Models\ProfileMigration;
|
|
use App\Services\ActivityPubFetchService;
|
|
use App\Services\WebfingerService;
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Validator;
|
|
|
|
class ProfileMigrationStoreRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
if (
|
|
(bool) config_cache('federation.activitypub.enabled') === false ||
|
|
(bool) config_cache('federation.migration') === false
|
|
) {
|
|
return false;
|
|
}
|
|
if (! $this->user() || $this->user()->status) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'acct' => 'required|email',
|
|
'password' => 'required|current_password',
|
|
];
|
|
}
|
|
|
|
public function after(): array
|
|
{
|
|
return [
|
|
function (Validator $validator) {
|
|
$err = $this->validateNewAccount();
|
|
if ($err !== 'noerr') {
|
|
$validator->errors()->add(
|
|
'acct',
|
|
$err
|
|
);
|
|
}
|
|
},
|
|
];
|
|
}
|
|
|
|
protected function validateNewAccount()
|
|
{
|
|
if (ProfileMigration::whereProfileId($this->user()->profile_id)->where('created_at', '>', now()->subDays(30))->exists()) {
|
|
return 'Error - You have migrated your account in the past 30 days, you can only perform a migration once per 30 days.';
|
|
}
|
|
$acct = WebfingerService::rawGet($this->acct);
|
|
if (! $acct) {
|
|
return 'The new account you provided is not responding to our requests.';
|
|
}
|
|
$prRes = ActivityPubFetchService::get($acct, true);
|
|
if (empty($prRes)) {
|
|
return 'Invalid response.';
|
|
}
|
|
$pr = json_decode($prRes, true);
|
|
if (! $pr || ! isset($pr['alsoKnownAs'])) {
|
|
return 'Invalid account lookup response.';
|
|
}
|
|
if (! count($pr['alsoKnownAs']) || ! is_array($pr['alsoKnownAs'])) {
|
|
return 'The new account does not contain an alias to your current account.';
|
|
}
|
|
$curAcctUrl = $this->user()->profile->permalink();
|
|
$aka = (array) $pr['alsoKnownAs'];
|
|
|
|
if (empty($aka) || ! in_array($curAcctUrl, $aka)) {
|
|
return 'The new account does not contain an alias to your current account.';
|
|
}
|
|
|
|
return 'noerr';
|
|
}
|
|
}
|