Updated user settings page and user forms

This commit is contained in:
2023-06-01 10:59:51 +01:00
parent fc707b1abf
commit a4da55aa47
15 changed files with 386 additions and 1 deletions

View File

@@ -0,0 +1,111 @@
<?php
/*
* AVSDev UF Tweaks (https://avsdev.uk)
*
* @link https://git.avsdev.uk/avsdev/sprinkle-uf-tweaks
* @license https://git.avsdev.uk/avsdev/sprinkle-uf-tweaks/blob/master/LICENSE.md (LGPL-3.0 License)
*/
namespace UserFrosting\Sprinkle\UFTweaks\Controller;
use Carbon\Carbon;
use Illuminate\Database\Capsule\Manager as Capsule;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use UserFrosting\Fortress\Adapter\JqueryValidationAdapter;
use UserFrosting\Fortress\RequestDataTransformer;
use UserFrosting\Fortress\RequestSchema;
use UserFrosting\Fortress\ServerSideValidator;
use UserFrosting\Sprinkle\Account\Controller\AccountController as UFAccountController;
use UserFrosting\Support\Exception\BadRequestException;
use UserFrosting\Support\Exception\ForbiddenException;
use UserFrosting\Support\Exception\NotFoundException;
/**
* Override account controller class to tweak the Settings page
*
* @author Craig Williams (craig@avsdev.uk)
*/
class AccountController extends UFAccountController
{
/**
* Account settings page.
*
* Provides a form for users to modify various properties of their account, such as name, email, locale, etc.
* Any fields that the user does not have permission to modify will be automatically disabled.
* This page requires authentication.
*
* AuthGuard: true
* Route: /account/settings
* Route Name: {none}
* Request type: GET
*
* @param Request $request
* @param Response $response
* @param array $args
*
* @throws ForbiddenException If user is not authorized to access page
*/
public function pageSettings(Request $request, Response $response, $args)
{
/** @var \UserFrosting\Support\Repository\Repository $config */
$config = $this->ci->config;
/** @var \UserFrosting\Sprinkle\Account\Authorize\AuthorizationManager */
$authorizer = $this->ci->authorizer;
/** @var \UserFrosting\Sprinkle\Account\Database\Models\Interfaces\UserInterface $currentUser */
$currentUser = $this->ci->currentUser;
// Access-controlled page
if (!$authorizer->checkAccess($currentUser, 'uri_account_settings')) {
throw new ForbiddenException();
}
// Load validation rules
$schema = new RequestSchema('schema://requests/account-settings.yaml');
$schema->set('password.validators.length.min', $config['site.password.length.min']);
$schema->set('password.validators.length.max', $config['site.password.length.max']);
$schema->set('passwordc.validators.length.min', $config['site.password.length.min']);
$schema->set('passwordc.validators.length.max', $config['site.password.length.max']);
$validatorAccountSettings = new JqueryValidationAdapter($schema, $this->ci->translator);
$schema = new RequestSchema('schema://requests/profile-settings.yaml');
$validatorProfileSettings = new JqueryValidationAdapter($schema, $this->ci->translator);
// Get a list of all locales
$locales = $this->ci->locale->getAvailableOptions();
// Hide the locale field if there is only 1 locale available
$fields = [
'hidden' => [],
'disabled' => [],
];
if (count($locales) <= 1) {
$fields['hidden'][] = 'locale';
}
if (!$authorizer->checkAccess($currentUser, 'update_account_settings')) {
$fields['disabled'][] = 'name';
$fields['disabled'][] = 'locale';
$fields['disabled'][] = 'email';
$fields['disabled'][] = 'copy';
$fields['hidden'][] = 'password';
$fields['hidden'][] = 'submit';
}
return $this->ci->view->render($response, 'pages/account-settings.html.twig', [
'user' => $currentUser,
'locales' => $locales,
'form' => [
'fields' => $fields,
],
'page' => [
'validators' => [
'account_settings' => $validatorAccountSettings->rules('json', false),
'profile_settings' => $validatorProfileSettings->rules('json', false),
]
],
]);
}
}