Jump to content

Admin Restrict Branch


Recommended Posts

  • 5 months later...
  • 1 month later...

This module can really be a lifesaver for me if I can use it. But unfortunately I am getting this error on certain pages when i have it installed and am using the  user restricted branch setting (when i uninstall the module, errors go away):

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes) in /home/u985981598/domains/clozewizard.com/public_html/wire/core/Wire.php on line 1160

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes) in Unknown on line 0


Link to comment
Share on other sites

@jon9china - sorry you're having problems with the module. You could please clarify what you mean by "the user restricted branch setting" ? Are you talking about the "User Specified Branch Parent" option for the "How to match user to branch" setting?

Do you have the same problem if you use one of the option ways to match the branch?

I can't replicate the problem here, so I am wondering if you might have some other additional hooks (custom or in a 3rd party module) that are also controlling access. It sounds like you are running out of memory due to an infinite loop.

If you can narrow it down to some other code interaction that will help. If it's not that, would you mind trying to debug this module to see where it might be goin awry?

Could you also post your other settings for the module?

Also, is the error only when viewing the admin with a restricted user?

  • Like 1
Link to comment
Share on other sites

Looks like I solved the problem. Your mention of an infinite loop got me looking out for a potential cause. I had a field of type Page Reference that selected from user pages. When I used that field in selectors - those are the pages that caused the error. They were looking for the page that identified the restricted user that was logged in - if that makes any sense.

So hope this description is helpful for understanding the problem. I just modified that field instead of choosing a page with template user, I use a field for email. Not the best solution for me for now, because the user could change their email and then my code will miss the match with that user, but does clean up the code so that I can use the module without having that fatal error.


Link to comment
Share on other sites

@jon9china - thanks for the explanation. I think I sort of understand "They were looking for the page that identified the restricted user that was logged in", but if you could provide the selector code you are using it would be helpful - perhaps I can implement something into the ARB module so that you can still use the selector the way you want, rather than changing to the email field.

So if you could post the old and new versions, I'll try to replicate and see if I can fix it somehow.

  • Like 1
Link to comment
Share on other sites

ok, here it goes.  Here is the offending code block after being changed:

//do we find that a user field for teacher root page matches the logged in user's data?



