Passed
Push — master ( cf35d9...1ae77d )
by Tony
13:43
created

UserPreferencesController::index()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 26
rs 9.3888
c 0
b 0
f 0
cc 5
nc 6
nop 1
1
<?php
2
/**
3
 * UserPreferencesController.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 <http://www.gnu.org/licenses/>.
19
 *
20
 * @package    LibreNMS
21
 * @link       http://librenms.org
22
 * @copyright  2019 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace App\Http\Controllers;
27
28
use App\Models\Dashboard;
29
use App\Models\Device;
30
use App\Models\UserPref;
31
use Illuminate\Http\Request;
32
use Illuminate\Validation\Rule;
33
use LibreNMS\Authentication\LegacyAuth;
34
use LibreNMS\Authentication\TwoFactor;
35
use LibreNMS\Config;
36
use Session;
37
38
class UserPreferencesController extends Controller
39
{
40
    public function __construct()
41
    {
42
        $this->middleware('deny-demo');
43
    }
44
45
    /**
46
     * Display a listing of the resource.
47
     *
48
     * @param \Illuminate\Http\Request $request
49
     * @return \Illuminate\Http\Response
50
     */
51
    public function index(Request $request)
52
    {
53
        $user = $request->user();
54
        $data = [
55
            'user' => $user,
56
            'can_change_password' => LegacyAuth::get()->canUpdatePasswords($user->username),
57
            'dashboards' => Dashboard::allAvailable($user)->with('user')->get(),
58
            'default_dashboard' => UserPref::getPref($user, 'dashboard'),
59
            'note_to_device' => UserPref::getPref($user, 'add_schedule_note_to_device'),
60
            'locale' => UserPref::getPref($user, 'locale') ?: 'en',
61
            'locales' => $this->getValidLocales(),
62
        ];
63
64
        if (Config::get('twofactor')) {
65
            $twofactor = UserPref::getPref($user, 'twofactor');
66
            if ($twofactor) {
67
                $data['twofactor_uri'] = TwoFactor::generateUri($user->username, $twofactor['key'], $twofactor['counter'] !== false);
68
            }
69
            $data['twofactor'] = $twofactor;
70
        }
71
72
        if (!$user->hasGlobalRead()) {
73
            $data['devices'] = Device::hasAccess($user)->get();
74
        }
75
76
        return view('user.preferences', $data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('user.preferences', $data) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
77
    }
78
79
    /**
80
     * Store a newly created resource in storage.
81
     *
82
     * @param \Illuminate\Http\Request $request
83
     * @return \Illuminate\Http\Response
84
     */
85
    public function store(Request $request)
86
    {
87
        $valid_prefs = [
88
            'dashboard' => 'required|integer',
89
            'add_schedule_note_to_device' => 'required|integer',
90
            'locale' => [
91
                'required',
92
                Rule::in(array_keys($this->getValidLocales())),
93
            ],
94
        ];
95
96
        $this->validate($request, [
97
            'pref' => ['required', Rule::in(array_keys($valid_prefs))],
98
            'value' => $valid_prefs[$request->pref] ?? 'required|integer',
99
        ]);
100
101
        UserPref::setPref($request->user(), $request->pref, $request->value);
102
103
        if ($request->pref == 'locale') {
104
            Session::put('locale', $request->value);
105
        }
106
107
        return response()->json(['status' => 'success']);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...'status' => 'success')) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
108
    }
109
110
    private function getValidLocales()
111
    {
112
        return array_reduce(glob(resource_path('lang') . '/*', GLOB_ONLYDIR), function ($locales, $locale) {
0 ignored issues
show
Bug introduced by
It seems like glob(resource_path('lang...ntrollers\GLOB_ONLYDIR) can also be of type false; however, parameter $input of array_reduce() does only seem to accept array, 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 ignore-type  annotation

112
        return array_reduce(/** @scrutinizer ignore-type */ glob(resource_path('lang') . '/*', GLOB_ONLYDIR), function ($locales, $locale) {
Loading history...
113
            {
114
                $locale = basename($locale);
115
                $lang = __('preferences.lang', [], $locale);
116
                $locales[$locale] = ($lang == 'preferences.lang' ? $locale : $lang);
117
                return $locales;
118
            }
119
        }, []);
120
    }
121
}
122