public static function collect(array $intents) { $intents = array_map(function (Intent $intent) { return self::convertIntentToArray($intent); }, array_values($intents)); $finalArray = []; foreach ($intents as $intent) { foreach ($intent['fields'] as $field) { foreach ($field['profiles'] as $profile) { $finalArray['' . $profile] = true; } } } return ['intents' => $intents, 'profiles' => array_map(function ($identifier) { return self::convertValidationProfileToArray(Engine::get()->getValidationProfile($identifier)); }, array_keys($finalArray))]; }
/** * Validates that we can actually use this action * @param IntentPayload $payload */ public function validate(IntentPayload $payload, $throw = true) { $payload->setIntent($this); //is this intent allowed at all? if (!Engine::get()->isIntentAllowed($this)) { if ($throw) { throw new ValidationException("Rights check failed for intent: [{$this->getName()}]: not allowed to access this intent."); } return false; } $checksPassed = 0; $shouldParametersBeValidated = Engine::get()->shouldParametersBeValidated($this); if (count($this->inputParameters) == 0) { return true; } //yes it is allowed, now validate each parameter //for each parameter we have, we must find the value in the payload foreach ($this->inputParameters as $parameter) { //check to see if the parameter exists if (!$payload->parameterExists($parameter->getName())) { if (!$throw) { return false; } throw new ValidationException("Rights check failed for intent: [{$this->getName()}] on parameter: [{$parameter->getName()}] " . "because the input variable was not found"); } //should we check parameters (useful if the user is an admin in your system, because you can then skip this step) if ($shouldParametersBeValidated) { //now we need to find validators for the nodes $profiles = Engine::get()->getValidationProfilesForIntentParameter($this, $parameter); //validate each profile which contains an entry node $failures = []; $success = 0; foreach ($profiles as $profile) { try { $result = $profile->validate($payload->get($parameter->getName())); } catch (ValidationException $ex) { $failures[] = "Rights check failed for intent: [{$this->getName()}] on parameter: [{$parameter->getName()}] reason: [{$ex->getMessage()}]"; continue; } //and use the value from the payload (if ever a payload returns false, then we know something went wrong, but always prefer exceptions) if ($result === false) { $failures[] = "Rights check failed for intent: [{$this->getName()}] on parameter: [{$parameter->getName()}]"; continue; } $checksPassed++; $success++; } //there were failures and nothing was successful if ($success == 0 && count($failures) > 0) { if (!$throw) { return false; } throw new ValidationException(implode(',', $failures)); } } } //if this happens, it means we didnt find any profiles if ($checksPassed == 0 && $shouldParametersBeValidated) { if (!$throw) { return false; } throw new ValidationException("Rights check failed for intent: [{$this->getName()}] because there were no profiles setup to test with"); } //all checks passed return true; }
public function serialize() { return ["type" => Engine::get()->getValidationNodeFactoryForClass(get_class($this))->getName()]; }
public function deserialize($data) { parent::deserialize($data); $this->profile = Engine::get()->getValidationProfile($data['external']); }