Skip to content

Commit

Permalink
Merge pull request #21 from liberu-genealogy/sweep/allow_one_dockerco…
Browse files Browse the repository at this point in the history
…mposeryml_instance_for_3288d

Sweep: Allow one docker-compose.yml instance for free hosting plan, unlimited instances for premium hosting plan
  • Loading branch information
curtisdelicata authored Jun 16, 2024
2 parents 8069b17 + b3e1510 commit 8428498
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ class CreateDomain extends CreateRecord
protected function handleRecordCreation(array $data): Domain
{
$user = auth()->user();

if ($user->hosting_plan === 'free' && $user->domains()->count() >= 1) {
throw new \Exception('Free users are limited to one domain.');
if ($user->hasReachedDockerComposeLimit()) {
throw new \Exception('You have reached the limit of Docker Compose instances for your hosting plan.');
}

$domain = static::getModel()::create($data);
$domain = static::getModel()::create([
...$data,
'hosting_plan_id' => $user->currentHostingPlan()->id,
]);

$composeContent = $this->generateDockerComposeContent($data);
Storage::disk('local')->put('docker-compose-'.$data['domain_name'].'.yml', $composeContent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ protected function getHeaderActions(): array

protected function handleRecordUpdate(Domain $record, array $data): Domain
{
$user = auth()->user();
if ($user->hasReachedDockerComposeLimit()) {
throw new \Exception('You have reached the limit of Docker Compose instances for your hosting plan.');
}

$record->update($data);

$composeContent = $this->generateDockerComposeContent($data);
Expand Down
9 changes: 9 additions & 0 deletions app/Models/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Domain extends Model
'domain_name',
'registration_date',
'expiration_date',
'hosting_plan_id',
];

/**
Expand All @@ -46,6 +47,14 @@ public function user()
return $this->belongsTo(User::class);
}

/**
* Get the hosting plan associated with the domain.
*/
public function hostingPlan()
{
return $this->belongsTo(UserHostingPlan::class);
}

/**
* Get the email accounts for the domain.
*/
Expand Down
34 changes: 24 additions & 10 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,6 @@ class User extends Authenticatable
'name',
'email',
'password',
'hosting_plan',
];

/**
* The model's default values for attributes.
*
* @var array<string, mixed>
*/
protected $attributes = [
'hosting_plan' => 'free',
];

/**
Expand Down Expand Up @@ -108,6 +98,30 @@ public function resourceUsages()
return $this->hasMany(ResourceUsage::class);
}

public function hasReachedDockerComposeLimit(): bool
{
$currentPlan = $this->currentHostingPlan();

if (!$currentPlan) {
return true; // Default to true if no hosting plan is found
}

if ($currentPlan->name === 'free') {
return $this->domains()->count() >= 1;
}

if ($currentPlan->name === 'premium') {
return false; // Unlimited instances for premium plan
}

return true; // Default to true for other plans
}

public function currentHostingPlan()
{
return $this->userHostingPlans()->latest()->first();
}



}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('domains', function (Blueprint $table) {
$table->id();
$table->string('domain_name');
$table->foreignId('user_id')->constrained()->onDelete('cascade');
$table->foreignId('hosting_plan_id')->constrained('user_hosting_plans')->onDelete('cascade');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('domains');
}
};

0 comments on commit 8428498

Please sign in to comment.