Hi, I have a plugin that adds a resource to a new resource group, a user from login.register to a new user group and the resource to the user group. Works fine. But I also need to add the resource to the anonymous user group so that users who are not logged-in can view the resources. When I try to add this part to the plugin it throws an error.
This is (part of) the plugin that works:
$resourceGroupAccess = $modx->newObject('modAccessResourceGroup');
$newResourcePolicy = $modx->getObject('modAccessPolicy',array('name' => 'Resource'));
$resourceGroupAccess->set('target', $resourceGroup->get('id'));
$resourceGroupAccess->set('principal_class','modUserGroup');
$resourceGroupAccess->set('principal',$userGroup->get('id'));
$resourceGroupAccess->set('authority', 9);
$resourceGroupAccess->set('policy',$newResourcePolicy->get('id'));
$resourceGroupAccess->set('context_key', 'web');
$resourceGroupAccess->save();
When I change it to add the resource to anonymous, via line 2, I get an error (with or without brackets around anonymous in line 2):
$resourceGroupAccess = $modx->newObject('modAccessResourceGroup');
$newGroup = $modx->getObject('modUserGroup',array('name' => 'anonymous' ));
$resourceGroupAccess->set('target', $resourceGroup->get('id'));
$resourceGroupAccess->set('principal_class','modUserGroup');
$resourceGroupAccess->set('principal',$newGroup->get('id'));
$resourceGroupAccess->set('authority', 9999);
$resourceGroupAccess->set('policy', 4);
$resourceGroupAccess->set('context_key', 'web');
$resourceGroupAccess->save();
I don't know if the error's caused because it can't be done programmatically (can't see why not) or because my code is incorrect (very likely I admit). Ideally, I want the resource added to both user groups at the same time of course, but without getting the anonymous add to work on its own I'm struggling. Can anyone help?