Skip to content

Commit

Permalink
Add a cachet:make:user command (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrooksuk authored Dec 9, 2024
1 parent 81f3bcd commit 0c43a0b
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/CachetCoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ private function registerCommands(): void
{
if ($this->app->runningInConsole()) {
$this->commands([
Commands\MakeUserCommand::class,
Commands\SendBeaconCommand::class,
Commands\VersionCommand::class,
]);
Expand Down
111 changes: 111 additions & 0 deletions src/Commands/MakeUserCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Cachet\Commands;

use Illuminate\Console\Command;
use function Laravel\Prompts\password;
use function Laravel\Prompts\text;

class MakeUserCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'cachet:make:user {email?} {--password=}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new user';

/**
* The user's data.
*/
protected array $data = [];

/**
* The user's email.
*/
protected ?string $email = null;

/**
* The user's password.
*/
protected ?string $password = null;

/**
* Execute the console command.
*/
public function handle()
{
if ($password = $this->option('password')) {
$this->password = $password;
}

if ($this->email = $this->argument('email')) {
$this->createUser();

return;
}

$this
->promptEmail()
->promptName()
->promptPassword()
->createUser();
}

/**
* Prompt the user for their email.
*/
protected function promptEmail(): self
{
$this->email = text('What is the user\'s email?', required: true);

return $this;
}

/**
* Prompt the user for their name.
*/
protected function promptName(): self
{
$this->data['name'] = text('What is the user\'s name?', required: true);

return $this;
}

/**
* Prompt the user for their password.
*/
protected function promptPassword(): self
{
if ($this->password) {
return $this;
}

$this->password = password('What is the user\'s password?', required: true);

return $this;
}

/**
* Create the user.
*/
protected function createUser(): void
{
$userModel = config('cachet.user_model');

$userModel::create([
'name' => $this->data['name'],
'email' => $this->email,
'password' => bcrypt($this->password),
]);

$this->components->info('User created successfully.');
}
}
8 changes: 8 additions & 0 deletions src/Concerns/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Cachet\Concerns;

interface User
{
//
}

0 comments on commit 0c43a0b

Please sign in to comment.