mirror of
https://github.com/pelican-dev/panel.git
synced 2025-05-19 21:04:44 +02:00
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:
parent
a03b604f2d
commit
875dca54f5
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
});
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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 ?? '',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user