Skip to content

Commit

Permalink
D8CORE-7715: Add constraint plugin and field validation plugin to sup…
Browse files Browse the repository at this point in the history
…port sunet validation on fields
  • Loading branch information
pookmish committed Feb 25, 2025
1 parent 0496539 commit ab3eb82
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/Plugin/FieldValidationRule/ValidSunetIDFieldValidationRule.php
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 src/Plugin/Validation/Constraint/ValidSunetIDConstraint.php
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.';

}
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);
}
}

}

0 comments on commit ab3eb82

Please sign in to comment.