User configurable locale (language) (#10204)

* Support for system APP_LOCALE

* Start preferences re-write

* port 2fa form

* Working user preferences

* Language user preference

* Don't look up locale from the DB every request

* Device list working

* Deny demo user middleware

* Finish password changing

* remove used resource methods

* remove leftover use

* warn that translation is incomplete

* fix style
This commit is contained in:
Tony Murray
2019-05-23 10:05:45 -05:00
committed by GitHub
parent 90bb68f026
commit 90a67c2ece
15 changed files with 449 additions and 250 deletions

View File

@@ -89,7 +89,7 @@ class TwoFactorController extends Controller
return view('auth.2fa')->with([
'key' => $twoFactorSettings['key'],
'uri' => $this->genUri($request->user(), $twoFactorSettings),
'uri' => TwoFactor::generateUri($request->user()->username, $twoFactorSettings['key'], $twoFactorSettings['counter'] !== false),
])->withErrors($errors);
}
@@ -211,18 +211,4 @@ class TwoFactorController extends Controller
return UserPref::getPref($user, 'twofactor');
}
private function genUri($user, $settings)
{
$title = "LibreNMS:" . urlencode($user->username);
$key = $settings['key'];
// time based
if ($settings['counter'] === false) {
return "otpauth://totp/$title?issuer=LibreNMS&secret=$key";
}
// counter based
return "otpauth://hotp/$title?issuer=LibreNMS&counter=1&secret=$key";
}
}

View File

@@ -30,15 +30,18 @@ use App\Http\Requests\UpdateUserRequest;
use App\Models\Dashboard;
use App\Models\User;
use App\Models\UserPref;
use Hash;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use LibreNMS\Authentication\LegacyAuth;
use LibreNMS\Config;
use Toastr;
use URL;
class UserController extends Controller
{
public function __construct()
{
$this->middleware('deny-demo');
}
/**
* Display a listing of the resource.
*
@@ -159,9 +162,8 @@ class UserController extends Controller
}
$user->fill($request->all());
$user->can_modify_passwd = $request->get('can_modify_passwd'); // checkboxes are missing when unchecked
if ($this->updateDashboard($user, $request->get('dashboard'))) {
if ($request->has('dashboard') && $this->updateDashboard($user, $request->get('dashboard'))) {
Toastr::success(__('Updated dashboard for :username', ['username' => $user->username]));
}
@@ -174,7 +176,7 @@ class UserController extends Controller
}
}
return redirect(route('users.index'));
return redirect(route(str_contains(URL::previous(), 'preferences') ? 'preferences.index' : 'users.index'));
}
/**

View File

@@ -0,0 +1,109 @@
<?php
/**
* UserPreferencesController.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 <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2019 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace App\Http\Controllers;
use App\Models\Dashboard;
use App\Models\Device;
use App\Models\UserPref;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use LibreNMS\Authentication\LegacyAuth;
use LibreNMS\Authentication\TwoFactor;
use LibreNMS\Config;
use Session;
class UserPreferencesController extends Controller
{
private $valid_prefs = [
'dashboard' => 'required|integer',
'add_schedule_note_to_device' => 'required|integer',
'locale' => 'required|in:en,ru',
];
public function __construct()
{
$this->middleware('deny-demo');
}
/**
* Display a listing of the resource.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$user = $request->user();
$data = [
'user' => $user,
'can_change_password' => LegacyAuth::get()->canUpdatePasswords($user->username),
'dashboards' => Dashboard::allAvailable($user)->with('user')->get(),
'default_dashboard' => UserPref::getPref($user, 'dashboard'),
'note_to_device' => UserPref::getPref($user, 'add_schedule_note_to_device'),
'locale' => UserPref::getPref($user, 'locale') ?: 'en',
'locales' => [
'en' => 'English',
'ru' => 'русский',
],
];
if (Config::get('twofactor')) {
$twofactor = UserPref::getPref($user, 'twofactor');
if ($twofactor) {
$data['twofactor_uri'] = TwoFactor::generateUri($user->username, $twofactor['key'], $twofactor['counter'] !== false);
}
$data['twofactor'] = $twofactor;
}
if (!$user->hasGlobalRead()) {
$data['devices'] = Device::hasAccess($user)->get();
}
return view('user.preferences', $data);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'pref' => ['required', Rule::in(array_keys($this->valid_prefs))],
'value' => $this->valid_prefs[$request->pref] ?? 'required|integer',
]);
UserPref::setPref($request->user(), $request->pref, $request->value);
if ($request->pref == 'locale') {
Session::put('locale', $request->value);
}
return response()->json(['status' => 'success']);
}
}