We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 41055
    • 24 Posts
    I am trying to set up a user manager that would restrict the user to a specific parent. My file structure looks like this:
    commercial properties
    - property 1
    -unit 1
    -unit 2
    - property 1
    -unit 1
    -unit 2
    residential properties
    - property 1
    -unit 1
    -unit 2
    - property 1
    -unit 1
    -unit 2

    I followed http://bobsguides.com/user-specific-pages.html and created my user, my user group and my resource group. I added my user to my user group, added a AllDocs group for the manager and hid all the content from my user. I added the residential property "folder" to one of my users in the resource group.

    I gave my user access to the mgr and the web, set their role to a custom role with a permission of 15.

    First, if within the user manager, I try to change the template, I get the following: Notice: Undefined index: resource_groups in manager/controllers/default/resource/create.class.php on line 112

    Notice: Undefined index: resource_groups in manager/controllers/default/resource/create.class.php on line 112

    That doesn't break the process but is far from desirable. I can ignore that and go on. However, when I create a resource within the allowed folder, it saves the resource but throws

    Notice: Undefined index: pagetitle in /manager/controllers/default/resource/update.class.php on line 206
    and returns Error!
    Resource not found with ID: 77

    So, I guess I could hide the notice using PHP, even if that's not ideal. However, I must be missing something because the resource created within a group group doesn't show within that resource group but withing the DocAll resource group. It will prevent me from saving in folders that aren't defined within the resource group permissions, so limitations are in place, but I simply cannot figure out how to allow the user to see their own resources.


    One last thing: if I just duplicate a resource, I can see it fine.
    Thanks in advance! [ed. note: helene last edited this post 14 years, 1 month ago.]
      • 3749
      • 24,544 Posts
      Do you have the current version of MODX? I think those undefined index warnings may have been fixed.

      When an E_NOTICE warning is thrown during a plugin, MODX tends to go a little nuts, so upgrading MODX or turning off E_NOTICE error_reporting might (or might not) solve the problem.

      Let me know if that doesn't work.


      ------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using.
      MODX info for everyone: http://bobsguides.com/modx.html
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 41055
        • 24 Posts
        Hey Bob, thanks for the quick answer. Sorry, I forgot to mention the version. I am using 2.2.4-pl (traditional) - I've double checked.
          • 41055
          • 24 Posts
          I've made progress with the new resources not showing in for the user when they create it with their user group and resource group.

          It appears that when the user saves from within their resource group, Modx places the resource in the AllDocs resource group but not within the user's resource group, making it impossible for the user to view the resource they just created.

          Is there a way to enable the user to save his newly created resource in his resource group automatically? The only way I have found so far, playing with ACLs, was to give them admin rights and manually select which resource group the resource should go to. Did I miss something in setting up the permissions? [ed. note: helene last edited this post 14 years, 1 month ago.]
            • 3749
            • 24,544 Posts
            No, you didn't miss anything. I think the user has to select the resource group before saving. The only alternative would be another plugin (or an addition to the DefaultResourceGroup plugin) that checked to see if the resource was under the "Members" directory and, if so, automatically added the Resource to the user's Resource Group.

            If the $user object is available in the plugin, it would be fairly simple, but if not, it would be impossible.

            It would look something like this (untested, off the top of my head, and probably not accurate), it assumes that the user group is named after the username:

            <?php
            $membersFolderId = 12;
            $ancestors = $modx->getParentIds( (integer) $resource->get('id'));
            
            if( in_array($membersFolderId, $ancestors)) {
                $username = $user->get('username'); // might need to be $modx->user
                $resource->joinGroup($userName);
            }



            ------------------------------------------------------------------------------------------
            PLEASE, PLEASE specify the version of MODX you are using.
            MODX info for everyone: http://bobsguides.com/modx.html

            [ed. note: BobRay last edited this post 14 years, 1 month ago.]
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 41055
              • 24 Posts
              Pardon my ignorance, I have no experience at all with plugins. I am unable to implement the plugin you described.

              I've tried with
              $membersFolderId = 14;
              $ancestors = $modx->getParentIds( $resource->get('id'));
               
              if( in_array($membersFolderId, $ancestors)) {
                  $username = $user->get('username'); // might need to be $modx->user
                  $resource->joinGroup($userName);
              }
              


              For $ancestors, I suppose I want the current's page parents id? If so, in this case, it would be 35. I have tried with $ancestors = $modx->getParentIds(35); but that didn't work either.

              I also tried with $modx->user.
              My user resource folder is 14, username and resource are the same. I set up the plugin on OnDocFormSave but after playing with it, it either hangs up upon saving or simply doesn't work.

              I also need to retrieve the ID of the resource group to be pulled dynamically, since I will have a large amount of users, I would prefer not to create 50 plugins for 50 users. Is that possible? I found this old post, but was not successful in making it work following that example as well.

              https://forums.modx.com/index.php?topic=60107.0 [ed. note: helene last edited this post 14 years, 1 month ago.]
                • 3749
                • 24,544 Posts
                Sorry it didn't work for you helene. You would definitely need only one plugin.

                I did a little testing and $modx->user is available, but for some reason getParentIds() doesn't work in that location. I had to write my own recursive version of it. I also added some code to create the user group and resource group if they don't exist and then add the user and resource to them. I've tested this and it seems to work, but you'll still have to create the Resource Group ACL entry for each User Group to give the users permission to access their documents (someday, I'll put that in the plugin). See the section here on Resource Group Access ACL entries: http://bobsguides.com/creating-a-new-acl-entry.html

                $membersFolderId should be set to the Resource ID of a folder that is an ancestor of all member pages, but not any non member pages.

                <?php
                /* @var $resource modResource */
                /* @var $modx modX */
                /* @var $mode int */
                
                $membersFolderId = 12;
                
                /* If there are users that already have resources, 
                   comment out the following line and the plugin will
                   work for them when they edit their existing 
                   resources. Once all the Users and Resources
                   are in their groups, you can uncomment it 
                   to make things a little faster */
                
                if ($mode != modSystemEvent::MODE_NEW) return;
                
                function getAncestors($id, &$ancestors) {
                    /* @var $p modResource */
                    global $modx;
                    if (!empty($id)) {
                        $p = $modx->getObject('modResource', $id);
                        if ($p) {
                            $parentId = $p->get('parent');
                            $ancestors[] = $id;
                            getAncestors($parentId, $ancestors);
                        }
                        else {
                            $ancestors[] = 'failed to get parent';
                        }
                    }
                    return;
                }
                
                $docId = (integer)$resource->get('id');
                $ancestors = array();
                
                $userName = $modx->user->get('username');
                $docId = $resource->get('parent');
                getAncestors($docId, $ancestors);
                if (!in_array($docId, $ancestors)) {
                    return '';
                }
                
                $group = $modx->getObject('modResourceGroup', array('name' => $userName));
                
                /* create resource group if it doesn't exist */
                if (empty($group)) {
                    /* @var $group modUserGroup */
                    $group = $modx->newObject('modResourceGroup');
                    if ($group) {
                        $group->set('name', $userName);
                        $group->save();
                    }
                }
                /* add Resource to Resource Group */
                $resource->joinGroup($userName);
                
                
                $group = $modx->getObject('modUserGroup', array('name' => $userName));
                
                /* create user's user group if it doesn't exist */
                if (empty($group)) {
                    $group = $modx->newObject('modUserGroup');
                    if ($group) {
                        $group->set('name', $userName);
                        $group->save();
                    }
                }
                /* Add user to group if not already a member */
                if (!$modx->user->isMember($userName)) {
                    $modx->user->joinGroup($userName);
                }
                $group = $modx->getObject('modUserGroup', array(
                                                               'name' => 'AllMembers'));
                
                return '';
                



                ------------------------------------------------------------------------------------------
                PLEASE, PLEASE specify the version of MODX you are using.
                MODX info for everyone: http://bobsguides.com/modx.html [ed. note: BobRay last edited this post 14 years, 1 month ago.]
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 41055
                  • 24 Posts
                  Thank you, Bob, for all your help. It worked wonderfully.

                  I changed my file structure a bit to allow the users under one parent as opposed to two as I had it and I was able to create resources under each user automatically!

                  I will simply hide any modification possible to the top parent since I need the user to save under it, without the ability to modify it (hiding it doesn't allow to save under that parent), but that will be easy with from customization, I am sure!

                  Thanks again for your hard work, time and tutorials, couldn't do it without you!
                    • 3749
                    • 24,544 Posts
                    I think this will do what you want, but I'm not positive. Install the DefaultUserGroup plugin. Set it to assign the User Group 'Members' and a Role of 'Member' (IIRC, those are the defaults). Create the 'Member' Role and the 'Members' User Group. Then:

                    1. Take the top parent out of any Member Resource Groups

                    2. Put it in its own Resource Group

                    3. Connect that Resource Group to the Administrator User Group with a Resource Group Access ACL entry with a Policy of Resource and a minimum role of admin Super User

                    4. Connect that Resource Group to the Members User Group group with a Resource Group Access ACL entry with a Policy that does not have save and edit permissions (with a minimum role of Member).

                    You'll have to put existing users in the Members User Group with a Roll of Member.


                    ------------------------------------------------------------------------------------------
                    PLEASE, PLEASE specify the version of MODX you are using.
                    MODX info for everyone: http://bobsguides.com/modx.html
                    [ed. note: BobRay last edited this post 14 years, 1 month ago.]
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting
                      • 41055
                      • 24 Posts
                      I already had a default Member role (authority 9999), so I am using that.

                      That works to limit access to the parent but then users do not have access to the children, they can see them but not save under the parent.

                      So, to summarize:
                      - Top parent (not editable)
                      - User created child - cannot be saved (no permissions to create under Top parent)
                      - User Created child of a a child - can be saved but not viewed

                      When saved, the child of a child goes to the right resource group but is not viewable by that User Resource Group. Also, not sure why, but I cannot view my updated pages. I get a white page when I change a template, or reload the page. I have E_Error disabled to remove the warnings on the create.class.php and update.class.php


                      Edit: I've been able to allow the update of a page, probably by disabling some snippets or unused plugins, not sure how it got restricted but it's working again. Only thing now is to create a child under the top parent, which is not possible with a load and view only.
                      [ed. note: helene last edited this post 14 years, 1 month ago.]