You are here

public function OwnOrAdminConstraintValidator::validate in Simple OAuth (OAuth2) & OpenID Connect 8

File

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

Class

OwnOrAdminConstraintValidator
Class OwnOrAdminConstraintValidator.

Namespace

Drupal\simple_oauth\Plugin\Validation\Constraint

Code

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(),
  ]);
}