"Fossies" - the Fresh Open Source Software Archive

Member "ampache-5.0.0/src/Module/Cli/AdminAddUserCommand.php" (31 Aug 2021, 2822 Bytes) of package /linux/www/ampache-5.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AdminAddUserCommand.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 /*
    3  * vim:set softtabstop=4 shiftwidth=4 expandtab:
    4  *
    5  * LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later)
    6  * Copyright 2001 - 2020 Ampache.org
    7  *
    8  * This program is free software: you can redistribute it and/or modify
    9  * it under the terms of the GNU Affero General Public License as published by
   10  * the Free Software Foundation, either version 3 of the License, or
   11  * (at your option) any later version.
   12  *
   13  * This program is distributed in the hope that it will be useful,
   14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16  * GNU Affero General Public License for more details.
   17  *
   18  * You should have received a copy of the GNU Affero General Public License
   19  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
   20  *
   21  */
   22 
   23 declare(strict_types=1);
   24 
   25 namespace Ampache\Module\Cli;
   26 
   27 use Ahc\Cli\Input\Command;
   28 use Ampache\Config\ConfigContainerInterface;
   29 use Ampache\Repository\Model\User;
   30 
   31 final class AdminAddUserCommand extends Command
   32 {
   33     private ConfigContainerInterface $configContainer;
   34 
   35     public function __construct(
   36         ConfigContainerInterface $configContainer
   37     ) {
   38         parent::__construct('admin:addUser', T_('Add a User'));
   39 
   40         $this->configContainer = $configContainer;
   41 
   42         $this
   43             ->option('-p|--password', T_('Password'), 'strval', bin2hex(random_bytes(20)))
   44             ->option('-e|--email', T_('E-mail'), 'strval', '')
   45             ->option('-w|--website', T_('Website'), 'strval', '')
   46             ->option('-n|--name', T_('Name'), 'strval', '')
   47             ->option('-l|--level', T_('Access Level'), 'intval', $this->configContainer->get('auto_user') ?? 5)
   48             ->argument('<username>', T_('Username'))
   49             ->usage('<bold>  admin:addUser some-user</end> <comment> ## ' . T_('Add a User with the name `some-user`') . '</end><eol/>');
   50     }
   51 
   52     public function execute(
   53         string $username
   54     ): void {
   55         $values     = $this->values();
   56         $interactor = $this->io();
   57 
   58         $result = (int) User::create(
   59             $username,
   60             $values['name'],
   61             $values['email'],
   62             $values['website'],
   63             $values['password'],
   64             $values['level']
   65         );
   66 
   67         if ($result !== null) {
   68             $interactor->ok(
   69                 sprintf(
   70                     T_('Created %s user %s with password %s'),
   71                     User::access_level_to_name((string) $values['level']),
   72                     $username,
   73                     $values['password']
   74                 ),
   75                 true
   76             );
   77             echo "\n";
   78 
   79             User::fix_preferences('-1');
   80         } else {
   81             $interactor->error(T_('User creation failed'), true);
   82         }
   83     }
   84 }