2023-03-24 13:54:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Actions\Fortify;
|
|
|
|
|
2023-04-25 08:06:45 +00:00
|
|
|
use App\Models\InstanceSettings;
|
2023-04-13 13:48:27 +00:00
|
|
|
use App\Models\Team;
|
2023-03-24 13:54:17 +00:00
|
|
|
use App\Models\User;
|
2023-04-13 13:48:27 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2023-03-24 13:54:17 +00:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
2023-04-14 10:54:29 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2023-03-24 13:54:17 +00:00
|
|
|
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 15:09:50 +00:00
|
|
|
$settings = InstanceSettings::get();
|
2023-04-25 08:06:45 +00:00
|
|
|
if (!$settings->is_registration_enabled) {
|
|
|
|
Log::info('Registration is disabled');
|
|
|
|
abort(403);
|
|
|
|
}
|
2023-03-24 13:54:17 +00:00
|
|
|
Validator::make($input, [
|
|
|
|
'name' => ['required', 'string', 'max:255'],
|
|
|
|
'email' => [
|
|
|
|
'required',
|
|
|
|
'string',
|
|
|
|
'email',
|
|
|
|
'max:255',
|
|
|
|
Rule::unique(User::class),
|
|
|
|
],
|
|
|
|
'password' => $this->passwordRules(),
|
|
|
|
])->validate();
|
|
|
|
|
2023-04-14 10:54:29 +00:00
|
|
|
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
|
|
|
|
$team = Team::find(0);
|
|
|
|
$user = User::create([
|
|
|
|
'id' => 0,
|
|
|
|
'name' => $input['name'],
|
|
|
|
'email' => $input['email'],
|
|
|
|
'password' => Hash::make($input['password']),
|
|
|
|
'is_root_user' => true,
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
$team = Team::create([
|
|
|
|
'name' => explode(' ', $input['name'], 2)[0] . "'s Team",
|
|
|
|
'personal_team' => true,
|
|
|
|
]);
|
|
|
|
$user = User::create([
|
|
|
|
'name' => $input['name'],
|
|
|
|
'email' => $input['email'],
|
|
|
|
'password' => Hash::make($input['password']),
|
|
|
|
'is_root_user' => false,
|
|
|
|
]);
|
|
|
|
}
|
2023-04-13 15:17:30 +00:00
|
|
|
|
2023-04-14 10:54:29 +00:00
|
|
|
// Add user to team
|
2023-04-13 13:48:27 +00:00
|
|
|
DB::table('team_user')->insert([
|
2023-04-13 15:17:30 +00:00
|
|
|
'user_id' => $user->id,
|
|
|
|
'team_id' => $team->id,
|
2023-04-13 13:48:27 +00:00
|
|
|
'role' => 'admin',
|
|
|
|
]);
|
2023-04-14 10:54:29 +00:00
|
|
|
|
|
|
|
// Set session variable
|
2023-04-13 15:17:30 +00:00
|
|
|
session(['currentTeam' => $user->currentTeam = $team]);
|
2023-04-13 13:48:27 +00:00
|
|
|
return $user;
|
2023-03-24 13:54:17 +00:00
|
|
|
}
|
|
|
|
}
|