Jump to content

Set created and modified user on cron scripts


Soma
 Share

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
Link to comment
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
  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...