foreach($myTeacherRootPages as $thisTeacherPage){

    $herUserName = $thisTeacherPage->teacher_user_name;
    if($herUserName==$loggedInUsername) {
             $thisTeacherId = $thisTeacherPage->id; //found a teacher match; this guyz'a teacher
             $isTeacherPage = 1;

the 'teacher_user_name' is a text field. Formerly, what was causing the error, was when this field was a page field that contained pages of template=user. (And on the appropriate page in that field, the restricted user would be selected to indicate that this was her branch) When the user who reaches this page on the frontend is a user who has been restricted to this branch of the tree, which also contains this page, then the page caused the error.

The template for these $myTeacherRootPages formerly contained a field of type Page, called 'user_profile'  where template=user

With the above code, where problem was solved, in the template, this field was deleted and replaced with a text field 'teacher_user_name' whose value is hardcoded to match the 'name' field of the desired user's user page.

If, incidentally, the user's restricted branch did not contain this page, then this user being logged in and opening this page did not cause an error.

the code formerly was something like this:

foreach($myTeacherRootPages as $thisTeacherPage){
    $herUserProfile = $thisTeacherPage->teacher_profile
    if($herUserProfile==$loggedInUser) {
             $thisTeacherId = $thisTeacherPage->id; //found a teacher match; this guyz'a teacher
             $isTeacherPage = 1;
It seemed on any page where I referenced the 'teacher_profile' field in the template, like above, when the restricted user was the one opening that page, then produced the error/infinite loop.
Link to comment
Share on other sites

If that is confusing, then it might be simpler to just say the following:

to recreate the error, create a template with a field of type page that accepts template=user. (you could name the field 'master_of_this_branch')

create a page with that template and select a test user as 'master_of_this_branch'

in the test user's profile, restrict him to a branch using that page as its root.

reference that field ('master_of_this_branch') in a page in the test user's tree and signed in as that test user, navigate to that page and should produce the infinite loop.

Link to comment
Share on other sites

@jon9china - I have tried your step by step and can't reproduce the error yet. I am wondering what version of PW you are running? There was a very long-standing circular reference bug in the PW core related to page reference fields that was fixed here: https://github.com/processwire/processwire/commit/8dbff5c7bc367e805badf51204b4e31123cf9b6e - so basically you need to be running at least 3.0.166 to have that fix in place.

Please let me know the version and if it is later than that, then I'll investigate this issue further.



Link to comment
Share on other sites

  • 2 weeks later...

Sorry for missing the reply, adrian. Yeah, in fact I am using the latest version of Processwire. Just installed it earlier this year for this new site.  Hmm. surprised that didn't recreate the error.  Not sure my description is accurate, then, in light of that fact. Nevertheless, my changing that field from taking a page where template=user, to just a text field did solve the issue and haven't had any surprises from the module since. At least this I can say.

Link to comment
Share on other sites

7 hours ago, jon9china said:

Yeah, in fact I am using the latest version of Processwire

Sorry, do you mean the latest master (3.0.165), or the latest dev? The latest master version does not include the circular reference fix I am talking about. If you are using a dev version from anytime this year, the bug should be fixed, but without clarity on that, I really can't help much 🙂

  • Like 1
Link to comment
Share on other sites

That is a really nasty bug - apparently it didn't affect many users but it affected several of my sites - not sure what that says about me, but I am definitely glad it's fixed because I was hacking around it for a couple of years there.

Link to comment
Share on other sites

Hi everyone.

I have a need for a specific user to add rights to multiple branches (below in the picture with the user selected two branches: branch1, branch2). Is this possible, and what is the best way to do it, and does the current modules need to be modified a lot?


Link to comment
Share on other sites

Hi @sandimilohanic - unfortunately that's just not possible with the way this module works. Restricting to one branch in PW is relatively simple (what this module does), but restricting to more than one requires a completely different approach which is a little more fiddly.

Perhaps you can build exactly what you need (without the need to be flexible like a publicly released module has to be). This gist might be a helpful starting point: https://gist.github.com/adrianbj/6fd1b770d9ce7a7252b6



Link to comment
Share on other sites

15 hours ago, adrian said:

Hi @sandimilohanic - unfortunately that's just not possible with the way this module works. Restricting to one branch in PW is relatively simple (what this module does), but restricting to more than one requires a completely different approach which is a little more fiddly.

Perhaps you can build exactly what you need (without the need to be flexible like a publicly released module has to be). This gist might be a helpful starting point: https://gist.github.com/adrianbj/6fd1b770d9ce7a7252b6

Hi @adrian - big thanks for the reply and direction for further development.

  • Like 1
Link to comment
Share on other sites

Recently needed to add something similar to a site I've been working on, and ended up with a slightly different approach. Can't share the code right now, but here's the general concept:

  • Store all allowed branches in user profile, similar to the screenshot above. 
  • Hook into ProcessPageList::execute and add a select option for choosing which branch is "active".
  • Hook into AdminRestrictBranch::getBranchRootParentId and check if a branch GET param was provided and matches one of those stored in user profile; if yes, store the value in session. Return either the first branch from user profile, or the value from session.

Technically it's only restricting the user to one branch at a time, so there are some limitations, but in my specific case that's actually preferable: this lets users focus on one section of the site at a time 🙂

  • Like 4
Link to comment
Share on other sites

@teppo - this sounds like a pretty decent solution. I wonder if it might be something that could be incorporated into the ARB module as a feature. Let me know if you'd be willing/able to share the code at some point, or even better if you would be happy to incorporate into ARB as a PR.

  • Like 3
Link to comment
Share on other sites

Hey @adrian! Just a heads-up that I posted an updated and extended version of my code as a new add-on module:

The functionality seemed pretty specific to one use case so I figured it was best not to bundle it in the main module, but feel free to use anything from my module if it makes sense to you. Turns out that the original implementation missed a few key points and was very specific to a particular site, so this is basically a full rewrite... 🙂

  • Like 4
Link to comment
Share on other sites

  • 2 months later...
On 2/5/2020 at 6:18 PM, adrian said:

Unfortunately with the way this module is built, it can only ever work for one branch.

Hi Adrian,

it's a shame your wonderful module can't do multiple branches - was really looking for something with that functionality and your module does everything that I need except this. Still found it useful to completely block the page tree and restrict breadcrumbs. I am grateful for your module! 

Maybe any plans to rework it to allow multiple branches? With my (not very experienced) eyes it seems it might be doable, although quite a lot of code would need changing.

Personally I was looking to restrict users to only those branches, where they have pages created - I am using "page-edit-created" permission to limit editing to only user created pages. I imagined using it with "Custom PHP code" option and something like: `$pages->find("created_users_id=".$user->id)->implode("|", "rootParent")`.

Link to comment
Share on other sites

  • 1 month later...

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 monollonom
      Github: https://github.com/romaincazier/PageMjmlToHtml
      Modules directory: https://processwire.com/modules/page-mjml-to-html/
      A module allowing you to write your Processwire template using MJML and get a converted HTML output using MJML API.
      This is considered to be in alpha and as such needs some testing before being used in production!

      Created by Mailjet, MJML is a markup language making it a breeze to create newsletters displayed consistently across all email clients.
      Write your template using MJML combined with Processwire’s API and this module will automatically convert your code into a working newsletter thanks to their free-to-use Rest API.
      For this module to work you will need to get an API key and paste it in the module’s configuration.
      Once your credentials are validated, select the template(s) in which you’re using the MJML syntax, save and go visualize your page(s) to see if everything’s good. You will either get error/warning messages or your email properly formatted and ready-to-go.
      From there you can copy/paste the raw generated code in an external mailing service or distribute your newsletter using ProMailer.
      The MJML output is cached to avoid repetitive API calls Not cached if there are errors/warnings Cleared if the page is saved Cleared if the template file has been modified A simple (dumb?) code viewer highlights lines with errors/warnings A button is added to quickly copy the raw code of the generated newsletter Not added if the page is rendered outside of a PageView Only visible to users with the page’s edit permission A shortcut is also added under “View” in the edit page to open the raw code in a new tab Multi-languages support
      The code viewer is only shown to superusers. If there’s an error the page will display:
      Only its title for guests Its title and a message inviting to contact the administrator for editors If you are using the markup regions output strategy, it might be best to not append files to preserve your MJML markup before calling the MJML API. This option is available in the module’s settings.
    • By Marco Ro
      Hi guys!
      I'm a bit anxious because this is the first module I present! (beta modulo) But I will finally be able to share something with the community too! :)
      This is a BETA version of the PayPal payment system called: PayPal Commerce Platform.
      It is an advanced system (Business Pro account is needed) that brings various benefits in terms of fees and above all integrates direct payment with credit/debit cards. 
      The module integrates with Padloper 0.0.2, which is the current installation I'm using.
      This system integrates the classic PayPal buy button, the alternative or local payment method and the new payment system: credit/debit cards that doesn't go through the PayPal account. It is a Stripe-style payment, it connects directly with the bank and integrates 3D security validation.
      I say that it is a BETA because this module currently only works with Sandbox account, to put it live you need to change API url manually (manually for the moment).
      Because this module is not ready for live:
      I would like to have your opinion on how I built the module (is the first one I do). I don't want to share something that is not fish but I need a comparison with someone more experienced than me, for be sure that this is the best way to code the module.
      If you want to try this I created a git, you will find all the instructions for installation and correct operation. (Git has a MIT licensed)
      https://github.com/MarcooRo/processwire-PayPal-Commerce-Platform I hope I did something that you guys can like :)
    • By monollonom
      (once again I was surprised to see a work of mine pop up in the newsletter, this time without even listing the module on PW modules website 😅. Thx @teppo !)
      Github: https://github.com/romaincazier/FieldtypeQRCode
      Modules directory: https://processwire.com/modules/fieldtype-qrcode/
      A simple fieldtype generating a QR Code from the public URL of the page, and more.
      Using the PHP library QR Code Generator by Kazuhiko Arase.

      In the field’s Details tab you can change between .gif or .svg formats. If you select .svg you will have the option to directly output the markup instead of a base64 image. SVG is the default.
      You can also change what is used to generate the QR code and even have several sources. The accepted sources (separated by a comma) are: httpUrl, editUrl, or the name of any text/URL/file/image field.
      If LanguageSupport is installed the compatible sources (httpUrl, text field, ...) will return as many QR codes as there are languages. Note however that when outputting on the front-end, only the languages visible to the user will be generated.
      Unformatted value
      When using $page->getUnformatted("qrcode_field") it returns an array with the following structure:
      [ [ "label" => string, // label used in the admin "qr" => string, // the qrcode image "source" => string, // the source, as defined in the configuration "text" => string // and the text used to generate the qrcode ], ... ] Formatted value
      The formatted value is an <img>/<svg> (or several right next to each other). There is no other markup.
      Should you need the same markup as in the admin you could use:
      $field = $fields->get("qrcode_field"); $field->type->markupValue($page, $field, $page->getUnformatted("qrcode_field")); But it’s a bit cumbersome, plus you need to import the FieldtypeQRCode's css/js. Best is to make your own markup using the unformatted value.
      Static QR code generator
      You can call FieldtypeQRCode::generateQRCode to generate any QR code you want. Its arguments are:
      string $text bool $svg Generate the QR code as svg instead of gif ? (default=true) bool $markup If svg, output its markup instead of a base64 ? (default=false) Hooks
      Please have a look at the source code for more details about the hookable functions.
      $wire->addHookAfter("FieldtypeQRCode::getQRText", function($event) { $page = $event->arguments("page"); $event->return = $page->title; // or could be: $event->return = "Your custom text"; }) $wire->addHookAfter("FieldtypeQRCode::generateQRCodes", function($event) { $qrcodes = $event->return; // keep everything except the QR codes generated from editUrl foreach($qrcodes as $key => &$qrcode) { if($qrcode["source"] === "editUrl") { unset($qrcodes[$key]); } } unset($qrcode); $event->return = $qrcodes; })
    • By Sebi
      AppApiFile adds the /file endpoint to the AppApi routes definition. Makes it possible to query files via the api. 
      This module relies on the base module AppApi, which must be installed before AppApiFile can do its work.
      You can access all files that are uploaded at any ProcessWire page. Call api/file/route/in/pagetree?file=test.jpg to access a page via its route in the page tree. Alternatively you can call api/file/4242?file=test.jpg (e.g.,) to access a page by its id. The module will make sure that the page is accessible by the active user.
      The GET-param "file" defines the basename of the file which you want to get.
      The following GET-params (optional) can be used to manipulate an image:
      width height maxwidth maxheight cropX cropY Use GET-Param format=base64 to receive the file in base64 format.
    • By MarkE
      This fieldtype and inputfield bundle was built for storing measurement values within a field, rendering them in a variety of formats and converting them to other units or otherwise modifying them via the API.
      The API consists of a number of predefined functions, some of which include...
      render() for rendering the measurement object, valueAs() for converting the value to another unit value, convertTo() for converting the whole measurement object to different units, and add() and subtract() for for modifying the stored value by the value (converted as required) in another measurement. In the admin the inputfield includes a checkbox (which can be optionally disabled) for converting values on page save. For an example if a value was typed in as centimeters, the unit was changed to metres, and the page saved with this checkbox selected, said value would be automatically converted so that e.g. 170 cm becomes 1.7 m.

      A simple length field using Fieldtype Measurement and Inputfield Measurement.
      Combination units (e.g. feet and inches) are also supported.
      Please note that this module is 'proof of concept' at the moment - there are limited units available and quite a lot of code tidying to do. More units will be added shortly.
      See the GitHub at https://github.com/MetaTunes/FieldtypeMeasurement for full details and updates.
  • Create New...