Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a UserCreate console command. #597

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
71 changes: 71 additions & 0 deletions app/Console/Commands/CreateUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace App\Console\Commands;

use App\Factories\UserFactory;
use App\Helpers\UserHelper;
use Illuminate\Console\Command;
use Illuminate\Support\Str;

class CreateUser extends Command {

/**
* @inheritdoc
*/
protected $signature = 'create:user {username} {email} {--password=} {--active=1} {--ip=127.0.0.1} {--api_key=} {--api_active=0} {--role=default}';

/**
* @inheritdoc
*/
protected $description = 'Create a new user.';

/**
* Run the command.
*
* @return int
*/
public function handle() {
$username = $this->argument('username');
$email = $this->argument('email');
$password = $this->option('password');
$active = (int)$this->option('active');
$ip = $this->option('ip');
$api_key = $this->option('api_key');
$api_active = (int)$this->option('api_active');
$role = $this->option('role');

// Username and email should not be empty.
if(empty($username) || empty($email)) {
$this->output->error('Username and email should not be empty.');
return 1;
}

// Exit if the user already exists.
if(UserHelper::userExists($username)) {
$this->output->error(sprintf('User "%s" already exists.', $username));
return 1;
}

// If password arg is empty, generate a random password.
if(empty($password)) {
$password = Str::random(8);
$this->output->writeln(sprintf('Using generated password: %s', $password));
}

// Set the user role.
if(array_key_exists($role, UserHelper::$USER_ROLES)) {
$role = UserHelper::$USER_ROLES[$role];
}
else {
// Exit if the given role is invalid.
$this->output->error('User role is invalid.');
return 1;
}

// Create the user.
UserFactory::createUser($username, $email, $password, $active, $ip, $api_key, $api_active, $role);

$this->output->writeln(sprintf('User %s created!', $username));
}

}
3 changes: 2 additions & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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\CreateUser::class
];

/**
Expand Down