$model * @return array> */ public function for(string $model, bool $applicationApi = false): array { return array_filter( $this->registry->for($model, $applicationApi), $this->extensions->isAvailable(...), ARRAY_FILTER_USE_KEY, ); } /** * The running extensions whose fields the signed-in user may change on a model. One * whose authorize() throws is recorded as failing and left out. * * @param Model|class-string $model the model being updated, or the class being created * @return list */ public function authorizedFor(Model|string $model, bool $applicationApi = false): array { $authorized = []; foreach ($this->for($this->modelClass($model), $applicationApi) as $extension => $fields) { try { if ($this->authorized($fields, $model)) { $authorized[] = $extension; } } catch (Throwable $exception) { $this->extensions->recordFailure($extension, sprintf('Authorizing its %s fields failed with %s; the log has the details.', class_basename($this->modelClass($model)), class_basename($exception)), $exception, 'fields'); } } return $authorized; } /** * Refuses a request that sends values for fields the signed-in user may not change. * * @param Model|class-string $model the model being updated, or the class being created * @param list $submitted the extensions the request sends values for * * @throws AuthorizationException */ public function authorize(Model|string $model, array $submitted, bool $applicationApi = false): void { foreach (array_intersect_key($this->for($this->modelClass($model), $applicationApi), array_flip($submitted)) as $extension => $fields) { throw_unless($this->authorized($fields, $model), AuthorizationException::class, sprintf('You are not allowed to change the %s fields.', $extension)); } } /** * Validates each extension's submitted values against its own rules, adding failures to * $validator as `extensions..`. Values for extensions that are not running * are ignored, so their stored values stay as they are. Each extension in $defaults * that $submitted leaves out is validated with no values, so its required rules hold, * and nothing is saved for it. A secret field sent as its mask or not at all keeps its * stored value. An extension whose fields throw while validating is recorded as failing * and its values are refused. * * @param Model|class-string $model the model being updated, or the class being created * @param array> $submitted each extension's submitted values * @param list $defaults extensions to validate even when $submitted leaves them out */ public function validate(Validator $validator, Model|string $model, array $submitted, bool $applicationApi = false, array $defaults = []): ValidatedExtensionValues { $values = $submitted + array_fill_keys($defaults, []); $validated = []; foreach (array_intersect_key($this->for($this->modelClass($model), $applicationApi), $values) as $extension => $fields) { try { [$errors, $checked] = $this->check($extension, $fields, $model, $values[$extension]); } catch (Throwable $exception) { // The message can carry query bindings or credentials, so only the log gets it. $this->extensions->recordFailure($extension, sprintf('Validating its %s fields failed with %s; the log has the details.', class_basename($this->modelClass($model)), class_basename($exception)), $exception, 'fields'); $validator->errors()->add('extensions.'.$extension, sprintf('The %s fields could not be read, so they were not saved.', $extension)); continue; } if ($errors !== []) { foreach ($errors as $field => $messages) { foreach ($messages as $message) { $validator->errors()->add(sprintf('extensions.%s.%s', $extension, $field), $message); } } continue; } if (array_key_exists($extension, $submitted)) { $validated[$extension] = $checked; } } return ValidatedExtensionValues::fromValidation($validated); } /** * Saves the validated values an action received in its data's `extensions` entry. Call * it inside the transaction that writes the model, after the row is written. */ public function save(Model $model, ValidatedExtensionValues $values): void { $values = $values->all(); foreach (array_intersect_key($this->for($model::class), $values) as $extension => $fields) { if (method_exists($fields, 'save')) { $this->container->call($fields.'@save', [...$this->modelParameters($model), 'values' => $values[$extension]]); } else { $this->extensions->settings($extension)->fields($model)->setManySecrets($values[$extension], $this->secrets($fields, $model)); } } } /** * The values of every extension the signed-in user may see, keyed by extension id, with * secret fields masked. An extension that fails to read them is recorded as failing and * left out, so the admin forms hide its fields rather than saving over its values. * * @return ExtensionFieldInput */ public function values(Model $model, bool $applicationApi = false): array { $values = []; foreach ($this->for($model::class, $applicationApi) as $extension => $fields) { try { if ($this->authorized($fields, $model)) { $values[$extension] = $this->masked($this->current($extension, $fields, $model), $this->secrets($fields, $model)); } } catch (Throwable $exception) { // The message can carry query bindings or credentials, so only the log gets it. $this->extensions->recordFailure($extension, sprintf('Reading its %s fields failed with %s; the log has the details.', class_basename($model), class_basename($exception)), $exception, 'fields'); } } return $values; } /** * The extensions whose fields each admin form shows to the signed-in user, keyed by * form name (`admin.user`, `admin.database_host`, ...). * * @return array> */ public function forms(): array { $forms = []; foreach (ExtensionSettings::SCOPES as $alias => $model) { foreach ($this->for($model) as $extension => $fields) { if (rescue(fn (): bool => $this->authorized($fields, $model), false)) { $forms['admin.'.$alias][] = ['id' => $extension, 'name' => $this->extensions->enabled()->get($extension)->name ?? $extension]; } } } return $forms; } /** * The model for a parameter typed with its class: null while it is being created. * * @param Model|class-string $model * @return array */ private function modelParameters(Model|string $model): array { $instance = $model instanceof Model ? $model : null; return [$this->modelClass($model) => $instance, Model::class => $instance]; } /** * Checks one extension's values against its rules: the errors keyed by field, and the * validated values when there are none. * * @param class-string $fields * @param Model|class-string $model * @param array $submitted * @return array{array>, ExtensionFieldValues} */ private function check(string $extension, string $fields, Model|string $model, array $submitted): array { $rules = $this->rules($fields, $model); $values = $this->withStoredSecrets($extension, $fields, $model, $submitted); $own = $this->validation->make($values, $rules, $this->strings($fields, 'messages', $model), $this->strings($fields, 'attributes', $model)); $own->addRules(array_fill_keys($this->fieldNames($rules), [new ExtensionFieldValue])); return $own->fails() ? [$own->errors()->getMessages(), []] : [[], ExtensionSettingValueGuard::fieldValues($own->validated())]; } /** * The fields rules name, without the nested keys of list fields (`tags.*`). * * @param ValidationRules $rules * @return list */ private function fieldNames(array $rules): array { return array_values(array_unique(array_map(fn (string $rule): string => explode('.', $rule, 2)[0], array_keys($rules)))); } /** * @param Model|class-string $model * @return class-string */ private function modelClass(Model|string $model): string { return $model instanceof Model ? $model::class : $model; } /** * The current values of one extension's fields, unmasked. * * @param class-string $fields * @return ExtensionFieldValues */ private function current(string $extension, string $fields, Model $model): array { return method_exists($fields, 'values') ? ExtensionSettingValueGuard::fieldValues($this->container->call($fields.'@values', $this->modelParameters($model))) : $this->stored($extension, $fields, $model); } /** * Values the panel stores for an extension that has no values() and save(): one per * field named in its rules, null until set. * * @param class-string $fields * @return ExtensionFieldValues */ private function stored(string $extension, string $fields, Model $model): array { $settings = $this->extensions->settings($extension)->fields($model); $values = []; foreach ($this->fieldNames($this->rules($fields, $model)) as $field) { $values[$field] = ExtensionSettingValueGuard::fieldValue($settings->get($field)); } return $values; } /** * The submitted values with each secret field sent as its mask or not at all replaced * by its stored value, so the rules check the real value and saving keeps it. With * nothing stored, or when creating, such a field is left out. A secret sent as null or * empty is kept as sent, so saving clears it. * * @param class-string $fields * @param Model|class-string $model * @param array $submitted * @return array */ private function withStoredSecrets(string $extension, string $fields, Model|string $model, array $submitted): array { $secrets = $this->secrets($fields, $model); if ($secrets === []) { return $submitted; } $stored = null; foreach ($secrets as $field) { if (array_key_exists($field, $submitted) && $submitted[$field] !== ExtensionSettingDefinition::MASK) { continue; } $stored ??= $model instanceof Model ? $this->current($extension, $fields, $model) : []; if (in_array($stored[$field] ?? null, [null, ''], true)) { unset($submitted[$field]); } else { $submitted[$field] = $stored[$field]; } } return $submitted; } /** * @param ExtensionFieldValues $values * @param list $secrets * @return ExtensionFieldValues */ private function masked(array $values, array $secrets): array { foreach ($secrets as $field) { if (array_key_exists($field, $values)) { $values[$field] = in_array($values[$field], [null, ''], true) ? null : ExtensionSettingDefinition::MASK; } } return $values; } /** * @param class-string $fields * @param Model|class-string $model */ private function authorized(string $fields, Model|string $model): bool { return ! method_exists($fields, 'authorize') || $this->container->call($fields.'@authorize', $this->modelParameters($model)) === true; } /** * @param class-string $fields * @param Model|class-string $model * @return ValidationRules */ private function rules(string $fields, Model|string $model): array { return method_exists($fields, 'rules') ? ExtensionSettingValueGuard::validationRules($this->container->call($fields.'@rules', $this->modelParameters($model))) : []; } /** * The fields secrets() names. * * @param class-string $fields * @param Model|class-string $model * @return list */ private function secrets(string $fields, Model|string $model): array { if (! method_exists($fields, 'secrets')) { return []; } $secrets = $this->container->call($fields.'@secrets', $this->modelParameters($model)); throw_unless(is_array($secrets), UnexpectedValueException::class, 'Extension field secrets must be a list of field names.'); $names = []; foreach ($secrets as $name) { throw_unless(is_string($name), UnexpectedValueException::class, 'Extension field secrets must be a list of field names.'); $names[] = $name; } return $names; } /** * The result of attributes() or messages(). * * @param class-string $fields * @param 'attributes'|'messages' $method * @param Model|class-string $model * @return array */ private function strings(string $fields, string $method, Model|string $model): array { return method_exists($fields, $method) ? ExtensionSettingValueGuard::stringMap($this->container->call($fields.'@'.$method, $this->modelParameters($model))) : []; } }