Loading nodeaccess.module +104 −96 Original line number Diff line number Diff line Loading @@ -7,7 +7,7 @@ use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Session\AccountInterface; use Drupal\user\RoleInterface; use Drupal\user\Entity\Role; use Drupal\Core\Entity\EntityInterface; use Drupal\node\NodeTypeInterface; use Drupal\node\NodeInterface; Loading Loading @@ -102,124 +102,132 @@ function nodeaccess_node_grants(AccountInterface $account, $op) { } /** * Implements hook_entity_update(). * Implements hook_ENTITY_TYPE_insert(). */ function nodeaccess_entity_update(EntityInterface $entity) { $config = \Drupal::configFactory()->getEditable('nodeaccess.settings'); if ($entity instanceof RoleInterface) { $role_alias = $config->get('role_alias'); if ($role_alias[$entity->id()]) { $role_alias[$entity->id()]['alias'] = $entity->label(); $role_alias[$entity->id()]['name'] = $entity->label(); } else { $role_alias[$entity->id()] = [ 'alias' => $entity->label(), 'name' => $entity->label(), 'weight' => 0, 'allow' => 0, function nodeaccess_node_type_insert(NodeTypeInterface $node_type) { // Update `grants_tab_availability` and `bundles_roles_grants` in // `nodeacess.settings` to add this bundle related settings. $bundle = $node_type->id(); $nodeaccess_settings = \Drupal::configFactory()->getEditable('nodeaccess.settings'); $bundle_roles_grants = []; foreach (Role::loadMultiple() as $role_id => $role) { $bundle_roles_grants[$role_id] = [ 'grant_view' => 0, 'grant_update' => 0, 'grant_delete' => 0, ]; } $config->set('role_alias', $role_alias); $config->save(); } } /** * Implements hook_entity_insert(). */ function nodeaccess_entity_insert(EntityInterface $entity) { $config = \Drupal::configFactory()->getEditable('nodeaccess.settings'); if ($entity instanceof RoleInterface) { $roles_gids = array_flip($config->get('role_map')); $roles_gids[] = $entity->id(); $config->set('role_map', array_flip($roles_gids)); $role_alias = $config->get('role_alias'); $role_alias[$entity->id()] = [ 'alias' => $entity->label(), 'name' => $entity->label(), 'weight' => 0, 'allow' => 0, $bundle_roles_grants['author'] = [ 'grant_view' => 0, 'grant_update' => 0, 'grant_delete' => 0, ]; $config->set('role_alias', $role_alias); $config->save(); } $grants_tab_availability = $nodeaccess_settings->get('grants_tab_availability'); $grants_tab_availability[$bundle] = FALSE; $bundles_roles_grants = $nodeaccess_settings->get('bundles_roles_grants'); $bundles_roles_grants[$bundle] = $bundle_roles_grants; $nodeaccess_settings ->set('grants_tab_availability', $grants_tab_availability) ->set('bundles_roles_grants', $bundles_roles_grants) ->save(); // @todo display a message or something to promote end users to update // `nodeaccess.settings`. node_access_needs_rebuild(TRUE); } /** * Implements hook_entity_delete(). * Implements hook_ENTITY_TYPE_delete(). */ function nodeaccess_entity_delete(EntityInterface $entity) { $config = \Drupal::configFactory()->getEditable('nodeaccess.settings'); if ($entity instanceof RoleInterface) { $roles_gids = $config->get('role_map'); unset($roles_gids[$entity->id()]); $config->set('role_map', $roles_gids); $role_alias = $config->get('role_alias'); unset($role_alias[$entity->id()]); $config->set('role_alias', $role_alias); $config->save(); } function nodeaccess_node_type_delete(NodeTypeInterface $node_type) { // Update `grants_tab_availability` and `bundles_roles_grants` in // `nodeacess.settings` to remove this bundle related settings. $bundle = $node_type->id(); $nodeaccess_settings = \Drupal::configFactory()->getEditable('nodeaccess.settings'); $grants_tab_availability = $nodeaccess_settings->get('grants_tab_availability'); $bundles_roles_grants = $nodeaccess_settings->get('bundles_roles_grants'); unset($grants_tab_availability[$bundle]); unset($bundles_roles_grants[$bundle]); $nodeaccess_settings ->set('grants_tab_availability', $grants_tab_availability) ->set('bundles_roles_grants', $bundles_roles_grants) ->save(); } /** * Implements hook_node_delete(). * Implements hook_ENTITY_TYPE_delete(). */ function nodeaccess_node_delete(EntityInterface $node) { // Deleting node, delete related permissions. // Remove related records from the `nodeaccess` table . \Drupal::database()->delete('nodeaccess') ->condition('nid', $node->id()) ->execute(); } /** * Implements hook_ENTITY_TYPE_delete(). * Implements hook_ENTITY_TYPE_insert(). */ function nodeaccess_node_type_delete(NodeTypeInterface $type) { $config = \Drupal::configFactory()->getEditable('nodeaccess.settings'); $allowed_types = $config->get('allowed_types'); unset($allowed_types[$type->id()]); $config->clear($type->id()) ->set('allowed_types', $allowed_types) function nodeaccess_user_role_insert(EntityInterface $entity) { $nodeaccess_settings = \Drupal::configFactory()->getEditable('nodeaccess.settings'); // Swap the role ID and grant ID for adding new item. $map_gid_rid = array_flip($nodeaccess_settings->get('map_rid_gid')); $role_id = $entity->id(); $role_name = $entity->label(); // Add the new one. Grant ID added implicitly. $map_gid_rid[] = $role_id; $roles_settings = $nodeaccess_settings->get('roles_settings'); $roles_settings[$role_id] = [ 'display_name' => $role_name, 'name' => $role_name, 'weight' => 0, 'selected' => FALSE, ]; $nodeaccess_settings ->set('map_rid_gid', array_flip($map_gid_rid)) ->set('roles_settings', $roles_settings) ->save(); } /** * Implements hook_ENTITY_TYPE_insert(). * Implements hook_ENTITY_TYPE_update(). */ function nodeaccess_node_type_insert(NodeTypeInterface $type) { $config = \Drupal::configFactory()->getEditable('nodeaccess.settings'); // New node type, default to whatever is set for access content permission. $role_perms = user_role_permissions([ AccountInterface::ANONYMOUS_ROLE, AccountInterface::AUTHENTICATED_ROLE, ]); $anonymous_access = (int) in_array('access content', $role_perms[AccountInterface::ANONYMOUS_ROLE]); $authenticated_access = (int) in_array('access content', $role_perms[AccountInterface::AUTHENTICATED_ROLE]); $grants[AccountInterface::ANONYMOUS_ROLE] = [ 'grant_view' => $anonymous_access, 'grant_update' => 0, 'grant_delete' => 0, ]; $grants[AccountInterface::AUTHENTICATED_ROLE] = [ 'grant_view' => $authenticated_access, 'grant_update' => 0, 'grant_delete' => 0, ]; $grants['author'] = [ 'grant_view' => 0, 'grant_update' => 0, 'grant_delete' => 0, function nodeaccess_user_role_update(EntityInterface $entity) { $nodeaccess_settings = \Drupal::configFactory()->getEditable('nodeaccess.settings'); $roles_settings = $nodeaccess_settings->get('roles_settings'); $role_id = $entity->id(); $role_name = $entity->label(); // Usually, update `display_name` and `name`. if (isset($roles_settings[$role_id])) { $roles_settings[$role_id]['display_name'] = $role_name; $roles_settings[$role_id]['name'] = $role_name; } else { // This is a rare case or a case never be reached. $roles_settings[$role_id] = [ 'display_name' => $role_name, 'name' => $role_name, 'weight' => 0, 'selected' => FALSE, ]; $allowed_types = $config->get('allowed_types'); $allowed_types[$type->id()] = 0; $config->set($type->id(), $grants) ->set('allowed_types', $allowed_types) } $nodeaccess_settings ->set('roles_settings', $roles_settings) ->save(); } /** * Implements hook_ENTITY_TYPE_delete(). */ function nodeaccess_user_role_delete(EntityInterface $entity) { $nodeaccess_settings = \Drupal::configFactory()->getEditable('nodeaccess.settings'); $role_id = $entity->id(); $map_rid_gid = $nodeaccess_settings->get('map_rid_gid'); $roles_settings = $nodeaccess_settings->get('roles_settings'); unset($map_rid_gid[$role_id]); unset($roles_settings[$role_id]); $nodeaccess_settings ->set('map_rid_gid', $map_rid_gid) ->set('roles_settings', $roles_settings) ->save(); node_access_needs_rebuild(TRUE); } Loading
nodeaccess.module +104 −96 Original line number Diff line number Diff line Loading @@ -7,7 +7,7 @@ use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Session\AccountInterface; use Drupal\user\RoleInterface; use Drupal\user\Entity\Role; use Drupal\Core\Entity\EntityInterface; use Drupal\node\NodeTypeInterface; use Drupal\node\NodeInterface; Loading Loading @@ -102,124 +102,132 @@ function nodeaccess_node_grants(AccountInterface $account, $op) { } /** * Implements hook_entity_update(). * Implements hook_ENTITY_TYPE_insert(). */ function nodeaccess_entity_update(EntityInterface $entity) { $config = \Drupal::configFactory()->getEditable('nodeaccess.settings'); if ($entity instanceof RoleInterface) { $role_alias = $config->get('role_alias'); if ($role_alias[$entity->id()]) { $role_alias[$entity->id()]['alias'] = $entity->label(); $role_alias[$entity->id()]['name'] = $entity->label(); } else { $role_alias[$entity->id()] = [ 'alias' => $entity->label(), 'name' => $entity->label(), 'weight' => 0, 'allow' => 0, function nodeaccess_node_type_insert(NodeTypeInterface $node_type) { // Update `grants_tab_availability` and `bundles_roles_grants` in // `nodeacess.settings` to add this bundle related settings. $bundle = $node_type->id(); $nodeaccess_settings = \Drupal::configFactory()->getEditable('nodeaccess.settings'); $bundle_roles_grants = []; foreach (Role::loadMultiple() as $role_id => $role) { $bundle_roles_grants[$role_id] = [ 'grant_view' => 0, 'grant_update' => 0, 'grant_delete' => 0, ]; } $config->set('role_alias', $role_alias); $config->save(); } } /** * Implements hook_entity_insert(). */ function nodeaccess_entity_insert(EntityInterface $entity) { $config = \Drupal::configFactory()->getEditable('nodeaccess.settings'); if ($entity instanceof RoleInterface) { $roles_gids = array_flip($config->get('role_map')); $roles_gids[] = $entity->id(); $config->set('role_map', array_flip($roles_gids)); $role_alias = $config->get('role_alias'); $role_alias[$entity->id()] = [ 'alias' => $entity->label(), 'name' => $entity->label(), 'weight' => 0, 'allow' => 0, $bundle_roles_grants['author'] = [ 'grant_view' => 0, 'grant_update' => 0, 'grant_delete' => 0, ]; $config->set('role_alias', $role_alias); $config->save(); } $grants_tab_availability = $nodeaccess_settings->get('grants_tab_availability'); $grants_tab_availability[$bundle] = FALSE; $bundles_roles_grants = $nodeaccess_settings->get('bundles_roles_grants'); $bundles_roles_grants[$bundle] = $bundle_roles_grants; $nodeaccess_settings ->set('grants_tab_availability', $grants_tab_availability) ->set('bundles_roles_grants', $bundles_roles_grants) ->save(); // @todo display a message or something to promote end users to update // `nodeaccess.settings`. node_access_needs_rebuild(TRUE); } /** * Implements hook_entity_delete(). * Implements hook_ENTITY_TYPE_delete(). */ function nodeaccess_entity_delete(EntityInterface $entity) { $config = \Drupal::configFactory()->getEditable('nodeaccess.settings'); if ($entity instanceof RoleInterface) { $roles_gids = $config->get('role_map'); unset($roles_gids[$entity->id()]); $config->set('role_map', $roles_gids); $role_alias = $config->get('role_alias'); unset($role_alias[$entity->id()]); $config->set('role_alias', $role_alias); $config->save(); } function nodeaccess_node_type_delete(NodeTypeInterface $node_type) { // Update `grants_tab_availability` and `bundles_roles_grants` in // `nodeacess.settings` to remove this bundle related settings. $bundle = $node_type->id(); $nodeaccess_settings = \Drupal::configFactory()->getEditable('nodeaccess.settings'); $grants_tab_availability = $nodeaccess_settings->get('grants_tab_availability'); $bundles_roles_grants = $nodeaccess_settings->get('bundles_roles_grants'); unset($grants_tab_availability[$bundle]); unset($bundles_roles_grants[$bundle]); $nodeaccess_settings ->set('grants_tab_availability', $grants_tab_availability) ->set('bundles_roles_grants', $bundles_roles_grants) ->save(); } /** * Implements hook_node_delete(). * Implements hook_ENTITY_TYPE_delete(). */ function nodeaccess_node_delete(EntityInterface $node) { // Deleting node, delete related permissions. // Remove related records from the `nodeaccess` table . \Drupal::database()->delete('nodeaccess') ->condition('nid', $node->id()) ->execute(); } /** * Implements hook_ENTITY_TYPE_delete(). * Implements hook_ENTITY_TYPE_insert(). */ function nodeaccess_node_type_delete(NodeTypeInterface $type) { $config = \Drupal::configFactory()->getEditable('nodeaccess.settings'); $allowed_types = $config->get('allowed_types'); unset($allowed_types[$type->id()]); $config->clear($type->id()) ->set('allowed_types', $allowed_types) function nodeaccess_user_role_insert(EntityInterface $entity) { $nodeaccess_settings = \Drupal::configFactory()->getEditable('nodeaccess.settings'); // Swap the role ID and grant ID for adding new item. $map_gid_rid = array_flip($nodeaccess_settings->get('map_rid_gid')); $role_id = $entity->id(); $role_name = $entity->label(); // Add the new one. Grant ID added implicitly. $map_gid_rid[] = $role_id; $roles_settings = $nodeaccess_settings->get('roles_settings'); $roles_settings[$role_id] = [ 'display_name' => $role_name, 'name' => $role_name, 'weight' => 0, 'selected' => FALSE, ]; $nodeaccess_settings ->set('map_rid_gid', array_flip($map_gid_rid)) ->set('roles_settings', $roles_settings) ->save(); } /** * Implements hook_ENTITY_TYPE_insert(). * Implements hook_ENTITY_TYPE_update(). */ function nodeaccess_node_type_insert(NodeTypeInterface $type) { $config = \Drupal::configFactory()->getEditable('nodeaccess.settings'); // New node type, default to whatever is set for access content permission. $role_perms = user_role_permissions([ AccountInterface::ANONYMOUS_ROLE, AccountInterface::AUTHENTICATED_ROLE, ]); $anonymous_access = (int) in_array('access content', $role_perms[AccountInterface::ANONYMOUS_ROLE]); $authenticated_access = (int) in_array('access content', $role_perms[AccountInterface::AUTHENTICATED_ROLE]); $grants[AccountInterface::ANONYMOUS_ROLE] = [ 'grant_view' => $anonymous_access, 'grant_update' => 0, 'grant_delete' => 0, ]; $grants[AccountInterface::AUTHENTICATED_ROLE] = [ 'grant_view' => $authenticated_access, 'grant_update' => 0, 'grant_delete' => 0, ]; $grants['author'] = [ 'grant_view' => 0, 'grant_update' => 0, 'grant_delete' => 0, function nodeaccess_user_role_update(EntityInterface $entity) { $nodeaccess_settings = \Drupal::configFactory()->getEditable('nodeaccess.settings'); $roles_settings = $nodeaccess_settings->get('roles_settings'); $role_id = $entity->id(); $role_name = $entity->label(); // Usually, update `display_name` and `name`. if (isset($roles_settings[$role_id])) { $roles_settings[$role_id]['display_name'] = $role_name; $roles_settings[$role_id]['name'] = $role_name; } else { // This is a rare case or a case never be reached. $roles_settings[$role_id] = [ 'display_name' => $role_name, 'name' => $role_name, 'weight' => 0, 'selected' => FALSE, ]; $allowed_types = $config->get('allowed_types'); $allowed_types[$type->id()] = 0; $config->set($type->id(), $grants) ->set('allowed_types', $allowed_types) } $nodeaccess_settings ->set('roles_settings', $roles_settings) ->save(); } /** * Implements hook_ENTITY_TYPE_delete(). */ function nodeaccess_user_role_delete(EntityInterface $entity) { $nodeaccess_settings = \Drupal::configFactory()->getEditable('nodeaccess.settings'); $role_id = $entity->id(); $map_rid_gid = $nodeaccess_settings->get('map_rid_gid'); $roles_settings = $nodeaccess_settings->get('roles_settings'); unset($map_rid_gid[$role_id]); unset($roles_settings[$role_id]); $nodeaccess_settings ->set('map_rid_gid', $map_rid_gid) ->set('roles_settings', $roles_settings) ->save(); node_access_needs_rebuild(TRUE); }