Skip to content

Instantly share code, notes, and snippets.

@mortenson
Created October 11, 2017 17:06
Show Gist options
  • Save mortenson/b21d24c6799cf065363d93b7562e986b to your computer and use it in GitHub Desktop.
Save mortenson/b21d24c6799cf065363d93b7562e986b to your computer and use it in GitHub Desktop.
See what operations are available for all entities on a Drupal site for common Users
<?php
// Run this with drush, like "drush scr access.php"
use Drupal\user\Entity\User;
// You should delete this user after you're done testing.
if (!($auth = user_load_by_name('tmp_access_script'))) {
$auth = User::create(['name' => 'tmp_access_script']);
$auth->save();
}
$users = [
'anon' => User::getAnonymousUser(),
'admin' => User::load(1),
'auth' => $auth,
];
$etm = \Drupal::entityTypeManager();
foreach ($etm->getDefinitions() as $type) {
foreach ($etm->getStorage($type->id())->loadMultiple() as $entity) {
foreach (['create', 'view', 'update', 'delete'] as $operation) {
try {
foreach ($users as $user_type => $user) {
if ($entity->access($operation, $user)) {
echo "{$user_type} {$operation} {$type->id()} {$entity->id()}\n";
}
}
}
catch (\Exception $e) {}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment