You are here

class OwnOrAdminConstraintValidator in Simple OAuth (OAuth2) & OpenID Connect 8

Class OwnOrAdminConstraintValidator.

@package Drupal\simple_oauth\Plugin\Validation\Constraint

Hierarchy

Expanded class hierarchy of OwnOrAdminConstraintValidator

1 file declares its use of OwnOrAdminConstraintValidator
OwnOrAdminConstraintValidatorTest.php in tests/src/Unit/Plugin/Validation/Constraint/OwnOrAdminConstraintValidatorTest.php

File

src/Plugin/Validation/Constraint/OwnOrAdminConstraintValidator.php, line 13

Namespace

Drupal\simple_oauth\Plugin\Validation\Constraint
View source
class OwnOrAdminConstraintValidator extends ConstraintValidator {

  /**
   * {@inheritdoc}
   */
  public function validate($value, Constraint $constraint) {

    /* @var OwnOrAdminConstraintInterface $constraint */
    $account = $constraint
      ->getAccount();
    if ($value == $account
      ->id()) {

      // No violation if the user is the same as the provided one.
      return NULL;
    }
    if ($account
      ->hasPermission($constraint
      ->getPermission())) {

      // No violation if the current user has admin rights.
      return NULL;
    }
    $this->context
      ->addViolation($constraint->message, [
      '@permission' => $constraint
        ->getPermission(),
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OwnOrAdminConstraintValidator::validate public function Checks if the passed value is valid.