First change is to menu.module. It restricts viewing so that if the menu_per_role is installed, then the user must have access to view this menu. This is in the menu_overview_tree() function. Note, just adding the if statement around the block.

function menu_overview_tree() {
  $menu = menu_get_menu();
  $root_menus = menu_get_root_menus();
  $header = array(t('Menu item'), t('Expanded'), array('data' => t('Operations'), 'colspan' => '3'));
  $output = '';

  // We reverse the root menu to show user created menus first.
  foreach (array_reverse($root_menus, true) as $mid => $title) {
  //Modification here
	if (!module_exists('menu_per_role') || menu_per_role_access($mid)) {
		
    $operations = array();
    if ($menu['items'][$mid]['type'] & MENU_MODIFIABLE_BY_ADMIN) {
      $operations[] = l(t('Edit'), 'admin/build/menu/menu/edit/'. $mid);
    }
    if ($menu['items'][$mid]['type'] & MENU_CREATED_BY_ADMIN) {
      $operations[] = l(t('Delete'), 'admin/build/menu/menu/delete/'. $mid);
    }
    $operations[] = l(t('Add item'), 'admin/build/menu/item/add/'. $mid);
    $table = theme('item_list', $operations);

    $rows = menu_overview_tree_rows($mid);
    $table .= theme('table', $header, $rows ? $rows : array(array(array('data' => t('No menu items defined.'), 'colspan' => 5))));

    $output .= theme('box', check_plain($title), $table);
    }
  }
  return $output;
}

Next, I modified menu_per_role.module to allow users in the 'admin' role to see all menu's, and any others uers/groups, must be explicitly allowed access, or they won't see the menu (note, this is a change from default)

function menu_per_role_access($mid) {
	global $user;
	if(in_array('admin', array_values($user->roles))){
		return 1;
	}
  	else{
  	  $rids = menu_per_role_get_roles($mid);

		//NULL means inherit access from parent item
		return count($rids) ? count(array_intersect($rids, array_keys($user->roles))) : 0;
  	}

}

Finally, this is optional, but I modified this function in menu_per_role.module so that you can restrict access to the entire menu, and not just the menu items.

function menu_per_role_form_alter($form_id, &$form) {
//Modification here
  if ($form_id == 'menu_edit_item_form' || $form_id == 'menu_edit_menu_form') {
    $form['menu_per_role'] = array(
      '#type' => 'fieldset',
      '#title' => t('Restrict access permission'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => 5,
    );
    $form['menu_per_role']['roles'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Restrict access to only these roles'),
      '#options' => user_roles(),
      '#default_value' => $form['mid']['#value'] ? menu_per_role_get_roles($form['mid']['#value']) : array(),
      '#description' => t('Check no role to leave the access permission to the default.'),
    );
    $form['submit']['#weight'] = 10;
    $form['#submit']['menu_per_role_form_submit'] = array();
  } 
} 

Comments

AlexisWilke’s picture

Status: Active » Closed (won't fix)

This is now 2 years old so I'm closing it. If you can co-maintain, let us know.