Files
librenms-librenms/app/Console/Commands/AddUserCommand.php
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

105 lines
3.3 KiB
PHP
Raw Normal View History

2019-02-15 09:00:07 -06:00
<?php
/**
* AddUserCommand.php
*
* CLI command to add a user to LibreNMS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2021-02-09 00:29:04 +01:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-02-15 09:00:07 -06:00
*
2021-02-09 00:29:04 +01:00
* @link https://www.librenms.org
2021-09-10 20:09:53 +02:00
*
2019-02-15 09:00:07 -06:00
* @copyright 2019 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace App\Console\Commands;
use App\Console\LnmsCommand;
use App\Models\User;
2023-08-28 10:27:56 -05:00
use Bouncer;
2019-02-15 09:00:07 -06:00
use Illuminate\Validation\Rule;
2019-04-22 19:01:39 -05:00
use LibreNMS\Authentication\LegacyAuth;
2019-02-15 09:00:07 -06:00
use LibreNMS\Config;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
class AddUserCommand extends LnmsCommand
{
protected $name = 'user:add';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->setDescription(__('commands.user:add.description'));
$this->addArgument('username', InputArgument::REQUIRED);
$this->addOption('password', 'p', InputOption::VALUE_REQUIRED);
$this->addOption('role', 'r', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, __('commands.user:add.options.role', ['roles' => '[user, global-read, admin]']), ['user']);
2019-02-15 09:00:07 -06:00
$this->addOption('email', 'e', InputOption::VALUE_REQUIRED);
$this->addOption('full-name', 'l', InputOption::VALUE_REQUIRED);
$this->addOption('descr', 's', InputOption::VALUE_REQUIRED);
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
if (Config::get('auth_mechanism') != 'mysql') {
$this->warn(__('commands.user:add.wrong-auth'));
}
2023-08-28 10:27:56 -05:00
$roles = Bouncer::role()->pluck('name');
2019-02-15 09:00:07 -06:00
$this->validate([
'username' => ['required', Rule::unique('users', 'username')->where('auth_type', 'mysql')],
'email' => 'nullable|email',
2023-08-28 10:27:56 -05:00
'role.*' => Rule::in($roles),
2019-02-15 09:00:07 -06:00
]);
// set get password
$password = $this->option('password');
if (! $password) {
$password = $this->secret(__('commands.user:add.password-request'));
}
$user = new User([
'username' => $this->argument('username'),
2019-04-22 19:01:39 -05:00
'descr' => $this->option('descr'),
'email' => $this->option('email'),
'realname' => $this->option('full-name'),
2019-02-15 09:00:07 -06:00
'auth_type' => 'mysql',
]);
2019-04-22 19:01:39 -05:00
2019-02-15 09:00:07 -06:00
$user->setPassword($password);
$user->save();
$user->assign($this->option('role'));
2019-02-15 09:00:07 -06:00
2022-02-20 22:05:51 +01:00
$user->auth_id = (string) LegacyAuth::get()->getUserid($user->username) ?: $user->user_id;
2019-04-22 19:01:39 -05:00
$user->save();
2019-02-15 09:00:07 -06:00
$this->info(__('commands.user:add.success', ['username' => $user->username]));
2020-09-21 14:54:51 +02:00
2019-02-15 09:00:07 -06:00
return 0;
}
}