coolify/app/Actions/Fortify/CreateNewUser.php

67 lines
2.0 KiB
PHP
Raw Normal View History

<?php
namespace App\Actions\Fortify;
2023-04-25 10:06:45 +02:00
use App\Models\InstanceSettings;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Laravel\Fortify\Contracts\CreatesNewUsers;
class CreateNewUser implements CreatesNewUsers
{
use PasswordValidationRules;
/**
* Validate and create a newly registered user.
*
* @param array<string, string> $input
*/
public function create(array $input): User
{
2023-05-16 17:09:50 +02:00
$settings = InstanceSettings::get();
2023-04-25 10:06:45 +02:00
if (!$settings->is_registration_enabled) {
abort(403);
}
Validator::make($input, [
'name' => ['required', 'string', 'max:255'],
'email' => [
'required',
'string',
'email',
'max:255',
Rule::unique(User::class),
],
'password' => $this->passwordRules(),
])->validate();
if (User::count() == 0) {
// If this is the first user, make them the root user
// Team is already created in the database/seeders/ProductionSeeder.php
$user = User::create([
'id' => 0,
'name' => $input['name'],
'email' => $input['email'],
'password' => Hash::make($input['password']),
]);
2023-06-09 15:55:21 +02:00
$team = $user->teams()->first();
2023-06-13 15:59:25 +02:00
// Disable registration after first user is created
$settings = InstanceSettings::get();
$settings->is_registration_enabled = false;
$settings->save();
} else {
$user = User::create([
'name' => $input['name'],
'email' => $input['email'],
'password' => Hash::make($input['password']),
]);
2023-06-09 15:55:21 +02:00
$team = $user->teams()->first();
}
// Set session variable
2023-04-13 17:17:30 +02:00
session(['currentTeam' => $user->currentTeam = $team]);
2023-04-13 15:48:27 +02:00
return $user;
}
}