Check available paths for a user

Public

This example code checks all menu paths and logs if the set user has access to that path or not.

Get raw version
php
  1. // Login as the user that we want to check.
  2. global $user;
  3. $original_user = $user;
  4. $old_state = drupal_save_session();
  5. drupal_save_session(FALSE);
  6. $user = user_load(6);
  7.  
  8. // Check access from menu links table.
  9. $query = db_select('menu_links', 'ml');
  10. $query->fields('ml');
  11. $links = $query->execute()->fetchAll(PDO::FETCH_ASSOC);
  12. foreach ($links as $link) {
  13. $menu_item = menu_get_item($link['link_path']);
  14. if ($menu_item['access']) {
  15. drupal_set_message(t('%username has access to %path', array('%username' => $user->name, '%path' => $link['link_path'])));
  16. }
  17. }
  18.  
  19. // Login with the original user.
  20. $user = $original_user;
  21. drupal_save_session($old_state);