diff --git a/app/Console/Commands/CreateUser.php b/app/Console/Commands/CreateUser.php new file mode 100644 index 000000000..99058ed98 --- /dev/null +++ b/app/Console/Commands/CreateUser.php @@ -0,0 +1,71 @@ +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)); + } + +} \ No newline at end of file diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 35aca72d2..31d841e8e 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\CreateUser::class ]; /**