Skip to content

Commit 0e7cfe4

Browse files
committed
fix: #72 assign correct team when the service is accessed via api
1 parent 05bc449 commit 0e7cfe4

14 files changed

+90
-9
lines changed

app/Http/Controllers/NodeController.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ public function create()
3636
*/
3737
public function store(StoreNodeRequest $request)
3838
{
39-
$node = Node::create($request->validated());
39+
$node = Node::make($request->validated());
40+
$node->team_id = auth()->user()->current_team_id;
41+
$node->save();
4042

4143
return to_route('nodes.show', ['node' => $node->id]);
4244
}

app/Http/Controllers/ServiceController.php

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function store(StoreServiceRequest $request)
6262
$deploymentData = DeploymentData::validateAndCreate($request->get('deploymentData'));
6363

6464
$service = Service::make($request->validated());
65+
$service->team_id = auth()->user()->current_team_id;
6566
DB::transaction(function () use ($service, $deploymentData) {
6667
$service->save();
6768

app/Http/Controllers/SwarmController.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,11 @@ public function updateDockerRegistries(Swarm $swarm, Request $request)
140140
'type' => NodeTaskGroupType::UpdateDockerRegistries,
141141
'swarm_id' => $swarm->id,
142142
'invoker_id' => auth()->user()->id,
143+
'team_id' => auth()->user()->current_team_id,
143144
]);
144145

145146
$taskGroup->tasks()->createMany($tasks);
146147
}
147148
});
148-
149-
// return back();
150149
}
151150
}

app/Http/Controllers/SwarmTaskController.php

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Http\Controllers;
44

