Skip to content

Commit

Permalink
Merge pull request #11654 from nanaya/country-access
Browse files Browse the repository at this point in the history
 Use singleton for country lookup
  • Loading branch information
notbakaneko authored Nov 21, 2024
2 parents 3d826c2 + 604eff0 commit caae318
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
10 changes: 6 additions & 4 deletions app/Http/Controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use App\Libraries\UserRegistration;
use App\Models\Beatmap;
use App\Models\BeatmapDiscussion;
use App\Models\Country;
use App\Models\IpBan;
use App\Models\Log;
use App\Models\User;
Expand Down Expand Up @@ -983,9 +982,8 @@ private function storeUser(array $rawParams)
'username',
], ['null_missing' => true]);
$countryCode = request_country();
$country = Country::find($countryCode);
$params['user_ip'] = $ip;
$params['country_acronym'] = $country === null ? '' : $country->getKey();
$params['country_acronym'] = $countryCode;
$params['user_lang'] = \App::getLocale();

$registration = new UserRegistration($params);
Expand All @@ -1009,7 +1007,11 @@ private function storeUser(array $rawParams)
$user = $registration->user();

// report unknown country code but ignore non-country from cloudflare
if ($countryCode !== null && $country === null && $countryCode !== 'T1') {
if (
$countryCode !== null
&& $countryCode !== 'T1'
&& app('countries')->byCode($countryCode) === null
) {
app('sentry')->getClient()->captureMessage(
'User registered from unknown country',
null,
Expand Down
3 changes: 1 addition & 2 deletions app/Libraries/User/CountryChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

use App\Exceptions\InvariantException;
use App\Models\Beatmap;
use App\Models\Country;
use App\Models\User;
use App\Models\UserAccountHistory;

Expand All @@ -18,7 +17,7 @@ class CountryChange
public static function handle(User $user, string $newCountry, string $reason): void
{
// Assert valid country acronym
$country = Country::find($newCountry);
$country = app('countries')->byCode($newCountry);
if ($country === null) {
throw new InvariantException('invalid country specified');
}
Expand Down
7 changes: 4 additions & 3 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2320,11 +2320,12 @@ public function isValid()

if ($this->isDirty('country_acronym')) {
if (present($this->country_acronym)) {
if (($country = Country::find($this->country_acronym)) !== null) {
$country = app('countries')->byCode($this->country_acronym);
if ($country === null) {
$this->validationErrors()->add('country', '.invalid_country');
} else {
// ensure matching case
$this->country_acronym = $country->getKey();
} else {
$this->validationErrors()->add('country', '.invalid_country');
}
} else {
$this->country_acronym = Country::UNKNOWN;
Expand Down
5 changes: 5 additions & 0 deletions database/factories/CountryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class CountryFactory extends Factory
{
protected $model = Country::class;

public function configure(): static
{
return $this->afterCreating(fn () => app('countries')->resetMemoized());
}

public function definition(): array
{
return [
Expand Down

0 comments on commit caae318

Please sign in to comment.