We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 49499
    • 48 Posts
    I've run through the examples for Resource Groups / Members Only Pages and would like to set up something a little different. Rather than rejecting access to the entire resource, I would rather restrict specific content on the page, or display alternate content on the page based upon access.

    For example, if I had a placeholder that told me whether someone should have access to the page, I might get something like this:
    [[+has_access:is=`1`:then=`
        Show the premium content.
    `:else=`
        Sorry, you need to purchase this content first. Here's a form, etc.
    `]]
    


    My guess is that I should give anonymous users access to the Resource Group and then control the content via templating on the front end, however I'm not sure how to access the Resource Group from the front-end to show or hide content.

    Any suggestions?

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

      • 49499
      • 48 Posts
      Still working out the details, but here is what I think will work conceptually:

      • Make the Resource Group accessible by anonymous.
      • Write a snippet that retrieves all Resource Groups for the current Resource. (something like this: http://forums.modx.com/index.php?action=thread&thread=60107&i=1)
      • Use the modUser.isMember method to determine if user is a member of any of the Resource Groups for that Resource.
      • If true, show premium content. If false, show alternate content.

      I'm open to suggestions if there is a better way to do this... Thank you!
        • 3749
        • 24,544 Posts
        Check out the &allowedGroups property here.

        That will show one chunk to logged-in users that belong to the right group(s) and another chunk to everyone else.
          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
        • discuss.answer
          • 49499
          • 48 Posts
          I ended up writing a snippet called get_user_access:

          // Set Properties
          $current_resource_id = $modx->resource->get('id');  // Gets the current Resource ID
          $target_resource_id = $modx->getOption('id', $scriptProperties, $current_resource_id); // If specified, overrides the current Resource ID
          $target_resource = $modx->getObject('modResource',$target_resource_id); // Gets the Resource object of the targeted Resource ID
          
          $resourceGroups = array();
          $hasAccess = true;
          
          foreach($target_resource->getGroupsList() as $resourceGroupsList){
          	if(is_array($resourceGroupsList)) {
          		foreach ($resourceGroupsList as $resourceGroup) {
          			$resourceGroupArray = $resourceGroup->toArray();
          
          			if(is_array($resourceGroup->toArray())) {
          				if ($resourceGroup->get('access')=='1') {
          					foreach ($resourceGroup->toArray() as $key => $value) {
          						if ($key == 'name') {
          							$resourceGroups[] = $value;
          						}
          					}
          
          
          				}
          			}
          		}
          	}
          }
          
          if (!empty($resourceGroups)) {
          	$user = $modx->getUser();
          	$hasAccess = $user->isMember($resourceGroups);
          }
          
          return $hasAccess;


          You could probably output to a placeholder if you need to. I am outputting the value directly, in case I need to use it within a loop or in reference to a specific page.

          [[!get_user_access]]

          Will return true (or 1) if user has access to current resource.
          Will return false (or blank) if user does not have access to current resource.

          [[!get_user_access? &id=`24`]]

          Will return true (or 1) if user has access to Resource 24.
          Will return false (or blank) if user does not have access to Resource 24.

          What was weird to me, was that getGroupsList returned every Resource Group in the system. I had to loop through the entire collection to find the one with access==1. I'm sure there's a better way to get the targeted resource groups without looping through everything, but that's what I was able to come up with. Hope this helps somebody.

          NOTE: This only actually works if the user group and resource group have the same name (which is the case for me most of the time). [ed. note: patrickappelman last edited this post 8 years, 3 months ago.]