Files
librenms-librenms/app/Http/Controllers/Install/MakeUserController.php

122 lines
3.2 KiB
PHP
Raw Normal View History

2020-06-05 14:26:57 -05:00
<?php
/**
* MakeUserController.php
*
* -Description-
*
* 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
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2020-06-05 14:26:57 -05:00
*
* @link https://www.librenms.org
2020-06-05 14:26:57 -05:00
* @copyright 2020 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace App\Http\Controllers\Install;
2020-06-06 17:03:32 -05:00
use App\Models\User;
2020-06-21 11:27:07 -05:00
use Illuminate\Database\QueryException;
2020-06-06 17:03:32 -05:00
use Illuminate\Http\Request;
2020-06-09 07:24:05 -05:00
use Illuminate\Support\Arr;
2020-06-15 00:45:40 -05:00
use LibreNMS\Interfaces\InstallerStep;
2020-06-06 17:03:32 -05:00
2020-06-15 00:45:40 -05:00
class MakeUserController extends InstallationController implements InstallerStep
2020-06-05 14:26:57 -05:00
{
2020-06-21 01:07:56 -05:00
protected $step = 'user';
2020-06-11 01:06:16 -05:00
public function index(Request $request)
2020-06-05 14:26:57 -05:00
{
if (! $this->initInstallStep()) {
2020-06-21 01:07:56 -05:00
return $this->redirectToIncomplete();
2020-06-11 01:06:16 -05:00
}
2020-06-06 17:03:32 -05:00
if (session('install.database')) {
2020-06-19 00:17:18 -05:00
$user = User::adminOnly()->first();
2020-06-06 17:03:32 -05:00
}
if (isset($user)) {
2020-06-21 01:07:56 -05:00
$this->markStepComplete();
2020-06-11 01:06:16 -05:00
return view('install.user-created', $this->formatData([
2020-06-06 17:03:32 -05:00
'user' => $user,
2020-06-11 01:06:16 -05:00
]));
2020-06-06 17:03:32 -05:00
}
2020-06-11 01:06:16 -05:00
return view('install.make-user', $this->formatData([
'messages' => Arr::wrap(session('message')),
2020-06-11 01:06:16 -05:00
]));
2020-06-06 17:03:32 -05:00
}
public function create(Request $request)
{
$this->validate($request, [
'username' => 'required',
'password' => 'required',
]);
$message = trans('install.user.failure');
2020-06-06 17:03:32 -05:00
try {
2020-06-21 11:27:07 -05:00
// only allow the first admin to be created
if (! $this->complete()) {
2020-06-21 11:27:07 -05:00
$this->configureDatabase();
$user = new User($request->only(['username', 'password', 'email']));
$user->level = 10; // admin
$user->setPassword($request->get('password'));
$res = $user->save();
if ($res) {
$message = trans('install.user.success');
$this->markStepComplete();
}
}
2020-06-06 17:03:32 -05:00
} catch (\Exception $e) {
$message = $e->getMessage();
}
2020-06-07 10:36:45 -05:00
return redirect()->back()->with('message', $message);
2020-06-05 14:26:57 -05:00
}
2020-06-07 14:53:40 -05:00
2020-06-15 00:45:40 -05:00
public function complete(): bool
{
2020-06-21 11:27:07 -05:00
if ($this->stepCompleted('user')) {
return true;
}
try {
if ($this->stepCompleted('database')) {
$exists = User::adminOnly()->exists();
if ($exists) {
$this->markStepComplete();
}
2020-06-21 11:27:07 -05:00
return $exists;
}
} catch (QueryException $e) {
//
}
2020-06-21 11:27:07 -05:00
return false;
2020-06-15 00:45:40 -05:00
}
public function enabled(): bool
2020-06-07 14:53:40 -05:00
{
2020-06-21 11:27:07 -05:00
return $this->stepCompleted('database');
2020-06-07 14:53:40 -05:00
}
2020-06-15 00:45:40 -05:00
public function icon(): string
2020-06-07 14:53:40 -05:00
{
return 'fa-key';
}
2020-06-05 14:26:57 -05:00
}