Skip to content

Commit

Permalink
Updated parameters templates.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Dec 9, 2024
1 parent 377edc2 commit 76f7228
Show file tree
Hide file tree
Showing 23 changed files with 156 additions and 110 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ le désirent les éléments de l'application. Aucun droit d'utilisation
supplémentaire n'est accordé au preneur de contrat et aux utilisateurs
autorisés. Ils n'ont en particulier pas le droit de modifier cette application,
de la corriger, de la développer, de la traduire, de la déchiffrer ou de la
décompiler ni de la vendre, la louer, la prêter, la transmettre et ni, surtout,
décompiler ni de la vendre, la louer, la prêter, la transmettre et ni surtout,
de permettre à des tiers de l'utiliser.

## Mises à jour, développements, assistance technique
Expand Down
38 changes: 18 additions & 20 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Form/Admin/ApplicationParametersType.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,17 @@ protected function addSections(FormHelper $helper): void
private function addCustomerSection(FormHelper $helper): void
{
$helper->field(PropertyServiceInterface::P_CUSTOMER_NAME)
->updateOption('prepend_icon', 'fa-solid fa-user-group')
->updateAttribute('spellcheck', 'false')
->addTextType();

$helper->field(PropertyServiceInterface::P_CUSTOMER_ADDRESS)
->updateOption('prepend_icon', 'fa-solid fa-location-dot')
->notRequired()
->addTextType();

$helper->field(PropertyServiceInterface::P_CUSTOMER_ZIP_CITY)
->updateOption('prepend_icon', 'fa-solid fa-map-location-dot')
->notRequired()
->addTextType();

Expand Down
10 changes: 5 additions & 5 deletions src/Form/FormHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public function addDateType(): self
public function addEmailType(): self
{
return $this->updateAttribute('inputmode', 'email')
->updateOption('prepend_icon', 'fa-fw fa-solid fa-at')
->updateOption('prepend_icon', 'fa-solid fa-at')
->updateOption('prepend_class', 'input-group-email')
->add(EmailType::class);
}
Expand Down Expand Up @@ -275,7 +275,7 @@ public function addEnumType(string $class): self
public function addFaxType(?string $pattern = null): self
{
return $this->updateAttribute('pattern', $pattern)
->updateOption('prepend_icon', 'fa-fw fa-solid fa-fax')
->updateOption('prepend_icon', 'fa-solid fa-fax')
->updateOption('prepend_class', 'input-group-fax')
->add(TelType::class);
}
Expand Down Expand Up @@ -432,7 +432,7 @@ public function addSimulateAndConfirmType(TranslatorInterface $translator, bool
*/
public function addTelType(?string $pattern = null): self
{
return $this->updateOption('prepend_icon', 'fa-fw fa-solid fa-phone')
return $this->updateOption('prepend_icon', 'fa-solid fa-phone')
->updateOption('prepend_class', 'input-group-phone')
->updateAttribute('inputmode', 'tel')
->updateAttribute('pattern', $pattern)
Expand Down Expand Up @@ -485,7 +485,7 @@ public function addTrueFalseType(
public function addUrlType(string $protocol = 'https'): self
{
return $this->updateOption('default_protocol', $protocol)
->updateOption('prepend_icon', 'fa-fw fa-solid fa-globe')
->updateOption('prepend_icon', 'fa-solid fa-globe')
->updateOption('prepend_class', 'input-group-url')
->updateAttribute('inputmode', 'url')
->add(UrlType::class);
Expand All @@ -496,7 +496,7 @@ public function addUrlType(string $protocol = 'https'): self
*/
public function addUserNameType(string|bool $autocomplete = 'username'): self
{
return $this->updateOption('prepend_icon', 'fa-fw fa-regular fa-user')
return $this->updateOption('prepend_icon', 'fa-regular fa-user')
->autocomplete($autocomplete)
->updateAttributes([
'minLength' => UserInterface::MIN_USERNAME_LENGTH,
Expand Down
13 changes: 7 additions & 6 deletions src/Form/User/AbstractUserCaptchaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ public function getBlockPrefix(): string
*/
protected function addFormFields(FormHelper $helper): void
{
if ($this->application->isDisplayCaptcha()) {
$helper->field(SecurityAttributes::CAPTCHA_FIELD)
->label('captcha.label')
->constraints(new NotBlank(), new Captcha())
->updateOption('image', $this->service->generateImage())
->add(CaptchaImageType::class);
if (!$this->application->isDisplayCaptcha()) {
return;
}
$helper->field(SecurityAttributes::CAPTCHA_FIELD)
->label('captcha.label')
->constraints(new NotBlank(), new Captcha())
->updateOption('image', $this->service->generateImage())
->add(CaptchaImageType::class);
}
}
8 changes: 8 additions & 0 deletions src/Form/User/UserLoginType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,20 @@

use App\Form\FormHelper;
use App\Security\SecurityAttributes;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* User login type.
*/
class UserLoginType extends AbstractUserCaptchaType
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'csrf_protection' => false,
]);
}

protected function addFormFields(FormHelper $helper): void
{
$helper->field(SecurityAttributes::USER_FIELD)
Expand Down
42 changes: 26 additions & 16 deletions src/Security/LoginFormAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
use App\Service\CaptchaImageService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
Expand All @@ -45,10 +43,13 @@ public function __construct(
public function authenticate(Request $request): Passport
{
$this->validateCaptcha($request);
[$user, $password, $token] = $this->getCredentials($request);

$userIdentifier = $this->getUserIdentifier($request);
$password = $this->getPassword($request);
$token = $this->getToken($request);

return new Passport(
new UserBadge($user, $this->repository->loadUserByIdentifier(...)),
new UserBadge($userIdentifier, $this->repository->loadUserByIdentifier(...)),
new PasswordCredentials($password),
[
new RememberMeBadge(),
Expand All @@ -75,28 +76,37 @@ protected function getLoginUrl(Request $request): string
return $this->httpUtils->generateUri($request, SecurityAttributes::LOGIN_ROUTE);
}

/**
* @psalm-return list{non-empty-string, non-empty-string, non-empty-string}
*/
private function getCredentials(Request $request): array
private function getPassword(Request $request): string
{
$user = \trim($request->request->getString(SecurityAttributes::USER_FIELD));
if ('' === $user) {
throw new BadCredentialsException('The username must be a non-empty string.');
}
$password = $request->request->getString(SecurityAttributes::PASSWORD_FIELD);
if ('' === $password) {
throw new BadCredentialsException('The password must be a non-empty string.');
throw new CustomUserMessageAuthenticationException('authenticator.empty_password');
}

return $password;
}

private function getToken(Request $request): string
{
$token = $request->request->getString(SecurityAttributes::LOGIN_TOKEN);
if ('' === $token) {
throw new BadRequestHttpException('The token must be a non-empty string.');
throw new CustomUserMessageAuthenticationException('authenticator.empty_token');
}

return $token;
}

private function getUserIdentifier(Request $request): string
{
$userIdentifier = \trim($request->request->getString(SecurityAttributes::USER_FIELD));
if ('' === $userIdentifier) {
throw new CustomUserMessageAuthenticationException('authenticator.empty_user');
}
if ($request->hasSession()) {
$request->getSession()->set(SecurityRequestAttributes::LAST_USERNAME, $user);
$request->getSession()->set(SecurityRequestAttributes::LAST_USERNAME, $userIdentifier);
}

return [$user, $password, $token];
return $userIdentifier;
}

private function validateCaptcha(Request $request): void
Expand Down
15 changes: 12 additions & 3 deletions src/Traits/PropertyServiceTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,20 @@
*/
trait PropertyServiceTrait
{
use CacheTrait;
use CacheTrait {
getCacheValue as private getCacheValueFromTrait;
}

// The saved cache state property name
private const P_CACHE_SAVED = 'cache_saved';

public function getCacheValue(string $key, mixed $default = null): mixed
{
$this->initialize();

return $this->getCacheValueFromTrait($key, $default);
}

public function isActionEdit(): bool
{
return EntityAction::EDIT === $this->getEditAction();
Expand Down Expand Up @@ -193,11 +202,11 @@ protected function getPropertyString(string $name, ?string $default = null): ?st
}

/**
* Initialize cache properties.
* Initialize cached properties.
*/
protected function initialize(): void
{
if (!$this->getPropertyBoolean(self::P_CACHE_SAVED) && $this->isConnected()) {
if (!(bool) $this->getCacheValueFromTrait(self::P_CACHE_SAVED, false) && $this->isConnected()) {
$this->updateAdapter();
}
}
Expand Down
16 changes: 8 additions & 8 deletions templates/admin/parameters.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
{%- set title_description = 'parameters.description' -%}
{# sections #}
{% block sections -%}
{{ _self.collapseLine('customer', form, true) }}
{{ _self.collapseLine('default', form) }}
{{ _self.collapseLine('product', form) }}
{{ _self.collapseLine('display', form) }}
{{ _self.collapseLine('message', form) }}
{{ _self.collapseLine('home', form) }}
{{ _self.collapseLine('options', form) }}
{{ _self.collapseLine('customer', form, 'user-tie', true) }}
{{ _self.collapseLine('default', form, 'file-code') }}
{{ _self.collapseLine('product', form, 'file-alt') }}
{{ _self.collapseLine('display', form, 'display') }}
{{ _self.collapseLine('message', form, 'bell') }}
{{ _self.collapseLine('home', form, 'house-user') }}
{{ _self.collapseLine('options', form, 'list-check') }}
{% if is_super_admin %}
{{ _self.collapseLine('security', form, false, options) }}
{{ _self.collapseLine('security', form, 'lock', false, options) }}
{% endif %}
{%- endblock %}
4 changes: 2 additions & 2 deletions templates/fields.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@
<div class="input-group">
{% if prepend_icon %}
<div class="input-group-text"{% if prepend_title %} title="{{ prepend_title|trans }}"{% endif %}>
<i class="{{ prepend_icon }}"></i>
<i class="fa-fw {{ prepend_icon }}"></i>
</div>
{% endif %}
{{- parent() -}}
{% if append_icon %}
<div class="input-group-text"{% if append_title %} title="{{ append_title|trans }}"{% endif %}>
<i class="{{ append_icon }}"></i>
<i class="fa-fw {{ append_icon }}"></i>
</div>
{% endif %}
</div>
Expand Down
5 changes: 3 additions & 2 deletions templates/parameters/_parameters.html.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{% extends 'cards/card_edit.html.twig' %}
{% from 'macros/_icons.html.twig' import icon as output_icon %}
{% form_theme form with _self %}
{# macros #}
{% macro collapseLine(href, form, expanded = false, options = null) %}
{% macro collapseLine(href, form, icon, expanded = false, options = null) %}
{%- set header_id = 'header_' ~ href -%}
{%- set name = 'parameters.group_%s'|format(href)|trans -%}
{%- set template = 'parameters/_parameters_%s.html.twig'|format(href) -%}
Expand All @@ -10,7 +11,7 @@
{%- set title = expanded ? hide : show -%}
<div class="card-header d-flex-wrap-center rounded-0">
<a class="drop-down-icon-right card-title" href="#{{ href }}" data-bs-toggle="collapse" aria-expanded="{{ expanded|json_encode }}" title="{{ title }}" data-show="{{ show }}" data-hide="{{ hide }}">
{{- name -}}
{{- output_icon(icon ~ ' fa-fw me-1') -}}{{- name -}}
</a>
</div>
<div id="{{ href }}" class="card-body pt-0 collapse{% if expanded %} show{% endif %}" data-bs-parent=".card-parameter">
Expand Down
8 changes: 4 additions & 4 deletions templates/user/user_parameters.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
{%- set title_description = 'user.parameters.description' -%}
{# sections #}
{% block sections -%}
{{ _self.collapseLine('display', form, true) }}
{{ _self.collapseLine('message', form) }}
{{ _self.collapseLine('home', form) }}
{{ _self.collapseLine('options', form) }}
{{ _self.collapseLine('display', form, 'display', true) }}
{{ _self.collapseLine('message', form, 'bell') }}
{{ _self.collapseLine('home', form, 'house-user') }}
{{ _self.collapseLine('options', form, 'list-check') }}
{%- endblock %}
Loading

0 comments on commit 76f7228

Please sign in to comment.