Skip to content

Commit 11ec720

Browse files
authored
Merge pull request #57 from RonasIT/55-admins-migrations
[55] additional admins migrations
2 parents bc7cef4 + 35a90e9 commit 11ec720

16 files changed

+442
-86
lines changed

resources/views/add_default_user.blade.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22
use Illuminate\Support\Facades\App;
33
use Illuminate\Support\Facades\DB;
44
use Illuminate\Support\Facades\Hash;
5-
use RonasIT\Support\Traits\MigrationTrait;
65

7-
class AddDefaultUser extends Migration
6+
return new class extends Migration
87
{
9-
use MigrationTrait;
10-
11-
public function up()
8+
public function up(): void
129
{
1310
if (!App::environment('testing')) {
1411
DB::table('users')->insert([
@@ -20,12 +17,12 @@ public function up()
2017
}
2118
}
2219

23-
public function down()
20+
public function down(): void
2421
{
2522
if (!App::environment('testing')) {
2623
DB::table('users')
2724
->where('email', '{{ $email }}')
2825
->delete();
2926
}
3027
}
31-
}
28+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use Illuminate\Database\Migrations\Migration;
2+
use Illuminate\Support\Facades\App;
3+
use Illuminate\Support\Facades\DB;
4+
use Illuminate\Support\Facades\Hash;
5+
6+
return new class extends Migration
7+
{
8+
public function up(): void
9+
{
10+
if (!App::environment('testing')) {
11+
DB::table('admins')->insert([
12+
'email' => '{{ $email }}',
13+
'password' => Hash::make('{{ $password }}'),
14+
]);
15+
}
16+
}
17+
18+
public function down(): void
19+
{
20+
if (!App::environment('testing')) {
21+
DB::table('admins')
22+
->where('email', '{{ $email }}')
23+
->delete();
24+
}
25+
}
26+
};
Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
use Illuminate\Database\Migrations\Migration;
22
use Illuminate\Database\Schema\Blueprint;
3-
use Illuminate\Support\Facades\App;
4-
use Illuminate\Support\Facades\DB;
5-
use Illuminate\Support\Facades\Hash;
63
use Illuminate\Support\Facades\Schema;
74
use RonasIT\Support\Traits\MigrationTrait;
85

9-
class AdminsCreateTable extends Migration
6+
return new class extends Migration
107
{
118
use MigrationTrait;
129

@@ -17,17 +14,10 @@ public function up()
1714
$table->string('email')->unique();
1815
$table->string('password');
1916
});
20-
21-
if (!App::environment('testing')) {
22-
DB::table('admins')->insert([
23-
'email' => '{{ $email }}',
24-
'password' => Hash::make('{{ $password }}'),
25-
]);
26-
}
2717
}
2818

2919
public function down()
3020
{
3121
Schema::dropIfExists('admins');
3222
}
33-
}
23+
};

resources/views/users_add_clerk_id_field.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Illuminate\Support\Facades\Schema;
44
use RonasIT\Support\Traits\MigrationTrait;
55

6-
class UsersAddClerkIdField extends Migration
6+
return new class extends Migration
77
{
88
use MigrationTrait;
99

@@ -20,4 +20,4 @@ public function down()
2020
$table->dropColumn('clerk_id');
2121
});
2222
}
23-
}
23+
};

src/Commands/InitCommand.php

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ public function handle(): void
172172
}
173173

174174
if ($this->confirm('Do you want to generate an admin user?', true)) {
175+
if ($this->authType === AuthTypeEnum::Clerk) {
176+
$this->publishAdminsTableMigration();
177+
}
178+
175179
$this->createAdminUser($kebabName);
176180
}
177181

@@ -317,29 +321,30 @@ protected function runMigrations(): void
317321
shell_exec('php artisan migrate --ansi');
318322
}
319323