55
use App\Http\Requests\NodeTask\InitClusterFormRequest;
6+
use App\Models\Deployment;
67
use App\Models\DeploymentData;
78
use App\Models\DeploymentData\LaunchMode;
89
use App\Models\DeploymentData\ReleaseCommand;
@@ -14,6 +15,7 @@
1415
use App\Models\NodeTasks\InitSwarm\InitSwarmMeta;
1516
use App\Models\NodeTasks\UpdateCurrentNode\UpdateCurrentNodeMeta;
1617
use App\Models\NodeTaskType;
18+
use App\Models\Service;
1719
use App\Models\Swarm;
1820
use App\Models\SwarmData;
1921
use Illuminate\Support\Facades\DB;
@@ -25,6 +27,7 @@ public function initCluster(InitClusterFormRequest $request)
2527
DB::transaction(function () use ($request) {
2628
$swarm = Swarm::create([
2729
'name' => $request->name,
30+
'team_id' => auth()->user()->current_team_id,
2831
'data' => SwarmData::validateAndCreate([
2932
'registriesRev' => 0,
3033
'registries' => [],
@@ -38,6 +41,7 @@ public function initCluster(InitClusterFormRequest $request)
3841

3942
$network = Network::create([
4043
'swarm_id' => $swarm->id,
44+
'team_id' => auth()->user()->current_team_id,
4145
'name' => dockerize_name('ptah-net'),
4246
]);
4347

@@ -46,6 +50,7 @@ public function initCluster(InitClusterFormRequest $request)
4650
'swarm_id' => $swarm->id,
4751
'node_id' => $request->node_id,
4852
'invoker_id' => auth()->user()->id,
53+
'team_id' => auth()->user()->current_team_id,
4954
]);
5055

5156
$tasks = [
@@ -108,9 +113,11 @@ public function initCluster(InitClusterFormRequest $request)
108113

109114
$caddyService = $swarm->services()->create([
110115
'name' => 'caddy',
116+
'team_id' => auth()->user()->current_team_id,
111117
]);
112118

113119
$deployment = $caddyService->deployments()->create([
120+
'team_id' => auth()->user()->current_team_id,
114121
'data' => DeploymentData::validateAndCreate([
115122
'networkName' => $network->docker_name,
116123
'internalDomain' => 'caddy.ptah.local',
@@ -173,10 +180,13 @@ public function initCluster(InitClusterFormRequest $request)
173180
'replicas' => 1,
174181
'caddy' => [],
175182
'fastcgiVars' => null,
183+
'redirectRules' => [],
176184
],
177185
],
178186
]),
179187
]);
188+
$deployment->team_id = auth()->user()->current_team_id;
189+
$deployment->save();
180190

181191
foreach ($deployment->asNodeTasks() as $task) {
182192
$tasks[] = $task;

app/Models/Deployment.php

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class Deployment extends Model
2323
use HasOwningTeam;
2424

2525
protected $fillable = [
26+
'service_id',
27+
'team_id',
2628
'task_group_id',
2729
'data',
2830
];
@@ -124,6 +126,7 @@ public function asNodeTasks(): array
124126
'response' => [
125127
'set' => [
126128
'X-Powered-By' => ['https://ptah.sh'],
129+
'X-Ptah-Rule-Id' => [$caddy->id],
127130
],
128131
],
129132
],
@@ -158,6 +161,7 @@ public function asNodeTasks(): array
158161
'status_code' => (string) $redirectRule->statusCode,
159162
'headers' => [
160163
'X-Powered-By' => ['https://ptah.sh'],
164+
'X-Ptah-Rule-Id' => [$redirectRule->id],
161165
'Location' => ["{http.request.scheme}://{$redirectRule->domainTo}{$pathTo}"],
162166
],
163167
]

app/Models/DeploymentData/Caddy.php

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
class Caddy extends Data
1111
{
1212
public function __construct(
13+
public string $id,
1314
#[In(['http', 'fastcgi'])]
1415
public string $targetProtocol,
1516
#[Between(1, 65535)]

app/Models/Network.php

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Network extends Model
1414
protected $fillable = [
1515
'swarm_id',
1616
'name',
17+
'team_id',
1718
];
1819

1920
protected static function booted(): void

app/Models/Node.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ class Node extends Model
2121

2222
protected $fillable = [
2323
'name',
24+
'team_id',
2425
];
2526

2627
protected $appends = [
2728
'online',
2829
];
2930

30-
protected static function booted()
31+
protected static function booted(): void
3132
{
3233
self::creating(function (Node $node) {
3334
$node->agent_token = Str::random(42);
@@ -75,6 +76,7 @@ public function upgradeAgent($targetVersion): void
7576
'node_id' => $this->id,
7677
'type' => NodeTaskGroupType::SelfUpgrade,
7778
'invoker_id' => auth()->user()->id,
79+
'team_id' => auth()->user()->current_team_id,
7880
]);
7981

8082
$taskGroup->tasks()->createMany([

app/Models/NodeTask.php

+7
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ class NodeTask extends Model
3535
'formatted_result',
3636
];
3737

38+
protected static function booted(): void
39+
{
40+
self::creating(function (NodeTask $nodeTask) {
41+
$nodeTask->team_id = $nodeTask->taskGroup->team_id;
42+
});
43+
}
44+
3845
public function taskGroup(): BelongsTo
3946
{
4047
return $this->belongsTo(NodeTaskGroup::class);

app/Models/NodeTaskGroup.php

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class NodeTaskGroup extends Model
2525
'swarm_id',
2626
'node_id',
2727
'invoker_id',
28+
'team_id',
2829
];
2930

3031
public function swarm(): BelongsTo
@@ -75,6 +76,7 @@ public function retry(Node|null $node): void
7576
$taskGroup->forceFill(collect($this->attributes)->only([
7677
'swarm_id',
7778
'invoker_id',
79+
'team_id',
7880
])->toArray());
7981
$taskGroup->save();
8082

app/Models/Service.php

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Service extends Model
2424
protected $fillable = [
2525
'name',
2626
'swarm_id',
27+
'team_id',
2728
];
2829

2930
protected static function booted()
@@ -87,11 +88,13 @@ public function deploy(DeploymentData $deploymentData): Deployment
8788

8889
$taskGroup = NodeTaskGroup::create([
8990
'swarm_id' => $this->swarm_id,
91+
'team_id' => $this->team_id,
9092
'invoker_id' => auth()->id(),
9193
'type' => $this->deployments()->exists() ? NodeTaskGroupType::UpdateService : NodeTaskGroupType::CreateService,
9294
]);
9395

9496
$deployment = $this->deployments()->create([
97+
'team_id' => $this->team_id,
9598
'data' => $deploymentData,
9699
]);
97100

app/Models/Swarm.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class Swarm extends Model
1515

1616
protected $fillable = [
1717
'name',
18-
'data'
18+
'data',
19+
'team_id',
1920
];
2021

2122
protected $casts = [

app/Traits/HasOwningTeam.php

-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ trait HasOwningTeam
1010
protected static function bootHasOwningTeam(): void
1111
{
1212
static::addGlobalScope(new TeamScope());
13-
14-
static::creating(function ($model) {
15-
$model->team_id = auth()->user()->currentTeam->id;
16-
});
1713
}
1814

1915
public function team()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\DB;
6+
use Illuminate\Support\Facades\Schema;
7+
8+
return new class extends Migration
9+
{
10+
/**
11+
* Run the migrations.
12+
*/
13+
public function up(): void
14+
{
15+
DB::update("
16+
UPDATE deployments
17+
SET data = jsonb_set(
18+
data,
19+
'{processes}',
20+
(
21+
SELECT jsonb_agg(
22+
jsonb_set(
23+
process,
24+
'{caddy}',
25+
COALESCE(
26+
(
27+
SELECT jsonb_agg(
28+
caddy_item || jsonb_build_object(
29+
'id',
30+
concat('caddy-', substr(md5(random()::text), 1, 11))
31+
)
32+
)
33+
FROM jsonb_array_elements(process->'caddy') AS caddy_item
34+
),
35+
'[]'::jsonb
36+
)
37+
)
38+
)
39+
FROM jsonb_array_elements(data->'processes') AS process
40+
)
41+
);
42+
");
43+
}
44+
45+
/**
46+
* Reverse the migrations.
47+
*/
48+
public function down(): void
49+
{
50+
//
51+
}
52+
};

0 commit comments

Comments
 (0)