We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19328
    • 433 Posts
    The tree_root_id setting is great for easily giving a user access to just a part of the resource tree, but it would be nice if this could be a setting for an entire usergroup or even better for a role. But as this is not possible (as far as i can see), I thought it might be a good idea to create a plugin that automatically sets this setting when a user is added to certain usergroup. How would I do this?

    Maybe I could set the plugin on the OnBeforeUserFormSave event, but I have no idea how I would then check in which usergroup the user is placed, and if it's that certain group, set the tree_root_id.

    Would this even be possible? If someone could give me an example of the way to check and access those things, I would really appreciate it.
      • 33968
      • 863 Posts
      Hi Michelle

      As far as I know you can't set this on a per-user basis, but you could set it dynamically in your plugin like this:
      <?php
      $restrictedGroup = 3; // change to usergroup id
      $rootId = 6; // set to root id you want to limit to
      
      $isRestricted = $modx->getObject('modUserGroupMember',array(
          'user_group' => $restrictedGroup,
          'member' => $modx->user->get('id'),
      ));
      
      if($isRestricted) {
          $modx->setOption('tree_root_id',$rootId);
      }
      

      That will override the stored value of tree_root_id, only if the current user is a member of the usergroup you have specified.

      It would need to fire on the OnManagerPageInit event.
        • 19328
        • 433 Posts
        Thanks Lucas for helping me out! This looks like it's exactly what I'm looking for, setting the tree root id for an entire usergroup. I'm going to give it a try!