Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions resources/views/add_default_user.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
use Illuminate\Support\Facades\Hash;
use RonasIT\Support\Traits\MigrationTrait;

class AddDefaultUser extends Migration
class {{ $migrationName }} extends Migration
{
use MigrationTrait;

public function up()
public function up(): void
{
if (!App::environment('testing')) {
DB::table('users')->insert([
Expand All @@ -20,7 +20,7 @@ public function up()
}
}

public function down()
public function down(): void
{
if (!App::environment('testing')) {
DB::table('users')
Expand Down
29 changes: 29 additions & 0 deletions resources/views/admins_add_additional_admin.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use RonasIT\Support\Traits\MigrationTrait;

class {{ $migrationName }} extends Migration
{
use MigrationTrait;

public function up(): void
{
if (!App::environment('testing')) {
DB::table('admins')->insert([
'email' => '{{ $email }}',
'password' => Hash::make('{{ $password }}'),
]);
}
}

public function down(): void
{
if (!App::environment('testing')) {
DB::table('admins')
->where('email', '{{ $email }}')
->delete();
}
}
}
78 changes: 52 additions & 26 deletions src/Commands/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,29 +261,21 @@ protected function setAutoDocContactEmail(string $email): void
$config->write();
}

protected function createAdminUser(string $kebabName): void
protected function createAdminUser(string $kebabName, string $serviceName = '', string $adminPrefix = 'an'): array
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
protected function createAdminUser(string $kebabName, string $serviceName = '', string $adminPrefix = 'an'): array
protected function createAdminUser(string $kebabName, string $serviceKey = '', string $serviceName = ''): array

{
$adminEmail = $serviceName ? "admin.{$serviceName}@{$kebabName}.com" : "admin@{$kebabName}.com";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$adminEmail = $serviceName ? "admin.{$serviceName}@{$kebabName}.com" : "admin@{$kebabName}.com";
$adminEmail = when($serviceName, "admin.{$serviceName}@{$kebabName}.com", "admin@{$kebabName}.com");

$defaultPassword = substr(md5(uniqid()), 0, 8);

$this->adminCredentials = [
'email' => $this->ask('Please enter an admin email', "admin@{$kebabName}.com"),
'password' => $this->ask('Please enter an admin password', $defaultPassword),
$adminCredentials = [
'email' => $this->ask("Please enter {$adminPrefix} admin email", $adminEmail),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'email' => $this->ask("Please enter {$adminPrefix} admin email", $adminEmail),
'email' => $this->ask("Please enter admin email for {$serviceName}", $adminEmail),

'password' => $this->ask("Please enter {$adminPrefix} admin password", $defaultPassword),
];

if ($this->authType === AuthTypeEnum::Clerk) {
$this->publishMigration(
view: view('initializator::admins_create_table')->with($this->adminCredentials),
migrationName: 'admins_create_table',
);
} else {
$this->adminCredentials['name'] = $this->ask('Please enter an admin name', 'Admin');
$this->adminCredentials['role_id'] = $this->ask('Please enter an admin role id', RoleEnum::Admin->value);

$this->publishMigration(
view: view('initializator::add_default_user')->with($this->adminCredentials),
migrationName: 'add_default_user',
);
if (!$serviceName) {
$this->adminCredentials = $adminCredentials;
}

return $this->publishAdminMigration($adminCredentials, $adminPrefix, $serviceName);
}

protected function fillReadme(): void
Expand Down Expand Up @@ -402,18 +394,15 @@ protected function fillCredentialsAndAccess(string $kebabName): void
continue;
}

if (!empty($this->adminCredentials) && $this->confirm("Is {$title}'s admin the same as default one?", true)) {
$email = $this->adminCredentials['email'];
$password = $this->adminCredentials['password'];
if (isset($this->adminCredentials['email']) && $this->confirm("Is {$title}'s admin the same as default one?", true)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (isset($this->adminCredentials['email']) && $this->confirm("Is {$title}'s admin the same as default one?", true)) {
if (!empty($this->adminCredentials) && $this->confirm("Is {$title}'s admin the same as default one?", true)) {

$adminCredentials['email'] = $this->adminCredentials['email'];
$adminCredentials['password'] = $this->adminCredentials['password'];
Comment on lines +398 to +399
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$adminCredentials['email'] = $this->adminCredentials['email'];
$adminCredentials['password'] = $this->adminCredentials['password'];
$adminCredentials = $this->adminCredentials;

} else {
$defaultPassword = substr(md5(uniqid()), 0, 8);

$email = $this->ask("Please enter a {$title}'s admin email", "admin@{$kebabName}.com");
$password = $this->ask("Please enter a {$title}'s admin password", $defaultPassword);
$adminCredentials = $this->createAdminUser($kebabName, $key, "{$title}'s");
}

$this->setReadmeValue($filePart, "{$key}_email", $email);
$this->setReadmeValue($filePart, "{$key}_password", $password);
$this->setReadmeValue($filePart, "{$key}_email", $adminCredentials['email']);
$this->setReadmeValue($filePart, "{$key}_password", $adminCredentials['password']);
$this->removeTag($filePart, "{$key}_credentials");
}

Expand Down Expand Up @@ -607,4 +596,41 @@ protected function publishWebLogin(): void

file_put_contents(base_path('routes/web.php'), "\nAuth::routes();\n", FILE_APPEND);
}

protected function publishAdminMigration(array $adminCredentials, string $adminPrefix, string $serviceName): array
{
if ($this->authType === AuthTypeEnum::Clerk) {
if (isset($this->adminCredentials['is_table_created'])) {
$viewName = 'admins_add_additional_admin';
$migrationName = "add_{$serviceName}_admin";

$adminCredentials['migrationName'] = Str::studly($migrationName);
} else {
$viewName = $migrationName = 'admins_create_table';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure that such view is exists. Anyway, let's pusblish migration for the admins table creation outside of this method just after we decided that need to create user with the cleark auth


$this->adminCredentials['is_table_created'] = true;
}

$this->publishMigration(
view: view("initializator::{$viewName}")->with($adminCredentials),
migrationName: $migrationName,
);
} else {
$adminCredentials['name'] = $this->ask("Please enter {$adminPrefix} admin name", 'Admin');
$adminCredentials['role_id'] = $this->ask("Please enter {$adminPrefix} admin role id", RoleEnum::Admin->value);

$migrationName = !empty($this->adminCredentials) && !$serviceName
? 'add_default_user'
: "add_{$serviceName}_user";

$adminCredentials['migrationName'] = Str::studly($migrationName);

$this->publishMigration(
view: view('initializator::add_default_user')->with($adminCredentials),
migrationName: $migrationName,
);
}

return $adminCredentials;
}
}
Loading
Loading