Hi,
I need a custom field for my users. So I created a computed_field in everyone's content_profile, so far so good. Now the problem is when I want to recompute that field from another user's action, it seems it fails because of the permissions. Here's a part of the code:
foreach($comptes as $uid => $val) { $result = db_query("SELECT name from {users} where uid = '%d'", $uid); $user = drupal_unpack(db_fetch_object($result)); $profilnode = content_profile_load('profile', $uid); if($profilnode) node_save($profilnode); /* update the computed_field */ }
How can I bypass the permissions system to make node_save() work ? I'm using Drupal 6.
Thanks, Xav
PS: please Cc: me on answers
On 3/22/2011 10:48 AM, Xavier Bestel wrote:
I need a custom field for my users. So I created a computed_field in everyone's content_profile, so far so good. Now the problem is when I want to recompute that field from another user's action, it seems it fails because of the permissions. Here's a part of the code:
foreach($comptes as $uid => $val) { $result = db_query("SELECT name from {users} where uid = '%d'", $uid); $user = drupal_unpack(db_fetch_object($result)); $profilnode = content_profile_load('profile', $uid); if($profilnode) node_save($profilnode); /* update the computed_field */ }
How can I bypass the permissions system to make node_save() work ? I'm using Drupal 6.
user_access caches perms, otherwise you would be able to temporarily add a role with 'administer nodes'. Instead, you'll need to swap out the global $user for uid 1 temporarily (and rename your local $user var). After you're done with the node_save, remember to swap the old $user back.
You might also be interested in the patch(es) in this issue: http://drupal.org/node/697856
Ted
On Tue, 2011-03-22 at 13:59 -0400, Ted wrote:
On 3/22/2011 10:48 AM, Xavier Bestel wrote:
I need a custom field for my users. So I created a computed_field in everyone's content_profile, so far so good. Now the problem is when I want to recompute that field from another user's action, it seems it fails because of the permissions. Here's a part of the code:
foreach($comptes as $uid => $val) { $result = db_query("SELECT name from {users} where uid = '%d'", $uid); $user = drupal_unpack(db_fetch_object($result)); $profilnode = content_profile_load('profile', $uid); if($profilnode) node_save($profilnode); /* update the computed_field */ }
How can I bypass the permissions system to make node_save() work ? I'm using Drupal 6.
user_access caches perms, otherwise you would be able to temporarily add a role with 'administer nodes'. Instead, you'll need to swap out the global $user for uid 1 temporarily (and rename your local $user var). After you're done with the node_save, remember to swap the old $user back.
Great !
Would something like that work (I'm not a true drupalist;) ?
global $user; $usersave = $user; $user = user_load(1);
... do stuff with node_save() ...
$user = $usersave;
Thanks, Xav
On 3/23/2011 12:18 PM, Xavier Bestel wrote:
How can I bypass the permissions system to make node_save() work ? I'm using Drupal 6.
user_access caches perms, otherwise you would be able to temporarily add a role with 'administer nodes'. Instead, you'll need to swap out the global $user for uid 1 temporarily (and rename your local $user var). After you're done with the node_save, remember to swap the old $user back.
Great !
Would something like that work (I'm not a true drupalist;) ?
global $user; $usersave = $user; $user = user_load(1);
... do stuff with node_save() ...
$user = $usersave;
At first glance that looks okay. Give it a shot!
Take a look at:
which describes how to safely impersonate another user.
-----Original Message----- From: support-bounces@drupal.org [mailto:support-bounces@drupal.org] On Behalf Of Ted Sent: Wednesday, March 23, 2011 12:28 PM To: Xavier Bestel Cc: support@drupal.org Subject: Re: [support] How to bypass permissions for node_save() ?
On 3/23/2011 12:18 PM, Xavier Bestel wrote:
How can I bypass the permissions system to make node_save() work ? I'm using Drupal 6.
user_access caches perms, otherwise you would be able to temporarily
add
a role with 'administer nodes'. Instead, you'll need to swap out the global $user for uid 1 temporarily (and rename your local $user var). After you're done with the node_save, remember to swap the old $user
back.
Great !
Would something like that work (I'm not a true drupalist;) ?
global $user; $usersave = $user; $user = user_load(1);
... do stuff with node_save() ...
$user = $usersave;
At first glance that looks okay. Give it a shot!
I had problems doing a similar thing if a WSOD appear when uid=1; so I endend with this funcions:
//ensures a user return to its state function wu_boot() { global $user; if (array_key_exists('wu_sudo', $_SESSION)){ watchdog('wu_debug', 'S\ha penjat un sudo!!!!'); $user = array_shift($_SESSION['wu_sudo']); unset($_SESSION['wu_sudo']); } }
//give user superpowers function wu_sudo() { global $user; if (!array_key_exists('wu_sudo', $_SESSION)) { $_SESSION['wu_sudo'] = array(); } array_push($_SESSION['wu_sudo'], $user); $user = user_load(1); }
//return user to its original state function wu_unsudo() { global $user; if (array_key_exists('wu_sudo', $_SESSION)) { if (!empty($_SESSION['wu_sudo'])) { $user = array_pop($_SESSION['wu_sudo']); } if (empty($_SESSION['wu_sudo'])) { unset($_SESSION['wu_sudo']); } } }
2011/3/23 Metzler, David metzlerd@evergreen.edu:
Take a look at:
which describes how to safely impersonate another user.
-----Original Message----- From: support-bounces@drupal.org [mailto:support-bounces@drupal.org] On Behalf Of Ted Sent: Wednesday, March 23, 2011 12:28 PM To: Xavier Bestel Cc: support@drupal.org Subject: Re: [support] How to bypass permissions for node_save() ?
On 3/23/2011 12:18 PM, Xavier Bestel wrote:
How can I bypass the permissions system to make node_save() work ? I'm using Drupal 6.
user_access caches perms, otherwise you would be able to temporarily
add
a role with 'administer nodes'. Instead, you'll need to swap out the global $user for uid 1 temporarily (and rename your local $user var). After you're done with the node_save, remember to swap the old $user
back.
Great !
Would something like that work (I'm not a true drupalist;) ?
global $user; $usersave = $user; $user = user_load(1);
... do stuff with node_save() ...
$user = $usersave;
At first glance that looks okay. Give it a shot!
[ Drupal support list | http://lists.drupal.org/ ]
[ Drupal support list | http://lists.drupal.org/ ]
reading http://drupal.org/node/218104 i think my function lacks session_save_session(); to ensure session is saved before entering "god mode"
function wu_sudo() { global $user; if (!array_key_exists('wu_sudo', $_SESSION)) { $_SESSION['wu_sudo'] = array(); } array_push($_SESSION['wu_sudo'], $user); session_save_session(); $user = user_load(1); }
2011/3/24 Lluís Forns enboig@gmail.com:
I had problems doing a similar thing if a WSOD appear when uid=1; so I endend with this funcions:
//ensures a user return to its state function wu_boot() { global $user; if (array_key_exists('wu_sudo', $_SESSION)){ watchdog('wu_debug', 'S\ha penjat un sudo!!!!'); $user = array_shift($_SESSION['wu_sudo']); unset($_SESSION['wu_sudo']); } }
//give user superpowers function wu_sudo() { global $user; if (!array_key_exists('wu_sudo', $_SESSION)) { $_SESSION['wu_sudo'] = array(); } array_push($_SESSION['wu_sudo'], $user); $user = user_load(1); }
//return user to its original state function wu_unsudo() { global $user; if (array_key_exists('wu_sudo', $_SESSION)) { if (!empty($_SESSION['wu_sudo'])) { $user = array_pop($_SESSION['wu_sudo']); } if (empty($_SESSION['wu_sudo'])) { unset($_SESSION['wu_sudo']); } } }
2011/3/23 Metzler, David metzlerd@evergreen.edu:
Take a look at:
which describes how to safely impersonate another user.
-----Original Message----- From: support-bounces@drupal.org [mailto:support-bounces@drupal.org] On Behalf Of Ted Sent: Wednesday, March 23, 2011 12:28 PM To: Xavier Bestel Cc: support@drupal.org Subject: Re: [support] How to bypass permissions for node_save() ?
On 3/23/2011 12:18 PM, Xavier Bestel wrote:
How can I bypass the permissions system to make node_save() work ? I'm using Drupal 6.
user_access caches perms, otherwise you would be able to temporarily
add
a role with 'administer nodes'. Instead, you'll need to swap out the global $user for uid 1 temporarily (and rename your local $user var). After you're done with the node_save, remember to swap the old $user
back.
Great !
Would something like that work (I'm not a true drupalist;) ?
global $user; $usersave = $user; $user = user_load(1);
... do stuff with node_save() ...
$user = $usersave;
At first glance that looks okay. Give it a shot!
[ Drupal support list | http://lists.drupal.org/ ]
[ Drupal support list | http://lists.drupal.org/ ]
-- *Les normes hi són perquè hi pensis abans de saltar-te-les *La vida és com una taronja, què 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.
On Wed, 2011-03-23 at 15:28 -0400, Ted wrote:
On 3/23/2011 12:18 PM, Xavier Bestel wrote:
How can I bypass the permissions system to make node_save() work ? I'm using Drupal 6.
user_access caches perms, otherwise you would be able to temporarily add a role with 'administer nodes'. Instead, you'll need to swap out the global $user for uid 1 temporarily (and rename your local $user var). After you're done with the node_save, remember to swap the old $user back.
Great !
Would something like that work (I'm not a true drupalist;) ?
global $user; $usersave = $user; $user = user_load(1);
... do stuff with node_save() ...
$user = $usersave;
At first glance that looks okay. Give it a shot!
Works very well. Thank you Ted !
Xav