modify permissions using hook_menu_alter()
I am developing a module which will allow users to post certain content-types after 30 days of being registered. I think this is achieved using hook_menu_alter(); but my code freezes drupal with a white screen. Whats is missing in my code? function wu_menu_alter(&$menu) { foreach (node_get_types('types', NULL, TRUE) as $type) { $type_url_str = str_replace('_', '-', $type->type); $menu['node/add/'.$type_url_str]['access callback'] = 'wu_role_access'; $menu['node/add/'.$type_url_str]['access arguments'] = array('create', $type->type); } } function wu_role_access($a='',$b='',$c='',$d='',$e='') { // echo "**************"; //if uncommented this is displayed return true; //when it works I will implement the rest of the function } -- *La vida és com una taronja, que esperes a exprimir-la? *Si creus que l'educació és cara, prova la ignorància. *La vida és com una moneda, la pots gastar en el que vulguis però només una vegada. *Abans d'imprimir aquest missatge, pensa en el medi ambient.
Check at apache errors log. On Tue, 2009-04-28 at 16:21 +0200, Lluís wrote:
I am developing a module which will allow users to post certain content-types after 30 days of being registered. I think this is achieved using hook_menu_alter(); but my code freezes drupal with a white screen.
Whats is missing in my code?
function wu_menu_alter(&$menu) { foreach (node_get_types('types', NULL, TRUE) as $type) { $type_url_str = str_replace('_', '-', $type->type); $menu['node/add/'.$type_url_str]['access callback'] = 'wu_role_access'; $menu['node/add/'.$type_url_str]['access arguments'] = array('create', $type->type); } }
function wu_role_access($a='',$b='',$c='',$d='',$e='') { // echo "**************"; //if uncommented this is displayed return true; //when it works I will implement the rest of the function }
the only error in apache is: [Tue Apr 28 17:04:49 2009] [error] [client 192.168.1.8] File does not exist: /var/www/favicon.ico I solved it adding: $menu['node/add']['access callback'] = TRUE; But I think it has to be a better way On Tue, Apr 28, 2009 at 4:56 PM, Pedro Faria <predofaria@gmail.com> wrote:
Check at apache errors log.
On Tue, 2009-04-28 at 16:21 +0200, Lluís wrote:
I am developing a module which will allow users to post certain content-types after 30 days of being registered. I think this is achieved using hook_menu_alter(); but my code freezes drupal with a white screen.
Whats is missing in my code?
function wu_menu_alter(&$menu) { foreach (node_get_types('types', NULL, TRUE) as $type) { $type_url_str = str_replace('_', '-', $type->type); $menu['node/add/'.$type_url_str]['access callback'] = 'wu_role_access'; $menu['node/add/'.$type_url_str]['access arguments'] = array('create', $type->type); } }
function wu_role_access($a='',$b='',$c='',$d='',$e='') { // echo "**************"; //if uncommented this is displayed return true; //when it works I will implement the rest of the function }
-- *La vida és com una taronja, que esperes a exprimir-la? *Si creus que l'educació és cara, prova la ignorància. *La vida és com una moneda, la pots gastar en el que vulguis però només una vegada. *Abans d'imprimir aquest missatge, pensa en el medi ambient.
Quoting Lluís <enboig@gmail.com>:
the only error in apache is: [Tue Apr 28 17:04:49 2009] [error] [client 192.168.1.8] File does not exist: /var/www/favicon.ico
I solved it adding: $menu['node/add']['access callback'] = TRUE;
But I think it has to be a better way
I would ``touch /var/www/favicon.ico'' and make sure the apache user can read it. -- Earnie -- http://r-feed.com/ -- http://for-my-kids.com/ -- http://www.4offer.biz/ -- http://give-me-an-offer.com/
On Tue, Apr 28, 2009 at 5:21 PM, Earnie Boyd <earnie@users.sourceforge.net> wrote:
Quoting Lluís <enboig@gmail.com>:
the only error in apache is: [Tue Apr 28 17:04:49 2009] [error] [client 192.168.1.8] File does not exist: /var/www/favicon.ico
I solved it adding: $menu['node/add']['access callback'] = TRUE;
I made a mistake; it isn't solved. Adding this I can access node/add and it lists all content types, but when I try to access them I see a WSOD. If I insert echo "1234";I see the text, so my wu_role_access(...) is called. Any idea of how to debug where is the problem?
I would ``touch /var/www/favicon.ico'' and make sure the apache user can read it.
I don't care right now about favicon.ico right now; maybe later... :-)
-- Earnie -- http://r-feed.com/ -- http://for-my-kids.com/ -- http://www.4offer.biz/ -- http://give-me-an-offer.com/
-- *La vida és com una taronja, que esperes a exprimir-la? *Si creus que l'educació és cara, prova la ignorància. *La vida és com una moneda, la pots gastar en el que vulguis però només una vegada. *Abans d'imprimir aquest missatge, pensa en el medi ambient.
After reading og_user_roles I found I was able to modify $user->roles in hook_boot(); so no more changes are necessary It is a completely different approach, but everything is simplier in this way; I just need to create a role with the permissions I want, and then at hook_boot(); add the role to the users I want. On Tue, Apr 28, 2009 at 5:31 PM, Lluís <enboig@gmail.com> wrote:
On Tue, Apr 28, 2009 at 5:21 PM, Earnie Boyd <earnie@users.sourceforge.net> wrote:
Quoting Lluís <enboig@gmail.com>:
the only error in apache is: [Tue Apr 28 17:04:49 2009] [error] [client 192.168.1.8] File does not exist: /var/www/favicon.ico
I solved it adding: $menu['node/add']['access callback'] = TRUE;
I made a mistake; it isn't solved. Adding this I can access node/add and it lists all content types, but when I try to access them I see a WSOD. If I insert echo "1234";I see the text, so my wu_role_access(...) is called. Any idea of how to debug where is the problem?
I would ``touch /var/www/favicon.ico'' and make sure the apache user can read it.
I don't care right now about favicon.ico right now; maybe later... :-)
-- Earnie -- http://r-feed.com/ -- http://for-my-kids.com/ -- http://www.4offer.biz/ -- http://give-me-an-offer.com/
-- *La vida és com una taronja, que esperes a exprimir-la? *Si creus que l'educació és cara, prova la ignorància. *La vida és com una moneda, la pots gastar en el que vulguis però només una vegada. *Abans d'imprimir aquest missatge, pensa en el medi ambient.
-- *La vida és com una taronja, que esperes a exprimir-la? *Si creus que l'educació és cara, prova la ignorància. *La vida és com una moneda, la pots gastar en el que vulguis però només una vegada. *Abans d'imprimir aquest missatge, pensa en el medi ambient.
Quoting Lluís <enboig@gmail.com>:
After reading og_user_roles I found I was able to modify $user->roles in hook_boot(); so no more changes are necessary
It is a completely different approach, but everything is simplier in this way; I just need to create a role with the permissions I want, and then at hook_boot(); add the role to the users I want.
Or even easier just add the role to the user via the admin/user/user UI. -- Earnie -- http://r-feed.com/ -- http://for-my-kids.com/ -- http://www.4offer.biz/ -- http://give-me-an-offer.com/
Well, that was an example, the permission of a user to belong to a role or not can change during its session; so I need a way to make it change dynamically during the session. On Tue, Apr 28, 2009 at 6:16 PM, Earnie Boyd <earnie@users.sourceforge.net> wrote:
Quoting Lluís <enboig@gmail.com>:
After reading og_user_roles I found I was able to modify $user->roles in hook_boot(); so no more changes are necessary
It is a completely different approach, but everything is simplier in this way; I just need to create a role with the permissions I want, and then at hook_boot(); add the role to the users I want.
Or even easier just add the role to the user via the admin/user/user UI.
-- Earnie -- http://r-feed.com/ -- http://for-my-kids.com/ -- http://www.4offer.biz/ -- http://give-me-an-offer.com/
-- *La vida és com una taronja, que esperes a exprimir-la? *Si creus que l'educació és cara, prova la ignorància. *La vida és com una moneda, la pots gastar en el que vulguis però només una vegada. *Abans d'imprimir aquest missatge, pensa en el medi ambient.
Quoting Lluís <enboig@gmail.com>:
Well, that was an example, the permission of a user to belong to a role or not can change during its session; so I need a way to make it change dynamically during the session.
If you want to trap the user to a new set of permissions during the session you would need to use hook_form_alter and reload the global $user. Something like the untested function function foo_form_alter(&$form, &$form_state, $form_id) { global $user $user = user_load(array('uid' => $user->uid)); } This would reload the $user object for each form modifying the roles to the current set. If you only want certain forms to do this you can filter with the form id. -- Earnie -- http://r-feed.com/ -- http://for-my-kids.com/ -- http://www.4offer.biz/ -- http://give-me-an-offer.com/
On Tue, Apr 28, 2009 at 8:21 AM, Lluís <enboig@gmail.com> wrote:
I am developing a module which will allow users to post certain content-types after 30 days of being registered. I think this is achieved using hook_menu_alter(); but my code freezes drupal with a white screen.
Whats is missing in my code?
function wu_menu_alter(&$menu) { foreach (node_get_types('types', NULL, TRUE) as $type) { $type_url_str = str_replace('_', '-', $type->type); $menu['node/add/'.$type_url_str]['access callback'] = 'wu_role_access'; $menu['node/add/'.$type_url_str]['access arguments'] = array('create', $type->type); } }
function wu_role_access($a='',$b='',$c='',$d='',$e='') { // echo "**************"; //if uncommented this is displayed return true; //when it works I will implement the rest of the function }
FYI - I tried your code in my own module, works fine here. Did you clear your menu cache after making changes in hook_menu_alter()?
Quoting Ryan Courtnage <subscribe@courtnage.ca>:
On Tue, Apr 28, 2009 at 8:21 AM, Lluís <enboig@gmail.com> wrote:
I am developing a module which will allow users to post certain content-types after 30 days of being registered. I think this is achieved using hook_menu_alter(); but my code freezes drupal with a white screen.
Whats is missing in my code?
function wu_menu_alter(&$menu) { foreach (node_get_types('types', NULL, TRUE) as $type) { $type_url_str = str_replace('_', '-', $type->type); $menu['node/add/'.$type_url_str]['access callback'] = 'wu_role_access'; $menu['node/add/'.$type_url_str]['access arguments'] = array('create', $type->type); } }
You actually want to put this code in wu_menu() instead. Sorry, I missed the first post.
function wu_role_access($a='',$b='',$c='',$d='',$e='') { // echo "**************"; //if uncommented this is displayed return true; //when it works I will implement the rest of the function }
FYI - I tried your code in my own module, works fine here. Did you clear your menu cache after making changes in hook_menu_alter()?
We don't want to be building the menu cache dynamically. It is an expensive operation. -- Earnie -- http://r-feed.com/ -- http://for-my-kids.com/ -- http://www.4offer.biz/ -- http://give-me-an-offer.com/
Now, enough of this. There is no menu cache as it was in D5. There is the router table that can be rebuilt and the navigation trees are cached but this caching does not affect routing.
If this were the support mail group, I would shut up. But here on dev, I have to say it... Drupal is getting soooooooo complicated... it's an unkiss. Victor Kane http://awebfactory.com.ar http://projectflowandtracker.com On Tue, Apr 28, 2009 at 4:47 PM, Karoly Negyesi <karoly@negyesi.net> wrote:
Now, enough of this. There is no menu cache as it was in D5. There is the router table that can be rebuilt and the navigation trees are cached but this caching does not affect routing.
Excellent mail. Try throwing a match into a barrel full of gasoline next time. http://drupal4hu.com/node/137 a year old but on the spot: "People come to me months after a release and complain that they do not like how feature X is implemented. " I was talking about menu to anyone who wanted to listen since 2006 fall (ask the unfortunate attendedees of the Brussels DrupalCon where I talked even to some of those who did not want to listen :p). Every single line of code I wrote was not sneaken into core behind you guys back. It was in the open.
I don't mean anything against your excellent contributions. I am saying that the famous "code debt" is growing, and that instead of so much pressure being placed on more and more features, which has nothing to do with you, quite the contrary, the emphasis should be on refactoring. There is nothing wrong with the menu system or anything else. The code is excellent. It's the whole kit-and-kaboodle that should be refactored, as per the spirit of several threads, in the interest of simplicity, and the code debt acknowledged, which it never is. That's my emphasis. Victor Kane http://awebfactory.com.ar http://projectflowandtracker.com On Tue, Apr 28, 2009 at 5:09 PM, Karoly Negyesi <karoly@negyesi.net> wrote:
Excellent mail. Try throwing a match into a barrel full of gasoline next time.
http://drupal4hu.com/node/137 a year old but on the spot: "People come to me months after a release and complain that they do not like how feature X is implemented. "
I was talking about menu to anyone who wanted to listen since 2006 fall (ask the unfortunate attendedees of the Brussels DrupalCon where I talked even to some of those who did not want to listen :p). Every single line of code I wrote was not sneaken into core behind you guys back. It was in the open.
Chx, I did not, and will not, accuse anyone of sneaking things into core. For the function that started this thread, I have already done something similar and it works fine. BTW, hook_menu_alter is called while the router table is being rebuilt, so it need not be rebuilt. In my other thread, it was something totally new to me, but fashioned on something that someone else had done in D5. I started out doing it the "right" way and it would have worked except for a detail I never could envision in your original plans for menu (which I vaguely remember now). When I stumbled upon it, yes, it upset me. I sort of understand why it was done and I have submitted a patch to try to make it work more like what I think makes more sense (to me). No one is perfect, and I suspect that you had not envisioned the problem I ran into. That's okay. I accept that part of the blame is mine for not providing input at design time. As I mature in Drupal, I hope to be more helpful before a release hits the streets. I also hope that I will be less surprised after they do. We can only hope that everyone does the same. Nancy E. Wichmann, PMP Injustice anywhere is a threat to justice everywhere. - Martin L. King, Jr. No virus found in this outgoing message. Checked by AVG - http://www.avg.com Version: 8.0.176 / Virus Database: 270.12.6/2084 - Release Date: 4/28/2009 6:15 AM
Quoting Karoly Negyesi <karoly@negyesi.net>:
Now, enough of this. There is no menu cache as it was in D5. There is the router table that can be rebuilt and the navigation trees are cached but this caching does not affect routing.
But the routing is built only when visiting some pages or after emptying the cache. It isn't built dynamically from hook_menu so therefore even though you call it routing that table and not the result of hook_menu is what is used to create the dynamic links on the pages. During development of a module, if I add a menu item without visiting the menu page I don't get to see the menu unless I empty the cache. Therefore I call it a cache (right or wrong) since I see the results of the new menu item after clearing that cache. I don't think the idea bad, on the contrary it is very good. It just needs a new understanding of the functionality. What I miss though is some method to dynamically change the menu item before the page is displayed. -- Earnie -- http://r-feed.com/ -- http://for-my-kids.com/ -- http://www.4offer.biz/ -- http://give-me-an-offer.com/
But the routing is built only when visiting some pages or after emptying the cache.
Wrong. No matter how much cache you clear, the menu_router table won't be rebuilt. This is why I stepped in: I do not want false information to be spread. Indeed specific admin pages trigger a menu rebuild and that's it.
Quoting Karoly Negyesi <karoly@negyesi.net>:
But the routing is built only when visiting some pages or after emptying the cache.
Wrong. No matter how much cache you clear, the menu_router table won't be rebuilt. This is why I stepped in: I do not want false information to be spread. Indeed specific admin pages trigger a menu rebuild and that's it.
So when is hook_menu called? I use the devel module to ``Empty Cache'' to cause new items in my custom module to be generated on the menu. The hook_menu isn't called until I ``Empty Cache''. So it appears to from the DX perspective that emptying the cache rebuilds the menu. -- Earnie -- http://r-feed.com/ -- http://for-my-kids.com/ -- http://www.4offer.biz/ -- http://give-me-an-offer.com/
Returning to the original post which was mine; I post what I have and what I want to do, and then discuss the best way to do that, If lot of people have the same needs as I do, then new drupal version "could" be implemented to make things easy. I have implemented a module with a functionally similar to OG, but using $_SESSION to save the active group. After this node records and node grants are done according to $_SESSION (and saved to a table to allow permission rebuild). Now I want to give a user permission to post when he have some groups active and not for some others, so I created a role with posting permissions and I have to put it in $user->roles just when $_SESSION tells me to do this. I make it in hook_boot because another module with similar functionality for OG was doing this way. Is this a good way to do this? I think its better than doing this in hook_menu or hook_menu_alter because "new" groups are available from the beginning; but I will gladly listen to any constructive comments. And thanks to eveyone On Wed, Apr 29, 2009 at 12:26 AM, Earnie Boyd <earnie@users.sourceforge.net> wrote:
Quoting Karoly Negyesi <karoly@negyesi.net>:
But the routing is built only when visiting some pages or after emptying the cache.
Wrong. No matter how much cache you clear, the menu_router table won't be rebuilt. This is why I stepped in: I do not want false information to be spread. Indeed specific admin pages trigger a menu rebuild and that's it.
So when is hook_menu called? I use the devel module to ``Empty Cache'' to cause new items in my custom module to be generated on the menu. The hook_menu isn't called until I ``Empty Cache''. So it appears to from the DX perspective that emptying the cache rebuilds the menu.
-- Earnie -- http://r-feed.com/ -- http://for-my-kids.com/ -- http://www.4offer.biz/ -- http://give-me-an-offer.com/
-- *La vida és com una taronja, que esperes a exprimir-la? *Si creus que l'educació és cara, prova la ignorància. *La vida és com una moneda, la pots gastar en el que vulguis però només una vegada. *Abans d'imprimir aquest missatge, pensa en el medi ambient.
The devel needs a bug report.
Quoting Karoly Negyesi <karoly@negyesi.net>:
The devel needs a bug report.
I don't understand what is wrong with devel to open a report. The callback just does <code> /** * Menu callback; clears all caches, then redirects to the previous page. */ function devel_cache_clear() { // If you used to implement our own cache clear hook, implement hook_flush_caches instead. See drupal_flush_all_caches() drupal_flush_all_caches(); drupal_set_message('Cache cleared.'); drupal_goto(); } </code> And http://api.drupal.org/api/function/drupal_flush_all_caches/6 calls menu_rebuild(). -- Earnie -- http://r-feed.com/ -- http://for-my-kids.com/ -- http://www.4offer.biz/ -- http://give-me-an-offer.com/
Then core needs fixing too. Or people need understanding? Because when you say "emptying the cache" some might think that's simply TRUNCATE cache. Which, accidentally, DID rebuild the menu in Drupal 5.
Quoting Karoly Negyesi <karoly@negyesi.net>:
Then core needs fixing too. Or people need understanding? Because when you say "emptying the cache" some might think that's simply TRUNCATE cache. Which, accidentally, DID rebuild the menu in Drupal 5.
Yes, I understand and agree. The rebuild is triggered simply by adding a menu_rebuild_needed with a boolean TRUE and executing cron.php or by calling menu_rebuild directly which is where the actions occur and what the hook_cron implementation calls. I'm not sure how to title an issue the drupal_flush_all_caches calls rebuild_menu, drupal_rebuild_theme_registry, node_types_rebuild. For an API that is dealing with flushing caches does it have business rebuilding non cache tables? I've created #448750 for continued discussion. -- Earnie -- http://r-feed.com/ -- http://for-my-kids.com/ -- http://www.4offer.biz/ -- http://give-me-an-offer.com/
http://api.drupal.org/api/function/menu_rebuild/6 Nancy E. Wichmann, PMP Injustice anywhere is a threat to justice everywhere. - Martin L. King, Jr. No virus found in this outgoing message. Checked by AVG - http://www.avg.com Version: 8.0.176 / Virus Database: 270.12.6/2084 - Release Date: 4/28/2009 6:15 AM
participants (7)
-
Earnie Boyd -
Karoly Negyesi -
Lluís -
Nancy Wichmann -
Pedro Faria -
Ryan Courtnage -
Victor Kane