librenms /
librenms
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * AddUserCommand.php |
||||
| 4 | * |
||||
| 5 | * CLI command to add a user to LibreNMS |
||||
| 6 | * |
||||
| 7 | * This program is free software: you can redistribute it and/or modify |
||||
| 8 | * it under the terms of the GNU General Public License as published by |
||||
| 9 | * the Free Software Foundation, either version 3 of the License, or |
||||
| 10 | * (at your option) any later version. |
||||
| 11 | * |
||||
| 12 | * This program is distributed in the hope that it will be useful, |
||||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
||||
| 15 | * GNU General Public License for more details. |
||||
| 16 | * |
||||
| 17 | * You should have received a copy of the GNU General Public License |
||||
| 18 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||||
| 19 | * |
||||
| 20 | * @link https://www.librenms.org |
||||
| 21 | * |
||||
| 22 | * @copyright 2019 Tony Murray |
||||
| 23 | * @author Tony Murray <[email protected]> |
||||
| 24 | */ |
||||
| 25 | |||||
| 26 | namespace App\Console\Commands; |
||||
| 27 | |||||
| 28 | use App\Console\LnmsCommand; |
||||
| 29 | use App\Models\User; |
||||
| 30 | use Illuminate\Validation\Rule; |
||||
| 31 | use LibreNMS\Authentication\LegacyAuth; |
||||
| 32 | use LibreNMS\Config; |
||||
| 33 | use Symfony\Component\Console\Input\InputArgument; |
||||
| 34 | use Symfony\Component\Console\Input\InputOption; |
||||
| 35 | |||||
| 36 | class AddUserCommand extends LnmsCommand |
||||
| 37 | { |
||||
| 38 | protected $name = 'user:add'; |
||||
| 39 | |||||
| 40 | /** |
||||
| 41 | * Create a new command instance. |
||||
| 42 | * |
||||
| 43 | * @return void |
||||
| 44 | */ |
||||
| 45 | public function __construct() |
||||
| 46 | { |
||||
| 47 | parent::__construct(); |
||||
| 48 | |||||
| 49 | $this->setDescription(__('commands.user:add.description')); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 50 | |||||
| 51 | $this->addArgument('username', InputArgument::REQUIRED); |
||||
| 52 | $this->addOption('password', 'p', InputOption::VALUE_REQUIRED); |
||||
| 53 | $this->addOption('role', 'r', InputOption::VALUE_REQUIRED, __('commands.user:add.options.role', ['roles' => '[normal, global-read, admin]']), 'normal'); |
||||
|
0 ignored issues
–
show
It seems like
__('commands.user:add.op... global-read, admin]')) can also be of type array and array; however, parameter $description of App\Console\LnmsCommand::addOption() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 54 | $this->addOption('email', 'e', InputOption::VALUE_REQUIRED); |
||||
| 55 | $this->addOption('full-name', 'l', InputOption::VALUE_REQUIRED); |
||||
| 56 | $this->addOption('descr', 's', InputOption::VALUE_REQUIRED); |
||||
| 57 | } |
||||
| 58 | |||||
| 59 | /** |
||||
| 60 | * Execute the console command. |
||||
| 61 | * |
||||
| 62 | * @return mixed |
||||
| 63 | */ |
||||
| 64 | public function handle() |
||||
| 65 | { |
||||
| 66 | if (Config::get('auth_mechanism') != 'mysql') { |
||||
| 67 | $this->warn(__('commands.user:add.wrong-auth')); |
||||
|
0 ignored issues
–
show
It seems like
__('commands.user:add.wrong-auth') can also be of type array and array; however, parameter $string of Illuminate\Console\Command::warn() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 68 | } |
||||
| 69 | |||||
| 70 | $roles = [ |
||||
| 71 | 'normal' => 1, |
||||
| 72 | 'global-read' => 5, |
||||
| 73 | 'admin' => 10, |
||||
| 74 | ]; |
||||
| 75 | |||||
| 76 | $this->validate([ |
||||
| 77 | 'username' => ['required', Rule::unique('users', 'username')->where('auth_type', 'mysql')], |
||||
| 78 | 'email' => 'nullable|email', |
||||
| 79 | 'role' => Rule::in(array_keys($roles)), |
||||
| 80 | ]); |
||||
| 81 | |||||
| 82 | // set get password |
||||
| 83 | $password = $this->option('password'); |
||||
| 84 | if (! $password) { |
||||
| 85 | $password = $this->secret(__('commands.user:add.password-request')); |
||||
| 86 | } |
||||
| 87 | |||||
| 88 | $user = new User([ |
||||
| 89 | 'username' => $this->argument('username'), |
||||
| 90 | 'level' => $roles[$this->option('role')], |
||||
| 91 | 'descr' => $this->option('descr'), |
||||
| 92 | 'email' => $this->option('email'), |
||||
| 93 | 'realname' => $this->option('full-name'), |
||||
| 94 | 'auth_type' => 'mysql', |
||||
| 95 | ]); |
||||
| 96 | |||||
| 97 | $user->setPassword($password); |
||||
| 98 | $user->save(); |
||||
| 99 | |||||
| 100 | $user->auth_id = LegacyAuth::get()->getUserid($user->username) ?: $user->user_id; |
||||
| 101 | $user->save(); |
||||
| 102 | |||||
| 103 | $this->info(__('commands.user:add.success', ['username' => $user->username])); |
||||
| 104 | |||||
| 105 | return 0; |
||||
| 106 | } |
||||
| 107 | } |
||||
| 108 |