[support] How to bypass permissions for node_save() ?

Xavier Bestel xavier.bestel at free.fr
Wed Mar 23 16:18:58 UTC 2011


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




More information about the support mailing list