Jump to content
Soma

Set created and modified user on cron scripts

Recommended Posts

Everybody who has import scripts running as a cronjob, there's this effect that the cronjob is run als guest user. So imported pages via API are created and modified by "guest". This is not nice and can lead to confusion. "What?! How can a guest create this page?"

To avoid this and set the current user via API you can do this at the beginning after bootstrapping PW:

include_once("./index.php");

/**
 * Overwrite static user for imports. All created pages will now
 * have created and modified user set to "importer" PW user.
 * Otherwise API created pages will have the created user "guest"
 * -------------------------------------------------------------------------------------
 */

$importerUser = wire("users")->get("importer");
if($importerUser->id) wire()->setFuel("user", $importerUser, $lock = true);

Only thing is you have to create a new superuser with the name you wish to use for such scripts and replace the name in the script.

Now if you run this script no matter from where or with what current user you'll have "importer" as the created and modified user.

-----

Note: There's a new way to set system vars like $user via wire(name, value), but this doesn't seem to work in this case in my tests. Cause the user will get overwritten again when creating a page by the current user calling the script. I'm not sure it's a bug or what, so it might be possible in future to use the shorter version of wire()->setFuel(name, value).

  • Like 7

Share this post


Link to post
Share on other sites

There's another method to be able to overwrite created user directly when creating a page.

There's a new template setting to allow overwriting the $page->createdUser that you can enable to allow this via API.

post-100-0-05581600-1426959947_thumb.png

Then you can do this:

$p = new Page();
$p->template = "basic-page";
$p->set("createdUser", wire("users")->get("importer"));
$p->title = "Imported page";
$p->save();
  • Like 5

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...