Skip to content

Commit

Permalink
Added admin:user:create command to Maho CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
fballiano committed Aug 22, 2024
1 parent cf1364f commit 779abae
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
90 changes: 90 additions & 0 deletions lib/MahoCLI/Commands/AdminUserCreate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace MahoCLI\Commands;

use Mage;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;

#[AsCommand(
name: 'admin:user:create',
description: 'Create an admin user'
)]
class AdminUserCreate extends BaseMahoCommand
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->initMaho();

$role = Mage::getModel('admin/roles')
->load('Administrators', 'role_name');
if (!$role->getId()) {
$output->writeln('<error>Role "Administrators" not found</error>');
return Command::FAILURE;
}

/** @var QuestionHelper $questionHelper */
$questionHelper = $this->getHelper('question');

$question = new Question('Username: ', '');
$username = $questionHelper->ask($input, $output, $question);
$username = trim($username);
if (!strlen($username)) {
$output->writeln('<error>Username cannot be empty</error>');
return Command::INVALID;
}

$question = new Question('Password: ', '');
$password = $questionHelper->ask($input, $output, $question);
$password = trim($password);
if (!strlen($password)) {
$output->writeln('<error>Password cannot be empty</error>');
return Command::INVALID;
}

$question = new Question('Email: ', '');
$email = $questionHelper->ask($input, $output, $question);
$email = trim($email);
if (!strlen($email)) {
$output->writeln('<error>Username cannot be empty</error>');
return Command::INVALID;
}

$question = new Question('Firstname: ', '');
$firstname = $questionHelper->ask($input, $output, $question);
$firstname = trim($firstname);
if (!strlen($firstname)) {
$output->writeln('<error>Firstname cannot be empty</error>');
return Command::INVALID;
}

$question = new Question('Lastname: ', '');
$lastname = $questionHelper->ask($input, $output, $question);
$lastname = trim($lastname);
if (!strlen($lastname)) {
$output->writeln('<error>Lastname cannot be empty</error>');
return Command::INVALID;
}

$user = Mage::getModel('admin/user')
->setUsername($username)
->setPassword($password)
->setEmail($email)
->setFirstname($firstname)
->setLastname($lastname)
->setIsActive(1)
->save();

$user
->setRoleIds([$role->getId()])
->setRoleUserId($user->getId())
->saveRelations();

$output->writeln("<info>User {$username} created with {$role->getName()} role</info>");
return Command::SUCCESS;
}
}
1 change: 1 addition & 0 deletions maho
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use Symfony\Component\Console\Application;
$application = new Application('');

$application->add(new \MahoCLI\Commands\AdminUserList());
$application->add(new \MahoCLI\Commands\AdminUserCreate());
$application->add(new \MahoCLI\Commands\AdminUserChangepassword());
$application->add(new \MahoCLI\Commands\AdminUserEnable());
$application->add(new \MahoCLI\Commands\AdminUserDisable());
Expand Down

0 comments on commit 779abae

Please sign in to comment.