refactor: [WIP] Migrate user settings controller.

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2022-06-12 11:31:59 +02:00
parent b6b8d352a0
commit 7dc998dc23
2 changed files with 121 additions and 106 deletions

View File

@@ -0,0 +1,121 @@
<?php
/*
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
* sysPass 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.
*
* sysPass 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 sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
namespace SP\Modules\Web\Controllers\UserSettingsGeneral;
use Exception;
use Klein\Klein;
use SP\Core\Acl\Acl;
use SP\Core\Application;
use SP\Core\Events\Event;
use SP\Core\PhpExtensionChecker;
use SP\Core\UI\ThemeInterface;
use SP\DataModel\UserPreferencesData;
use SP\Domain\User\Services\UserLoginResponse;
use SP\Domain\User\Services\UserService;
use SP\Domain\User\UserServiceInterface;
use SP\Http\JsonResponse;
use SP\Http\RequestInterface;
use SP\Modules\Web\Controllers\SimpleControllerBase;
use SP\Modules\Web\Controllers\Traits\JsonTrait;
/**
* Class SaveController
*
* @package SP\Modules\Web\Controllers
*/
final class SaveController extends SimpleControllerBase
{
use JsonTrait;
private UserService $userService;
public function __construct(
Application $application,
ThemeInterface $theme,
Klein $router,
Acl $acl,
RequestInterface $request,
PhpExtensionChecker $extensionChecker,
UserServiceInterface $userService
) {
parent::__construct($application, $theme, $router, $acl, $request, $extensionChecker);
$this->checks();
$this->userService = $userService;
}
/**
* @return bool
* @throws \JsonException
*/
public function saveAction(): bool
{
try {
$userData = $this->session->getUserData();
$userPreferencesData = $this->getUserPreferencesData($userData);
$this->userService->updatePreferencesById($userData->getId(), $userPreferencesData);
// Save preferences in current session
$userData->setPreferences($userPreferencesData);
return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Preferences updated'));
} catch (Exception $e) {
processException($e);
$this->eventDispatcher->notifyEvent('exception', new Event($e));
return $this->returnJsonResponseException($e);
}
}
/**
* @param \SP\Domain\User\Services\UserLoginResponse $userData
*
* @return \SP\DataModel\UserPreferencesData
*/
private function getUserPreferencesData(UserLoginResponse $userData): UserPreferencesData
{
$userPreferencesData = clone $userData->getPreferences();
$userPreferencesData->setUserId($userData->getId());
$userPreferencesData->setLang($this->request->analyzeString('userlang'));
$userPreferencesData->setTheme($this->request->analyzeString('usertheme', 'material-blue'));
$userPreferencesData->setResultsPerPage($this->request->analyzeInt('resultsperpage', 12));
$userPreferencesData->setAccountLink($this->request->analyzeBool('account_link', false));
$userPreferencesData->setSortViews($this->request->analyzeBool('sort_views', false));
$userPreferencesData->setTopNavbar($this->request->analyzeBool('top_navbar', false));
$userPreferencesData->setOptionalActions($this->request->analyzeBool('optional_actions', false));
$userPreferencesData->setResultsAsCards($this->request->analyzeBool('resultsascards', false));
$userPreferencesData->setCheckNotifications($this->request->analyzeBool('check_notifications', false));
$userPreferencesData->setShowAccountSearchFilters(
$this->request->analyzeBool('show_account_search_filters', false)
);
return $userPreferencesData;
}
}