Jump to content

How to access protected template fields when bootstraping


androbey
 Share

Recommended Posts

Hello again! 

Unfortunately I came across another problem. 

I have a "regular" ProcessWire setup and want to create a separate php script. That script should be executed by a cron job and is bootstrapped with my PW setup. Main goal is to send mails which are stored in a email field. 

My problem: I want to access a specific field from a template where only specific users have access to. Right now, the cron job is executed as "guest", of course. 

How can I bypass this restriction, so that I have access to that specific field (it's only one email field)? 

Does it make sense to set current user via api? 

Hope you can help me out there.

Cheers,
Andreas

Link to comment
Share on other sites

Hi,

Just an idea, you could create a new role, e.g. "cron-role" and assign to this role the permission "page-view".

Then you add a new user, e.g. "cron-user" and assign to this new user the new role "cron-role".

Now you add the role "cron-role" to the template you want to access the email field via "Template > Access".

And in your bootstrapped script, you could write something like:

<?php namespace ProcessWire;

include_once ('/path/to/pw/index.php');

$cronuser = wire('users')->get('cron-user'); // get the user "cron-user"
if($cronuser instanceof User) wire('users')->setCurrentUser($cronuser); // if found, set the current user to "cron-user"
else die('bad user'); // not sure about the die() call but whatever its an example

if($cronuser->hasRole("cron-role")) { // check if the role is assigned
    echo wire('pages')->get('/mysekretpage/')->sekretemailfield; // get the sekret field
}
else {
    echo 'access denied';
}

 

  • Like 3
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

×
×
  • Create New...