Passed
Push — master ( 9455a8...ea45fd )
by Tony
15:01 queued 04:17
created

UserPreferencesController::index()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 31
rs 8.6666
c 0
b 0
f 0
cc 7
nc 18
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
    private $valid_prefs = [
41
        'dashboard' => 'required|integer',
42
        'add_schedule_note_to_device' => 'required|integer',
43
        'locale' => 'required|in:en,ru',
44
    ];
45
46
    public function __construct()
47
    {
48
        $this->middleware('deny-demo');
49
    }
50
51
    /**
52
     * Display a listing of the resource.
53
     *
54
     * @param \Illuminate\Http\Request $request
55
     * @return \Illuminate\Http\Response
56
     */
57
    public function index(Request $request)
58
    {
59
        $user = $request->user();
60
        $data = [
61
            'user' => $user,
62
            'can_change_password' => LegacyAuth::get()->canUpdatePasswords($user->username),
63
            'dashboards' => Dashboard::allAvailable($user)->with('user')->get(),
64
            'default_dashboard' => UserPref::getPref($user, 'dashboard'),
65
            'note_to_device' => UserPref::getPref($user, 'add_schedule_note_to_device'),
66
            'locale' => UserPref::getPref($user, 'locale') ?: 'en',
67
        ];
68
69
        foreach (glob(resource_path('lang') . '/*', GLOB_ONLYDIR) as $locale) {
70
            $locale = basename($locale);
71
            $lang = __('preferences.lang', [], $locale);
72
            $data['locales'][$locale] = ($lang == 'preferences.lang' ? $locale : $lang);
73
        }
74
75
        if (Config::get('twofactor')) {
76
            $twofactor = UserPref::getPref($user, 'twofactor');
77
            if ($twofactor) {
78
                $data['twofactor_uri'] = TwoFactor::generateUri($user->username, $twofactor['key'], $twofactor['counter'] !== false);
79
            }
80
            $data['twofactor'] = $twofactor;
81
        }
82
83
        if (!$user->hasGlobalRead()) {
84
            $data['devices'] = Device::hasAccess($user)->get();
85
        }
86
87
        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...
88
    }
89
90
    /**
91
     * Store a newly created resource in storage.
92
     *
93
     * @param  \Illuminate\Http\Request  $request
94
     * @return \Illuminate\Http\Response
95
     */
96
    public function store(Request $request)
97
    {
98
        $this->validate($request, [
99
            'pref' => ['required', Rule::in(array_keys($this->valid_prefs))],
100
            'value' => $this->valid_prefs[$request->pref] ?? 'required|integer',
101
        ]);
102
103
        UserPref::setPref($request->user(), $request->pref, $request->value);
104
105
        if ($request->pref == 'locale') {
106
            Session::put('locale', $request->value);
107
        }
108
109
        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...
110
    }
111
}
112