We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 47556
    • 1 Posts
    Hello,

    i've been trying to create a user who have only rights to create and edit resources under one resource (res3).

    web
    -> res1
    -> res2
    ----> res2.1
    -> res3
    ----> res3.1
    ----> res3.2
    -> res4


    Is this possible?
      • 4172
      • 5,888 Posts
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 13218
        • 134 Posts
        Hi. And welcome to the forums.

        There are, as with most things MODX, several ways to achieve what you want.
        One official way would probably be to create a User Group, add the User to that User Group. Then create a Resource Group and add all resources to it, but not res3. Add Resource Group to User Group and give Resource Group an Access Policy where add_children is not allowed.

        This is just off the top of my head, but should work with a little tweaking. But sounds rather complicated, doesn't it? Well, it is, kind of. But very powerful, once you get the hang of it (like i am still trying very hard). If you intend to work with MODX regularly, i recommend you read some tutorials on it and give the whole Access Controls thing a try.


        Meanwhile there is another quick-and-dirty way to do what you want:
        - create a new Plugin
        - under System Events enable the row OnBeforeDocFormSave
        - put following code in 'Plugin code (PHP)'
        // enter here the ID of res3
        $allowedParentResourceId = 123;
        
        // checking if a new resource is being created
        // (other value would be 'upd' for update
        if( $mode === 'new' ) {
        
           // if new parent ID is not res3...
           if( $_REQUEST['parent'] != $allowedParentResourceId ) {
        
            // ...send error message to User, 
            // which also prevents resource from being saved
            $modx->event->output('You cannot create a document here!');
          }
        }


        I think this should do it for your example. But mind that if you wanted anything more complex, Access Controls would probably be the better choice.

        Edit:
        this would of course prevent any user, also the admin, to create any resource anywhere but under res3. If it is a very static website, maybe it would even be ok to just deactivate the plugin, if another resource needs to be created. If not, you would still need to create a User Group, add that User to it and check in the code, if active user belongs to the User Group. [ed. note: itWillBeOk last edited this post 12 years, 5 months ago.]
          @itWilllBeOK
          • 3749
          • 24,544 Posts
          A couple of suggestions for the plugin:

          // enter here the ID of res3
          $allowedParentResourceId = 123;
           
          // checking if a new resource is being created
          // (other value would be modSystemEvent::MODE_UPD for update
          if( $mode === modSystemEvent::MODE_NEW ) {  /* It's now a constant */
           
             // if new parent ID is not res3...
             if( $resource->get('parent') != $allowedParentResourceId ) { /* more future-proof */
           
              // ...send error message to User,
              // which also prevents resource from being saved
              $modx->event->output('You cannot create a document here!');
            }
          }


          I'm not sure if the last line will work properly or not.
            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
            • 13218
            • 134 Posts
            Hi Bob,
            i didn't know you could do the get() thing, since it is a new resource and OnBeforeDocSave. Much nicer though, if it works.

            I did try that last line recently on a 2.2.14 install and it worked fine. RTFM says, you need to return something != null, but it didn't seem to be neccessary. It just was outputted to the error log.
              @itWilllBeOK
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              I'm pretty sure that the onBefore...Save events supply an object containing the new values that will get saved. That way you can validate and modify them before they actually get saved.
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 13218
                • 134 Posts
                Ok, tested it.

                The code as Bob updated it, of course works fine.
                And sending an error message via $modx->event->output() actually prevents the resource from being saved.
                  @itWilllBeOK
                  • 3749
                  • 24,544 Posts
                  I'm glad that worked. I've had mixed success with returning error messages from a plugin. It depends on the event. For many events, the return value is ignored.
                    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
                    • 13218
                    • 134 Posts
                    You are right, it works with this event, but not all events.

                    For this plugin the event OnDocFormPrerender could probably be better, because you wouldn't even get to enter all your data before then being told you can't save the page.
                    But I don't know if you could stop the form from rendering with OnDocFormPrerender.
                      @itWilllBeOK
                      • 3749
                      • 24,544 Posts
                      AFAIK, anything returned from OnDocFormPrerender gets appended to the Create/Edit Resource form. You could redirect the user to another page, though.
                        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