-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
D8CORE-7715: Add constraint plugin and field validation plugin to sup…
…port sunet validation on fields
- Loading branch information
Showing
3 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
src/Plugin/FieldValidationRule/ValidSunetIDFieldValidationRule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Drupal\stanford_samlauth\Plugin\FieldValidationRule; | ||
|
||
use Drupal\Core\Form\FormStateInterface; | ||
use Drupal\field_validation\ConstraintFieldValidationRuleBase; | ||
|
||
/** | ||
* Provides functionality for ValidSunetIDValidation. | ||
* | ||
* @FieldValidationRule( | ||
* id = "valid_sunetid_constraint_rule", | ||
* label = @Translation("Valid SunetID"), | ||
* description = @Translation("Validate string is valid sunet.") | ||
* ) | ||
*/ | ||
class ValidSunetIDFieldValidationRule extends ConstraintFieldValidationRuleBase { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getConstraintName(): string { | ||
return "ValidSunetID"; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isPropertyConstraint(): bool { | ||
return TRUE; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function defaultConfiguration() { | ||
return ['message' => 'This is not a valid SunetID.'] + parent::defaultConfiguration(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { | ||
$form = parent::buildConfigurationForm($form, $form_state); | ||
|
||
$form['message'] = [ | ||
'#type' => 'textfield', | ||
'#title' => $this->t('Message'), | ||
'#default_value' => $this->configuration['message'], | ||
'#maxlength' => 255, | ||
]; | ||
|
||
return $form; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { | ||
parent::submitConfigurationForm($form, $form_state); | ||
$this->configuration['message'] = $form_state->getValue('message'); | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
src/Plugin/Validation/Constraint/ValidSunetIDConstraint.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\stanford_samlauth\Plugin\Validation\Constraint; | ||
|
||
use Drupal\Core\StringTranslation\TranslatableMarkup; | ||
use Symfony\Component\Validator\Constraint as SymfonyConstraint; | ||
use Drupal\Core\Validation\Attribute\Constraint; | ||
|
||
/** | ||
* Provides a Valid SunetID constraint. | ||
*/ | ||
#[Constraint( | ||
id: 'ValidSunetID', | ||
label: new TranslatableMarkup('Valid SunetID', [], ['context' => 'Validation']), | ||
)] | ||
final class ValidSunetIDConstraint extends SymfonyConstraint { | ||
|
||
public string $message = '@sunetid is not a valid SunetID.'; | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
src/Plugin/Validation/Constraint/ValidSunetIDConstraintValidator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\stanford_samlauth\Plugin\Validation\Constraint; | ||
|
||
use Drupal\Core\DependencyInjection\ContainerInjectionInterface; | ||
use Drupal\stanford_samlauth\Service\WorkgroupApiInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
|
||
/** | ||
* Validates the Valid SunetID constraint. | ||
*/ | ||
final class ValidSunetIDConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface { | ||
|
||
/** | ||
* Constructs the object. | ||
*/ | ||
public function __construct(private readonly WorkgroupApiInterface $workgroupApi) {} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function create(ContainerInterface $container): self { | ||
return new self($container->get('stanford_samlauth.workgroup_api')); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function validate(mixed $item, Constraint $constraint): void { | ||
if ($this->workgroupApi->connectionSuccessful() && !$this->workgroupApi->isSunetValid($item)) { | ||
$this->context->addViolation($constraint->message); | ||
} | ||
} | ||
|
||
} |