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

    I've written a plugin fires onDocFormSave and creates a resource group and usergroup with the name of the saved resource.

    However, I am still having to go into the usergroup and manually update the Resource Group access (see attachment). Is there a way to do this programmatically? I imagine it's in the modUerGroup array, but I can't figure out how to find what fields to set.

    Here's my current plugin:
    <?php
      $parent = $resource->get('parent');
    
    	if($parent == 1170 && $mode == 'new'){
        //set variables
        $page_id = $resource->get('id');  	
        $page_title = $resource->get('pagetitle');
    
        //create new resource group
      	$newResourceGroup = $modx->newObject('modResourceGroup',array('name'=>$page_title,'private_memgroup'=>0,'private_webgroup'=>0));
        if(!$newResourceGroup->save()){
    			return "Unable to save the Resource Group. The Group may already exist.";	
    		}
    
    		//add current resource to new resource group
    		$newResourceGroupId = $newResourceGroup->get('id');
    		$resource->joinGroup($newResourceGroupId);
    		if(!$resource->save()){
    			return "Unable to add to the User Group.";
    		}
    
        //create new usergroup
    		$newUserGroup = $modx->newObject('modUserGroup',array('name' => $page_title));
    		if($newUserGroup->save()){
      		$modx->log(MODX_LOG_LEVEL_ERROR, 'User group created.' );
    			return true;
    		}else{
    			return "Unable to save the User Group. The Group may already exist.";
    		}
    	}else{
    		return true;
    	}
    

    This question has been answered by BobRay. See the first response.

    • discuss.answer
      • 3749
      • 24,544 Posts
      I think what you want is to create a modAccessResourceGroup object.

      Here are the fields to set (I think I have these right, but I'm not positive)

            target (string) - ID of resource group
            principal_class (string) - 'modUserGroup'
            principal (integer) - ID of User Group
            authority (integer) - 9999
            policy (integer) - ID of policy
            context_key  (string) - 'web' // context of resources
      

      If it doesn't work at first, keep an eye on the modx_access_resource_groups table. You may get some spurious entries there until you get it right.

      Let me know if it works and I'll do a blog post on it.
        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
        • 46718
        • 39 Posts
        Great, thanks Bob!
          • 3749
          • 24,544 Posts
          My pleasure. smiley
            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