Issues (2963)

Http/Controllers/Install/MakeUserController.php (1 issue)

1
<?php
2
/**
3
 * MakeUserController.php
4
 *
5
 * -Description-
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  2020 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace App\Http\Controllers\Install;
27
28
use App\Models\User;
29
use Illuminate\Database\QueryException;
30
use Illuminate\Http\Request;
31
use Illuminate\Support\Arr;
32
use LibreNMS\Interfaces\InstallerStep;
33
34
class MakeUserController extends InstallationController implements InstallerStep
35
{
36
    protected $step = 'user';
37
38
    public function index(Request $request)
0 ignored issues
show
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

38
    public function index(/** @scrutinizer ignore-unused */ Request $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
39
    {
40
        if (! $this->initInstallStep()) {
41
            return $this->redirectToIncomplete();
42
        }
43
44
        if (session('install.database')) {
45
            $user = User::adminOnly()->first();
46
        }
47
48
        if (isset($user)) {
49
            $this->markStepComplete();
50
51
            return view('install.user-created', $this->formatData([
52
                'user' => $user,
53
            ]));
54
        }
55
56
        return view('install.make-user', $this->formatData([
57
            'messages' => Arr::wrap(session('message')),
58
        ]));
59
    }
60
61
    public function create(Request $request)
62
    {
63
        $this->validate($request, [
64
            'username' => 'required',
65
            'password' => 'required',
66
        ]);
67
68
        $message = trans('install.user.failure');
69
70
        try {
71
            // only allow the first admin to be created
72
            if (! $this->complete()) {
73
                $this->configureDatabase();
74
                $user = new User($request->only(['username', 'password', 'email']));
75
                $user->level = 10; // admin
76
                $user->setPassword($request->get('password'));
77
                $res = $user->save();
78
79
                if ($res) {
80
                    $message = trans('install.user.success');
81
                    $this->markStepComplete();
82
                }
83
            }
84
        } catch (\Exception $e) {
85
            $message = $e->getMessage();
86
        }
87
88
        return redirect()->back()->with('message', $message);
89
    }
90
91
    public function complete(): bool
92
    {
93
        if ($this->stepCompleted('user')) {
94
            return true;
95
        }
96
97
        try {
98
            if ($this->stepCompleted('database')) {
99
                $exists = User::adminOnly()->exists();
100
                if ($exists) {
101
                    $this->markStepComplete();
102
                }
103
104
                return $exists;
105
            }
106
        } catch (QueryException $e) {
107
            //
108
        }
109
110
        return false;
111
    }
112
113
    public function enabled(): bool
114
    {
115
        return $this->stepCompleted('database');
116
    }
117
118
    public function icon(): string
119
    {
120
        return 'fa-key';
121
    }
122
}
123