Jump to content
adrian

Admin Restrict Branch

Recommended Posts

Hi everyone,

Some critical updates this morning:

1) The module stopped doing anything in recent versions of PW 3 - I don't exactly when, but this is now fixed.

2) I also added support for restricting the new Pages > Tree dropdown menu that was added in 3.0.55

Please let me know if you find any problems with these changes or if you find any other situations where the module is no longer working. Given the rapid development of new ways to access the page tree that are being added to PW, I strongly recommend thoroughly testing your site if this module is protecting critical info from certain users. Hopefully everything is already taken care of for the current PW dev version, but with the UiKit admin theme, there could be possible breaks.

Thanks for testing and letting me know!

  • Like 3

Share this post


Link to post
Share on other sites

@adrian Is it possible with this module to hide specific pages under home or I should stay with the hardcoded HideOtherUserPages module?

Share this post


Link to post
Share on other sites
6 hours ago, PWaddict said:

@adrian Is it possible with this module to hide specific pages under home or I should stay with the hardcoded HideOtherUserPages module?

This module is for restricting users to a single branch of the page tree rather than hiding various ones throughout the entire tree. You'll need one of those other options for that. Maybe a module could be put together with more flexibility, but there are lots more issues when hiding specific pages.

Share this post


Link to post
Share on other sites
1 hour ago, adrian said:

This module is for restricting users to a single branch of the page tree rather than hiding various ones throughout the entire tree. You'll need one of those other options for that. Maybe a module could be put together with more flexibility, but there are lots more issues when hiding specific pages.

I selected Specified Branch Parent and then on the user's profile field I selected the Home branch. Then on the Branch edit exclusions I selected a page under the Home that I want to hide but it's still visible. What am I missing?

Share this post


Link to post
Share on other sites
5 minutes ago, PWaddict said:

I selected Specified Branch Parent and then on the user's profile field I selected the Home branch. Then on the Branch edit exclusions I selected a page under the Home that I want to hide but it's still visible. What am I missing?

Sorry, I think you have misunderstood how this works. If you are using the "Specified Branch Parent" - that is where you select the branch that you want the user restricted to. The "Branch Edit Exclusions" are just for things like external PageTable branches etc that the user still needs edit access to, even though they are outside their restricted branch.

Does that clear things up?

Share this post


Link to post
Share on other sites
1 hour ago, adrian said:

Sorry, I think you have misunderstood how this works. If you are using the "Specified Branch Parent" - that is where you select the branch that you want the user restricted to. The "Branch Edit Exclusions" are just for things like external PageTable branches etc that the user still needs edit access to, even though they are outside their restricted branch.

Does that clear things up?

Ok now I understand, thanks. I will stay then with the hardcoded HideOtherUserPages module.

Share this post


Link to post
Share on other sites

@adrian

I am working on a multisite project using single database. I forked multisite.module by Antti and another fork by Soma and started to adapt it for my needs. Luckily I found your module. I tried it out and it fits 90% to my needs and already saved a lot of work. Thanks for this great module. :rolleyes:

To get it working properly I need to make some changes. I think these changes could also be an enhancement of your module and you maybe want to take it.

First of all I need an option to set  branchRootParentId via hook which is currently not possible since the related function isn't hookable and private too.
It would be nice if you could change the visibility of the function getBranchRootParentId() to protected and make it hookable.
As far I can see there is no reason (security) to refrain from this.

