Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion Config/ObjectTypeDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ public function getDefinition()
->variableNode('isTypeOf')->end()
->variableNode('resolveField')->end()
->variableNode('fieldsDefaultAccess')
->info('Default access control to fields (expression language can be use here)')
->info('Default access control to fields (expression language can be use here)')
->end()
->variableNode('fieldsDefaultPublic')
->info('Default public control to fields (expression language can be use here)')
->end()
->end();

$this->treatFieldsDefaultAccess($node);
$this->treatFieldsDefaultPublic($node);
$this->treatResolveField($node);

return $node;
Expand Down Expand Up @@ -67,6 +71,31 @@ private function treatFieldsDefaultAccess(ArrayNodeDefinition $node)
->end();
}

/**
* set empty fields.public with fieldsDefaultPublic values if is set?
*
* @param ArrayNodeDefinition $node
*/
private function treatFieldsDefaultPublic(ArrayNodeDefinition $node)
{
$node->validate()
->ifTrue(function ($v) {
return array_key_exists('fieldsDefaultPublic', $v) && null !== $v['fieldsDefaultPublic'];
})
->then(function ($v) {
foreach ($v['fields'] as &$field) {
if (array_key_exists('public', $field) && null !== $field['public']) {
continue;
}

$field['public'] = $v['fieldsDefaultPublic'];
}

return $v;
})
->end();
}

/**
* resolveField is set as fields default resolver if not set
* then remove resolveField to keep "access" feature
Expand Down
6 changes: 3 additions & 3 deletions Generator/TypeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,16 @@ protected function generatePublic(array $value)
return 'null';
}

$publicCallback = $this->callableCallbackFromArrayValue($value, 'public');
$publicCallback = $this->callableCallbackFromArrayValue($value, 'public', '$typeName, $fieldName');

if ('null' === $publicCallback) {
return $publicCallback;
}

$code = <<<'CODE'
function () <closureUseStatements> {
function ($fieldName) <closureUseStatements> {
<spaces><spaces>$publicCallback = %s;
<spaces><spaces>return call_user_func($publicCallback);
<spaces><spaces>return call_user_func($publicCallback, $this->name, $fieldName);
<spaces>}
CODE;

Expand Down
18 changes: 18 additions & 0 deletions Resources/doc/security/fields-public-control.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,21 @@ AnObject:
public: "@=service('security.authorization_checker').isGranted('ROLE_ADMIN')"

```

You can also use `config.fieldsDefaultPublic` to handle the setting globally on an object :

```yaml
AnObject:
type: object
fieldsDefaultPublic: "@=service('my_service').isGranted(typeName, fieldName)"
config:
fields:
id:
type: "String!"
privateData:
type: "String"
```

Have you noticed `typeName` and `fieldName` here ? This variables are always set to the current
type name and current field name, meaning you can apply a per field `public` setting on all the
fields with one line of yaml.
2 changes: 1 addition & 1 deletion Resources/skeleton/TypeSystem.php.skeleton
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<spaces><spaces>foreach ($fields as $fieldName => $field) {
<spaces><spaces><spaces>$isPublic = isset($field['public']) ? $field['public'] : true;
<spaces><spaces><spaces>if (is_callable($isPublic)) {
<spaces><spaces><spaces><spaces>$isPublic = call_user_func($isPublic);
<spaces><spaces><spaces><spaces>$isPublic = call_user_func($isPublic, $fieldName);
<spaces><spaces><spaces>}
<spaces><spaces><spaces>if ($isPublic) {
<spaces><spaces><spaces><spaces>$filtered[$fieldName] = $field;
Expand Down