View menu item without access

TheRec - October 1, 2007 - 14:12
Project:Views
Version:5.x-1.6
Component:Code
Category:support request
Priority:normal
Assigned:Unassigned
Status:by design
Description

Hello,

I want to display a menu item pointing to the a specific view although the user viewing the site doesn't have access to this view (so anonymous users will see the link and when they click it they will be redirected to the login form (that redirection is working)). But when users doesn't have access to a view the corresponding menu items aren't displayed in the menus (it seems like Views has a hook on the menu module and it checks if the destination of the menu item is the "URL" of a view and if it is the case it applies the permissions accordingly). Is it possible to override this for a specific view or maybe for all the views ?

Thanks in advance.

#1

TheRec - October 1, 2007 - 21:09

Well, I think I've managed to find something of interest but I'm still not able to find out how to make the menu item appear without also givin access to the node to every user :S
Thing is I thought I had to clear the Views cache when I was testing the code but obviously I was wonger, since this is using the menu system hook it was the menu cache that had to be cleared and I can do this just by going on the "admin/build/menu" page.

So, in views.module, there "views_menu_standard_items", which calls "_views_create_menu_item" which itself calls "_views_menu_item" which checks user access like this before returning the parameters needed by the menu system hook to add the item :
'access' => user_access('access content') && $access,
I thought, perfect if I change access here and give an empty array for $access it would apply the default policy for anonymous users (which is let them see the content) and I was right, but it doesn't apply it ONLY for the menu it also enable anonymous users access the page. What I want is to be able to let only the anonymous users see the menu item but not access it (until they have logged in and are in the right group).

Mabye there is a way to check AGAIN the access rights when the View is display ? Because for now it seems like I uses the access rights set in the menu, and it's maybe logical (less requests to the DB) but doesn't achive what I intended to do :S

Can anyone help with this ? Thanks in advance.

#2

merlinofchaos - October 2, 2007 - 01:42
Status:active» by design

There is no easy way to do what you want to do. Drupal's menu system does not separate the access controls from the menu item visibility to the access of the actual page. You could do this by writing a module, creating a menu item, checking the caller's permissions, and either calling the view or the login page accordingly, but that'll require some development work.

This isn't something Views can do anything about, unfortunately.

#3

TheRec - October 2, 2007 - 06:51

OK, thanks for the answer.

I don't understand why it would not be possible to check the access rights again when the views are displayed and not take care of the menu items access rights. I figure it would happen in the "views_view_page" (would affect only "page") or even in "views_build_view" (would affect "page" and "block") functions in views.module, but I don't really know how to do this :S

Before I didn't have this problem because I was using a node where I was including manually the view with PHP and then used node_access module, which seems to be able to override the menu access rights, but the problem is when I do this I loose the possibility to user arguments in the URL (since it relies on an alias, thus view doesn't have a value in the "Url" field), and I need this function.

Writing a module doesn't seems like an idea I could use for the reason I need that menu item to be multi-language, so I need to create two menu items one for each language :S
Since there is no easy way, I'll take the hard way but is there any chance you could give me more details on how to do it please ?

#4

TheRec - October 2, 2007 - 11:39

Well, after many tires I ended up modifiying the core menu system (menu.inc.php), in the "_menu_build_visible_tree" function after :

    $visible = ($parent['type'] & MENU_VISIBLE_IN_TREE) ||
      ($parent['type'] & MENU_VISIBLE_IF_HAS_CHILDREN && count($children) > 0);
    $allowed = _menu_item_is_accessible($pid);

I added this :
    if($parent['path'] == '<path_to_my_view>') {
      $allowed = true;
    }

Of course "" has to be replaced by the value of the URL field (the one you set for the "View as page") of the view you want to see in the menu. Then all the items with this path will show up, but it won't give any supplemental rights to the users so if they use the link of this menu item and they don't have the access rights to this view they won't be able to access it.

That is a quick and dirty fix, I don't like to modfiy the core but I gave up after few hours of search... I just hope someone finds a better way, that's why I leave this support request open.

P.S.: Don't forget to go on the "admin/build/menu" page after you've modified the menu.inc.php, otherwise changes won't show up ;)

 
 

Drupal is a registered trademark of Dries Buytaert.