[ 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\Access\Access; 13 use Joomla\CMS\User\User; 14 use Joomla\CMS\User\UserHelper; 15 use Joomla\Console\Command\AbstractCommand; 16 use Joomla\Database\DatabaseAwareTrait; 17 use Joomla\Database\DatabaseInterface; 18 use Joomla\Database\ParameterType; 19 use Symfony\Component\Console\Command\Command; 20 use Symfony\Component\Console\Input\InputInterface; 21 use Symfony\Component\Console\Input\InputOption; 22 use Symfony\Component\Console\Output\OutputInterface; 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 deleting a user 31 * 32 * @since 4.0.0 33 */ 34 class DeleteUserCommand 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:delete'; 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 $username; 68 69 /** 70 * Command constructor. 71 * 72 * @param DatabaseInterface $db The database 73 * 74 * @since 4.2.0 75 */ 76 public function __construct(DatabaseInterface $db) 77 { 78 parent::__construct(); 79 80 $this->setDatabase($db); 81 } 82 83 /** 84 * Internal function to execute the command. 85 * 86 * @param InputInterface $input The input to inject into the command. 87 * @param OutputInterface $output The output to inject into the command. 88 * 89 * @return integer The command exit code 90 * 91 * @since 4.0.0 92 */ 93 protected function doExecute(InputInterface $input, OutputInterface $output): int 94 { 95 $this->configureIO($input, $output); 96 97 $this->ioStyle->title('Delete User'); 98 99 $this->username = $this->getStringFromOption('username', 'Please enter a username'); 100 101 $userId = UserHelper::getUserId($this->username); 102 $db = $this->getDatabase(); 103 104 if (empty($userId)) { 105 $this->ioStyle->error($this->username . ' does not exist!'); 106 107 return Command::FAILURE; 108 } 109 110 if ($input->isInteractive() && !$this->ioStyle->confirm('Are you sure you want to delete this user?', false)) { 111 $this->ioStyle->note('User not deleted'); 112 113 return Command::SUCCESS; 114 } 115 116 $groups = UserHelper::getUserGroups($userId); 117 $user = User::getInstance($userId); 118 119 if ($user->block == 0) { 120 foreach ($groups as $groupId) { 121 if (Access::checkGroup($groupId, 'core.admin')) { 122 $queryUser = $db->getQuery(true); 123 $queryUser->select('COUNT(*)') 124 ->from($db->quoteName('#__users', 'u')) 125 ->leftJoin( 126 $db->quoteName('#__user_usergroup_map', 'g'), 127 '(' . $db->quoteName('u.id') . ' = ' . $db->quoteName('g.user_id') . ')' 128 ) 129 ->where($db->quoteName('g.group_id') . " = :groupId") 130 ->where($db->quoteName('u.block') . " = 0") 131 ->bind(':groupId', $groupId, ParameterType::INTEGER); 132 133 $db->setQuery($queryUser); 134 $activeSuperUser = $db->loadResult(); 135 136 if ($activeSuperUser < 2) { 137 $this->ioStyle->error("You can't delete the last active Super User"); 138 139 return Command::FAILURE; 140 } 141 } 142 } 143 } 144 145 // Trigger delete of user 146 $result = $user->delete(); 147 148 if (!$result) { 149 $this->ioStyle->error("Can't remove " . $this->username . ' from usertable'); 150 151 return Command::FAILURE; 152 } 153 154 $this->ioStyle->success('User ' . $this->username . ' deleted!'); 155 156 return Command::SUCCESS; 157 } 158 159 /** 160 * Method to get a value from option 161 * 162 * @param string $option set the option name 163 * 164 * @param string $question set the question if user enters no value to option 165 * 166 * @return string 167 * 168 * @since 4.0.0 169 */ 170 protected function getStringFromOption($option, $question): string 171 { 172 $answer = (string) $this->getApplication()->getConsoleInput()->getOption($option); 173 174 while (!$answer) { 175 $answer = (string) $this->ioStyle->ask($question); 176 } 177 178 return $answer; 179 } 180 181 /** 182 * Configure the IO. 183 * 184 * @param InputInterface $input The input to inject into the command. 185 * @param OutputInterface $output The output to inject into the command. 186 * 187 * @return void 188 * 189 * @since 4.0.0 190 */ 191 private function configureIO(InputInterface $input, OutputInterface $output) 192 { 193 $this->cliInput = $input; 194 $this->ioStyle = new SymfonyStyle($input, $output); 195 } 196 197 /** 198 * Configure the command. 199 * 200 * @return void 201 * 202 * @since 4.0.0 203 */ 204 protected function configure(): void 205 { 206 $help = "<info>%command.name%</info> deletes a user 207 \nUsage: <info>php %command.full_name%</info>"; 208 209 $this->setDescription('Delete a user'); 210 $this->addOption('username', null, InputOption::VALUE_OPTIONAL, 'username'); 211 $this->setHelp($help); 212 } 213 }
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 |