[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Joomla! Content Management System 5 * 6 * @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org> 7 * @license GNU General Public License version 2 or later; see LICENSE.txt 8 */ 9 10 namespace Joomla\CMS\Console; 11 12 use Joomla\CMS\User\User; 13 use Joomla\Console\Command\AbstractCommand; 14 use Joomla\Database\DatabaseAwareTrait; 15 use Joomla\Database\DatabaseInterface; 16 use Joomla\Filter\InputFilter; 17 use Symfony\Component\Console\Command\Command; 18 use Symfony\Component\Console\Exception\InvalidOptionException; 19 use Symfony\Component\Console\Input\InputInterface; 20 use Symfony\Component\Console\Input\InputOption; 21 use Symfony\Component\Console\Output\OutputInterface; 22 use Symfony\Component\Console\Question\ChoiceQuestion; 23 use Symfony\Component\Console\Style\SymfonyStyle; 24 25 // phpcs:disable PSR1.Files.SideEffects 26 \defined('JPATH_PLATFORM') or die; 27 // phpcs:enable PSR1.Files.SideEffects 28 29 /** 30 * Console command for adding a user 31 * 32 * @since 4.0.0 33 */ 34 class AddUserCommand extends AbstractCommand 35 { 36 use DatabaseAwareTrait; 37 38 /** 39 * The default command name 40 * 41 * @var string 42 * @since 4.0.0 43 */ 44 protected static $defaultName = 'user:add'; 45 46 /** 47 * SymfonyStyle Object 48 * @var object 49 * @since 4.0.0 50 */ 51 private $ioStyle; 52 53 /** 54 * Stores the Input Object 55 * @var object 56 * @since 4.0.0 57 */ 58 private $cliInput; 59 60 /** 61 * The username 62 * 63 * @var string 64 * 65 * @since 4.0.0 66 */ 67 private $user; 68 69 /** 70 * The password 71 * 72 * @var string 73 * 74 * @since 4.0.0 75 */ 76 private $password; 77 78 /** 79 * The name 80 * 81 * @var string 82 * 83 * @since 4.0.0 84 */ 85 private $name; 86 87 /** 88 * The email address 89 * 90 * @var string 91 * 92 * @since 4.0.0 93 */ 94 private $email; 95 96 /** 97 * The usergroups 98 * 99 * @var array 100 * 101 * @since 4.0.0 102 */ 103 private $userGroups = []; 104 105 /** 106 * Command constructor. 107 * 108 * @param DatabaseInterface $db The database 109 * 110 * @since 4.2.0 111 */ 112 public function __construct(DatabaseInterface $db) 113 { 114 parent::__construct(); 115 116 $this->setDatabase($db); 117 } 118 119 /** 120 * Internal function to execute the command. 121 * 122 * @param InputInterface $input The input to inject into the command. 123 * @param OutputInterface $output The output to inject into the command. 124 * 125 * @return integer The command exit code 126 * 127 * @since 4.0.0 128 */ 129 protected function doExecute(InputInterface $input, OutputInterface $output): int 130 { 131 $this->configureIO($input, $output); 132 $this->ioStyle->title('Add User'); 133 $this->user = $this->getStringFromOption('username', 'Please enter a username'); 134 $this->name = $this->getStringFromOption('name', 'Please enter a name (full name of user)'); 135 $this->email = $this->getStringFromOption('email', 'Please enter an email address'); 136 $this->password = $this->getStringFromOption('password', 'Please enter a password'); 137 $this->userGroups = $this->getUserGroups(); 138 139 if (\in_array("error", $this->userGroups)) { 140 $this->ioStyle->error("'" . $this->userGroups[1] . "' user group doesn't exist!"); 141 142 return Command::FAILURE; 143 } 144 145 // Get filter to remove invalid characters 146 $filter = new InputFilter(); 147 148 $user['username'] = $filter->clean($this->user, 'USERNAME'); 149 $user['password'] = $this->password; 150 $user['name'] = $filter->clean($this->name, 'STRING'); 151 $user['email'] = $this->email; 152 $user['groups'] = $this->userGroups; 153 154 $userObj = User::getInstance(); 155 $userObj->bind($user); 156 157 if (!$userObj->save()) { 158 switch ($userObj->getError()) { 159 case "JLIB_DATABASE_ERROR_USERNAME_INUSE": 160 $this->ioStyle->error("The username already exists!"); 161 break; 162 case "JLIB_DATABASE_ERROR_EMAIL_INUSE": 163 $this->ioStyle->error("The email address already exists!"); 164 break; 165 case "JLIB_DATABASE_ERROR_VALID_MAIL": 166 $this->ioStyle->error("The email address is invalid!"); 167 break; 168 } 169 170 return 1; 171 } 172 173 $this->ioStyle->success("User created!"); 174 175 return Command::SUCCESS; 176 } 177 178 /** 179 * Method to get groupId by groupName 180 * 181 * @param string $groupName name of group 182 * 183 * @return integer 184 * 185 * @since 4.0.0 186 */ 187 protected function getGroupId($groupName) 188 { 189 $db = $this->getDatabase(); 190 $query = $db->getQuery(true) 191 ->select($db->quoteName('id')) 192 ->from($db->quoteName('#__usergroups')) 193 ->where($db->quoteName('title') . ' = :groupName') 194 ->bind(':groupName', $groupName); 195 $db->setQuery($query); 196 197 return $db->loadResult(); 198 } 199 200 /** 201 * Method to get a value from option 202 * 203 * @param string $option set the option name 204 * @param string $question set the question if user enters no value to option 205 * 206 * @return string 207 * 208 * @since 4.0.0 209 */ 210 public function getStringFromOption($option, $question): string 211 { 212 $answer = (string) $this->cliInput->getOption($option); 213 214 while (!$answer) { 215 if ($option === 'password') { 216 $answer = (string) $this->ioStyle->askHidden($question); 217 } else { 218 $answer = (string) $this->ioStyle->ask($question); 219 } 220 } 221 222 return $answer; 223 } 224 225 /** 226 * Method to get a value from option 227 * 228 * @return array 229 * 230 * @since 4.0.0 231 */ 232 protected function getUserGroups(): array 233 { 234 $groups = $this->getApplication()->getConsoleInput()->getOption('usergroup'); 235 $db = $this->getDatabase(); 236 237 $groupList = []; 238 239 // Group names have been supplied as input arguments 240 if (!\is_null($groups) && $groups[0]) { 241 $groups = explode(',', $groups); 242 243 foreach ($groups as $group) { 244 $groupId = $this->getGroupId($group); 245 246 if (empty($groupId)) { 247 $this->ioStyle->error("Invalid group name '" . $group . "'"); 248 throw new InvalidOptionException("Invalid group name " . $group); 249 } 250 251 $groupList[] = $this->getGroupId($group); 252 } 253 254 return $groupList; 255 } 256 257 // Generate select list for user 258 $query = $db->getQuery(true) 259 ->select($db->quoteName('title')) 260 ->from($db->quoteName('#__usergroups')) 261 ->order($db->quoteName('id') . 'ASC'); 262 $db->setQuery($query); 263 264 $list = $db->loadColumn(); 265 266 $choice = new ChoiceQuestion( 267 'Please select a usergroup (separate multiple groups with a comma)', 268 $list 269 ); 270 $choice->setMultiselect(true); 271 272 $answer = (array) $this->ioStyle->askQuestion($choice); 273 274 foreach ($answer as $group) { 275 $groupList[] = $this->getGroupId($group); 276 } 277 278 return $groupList; 279 } 280 281 /** 282 * Configure the IO. 283 * 284 * @param InputInterface $input The input to inject into the command. 285 * @param OutputInterface $output The output to inject into the command. 286 * 287 * @return void 288 * 289 * @since 4.0.0 290 */ 291 private function configureIO(InputInterface $input, OutputInterface $output) 292 { 293 $this->cliInput = $input; 294 $this->ioStyle = new SymfonyStyle($input, $output); 295 } 296 297 /** 298 * Configure the command. 299 * 300 * @return void 301 * 302 * @since 4.0.0 303 */ 304 protected function configure(): void 305 { 306 $help = "<info>%command.name%</info> will add a user 307 \nUsage: <info>php %command.full_name%</info>"; 308 309 $this->addOption('username', null, InputOption::VALUE_OPTIONAL, 'username'); 310 $this->addOption('name', null, InputOption::VALUE_OPTIONAL, 'full name of user'); 311 $this->addOption('password', null, InputOption::VALUE_OPTIONAL, 'password'); 312 $this->addOption('email', null, InputOption::VALUE_OPTIONAL, 'email address'); 313 $this->addOption('usergroup', null, InputOption::VALUE_OPTIONAL, 'usergroup (separate multiple groups with comma ",")'); 314 $this->setDescription('Add a user'); 315 $this->setHelp($help); 316 } 317 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |