androbey

How to access protected template fields when bootstraping

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

Share this post


Link to post
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

Share this post


Link to post
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


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Livius
      Hello fellow Process wire Developers!
      Today i have encountered an issue using the repeater where I can't find out how to solve it therefore i am asking for help.
      To be specific, I am trying to get the "file" field working properly in the repeater but i couldn't find a way on how to assign a property of a field that is being used by the repeater.
      download_pdf is the name of the field that i want to access properties from.
      What i wished i could do was something like $download->download_pdf.size or $download->download_pdf->name, something like that but obviously these didn't work
      Hope there is a solution and thanks in advance!
      Best wishes,
      Livius

    • By wheelmaker24
      Hi,
      I got a new idea which come in handy when creating ProcessWire pages with enclosed subsections. Currently within the template family settings there is a setting "Can this template be used for new pages?" with the options "Yes", "No" and "One". My proposal is to add a fourth option that is "Single Child". This would allow the template to be created only once within another page (meaning that there wouldn't be any siblings with the same template).
      To give you an example look at this page structure for a multi-site webpage:
      Newsroom root Newsroom for Country 1 News articles Article 1 Article 2 ... Tags Search 404 Newsroom for Country 2 News articles Tags Search 404 Newsroom for Country 3 ... The root template will automatically redirect to the country newsroom page according to the browser's locale settings. Within each country newsroom there should obviously only be one "Tags", "Search" and "404" template. The option mentioned above would enable me to do this.
      A work-around solution for my specific use-case would be to select the option "No" and automatically create the Country Newsroom's children via a hook that triggers when a new country newsroom page has been created. But I think that this fourth option could come in handy for other use-cases as well.
      What do you think?
    • By louisstephens
      I have a "strange" issue with my dev set up, that I just noticed today. I set up an image field named "dev_gallery" and checked the "use tags" option when setting it up. I went to add a couple image to the field, and noticed that the tags option is not displaying at all for the image. Currently, the description field is the only current field on image edit. Have I missed a basic setting to enable this?
       
      I am currently using PW 3.0.62 as well.
    • By JurgenP
      First of all I'm as new to ProcessWire (PW) as they come, so bear with me if I ask stupid questions with obvious answers.
      I started looking into PW because I'm looking for something that would take out some of the grunt work of building an applications, yet be flexible enough to be able to implement custom functionality.
      I'm trying to work out if PW is suitable for my needs, ...
      A the moment I'm trying to create an architecture for a web platform on which multiple websites/webshops (750-1000) for pharmacists will be run.
      The websites will have different types of information, obviously there will be the 'own' pages the pharmacists create themselves.
      But as pharmacists tend 'not to be computer people' there is also content provided by the platform to make the site look alive, as well as public health information provided by the goverment.
      I was wondering if there is a way to structure/set up PW so that they have full access to their own content, and limited access (basically chose to publish it or not) to the externally provided content.
      Also is there a way to have the templates/page for the own content located in their own folder, and the 'external ones' in a shared folder so that they don't have to be repeated 1000 times on the server (and if modifications need to be done, it only has to be done once as well).
      Sincerely,
      Jurgen
    • By FrancisChung
      Hi,
      I was wondering if people have any recommendation or share their experiences with websites that either provide free or commercial website templates?

      Thanks in advance!