Issues (2963)

app/Http/Controllers/UserPreferencesController.php (2 issues)

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 <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\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\Support\Facades\Auth;
33
use Illuminate\Validation\Rule;
34
use LibreNMS\Authentication\LegacyAuth;
35
use LibreNMS\Authentication\TwoFactor;
36
use LibreNMS\Config;
37
use LibreNMS\Util\DynamicConfig;
38
use Session;
39
40
class UserPreferencesController extends Controller
41
{
42
    private $cachedPreferences = ['locale', 'site_style'];
43
44
    public function __construct()
45
    {
46
        $this->middleware('deny-demo');
47
    }
48
49
    /**
50
     * Display a listing of the resource.
51
     *
52
     * @param  \Illuminate\Http\Request  $request
53
     * @return \Illuminate\View\View
54
     */
55
    public function index(Request $request)
56
    {
57
        $user = $request->user();
58
59
        $locales = $this->getValidLocales();
60
        $styles = $this->getValidStyles();
61
        $default_locale = \config('app.locale');
62
        $default_style = Config::get('site_style');
63
64
        $data = [
65
            'user' => $user,
66
            'can_change_password' => LegacyAuth::get()->canUpdatePasswords($user->username),
67
            'dashboards' => Dashboard::allAvailable($user)->with('user')->get(),
68
            'default_dashboard' => UserPref::getPref($user, 'dashboard'),
69
            'note_to_device' => UserPref::getPref($user, 'add_schedule_note_to_device'),
70
            'locale' => UserPref::getPref($user, 'locale'),
71
            'locale_default' => $locales[$default_locale] ?? $default_locale,
72
            'locales' => $locales,
73
            'site_style' => UserPref::getPref($user, 'site_style'),
74
            'site_style_default' => $styles[$default_style] ?? $default_style,
75
            'site_styles' => $styles,
76
            'hide_dashboard_editor' => UserPref::getPref($user, 'hide_dashboard_editor') ?? 0,
77
        ];
78
79
        if (Config::get('twofactor')) {
80
            $twofactor = UserPref::getPref($user, 'twofactor');
81
            if ($twofactor) {
82
                $data['twofactor_uri'] = TwoFactor::generateUri($user->username, $twofactor['key'], $twofactor['counter'] !== false);
83
            }
84
            $data['twofactor'] = $twofactor;
85
        }
86
87
        if (! $user->hasGlobalRead()) {
88
            $data['devices'] = Device::hasAccess($user)->get();
89
        }
90
91
        return view('user.preferences', $data);
92
    }
93
94
    /**
95
     * Store a newly created resource in storage.
96
     *
97
     * @param  \Illuminate\Http\Request  $request
98
     * @return \Illuminate\Http\JsonResponse
99
     */
100
    public function store(Request $request)
101
    {
102
        $valid_prefs = [
103
            'dashboard' => 'required|integer',
104
            'add_schedule_note_to_device' => 'required|integer',
105
            'locale' => [
106
                'required',
107
                Rule::in(array_merge(['default'], array_keys($this->getValidLocales()))),
108
            ],
109
            'site_style' => [
110
                'required',
111
                Rule::in(array_merge(['default'], array_keys($this->getValidStyles()))),
112
            ],
113
            'hide_dashboard_editor' => 'required|integer',
114
        ];
115
116
        $this->validate($request, [
117
            'pref' => ['required', Rule::in(array_keys($valid_prefs))],
118
            'value' => $valid_prefs[$request->pref] ?? 'required|integer',
119
        ]);
120
121
        $this->updatePreference($request->pref, $request->value);
122
123
        return response()->json(['status' => 'success']);
124
    }
125
126
    private function getValidLocales()
127
    {
128
        return array_reduce(glob(resource_path('lang') . '/*', GLOB_ONLYDIR), function ($locales, $locale) {
129
            $locale = basename($locale);
130
            $lang = __('preferences.lang', [], $locale);
131
            $locales[$locale] = ($lang == 'preferences.lang' ? $locale : $lang);
132
133
            return $locales;
134
        }, []);
135
    }
136
137
    private function getValidStyles()
138
    {
139
        $definitions = new DynamicConfig();
140
141
        return $definitions->get('site_style')->getOptions();
142
    }
143
144
    private function updatePreference($preference, $value)
145
    {
146
        if ($value == 'default') {
147
            UserPref::forgetPref(Auth::user(), $preference);
0 ignored issues
show
It seems like Illuminate\Support\Facades\Auth::user() can also be of type null; however, parameter $user of App\Models\UserPref::forgetPref() does only seem to accept App\Models\User, 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

147
            UserPref::forgetPref(/** @scrutinizer ignore-type */ Auth::user(), $preference);
Loading history...
148
            if (in_array($preference, $this->cachedPreferences)) {
149
                Session::forget('preferences.' . $preference);
150
            }
151
        } else {
152
            UserPref::setPref(Auth::user(), $preference, $value);
0 ignored issues
show
It seems like Illuminate\Support\Facades\Auth::user() can also be of type null; however, parameter $user of App\Models\UserPref::setPref() does only seem to accept App\Models\User, 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

152
            UserPref::setPref(/** @scrutinizer ignore-type */ Auth::user(), $preference, $value);
Loading history...
153
            if (in_array($preference, $this->cachedPreferences)) {
154
                Session::put('preferences.' . $preference, $value);
155
            }
156
        }
157
    }
158
}
159