<![CDATA[ Programmatically assign Resource Group Access to User Group - My Forums]]> https://forums.modx.com/thread/?thread=98042 <![CDATA[Programmatically assign Resource Group Access to User Group]]> https://forums.modx.com/thread/98042/programmatically-assign-resource-group-access-to-user-group#dis-post-530204
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;
	}
]]>
audioroger Aug 18, 2015, 06:21 PM https://forums.modx.com/thread/98042/programmatically-assign-resource-group-access-to-user-group#dis-post-530204
<![CDATA[Re: Programmatically assign Resource Group Access to User Group]]> https://forums.modx.com/thread/98042/programmatically-assign-resource-group-access-to-user-group#dis-post-530257 ]]> BobRay Aug 20, 2015, 03:28 AM https://forums.modx.com/thread/98042/programmatically-assign-resource-group-access-to-user-group#dis-post-530257 <![CDATA[Re: Programmatically assign Resource Group Access to User Group]]> https://forums.modx.com/thread/98042/programmatically-assign-resource-group-access-to-user-group#dis-post-530226 audioroger Aug 19, 2015, 10:17 AM https://forums.modx.com/thread/98042/programmatically-assign-resource-group-access-to-user-group#dis-post-530226 <![CDATA[Re: Programmatically assign Resource Group Access to User Group (Best Answer)]]> https://forums.modx.com/thread/98042/programmatically-assign-resource-group-access-to-user-group#dis-post-530207
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.]]>
BobRay Aug 18, 2015, 11:14 PM https://forums.modx.com/thread/98042/programmatically-assign-resource-group-access-to-user-group#dis-post-530207