72 lines
2.6 KiB
PHP
72 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\Fortify;
|
|
|
|
use App\Concerns\PasswordValidationRules;
|
|
use App\Concerns\ProfileValidationRules;
|
|
use App\Models\BillingOrder;
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use App\Services\TenantProvisioningService;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\Rule;
|
|
use Laravel\Fortify\Contracts\CreatesNewUsers;
|
|
|
|
class CreateNewUser implements CreatesNewUsers
|
|
{
|
|
use PasswordValidationRules, ProfileValidationRules;
|
|
|
|
/**
|
|
* Validate and create a newly registered user.
|
|
*
|
|
* @param array<string, string> $input
|
|
*/
|
|
public function __construct(private readonly TenantProvisioningService $provisioning) {}
|
|
|
|
public function create(array $input): User
|
|
{
|
|
Validator::make($input, [
|
|
'tenant_name' => ['required', 'string', 'max:255'],
|
|
...$this->profileRules(),
|
|
'password' => $this->passwordRules(),
|
|
'deployment_type' => ['required', Rule::in(['managed_cloud', 'self_hosted'])],
|
|
'deployment_domain' => ['nullable', 'string', 'max:255'],
|
|
'plan_code' => ['required', Rule::in(array_keys(config('billing.plans')))],
|
|
])->validate();
|
|
|
|
abort_unless(config('deployment.mode') === 'managed_cloud', 403);
|
|
|
|
$baseSlug = Str::slug($input['tenant_name']) ?: 'tenant';
|
|
$slug = $baseSlug;
|
|
$suffix = 2;
|
|
while (Tenant::where('slug', $slug)->exists()) {
|
|
$slug = $baseSlug.'-'.$suffix++;
|
|
}
|
|
|
|
$tenant = $this->provisioning->createWithOwner([
|
|
'name' => $input['tenant_name'],
|
|
'slug' => $slug,
|
|
'owner_name' => $input['name'],
|
|
'owner_email' => $input['email'],
|
|
'owner_password' => $input['password'],
|
|
'deployment_type' => $input['deployment_type'],
|
|
'deployment_domain' => $input['deployment_domain'] ?? null,
|
|
'plan_code' => $input['plan_code'],
|
|
'email_verified' => false,
|
|
'activate_license' => false,
|
|
]);
|
|
|
|
$plan = config("billing.plans.{$input['plan_code']}");
|
|
if ($plan['price'] > 0) {
|
|
$gateway = config('billing.default_gateway');
|
|
$order = BillingOrder::withoutGlobalScope('tenant')->create([
|
|
'tenant_id' => $tenant->id, 'plan_code' => $input['plan_code'], 'gateway' => $gateway,
|
|
'status' => 'pending', 'subtotal' => $plan['price'], 'tax' => 0, 'total' => $plan['price'], 'currency' => 'IDR', 'expires_at' => now()->addDay(),
|
|
]);
|
|
}
|
|
|
|
return User::withoutGlobalScope('tenant')->where('email', $input['email'])->firstOrFail();
|
|
}
|
|
}
|