-
-
Notifications
You must be signed in to change notification settings - Fork 555
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add bundle permissions for content entity (#4138)
* Add command option has-bundle-permissions and use on entity. * Add (Entity}Permissions.php file. * Add permission_callback to permissions.yml file. * Added fix from #4139 as it hurts. * Add own permissions checks.
- Loading branch information
1 parent
e03be58
commit 3c83f02
Showing
6 changed files
with
207 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
templates/module/src/entity-content-bundle-permissions.php.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
|
||
{% extends "base/class.php.twig" %} | ||
|
||
{% block file_path %} | ||
\Drupal\{{ module }}\Entity\{{ entity_class }}. | ||
{% endblock %} | ||
|
||
{% block namespace_class %} | ||
namespace Drupal\{{ module }}; | ||
{% endblock %} | ||
|
||
{% block use_class %} | ||
use Drupal\Core\StringTranslation\StringTranslationTrait; | ||
use Drupal\{{ module }}\Entity\{{ entity_class }}Type; | ||
|
||
{% endblock %} | ||
|
||
{% block class_declaration %} | ||
/** | ||
* Provides dynamic permissions for {{ label }} of different types. | ||
* | ||
* @ingroup {{ module }} | ||
* | ||
*/ | ||
class {{ entity_class }}Permissions{% endblock %} | ||
|
||
{% block class_methods %} | ||
use StringTranslationTrait; | ||
|
||
/** | ||
* Returns an array of node type permissions. | ||
* | ||
* @return array | ||
* The {{ entity_class }} by bundle permissions. | ||
* @see \Drupal\user\PermissionHandlerInterface::getPermissions() | ||
*/ | ||
public function generatePermissions() { | ||
$perms = []; | ||
|
||
foreach ({{ entity_class }}Type::loadMultiple() as $type) { | ||
$perms += $this->buildPermissions($type); | ||
} | ||
|
||
return $perms; | ||
} | ||
|
||
/** | ||
* Returns a list of node permissions for a given node type. | ||
* | ||
* @param \Drupal\{{ module }}\Entity\{{ entity_class }}Type $type | ||
* The {{ entity_class }} type. | ||
* | ||
* @return array | ||
* An associative array of permission names and descriptions. | ||
*/ | ||
protected function buildPermissions({{ entity_class}}Type $type) { | ||
$type_id = $type->id(); | ||
$type_params = ['%type_name' => $type->label()]; | ||
|
||
return [ | ||
"$type_id create entities" => [ | ||
'title' => $this->t('Create new %type_name entities', $type_params), | ||
], | ||
"$type_id edit own entities" => [ | ||
'title' => $this->t('Edit own %type_name entities', $type_params), | ||
], | ||
"$type_id edit any entities" => [ | ||
'title' => $this->t('Edit any %type_name entities', $type_params), | ||
], | ||
"$type_id delete own entities" => [ | ||
'title' => $this->t('Delete own %type_name entities', $type_params), | ||
], | ||
"$type_id delete any entities" => [ | ||
'title' => $this->t('Delete any %type_name entities', $type_params), | ||
], | ||
{% if revisionable %} | ||
"$type_id view revisions" => [ | ||
'title' => $this->t('View %type_name revisions', $type_params), | ||
'description' => t('To view a revision, you also need permission to view the entity item.'), | ||
], | ||
"$type_id revert revisions" => [ | ||
'title' => $this->t('Revert %type_name revisions', $type_params), | ||
'description' => t('To revert a revision, you also need permission to edit the entity item.'), | ||
], | ||
"$type_id delete revisions" => [ | ||
'title' => $this->t('Delete %type_name revisions', $type_params), | ||
'description' => $this->t('To delete a revision, you also need permission to delete the entity item.'), | ||
], | ||
{% endif %} | ||
]; | ||
} | ||
{% endblock %} |