Switch inserts to proper creates (#1190)

* Switch inserts to proper creates

* Push `$token` to `$tokens[]` in `ToggleTwoFactorService`

---------

Co-authored-by: RMartinOscar <40749467+RMartinOscar@users.noreply.github.com>
This commit is contained in:
Lance Pioch 2025-03-30 21:56:49 -04:00 committed by GitHub
parent a03b604f2d
commit 875dca54f5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 25 additions and 69 deletions

View File

@ -5,10 +5,8 @@ namespace App\Http\Controllers\Api\Remote;
use Carbon\Carbon;
use Illuminate\Support\Str;
use App\Models\User;
use Webmozart\Assert\Assert;
use App\Models\Server;
use App\Models\ActivityLog;
use App\Models\ActivityLogSubject;
use App\Http\Controllers\Controller;
use App\Http\Requests\Api\Remote\ActivityEventRequest;
@ -71,19 +69,17 @@ class ActivityProcessingController extends Controller
}
foreach ($logs as $key => $data) {
Assert::isInstanceOf($server = $servers->get($key), Server::class);
$server = $servers->get($key);
assert($server instanceof Server);
$batch = [];
foreach ($data as $datum) {
$id = ActivityLog::insertGetId($datum);
$batch[] = [
'activity_log_id' => $id,
/** @var ActivityLog $activityLog */
$activityLog = ActivityLog::forceCreate($datum);
$activityLog->subjects()->create([
'subject_id' => $server->id,
'subject_type' => $server->getMorphClass(),
];
]);
}
ActivityLogSubject::insert($batch);
}
}
}

View File

@ -120,11 +120,6 @@ class ActivityLog extends Model implements HasIcon, HasLabel
return $builder->whereMorphedTo('actor', $actor);
}
/**
* Returns models to be pruned.
*
* @see https://laravel.com/docs/9.x/eloquent#pruning-models
*/
public function prunable(): Builder
{
if (is_null(config('activity.prune_days'))) {
@ -134,10 +129,6 @@ class ActivityLog extends Model implements HasIcon, HasLabel
return static::where('timestamp', '<=', Carbon::now()->subDays(config('activity.prune_days')));
}
/**
* Boots the model event listeners. This will trigger an activity log event every
* time a new model is inserted which can then be captured and worked with as needed.
*/
protected static function boot(): void
{
parent::boot();

View File

@ -19,7 +19,7 @@ class RecoveryToken extends Model implements Validatable
use HasValidation;
/**
* There are no updates to this model, only inserts and deletes.
* There are no updates to this model, only creates and deletes.
*/
public const UPDATED_AT = null;

View File

@ -10,7 +10,6 @@ use Illuminate\Support\Collection;
use App\Models\ActivityLog;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Request;
use App\Models\ActivityLogSubject;
use App\Models\Server;
use Filament\Facades\Filament;
use Illuminate\Database\ConnectionInterface;
@ -236,16 +235,12 @@ class ActivityLogService
$response = $this->connection->transaction(function () {
$this->activity->save();
$subjects = Collection::make($this->subjects)
->map(fn (Model $subject) => [
'activity_log_id' => $this->activity->id,
foreach ($this->subjects as $subject) {
$this->activity->subjects()->forceCreate([
'subject_id' => $subject->getKey(),
'subject_type' => $subject->getMorphClass(),
])
->values()
->toArray();
ActivityLogSubject::insert($subjects);
]);
}
return $this->activity;
});

View File

@ -70,7 +70,7 @@ class AssignmentService
throw new InvalidPortMappingException($port);
}
$insertData = [];
$newAllocations = [];
if (preg_match(self::PORT_RANGE_REGEX, $port, $matches)) {
$block = range($matches[1], $matches[2]);
@ -83,7 +83,7 @@ class AssignmentService
}
foreach ($block as $unit) {
$insertData[] = [
$newAllocations[] = [
'node_id' => $node->id,
'ip' => $ip->__toString(),
'port' => (int) $unit,
@ -96,7 +96,7 @@ class AssignmentService
throw new PortOutOfRangeException();
}
$insertData[] = [
$newAllocations[] = [
'node_id' => $node->id,
'ip' => $ip->__toString(),
'port' => (int) $port,
@ -105,8 +105,8 @@ class AssignmentService
];
}
foreach ($insertData as $insert) {
$allocation = Allocation::query()->create($insert);
foreach ($newAllocations as $newAllocation) {
$allocation = Allocation::query()->create($newAllocation);
$ids[] = $allocation->id;
}
}

View File

@ -3,7 +3,6 @@
namespace App\Services\Servers;
use App\Enums\ServerState;
use App\Models\ServerVariable;
use Illuminate\Http\Client\ConnectionException;
use Ramsey\Uuid\Uuid;
use Illuminate\Support\Arr;
@ -199,20 +198,11 @@ class ServerCreationService
*/
private function storeEggVariables(Server $server, Collection $variables): void
{
$now = now();
$records = $variables->map(function ($result) use ($server, $now) {
return [
'server_id' => $server->id,
'variable_id' => $result->id,
'variable_value' => $result->value ?? '',
'created_at' => $now,
'updated_at' => $now,
];
})->toArray();
if (!empty($records)) {
ServerVariable::query()->insert($records);
foreach ($variables as $variable) {
$server->serverVariables()->forceCreate([
'variable_id' => $variable->id,
'variable_value' => $variable->value ?? '',
]);
}
}

View File

@ -2,9 +2,6 @@
namespace App\Services\Users;
use App\Models\RecoveryToken;
use Carbon\Carbon;
use Illuminate\Support\Str;
use App\Models\User;
use PragmaRX\Google2FA\Google2FA;
use Illuminate\Database\ConnectionInterface;
@ -49,27 +46,14 @@ class ToggleTwoFactorService
// on their account.
$tokens = [];
if ((!$toggleState && !$user->use_totp) || $toggleState) {
$inserts = [];
$user->recoveryTokens()->delete();
for ($i = 0; $i < 10; $i++) {
$token = Str::random(10);
$inserts[] = [
'user_id' => $user->id,
$token = str_random(10);
$user->recoveryTokens()->forceCreate([
'token' => password_hash($token, PASSWORD_DEFAULT),
// insert() won't actually set the time on the models, so make sure we do this
// manually here.
'created_at' => Carbon::now(),
];
]);
$tokens[] = $token;
}
// Before inserting any new records make sure all the old ones are deleted to avoid
// any issues or storing an unnecessary number of tokens in the database.
$user->recoveryTokens()->delete();
// Bulk insert the hashed tokens.
RecoveryToken::query()->insert($inserts);
}
$user->totp_authenticated_at = now();