adva-8.x-1.0-rc9/adva.install
adva.install
<?php
/**
* @file
* Install, schema, and requirement functions for adva.
*/
/**
* Implements hook_requirements().
*
* Set requirements for status page.
*/
function adva_requirements($phase) {
$requirements = [];
if ($phase === 'runtime') {
// Get services.
$translation = \Drupal::translation();
$urlGenerator = \Drupal::service("url_generator");
$consumerManager = \Drupal::service("plugin.manager.adva.consumer");
$entityTypeManager = \Drupal::service("entity_type.manager");
$accessStorage = \Drupal::service('adva.access_storage');
$overridingConsumers = $consumerManager->getOverrideConsumers();
$context_base = [
':configure' => $urlGenerator->generateFromRoute('adva.settings'),
];
foreach ($overridingConsumers as $consumer) {
$entityTypeId = $consumer->getEntityTypeId();
$entityType = $entityTypeManager->getDefinition($entityTypeId);
$status = t("Disabled.");
$routeParam = ["consumer" => $consumer->getPluginId()];
$context = array_merge($context_base, [
'%entityType' => $entityType->getLabel(),
':rebuild' => $urlGenerator->generateFromRoute('adva.access_rebuild', $routeParam),
]);
$providers = $consumer->getAccessProviders();
if (!empty($providers)) {
// If we have provides, display the count of active rules in the db.
$grant_count = $accessStorage->count($entityTypeId);
$status = $translation->formatPlural($grant_count, 'One permission in use.', '@count permissions in use.', ['@count' => $grant_count]);
}
else {
$status = t('Disabled. No Access Providers configured. <a href=":configure">Configure Providers</a>', $context);
}
$requirements['adva_access_' . $entityTypeId] = [
'title' => t('Advanced Access %entityType Permissions', $context),
'value' => $status,
'description' => t('If the site is experiencing problems with permissions for %entityType entities, you may have to rebuild the permissions cache. Rebuilding will remove all privileges to content and replace them with permissions based on the current modules and settings. Rebuilding may take some time if there is a lot of content or complex permission settings. After rebuilding has completed, content will automatically use the new permissions. <a href=":rebuild">Rebuild permissions</a>', $context),
];
if ($consumer->rebuildRequired()) {
$requirements['adva_access_' . $entityTypeId]['value'] .= ' ' . t('Rebuild Required');
$requirements['adva_access_' . $entityTypeId]['severity'] = REQUIREMENT_WARNING;
}
}
}
return $requirements;
}
/**
* Implements hook_schema().
*/
function adva_schema() {
$schema['adva_access'] = [
'description' => 'Identifies which realm/grant pairs a user must possess in order to view, update, or delete specific nodes.',
'fields' => [
'entity_type' => [
'description' => 'The entity_type for the entity_id this record affects.',
'type' => 'varchar_ascii',
'length' => 255,
'not null' => TRUE,
'default' => '',
],
'entity_id' => [
'description' => 'The entity_id this record affects.',
'type' => 'varchar_ascii',
'length' => 255,
'not null' => TRUE,
'default' => '',
],
'langcode' => [
'description' => 'The {language}.langcode of this entity.',
'type' => 'varchar_ascii',
'length' => 12,
'not null' => TRUE,
'default' => '',
],
'fallback' => [
'description' => 'Boolean indicating whether this record should be used as a fallback if a language condition is not provided.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 1,
],
'gid' => [
'description' => "The grant ID a user must possess in the specified realm to gain this row's privileges.",
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'realm' => [
'description' => 'The realm in which the user must possess the grant ID. Each AccessProvider can define one or more realms.',
'type' => 'varchar_ascii',
'length' => 255,
'not null' => TRUE,
'default' => '',
],
'grant_view' => [
'description' => 'Boolean indicating whether a user with the realm/grant pair can view this entity.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'size' => 'tiny',
],
'grant_update' => [
'description' => 'Boolean indicating whether a user with the realm/grant pair can edit this entity.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'size' => 'tiny',
],
'grant_delete' => [
'description' => 'Boolean indicating whether a user with the realm/grant pair can delete this entity.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'size' => 'tiny',
],
],
'primary key' => ['entity_id', 'entity_type', 'gid', 'realm', 'langcode'],
'indexes' => [
'entity' => ['entity_id', 'entity_type'],
],
];
return $schema;
}
/**
* Deletes old rebuild state values.
*/
function adva_update_8101() {
/* @var \Drupal\adva\Plugin\adva\Manager\AccessConsumerManagerInterface $consumerManager */
$consumerManager = \Drupal::service('plugin.manager.adva.consumer');
/* @var \Drupal\Core\State\StateInterface $state */
$state_ids = [];
foreach ($consumerManager->getConsumers() as $consumer) {
$state_ids[] = 'adva_access.needs_rebuild.' . $consumer->getEntityTypeId();
}
\Drupal::service('state')->deleteMultiple($state_ids);
}
