mirror of
https://gitlab.unicamp.br/infimecc_drupal11_modules/site_tools.git
synced 2026-05-04 06:00:42 -03:00
Adiciona condição PageUserIsCurrentUser e atualiza README
- Nova condição de visibilidade de bloco (site_tools_page_owner):
restringe exibição à situação em que o usuário logado é o dono da
rota /user/{id}. Ativada por checkbox na UI; desativada por padrão.
- README atualizado com documentação da condição, do widget MSC 2020
e do sub-módulo de migrate.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
111
src/Plugin/Condition/PageUserIsCurrentUser.php
Normal file
111
src/Plugin/Condition/PageUserIsCurrentUser.php
Normal file
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\site_tools\Plugin\Condition;
|
||||
|
||||
use Drupal\Core\Condition\ConditionPluginBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\Core\Routing\RouteMatchInterface;
|
||||
use Drupal\Core\Session\AccountInterface;
|
||||
use Drupal\user\UserInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Condição: o usuário logado é o dono da página /user/{id}.
|
||||
*
|
||||
* Quando ativa, o bloco só é exibido se o {id} da rota coincide com o
|
||||
* ID do usuário atualmente autenticado.
|
||||
*
|
||||
* @Condition(
|
||||
* id = "site_tools_page_owner",
|
||||
* label = @Translation("Usuário da página é o usuário logado"),
|
||||
* )
|
||||
*/
|
||||
class PageUserIsCurrentUser extends ConditionPluginBase implements ContainerFactoryPluginInterface {
|
||||
|
||||
public function __construct(
|
||||
array $configuration,
|
||||
$plugin_id,
|
||||
$plugin_definition,
|
||||
protected RouteMatchInterface $routeMatch,
|
||||
protected AccountInterface $currentUser,
|
||||
) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
}
|
||||
|
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static {
|
||||
return new static(
|
||||
$configuration,
|
||||
$plugin_id,
|
||||
$plugin_definition,
|
||||
$container->get('current_route_match'),
|
||||
$container->get('current_user'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function defaultConfiguration(): array {
|
||||
return ['enabled' => FALSE] + parent::defaultConfiguration();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
|
||||
$form['enabled'] = [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => $this->t('Exibir apenas para o dono da página'),
|
||||
'#description' => $this->t('Quando marcado, o bloco só é exibido se o usuário logado for o mesmo da rota /user/{id}.'),
|
||||
'#default_value' => $this->configuration['enabled'],
|
||||
];
|
||||
return parent::buildConfigurationForm($form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void {
|
||||
$this->configuration['enabled'] = (bool) $form_state->getValue('enabled');
|
||||
parent::submitConfigurationForm($form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function evaluate(): bool {
|
||||
// Condição desativada: sempre passa.
|
||||
if (empty($this->configuration['enabled'])) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
$param = $this->routeMatch->getParameter('user');
|
||||
|
||||
if ($param instanceof UserInterface) {
|
||||
$page_uid = (int) $param->id();
|
||||
}
|
||||
elseif (is_numeric($param)) {
|
||||
$page_uid = (int) $param;
|
||||
}
|
||||
else {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return $page_uid === (int) $this->currentUser->id();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function summary(): string {
|
||||
if (empty($this->configuration['enabled'])) {
|
||||
return (string) $this->t('Sem restrição por dono da página.');
|
||||
}
|
||||
if ($this->isNegated()) {
|
||||
return (string) $this->t('O usuário logado não é o dono da página.');
|
||||
}
|
||||
return (string) $this->t('O usuário logado é o dono da página.');
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user