320-
protected function createAdminUser(string $kebabName): void
324+
protected function createAdminUser(string $kebabName, string $serviceKey = '', string $serviceName = ''): array
321325
{
326+
$adminEmail = when(empty($serviceKey), "admin@{$kebabName}.com", "admin.{$serviceKey}@{$kebabName}.com");
322327
$defaultPassword = substr(md5(uniqid()), 0, 8);
323328

324-
$this->adminCredentials = [
325-
'email' => $this->ask('Please enter an admin email', "admin@{$kebabName}.com"),
326-
'password' => $this->ask('Please enter an admin password', $defaultPassword),
329+
$serviceLabel = when(!empty($serviceName), " for {$serviceName}");
330+
331+
$adminCredentials = [
332+
'email' => $this->ask("Please enter admin email{$serviceLabel}", $adminEmail),
333+
'password' => $this->ask("Please enter admin password{$serviceLabel}", $defaultPassword),
327334
];
328335

329-
if ($this->authType === AuthTypeEnum::Clerk) {
330-
$this->publishMigration(
331-
view: view('initializator::admins_create_table')->with($this->adminCredentials),
332-
migrationName: 'admins_create_table',
333-
);
334-
} else {
335-
$this->adminCredentials['name'] = $this->ask('Please enter an admin name', 'Admin');
336-
$this->adminCredentials['role_id'] = $this->ask('Please enter an admin role id', RoleEnum::Admin->value);
336+
if ($this->authType === AuthTypeEnum::None) {
337+
$adminCredentials['name'] = $this->ask("Please enter admin name{$serviceLabel}", "{$serviceName} Admin");
338+
$adminCredentials['role_id'] = $this->ask("Please enter admin role id{$serviceLabel}", RoleEnum::Admin->value);
339+
}
337340

338-
$this->publishMigration(
339-
view: view('initializator::add_default_user')->with($this->adminCredentials),
340-
migrationName: 'add_default_user',
341-
);
341+
if (empty($serviceName)) {
342+
$this->adminCredentials = $adminCredentials;
342343
}
344+
345+
$this->publishAdminMigration($adminCredentials, $serviceKey);
346+
347+
return $adminCredentials;
343348
}
344349

345350
protected function fillReadme(): void
@@ -459,17 +464,17 @@ protected function fillCredentialsAndAccess(string $kebabName): void
459464
}
460465

461466
if (!empty($this->adminCredentials) && $this->confirm("Is {$title}'s admin the same as default one?", true)) {
462-
$email = $this->adminCredentials['email'];
463-
$password = $this->adminCredentials['password'];
467+
$adminCredentials = $this->adminCredentials;
464468
} else {
465-
$defaultPassword = substr(md5(uniqid()), 0, 8);
469+
if ($this->authType === AuthTypeEnum::Clerk && !$this->isMigrationExists('admins_create_table')) {
470+
$this->publishAdminsTableMigration();
471+
}
466472

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

471-
$this->setReadmeValue($filePart, "{$key}_email", $email);
472-
$this->setReadmeValue($filePart, "{$key}_password", $password);
476+
$this->setReadmeValue($filePart, "{$key}_email", $adminCredentials['email']);
477+
$this->setReadmeValue($filePart, "{$key}_password", $adminCredentials['password']);
473478
$this->removeTag($filePart, "{$key}_credentials");
474479
}
475480

@@ -652,4 +657,31 @@ protected function changeMiddlewareForTelescopeAuthorization(): void
652657

653658
$config->write();
654659
}
660+
661+
protected function publishAdminMigration(array $adminCredentials, ?string $serviceKey): void
662+
{
663+
$migrationName = (empty($serviceKey)) ? 'add_default_admin' : "add_{$serviceKey}_admin";
664+
665+
$viewName = ($this->authType === AuthTypeEnum::Clerk)
666+
? 'initializator::admins_add_additional_admin'
667+
: 'initializator::add_default_user';
668+
669+
$this->publishMigration(
670+
view: view($viewName)->with($adminCredentials),
671+
migrationName: $migrationName,
672+
);
673+
}
674+
675+
protected function isMigrationExists(string $migrationName): bool
676+
{
677+
return !empty(glob(base_path("database/migrations/*_{$migrationName}.php")));
678+
}
679+
680+
protected function publishAdminsTableMigration(): void
681+
{
682+
$this->publishMigration(
683+
view: view('initializator::admins_create_table'),
684+
migrationName: 'admins_create_table',
685+
);
686+
}
655687
}

0 commit comments

Comments
 (0)