Furthermore there are 2 unexpected behaviours or potential issues:

  1. In Processwire the breadcrumb 'Pages' is prepended to breadcrumbs in the PageEdit area of the root page only. @see ProcessPageEdit::setupBreadcrumbs()
    This behaviour is different in your module. The breadcrumb is prepended in any Editor not only for the branchRootParent since $this->wire('breadcrumbs')->count() returns always 0. Is this wanted like that?
    I would prefer the default behavior here. I found a working solution. If you like I could post an issue on github.
     
  2. The BranchRootParent is not prepended to the Pagetree under PagesTab.
    ProcessPageList::executeNavJSON() doesn't prepend branchRootParent here. This couldn't be solved inside your module.
    I can see a PW issue here. ProcessPageList::executeNavJSON() should use $config->rootPageID instead of hardcoded 1

    Possible Solution
    // in ProcessPageList::executeNavJSON
    // Line 489 from
    if(!$parentID) $parentID = 1;
    // to 
    if(!$parentID) $parentID = $config->rootPageID;
    
    // Line 494 from
    if($parentID === 1 && $parentViewable) $items->prepend($parent);
    // to
    if($parentID === $config->rootPageID && $parentViewable) $items->prepend($parent);
    
    // Line 523 from
    $numChildren = $id > 1 ? $page->numChildren : 0;
    // to
    $numChildren = $id != $config->rootPageID ? $page->numChildren : 0;
    
    // Line 551 from
    if($page->id > 1 && $page->numChildren) {
    // to
    if($page->id != $config->rootPageID && $page->numChildren) {
    
    //in your module
    public function ready() {
    $this->wire('config')->rootPageID = $this->branchRootParentId;
    // ...
    }

 

  • Like 2

Share this post


Link to post
Share on other sites
On 5/7/2017 at 5:34 AM, kixe said:

@adrian

I am working on a multisite project using single database. I forked multisite.module by Antti and another fork by Soma and started to adapt it for my needs. Luckily I found your module. I tried it out and it fits 90% to my needs and already saved a lot of work. Thanks for this great module. :rolleyes:

 

Working on a similar project, a kind of site parameterised site cloner. All in one PW install for maintenance commodity. Would love to see the outcome of this discussion :)

Share this post


Link to post
Share on other sites

Hi @kixe - thanks for all your thoughts here. Hopefully you know that I have been away and not just ignoring your post!

On 5/7/2017 at 3:34 AM, kixe said:

First of all I need an option to set  branchRootParentId via hook which is currently not possible since the related function isn't hookable and private too.
It would be nice if you could change the visibility of the function getBranchRootParentId() to protected and make it hookable.

The latest version has the getBranchRootParentId function hookable.

 

On 5/7/2017 at 3:34 AM, kixe said:

In Processwire the breadcrumb 'Pages' is prepended to breadcrumbs in the PageEdit area of the root page only. @see ProcessPageEdit::setupBreadcrumbs()
This behaviour is different in your module. The breadcrumb is prepended in any Editor not only for the branchRootParent since $this->wire('breadcrumbs')->count() returns always 0. Is this wanted like that?
I would prefer the default behavior here. I found a working solution. If you like I could post an issue on github.

The reason I did this is because when a user is restricted to a branch of the page tree, there is no "Home" breadcrumb, so I added this to give them easy access back to the root of the pages they have access to. I am curious about the working solution you have though - maybe it takes care of what we both want?

 

On 5/7/2017 at 3:34 AM, kixe said:

The BranchRootParent is not prepended to the Pagetree under PagesTab.
ProcessPageList::executeNavJSON() doesn't prepend branchRootParent here. This couldn't be solved inside your module.
I can see a PW issue here. ProcessPageList::executeNavJSON() should use $config->rootPageID instead of hardcoded 1

I think that regardless of the interaction with this, module those "1" values shouldn't be hardcoded in the PW as you noted - maybe you could submit a PR to Ryan to fix those? As for the actual prepending of BranchRootParent to the Pagetree under PagesTab, do you mean here:

595d410885623_ScreenShot2017-07-05at12_41_40PM.png.c958f147dce322a2066dd0a1bfe6c4a7.png

Test One and Test Two and children of the BranchRootParent and as you note, the BranchRootParent itself is not shown. Not critical, but I agree it would be nicer if it was, but from some quick looking around I don't think it's going to be possible at the moment without manually prepending the BranchRootParent to the markup returned by  ProcessPageList::executeNavJSON - certainly possible, but not pretty.

Any thoughts on my comments?

I'd certainly like to help as much as possible for you on this, so just let me know.

Cheers,
Adrian

 

  • Like 1

Share this post


Link to post
Share on other sites

@adrian First of all: Welcome back!

5 hours ago, adrian said:

The latest version has the getBranchRootParentId function hookable.

Great, Thanks!

Check out my fork to see the changes I made.
https://github.com/kixe/AdminRestrictBranch/tree/kixe

Everything is working pretty good together with the small changes in ProcessPageList::executeNavJSON()

6 hours ago, adrian said:

I think that regardless of the interaction with this, module those "1" values shouldn't be hardcoded in the PW as you noted - maybe you could submit a PR to Ryan to fix those?

Ryan is very busy and there are some other unsolved issues I am interested in. Furthermore there are many other places in the core where '1' is used instead of $config->rootPageID. I think it should be consistent but its not so easy to change this.

 

Share this post


Link to post
Share on other sites

@adrian Thanks for this awesome module!

Until now, I didn't have a real use-case for this module, but for an upcoming project I think I might use it.

Is it possible to define more than one single branch tree? Let's say I have three areas (parents) I want to allow user x to see/edit, would I define this via "Branch parent to restrict access to"? Is that the way to go?

Share this post


Link to post
Share on other sites
Just now, dragan said:

Is it possible to define more than one single branch tree? Let's say I have three areas (parents) I want to allow user x to see/edit, would I define this via "Branch parent to restrict access to"? Is that the way to go?

Unfortunately that's not possible with the way this module works.

There is lots of discussion floating around about why it's difficult to hide parts of the page tree - have a read starting here: https://processwire.com/talk/topic/1176-hiding-uneditable-pages-from-users/?do=findComment&comment=84916 - there are links to some different gists that might be helpful for you.

  • Like 1

Share this post


Link to post
Share on other sites

Great module @adrian.

For the Custom PHP Code option, can you make it also have access to the $pages variable (in addition to just the $user variable)?  I have a use case for it and it would be helpful.  I modified the module to include it and it worked fine for me.

  • Like 1

Share this post


Link to post
Share on other sites
1 minute ago, Jonathan Lahijani said:

Great module @adrian.

For the Custom PHP Code option, can you make it also has access to the $pages variable (in addition to just the $user variable)?  I have a use case for it and it would be helpful.  I modified the module to include it and it worked fine for me.

Glad you like it 🙂

I don't see any reason not to. Would you mind submitting a PR? Please bump the version number as well so I can just accept and it's done.

Thanks!

  • Like 1

Share this post


Link to post
Share on other sites
25 minutes ago, adrian said:

I don't see any reason not to.

Using the fuctions API would be one reason 😛😉 

Share this post


Link to post
Share on other sites
14 minutes ago, bernhard said:

Using the fuctions API would be one reason 😛😉 

I guess that is a way around needing to make $pages available. I just haven't got into using it yet - maybe I should?

Share this post


Link to post
Share on other sites

I was not totally serious and it was more targeting @Jonathan Lahijani and meaning something like: Why not use the functions api when it is only one setting away and does not need any modifications to any module? $config->useFunctionsAPI = true, that's it.

22 minutes ago, adrian said:

I just haven't got into using it yet - maybe I should?

Of course that's everybody's own decision, but I also started using it quite late and now I really like it. It makes things easier, shorter and better to read:

  public function executeTransactions() {
    $form = $this->modules->get('InputfieldForm');

    $f = $this->modules->get('InputfieldRockGrid');
    $f->name = 'transactions';
    $f->label = 'Overview of Transactions';
    $f->ajax = true;
    $form->add($f);

    return $form->render();
  }

I also prefer the array syntax now for adding fields:

  public function executeEfforts() {
    $form = modules('InputfieldForm');

    $form->add([
      'type' => 'RockGrid',
      'name' => 'efforts',
      'label' => 'Overview of Efforts',
      'ajax' => true,
    ]);

    return $form->render();
  }

To answer your question: If you've ever come across errors like "$pages/$modules/$users is not defined" or the like I'd suggest trying it out 😉 

Using VSCode + Intelephense I also get very nice code completion, so I don't see any reason not to use it. Only thing bothering me is when I am developing public modules I need to remember not to use the functions API as I can't assume everybody is using it.
iKum9eU.png 

zobNtEC.png 

ynKTA8F.png that's a lot worse IMHO 🙂 

  • Like 1

Share this post


Link to post
Share on other sites
On 9/25/2018 at 7:24 PM, adrian said:

I just haven't got into using it yet - maybe I should?

Another snippet promoting the functions api 😉

GPgNvNY.png41JBNEU.png

  • Like 1

Share this post


Link to post
Share on other sites
On 1/19/2016 at 2:26 AM, adrian said:

Sorry it's taken so long to get to, but the latest version of the module now has a new config setting to optionally exclude pages outside the restricted branch from the search results of pages.

I tried the latest version of this wonderful module today: I activated the module's config setting "Restrict from search results". After that, I logged in with a user who is restricted to a certain branch and tried to find some pages outside the restricted branch.

I used the search box in the upper right corner of the PW admin and was surprised to see that results are found outside the restricted branch.

The quoted sentence above is related to a search by Lister, I guess?

Share this post


Link to post
Share on other sites

@titanium - this used to work as expected - ie these pages could not be found via the search. I just took a look and it appears that relatively recent changes to this live search feature means that now other results are returned, but note that the links from these results go to view the returned pages, rather than allowing the user to edit them. So I think the main functionality has not been compromised.

Remember that this module is not designed to control view permissions, just editing.

  • Thanks 1

Share this post


Link to post
Share on other sites
8 hours ago, adrian said:

I just took a look and it appears that relatively recent changes to this live search feature means that now other results are returned, but note that the links from these results go to view the returned pages, rather than allowing the user to edit them.

Yeah, these view links are not helpful IMO. Related request: https://github.com/processwire/processwire-requests/issues/274

Edit: just noticed that you already thumbs-upped that. 🙂

  • Like 1

Share this post


Link to post
Share on other sites

IMPORTANT NOTICE!!!

Hi everyone. I just discovered something pretty important with this module.

If you are using the Role Name or Custom PHP Code option and matching a page name or path, you MUST be sure to check "No Access" for the "If no match, give all access or no access?" setting. If you don't do this, a site editor could change the name of their branch parent, which would result in no match and hence they could gain access to the entire page tree.

I am tempted to actually remove these two matching options to improve security - is anyone using them?

To make life easier, I have added a new "Role Specified Branch Parent" option which allows you to select a branch parent on a per role basis, rather than per user. I think in most cases this is the best option to use.

image.png.3d9d6575574a6ba68e4c8487707c25cf.png

Please be sure to check your settings and if you can please switch to the "Role specified" or "User specified" options in the new version.

Let me know if you have any questions or concerns.

Thanks,
Adrian

  • Like 3

Share this post


Link to post
Share on other sites

Hi

is it normal that if I add two different roles (that have different restricted branch) to a user, when I log in I can see only one branch?

Share this post


Link to post
Share on other sites
43 minutes ago, nabo said:

Hi

is it normal that if I add two different roles (that have different restricted branch) to a user, when I log in I can see only one branch?

Yep, because of the way this module works, it can only limit to one branch of the tree. This limit affects all places where the user could view and/or edit pages outside that branch. If you want to give access to multiple branches you'll need a different approach - this thread might be helpful: 

 

  • Like 1

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 bernhard
      --- Please use RockFinder3 ---
    • By MoritzLost
      Cacheable Placeholders
      This module allows you to have pieces of dynamic content inside cached output. This aims to solve the common problem of having a mostly cacheable site, but with pieces of dynamic output here and there.  Consider this simple example, where you want to output a custom greeting to the current user:
      <h1>Good morning, <?= ucfirst($user->name) ?></h1> This snippet means you can't use the template cache (at least for logged-in users), because each user has a different name. Even if 99% of your output is static, you can only cache the pieces that you know won't include this personal greeting. A more common example would be CSRF tokens for HTML forms - those need to be unique by definition, so you can't cache the form wholesale.
      This module solves this problem by introducing cacheable placeholders - small placeholder tokens that get replaced during every request. The replacement is done inside a Page::render hook so it runs during every request, even if the response is served from the template cache. So you can use something like this:
      <h1>Good morning, {{{greeting}}}</h1> Replacement tokens are defined with a callback function that produces the appropriate output and added to the module through a simple hook:
      // site/ready.php wire()->addHookAfter('CachePlaceholders::getTokens', function (HookEvent $e) { $tokens = $e->return; $tokens['greeting'] = [ 'callback' => function (array $tokenData) { return ucfirst(wire('user')->name); } ]; $e->return = $tokens; }); Tokens can also include parameters that are parsed and passed to the callback function. There are more fully annotated examples and step-by-step instructions in the README on Github!
      Features
      A simple and fast token parser that calls the appropriate callback and runs automatically. Tokens may include multiple named or positional parameters, as well as multi-value parameters. A manual mode that allows you to replace tokens in custom pieces of cached content (useful if you're using the $cache API). Some built-in tokens for common use-cases: CSRF-Tokens, replacing values from superglobals and producing random hexadecimal strings. The token format is completely customizable, all delimiters can be changed to avoid collisions with existing tag parsers or template languages. Links
      Github Repository & documentation Module directory (pending approval) If you are interested in learning more, the README is very extensive, with more usage examples, code samples and usage instructions!
    • By Craig
      I've been using Fathom Analytics for a while now and on a growing number of sites, so thought it was about time there was a PW module for it.
      WayFathomAnalytics
      WayFathomAnalytics is a group of modules which will allow you to view your Fathom Analytics dashboard in the PW admin panel and (optionally) automatically add and configure the tracking code on front-end pages.
      Links
      GitHub Readme & documentation Download Zip Modules directory Module settings screenshot What is Fathom Analytics?
      Fathom Analytics is a simple, privacy-focused website analytics tool for bloggers and businesses.

      Stop scrolling through pages of reports and collecting gobs of personal data about your visitors, both of which you probably don't need. Fathom is a simple and private website analytics platform that lets you focus on what's important: your business.
      Privacy focused Fast-loading dashboards, all data is on a single screen Easy to get what you need, no training required Unlimited email reports Private or public dashboard sharing Cookie notices not required (it doesn't use cookies or collect personal data) Displays: top content, top referrers, top goals and more
    • By daniels
      This is a lightweight alternative to other newsletter & newsletter-subscription modules.
      You can find the Module in the Modules directory and on Github
      It can subscribe, update, unsubscribe & delete a user in a list in Mailchimp with MailChimp API 3.0. It does not provide any forms or validation, so you can feel free to use your own. To protect your users, it does not save any user data in logs or sends them to an admin.
      This module fits your needs if you...
      ...use Mailchimp as your newsletter / email-automation tool ...want to let users subscribe to your newsletter on your website ...want to use your own form, validation and messages (with or without the wire forms) ...don't want any personal user data saved in any way in your ProcessWire environment (cf. EU data regulation terms) ...like to subscribe, update, unsubscribe or delete users to/from different lists ...like the Mailchimp UI for creating / sending / reviewing email campaigns *I have only tested it with PHP 7.x so far, so use on owners risk
      EDIT:
      Since 0.0.4, instructions and changelog can be found in the README only. You can find it here  🙂
      If you have questions or like to contribute, just post a reply or create an issue or pr on github, thanks!
    • By MoritzLost
      Sorry for the convoluted title. I have a problem with Process modules that define a custom page using the page key through getModuleInfo (as demonstrated in this excellent tutorial by @bernhard). Those pages are created automatically when the module is installed. The problem is that the title of the page only gets set in the current language. That's not a problem if the current language (language of the superuser who is installing the module) is the default language; if it isn't, the Process page is missing a title in the default language. This has the very awkward effect that a user using the backend in the default language (or any other language) will see an empty entry in the setup menu:

      This screenshot comes from my Cache Control module which includes a Process page. Now I realize the description sounds obscure, but for us it's a common setup: We a multiple bilingual sites where the default language is German and the second language is English. While the clients use the CMS in German, as a developer I prefer the English interface, so whenever I install a Process module I get this problem.
      As a module author, is there a way to handle this situation? I guess it would be possible to use post-installation hooks or create the pages manually, but I very much prefer the declarative approach. The page title is already translatable (through the __ function), but of course at the time of installation there is no translation, and as far as I'm aware it's not possible to ship translations with a module so they are used automatically. Could this situation be handled better in the core? I would prefer if the module installation process would always set the title of the Process page in the default language, instead of the language of the current user.
×
×
  • Create New...