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

    I'm trying to setup some sort of friending functionality within MODx users. The idea is that each member will have a profile page (probably with some child pages), and you can't see their page until you are their friend. Ignoring the part where they actually get friended my idea API wise is this:

    - when user registers we create a user, a userGroup, a resourceGroup, and a resource
    - the user is added to the usergroup and the resource is added to the resourceGroup

    I've got that working - my problem is synching them up so if you are a member of the user group you can access resources in that resource group.

    It's the programatical equivalent of:

    Security -> access controls -> update user (user_1) -> resource group access -> add group (user_1)

    so far I've create a user and a user group, added the user to the group and passed the usergroup object to this snippet as 'thegroup':

    <?php
    //create resource group for this users pages
    $resourceGroup = $modx->newObject('modResourceGroup', array('name'=>$thegroup->get('name')));
    
    //create the page
    $object = $modx->newObject('modResource');
    $object->set('pagetitle', $thegroup->get('name'));
    $object->set('content', 'A New Profile Page.');
    $object->set('description', ' Profile Page');
    $object->set('published', 1);
    $object->set('parent', 17);
    $object->set('template', 5);
    $object->save();
    
    //Add page to resource group
    $object->joinGroup($resourceGroup);
    
    //Here's where I allow users in this group read access to this resource group...
    
    ?>



    Any advice would be greatly be appreciated.

    Cheers,

    Tom

    P.S - I'm a longtime user of Evolution and just made the switch to Revolution - loving it so far!
    • Now you just need to connect the User Group and Resource Group with a Resource Group Access record; i.e. you need to assign a policy describing the permissions the user group has to that resource group. This only needs to be done once when creating the Resource Group, and in your case, you likely need to create it with the Load, List, and View policy (and with an Authority of 9999).
        • 38285
        • 17 Posts
        Thanks opengeek. After a bit of trial and error I've got some working code:

        $resourceGroupAccess = $modx->newObject('modAccessResourceGroup');
        $resourceGroupAccess->set('target', $resourceGroup->get('id'));
        $resourceGroupAccess->set('principal_class', 'modUserGroup');
        $resourceGroupAccess->set('principal', $thegroup->get('id'));
        $resourceGroupAccess->set('authority', 9999);
        $resourceGroupAccess->set('policy', 4 ); // 4 for Load, List and View
        $resourceGroupAccess->set('context_key', 'web');
        $resourceGroupAccess->save();


        That did the trick.