mirror of
https://github.com/pelican-dev/panel.git
synced 2025-05-20 00:34: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 Carbon\Carbon;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use Webmozart\Assert\Assert;
|
|
||||||
use App\Models\Server;
|
use App\Models\Server;
|
||||||
use App\Models\ActivityLog;
|
use App\Models\ActivityLog;
|
||||||
use App\Models\ActivityLogSubject;
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Http\Requests\Api\Remote\ActivityEventRequest;
|
use App\Http\Requests\Api\Remote\ActivityEventRequest;
|
||||||
|
|
||||||
@ -71,19 +69,17 @@ class ActivityProcessingController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach ($logs as $key => $data) {
|
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) {
|
foreach ($data as $datum) {
|
||||||
$id = ActivityLog::insertGetId($datum);
|
/** @var ActivityLog $activityLog */
|
||||||
$batch[] = [
|
$activityLog = ActivityLog::forceCreate($datum);
|
||||||
'activity_log_id' => $id,
|
$activityLog->subjects()->create([
|
||||||
'subject_id' => $server->id,
|
'subject_id' => $server->id,
|
||||||
'subject_type' => $server->getMorphClass(),
|
'subject_type' => $server->getMorphClass(),
|
||||||
];
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
ActivityLogSubject::insert($batch);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -120,11 +120,6 @@ class ActivityLog extends Model implements HasIcon, HasLabel
|
|||||||
return $builder->whereMorphedTo('actor', $actor);
|
return $builder->whereMorphedTo('actor', $actor);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns models to be pruned.
|
|
||||||
*
|
|
||||||
* @see https://laravel.com/docs/9.x/eloquent#pruning-models
|
|
||||||
*/
|
|
||||||
public function prunable(): Builder
|
public function prunable(): Builder
|
||||||
{
|
{
|
||||||
if (is_null(config('activity.prune_days'))) {
|
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')));
|
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
|
protected static function boot(): void
|
||||||
{
|
{
|
||||||
parent::boot();
|
parent::boot();
|
||||||
|
@ -19,7 +19,7 @@ class RecoveryToken extends Model implements Validatable
|
|||||||
use HasValidation;
|
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;
|
public const UPDATED_AT = null;
|
||||||
|
|
||||||
|
@ -10,7 +10,6 @@ use Illuminate\Support\Collection;
|
|||||||
use App\Models\ActivityLog;
|
use App\Models\ActivityLog;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Support\Facades\Request;
|
use Illuminate\Support\Facades\Request;
|
||||||
use App\Models\ActivityLogSubject;
|
|
||||||
use App\Models\Server;
|
use App\Models\Server;
|
||||||
use Filament\Facades\Filament;
|
use Filament\Facades\Filament;
|
||||||
use Illuminate\Database\ConnectionInterface;
|
use Illuminate\Database\ConnectionInterface;
|
||||||
@ -236,16 +235,12 @@ class ActivityLogService
|
|||||||
$response = $this->connection->transaction(function () {
|
$response = $this->connection->transaction(function () {
|
||||||
$this->activity->save();
|
$this->activity->save();
|
||||||
|
|
||||||
$subjects = Collection::make($this->subjects)
|
foreach ($this->subjects as $subject) {
|
||||||
->map(fn (Model $subject) => [
|
$this->activity->subjects()->forceCreate([
|
||||||
'activity_log_id' => $this->activity->id,
|
|
||||||
'subject_id' => $subject->getKey(),
|
'subject_id' => $subject->getKey(),
|
||||||
'subject_type' => $subject->getMorphClass(),
|
'subject_type' => $subject->getMorphClass(),
|
||||||
])
|
]);
|
||||||
->values()
|
}
|
||||||
->toArray();
|
|
||||||
|
|
||||||
ActivityLogSubject::insert($subjects);
|
|
||||||
|
|
||||||
return $this->activity;
|
return $this->activity;
|
||||||
});
|
});
|
||||||
|
@ -70,7 +70,7 @@ class AssignmentService
|
|||||||
throw new InvalidPortMappingException($port);
|
throw new InvalidPortMappingException($port);
|
||||||
}
|
}
|
||||||
|
|
||||||
$insertData = [];
|
$newAllocations = [];
|
||||||
if (preg_match(self::PORT_RANGE_REGEX, $port, $matches)) {
|
if (preg_match(self::PORT_RANGE_REGEX, $port, $matches)) {
|
||||||
$block = range($matches[1], $matches[2]);
|
$block = range($matches[1], $matches[2]);
|
||||||
|
|
||||||
@ -83,7 +83,7 @@ class AssignmentService
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach ($block as $unit) {
|
foreach ($block as $unit) {
|
||||||
$insertData[] = [
|
$newAllocations[] = [
|
||||||
'node_id' => $node->id,
|
'node_id' => $node->id,
|
||||||
'ip' => $ip->__toString(),
|
'ip' => $ip->__toString(),
|
||||||
'port' => (int) $unit,
|
'port' => (int) $unit,
|
||||||
@ -96,7 +96,7 @@ class AssignmentService
|
|||||||
throw new PortOutOfRangeException();
|
throw new PortOutOfRangeException();
|
||||||
}
|
}
|
||||||
|
|
||||||
$insertData[] = [
|
$newAllocations[] = [
|
||||||
'node_id' => $node->id,
|
'node_id' => $node->id,
|
||||||
'ip' => $ip->__toString(),
|
'ip' => $ip->__toString(),
|
||||||
'port' => (int) $port,
|
'port' => (int) $port,
|
||||||
@ -105,8 +105,8 @@ class AssignmentService
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($insertData as $insert) {
|
foreach ($newAllocations as $newAllocation) {
|
||||||
$allocation = Allocation::query()->create($insert);
|
$allocation = Allocation::query()->create($newAllocation);
|
||||||
$ids[] = $allocation->id;
|
$ids[] = $allocation->id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
namespace App\Services\Servers;
|
namespace App\Services\Servers;
|
||||||
|
|
||||||
use App\Enums\ServerState;
|
use App\Enums\ServerState;
|
||||||
use App\Models\ServerVariable;
|
|
||||||
use Illuminate\Http\Client\ConnectionException;
|
use Illuminate\Http\Client\ConnectionException;
|
||||||
use Ramsey\Uuid\Uuid;
|
use Ramsey\Uuid\Uuid;
|
||||||
use Illuminate\Support\Arr;
|
use Illuminate\Support\Arr;
|
||||||
@ -199,20 +198,11 @@ class ServerCreationService
|
|||||||
*/
|
*/
|
||||||
private function storeEggVariables(Server $server, Collection $variables): void
|
private function storeEggVariables(Server $server, Collection $variables): void
|
||||||
{
|
{
|
||||||
$now = now();
|
foreach ($variables as $variable) {
|
||||||
|
$server->serverVariables()->forceCreate([
|
||||||
$records = $variables->map(function ($result) use ($server, $now) {
|
'variable_id' => $variable->id,
|
||||||
return [
|
'variable_value' => $variable->value ?? '',
|
||||||
'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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,9 +2,6 @@
|
|||||||
|
|
||||||
namespace App\Services\Users;
|
namespace App\Services\Users;
|
||||||
|
|
||||||
use App\Models\RecoveryToken;
|
|
||||||
use Carbon\Carbon;
|
|
||||||
use Illuminate\Support\Str;
|
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use PragmaRX\Google2FA\Google2FA;
|
use PragmaRX\Google2FA\Google2FA;
|
||||||
use Illuminate\Database\ConnectionInterface;
|
use Illuminate\Database\ConnectionInterface;
|
||||||
@ -49,27 +46,14 @@ class ToggleTwoFactorService
|
|||||||
// on their account.
|
// on their account.
|
||||||
$tokens = [];
|
$tokens = [];
|
||||||
if ((!$toggleState && !$user->use_totp) || $toggleState) {
|
if ((!$toggleState && !$user->use_totp) || $toggleState) {
|
||||||
$inserts = [];
|
$user->recoveryTokens()->delete();
|
||||||
for ($i = 0; $i < 10; $i++) {
|
for ($i = 0; $i < 10; $i++) {
|
||||||
$token = Str::random(10);
|
$token = str_random(10);
|
||||||
|
$user->recoveryTokens()->forceCreate([
|
||||||
$inserts[] = [
|
|
||||||
'user_id' => $user->id,
|
|
||||||
'token' => password_hash($token, PASSWORD_DEFAULT),
|
'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;
|
$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();
|
$user->totp_authenticated_at = now();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user