We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9439
    • 90 Posts
    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?
      David Heriot
      pfsmedia
      • 22303 MODX Staff
      • 10,725 Posts
      There is no anonymous User Group. The Resource Group ACL records have a principal value of 0 indicating that they apply to anonymous users...
        • 9439
        • 90 Posts
        Wow! Thanks opengeek. Wished I'd asked sooner. Perfect.
          David Heriot
          pfsmedia
          • 9439
          • 90 Posts
          So for reference, the anonymous section becomes:
          $resourceGroupAccess = $modx->newObject('modAccessResourceGroup');
          $resourceGroupAccess->set('target', $resourceGroup->get('id'));
          $resourceGroupAccess->set('principal_class','modUserGroup');
          $resourceGroupAccess->set('principal',0);
          $resourceGroupAccess->set('authority', 9999);
          $resourceGroupAccess->set('policy', 4);
          $resourceGroupAccess->set('context_key', 'web');
          $resourceGroupAccess->save();
          
            David Heriot
            pfsmedia