snowfield/packages/polr/createsuperuser.patch
2022-08-14 17:45:00 +02:00

88 lines
2.1 KiB
Diff

diff --git a/app/Console/Commands/CreateSuperUser.php b/app/Console/Commands/CreateSuperUser.php
new file mode 100755
index 0000000..34a51a3
--- /dev/null
+++ b/app/Console/Commands/CreateSuperUser.php
@@ -0,0 +1,68 @@
+<?php
+#/App/Console/Commands/CreateSuperUser.php
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use App\Models\User;
+use App\Helpers\UserHelper;
+use App\Factories\UserFactory;
+use Illuminate\Database\QueryException;
+use Hash;
+class CreateSuperUser extends Command
+{
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'init:createsuperuser {pw}';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = 'Create a superuser';
+
+ /**
+ * Create a new command instance.
+ *
+ * @return void
+ */
+ public function __construct()
+ {
+ parent::__construct();
+ }
+
+ /**
+ * Execute the console comman.
+ *
+ * @return mixed
+ */
+ public function handle()
+
+ {
+ $pw = $this->argument('pw');
+ try
+ {
+ if (UserHelper::userExists("admin"))
+ {
+ $user = UserHelper::getUserByUsername("admin");
+ $user->password = Hash::make($pw);
+ $user->save();
+ }
+ else
+ {
+ $user = UserFactory::createUser("admin", "admin@admin.fr", $pw, 1, "127.0.0.1", false, 0, UserHelper::$USER_ROLES['admin']);
+ }
+ }
+ catch(QueryException $e)
+ {
+ if ($e->getCode() != 23000)
+ {
+ throw $e;
+ }
+ }
+ }
+}
+
diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php
index 35aca72..753ba92 100644
--- a/app/Console/Kernel.php
+++ b/app/Console/Kernel.php
@@ -13,7 +13,8 @@ class Kernel extends ConsoleKernel
* @var array
*/
protected $commands = [
- \Torann\GeoIP\Console\Update::class
+ \Torann\GeoIP\Console\Update::class,
+ 'App\Console\Commands\CreateSuperUser'
];
/**