You are here

function nodeaccess_install in Nodeaccess 8

Same name and namespace in other branches
  1. 8.2 nodeaccess.install \nodeaccess_install()
  2. 5 nodeaccess.install \nodeaccess_install()
  3. 6.2 nodeaccess.install \nodeaccess_install()
  4. 6 nodeaccess.install \nodeaccess_install()
  5. 7 nodeaccess.install \nodeaccess_install()

Implements hook_install().

File

./nodeaccess.install, line 13
Install/uninstall functions for Nodeaccess.

Code

function nodeaccess_install() {

  // Set up default permissions to be view for authenticated and
  // anonymous users, and all permissions for author.
  $grants = [];
  $role_perms = user_role_permissions([
    'anonymous',
    'authenticated',
  ]);
  $grants['anonymous']['grant_view'] = (int) in_array('access content', $role_perms['anonymous']);
  $grants['authenticated']['grant_view'] = (int) in_array('access content', $role_perms['authenticated']);
  $role_alias = [];
  $settings = \Drupal::configFactory()
    ->getEditable('nodeaccess.settings');
  foreach (NodeType::loadMultiple() as $type => $bundle) {

    // We check the edit permissions for anonymous and authenticated users.
    $edit_perm = 'edit any ' . $type . ' content';
    $grants['anonymous']['grant_update'] = (int) in_array($edit_perm, $role_perms['anonymous']);
    $grants['authenticated']['grant_update'] = (int) in_array($edit_perm, $role_perms['authenticated']);

    // We check the delete permissions for anonymous and authenticated users.
    $delete_perm = 'delete any ' . $type . ' content';
    $grants['anonymous']['grant_delete'] = (int) in_array($delete_perm, $role_perms['anonymous']);
    $grants['authenticated']['grant_delete'] = (int) in_array($delete_perm, $role_perms['authenticated']);

    // Author Permissions.
    $grants['author'] = [
      'grant_view' => 0,
      'grant_update' => 0,
      'grant_delete' => 0,
    ];
    $settings
      ->set($type, $grants);
  }
  $i = 1;
  foreach (user_roles() as $id => $role) {
    $role_alias[$id] = [
      'alias' => $role
        ->label(),
      'name' => $role
        ->label(),
      'weight' => 0,
      'allow' => 0,
    ];
    $role_map[$id] = $i;
    $i++;
  }
  $settings
    ->set('role_alias', $role_alias);
  $settings
    ->set('role_map', $role_map);
  $settings
    ->save();
}