Is there a way to retrieve all users with a specific permission (eg "Post comments"). I only found the possibility to get them by their role:

->condition('status', 1)
->condition('roles', 'administrator')


->loadByProperties(['roles' => 'administrator']);

I had to get the roles that had a given permission to get users with a permission, example:

$roles = user_role_names(FALSE, 'example_permission');
$rids = array_keys($roles);

$uids = Drupal::service('entity_type.manager')->getStorage('user')->getQuery()
  ->condition('roles', $rids, 'IN')

Apparently thats what Drupal core does in the permission filter for views, see: \Drupal\user\Plugin\views\filter\Permissions

Hope that helps some!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.