Как программно создать узел Drupal как пользователь X, а не Y, когда Y вошел в систему - PullRequest
1 голос
/ 07 сентября 2010

Могу ли я сделать следующее:

Создать новый объект контента типа ' A ':

//The global user is user Y

$node = new Object();
$node->type = 'A'
//etc..

//Save node - but I want the node to look like it was created by user X
node_save($node);

Это случайгде пользователь Y не имеет разрешения 'create A content', но пользователь X имеет и все содержимое типа A должно быть создано пользователем X (т.е. скрипт);

1 Ответ

2 голосов
/ 08 сентября 2010

С Безопасное олицетворение другого пользователя в руководстве Drupal:

<?php
global $user;
$original_user = $user;
session_save_session(FALSE); // D7: use drupal_save_session(FALSE);
$user = user_load(array('uid' => 1)); // D7: use user_load(1);

// DO YOUR STUFF HERE, for example node_save($node);
// If your code fails, it's not a problem because the session will not be saved

$user = $original_user;
session_save_session(TRUE); // // D7: use drupal_save_session(TRUE);

// From here on the $user is back to normal so it's OK for the session to be saved
?>
...