We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36760
    • 136 Posts
    I have a subscription-based website I am helping someone work on, and they asked if it would be possible to have article previews on member only pages that would show instead of the full article to non-members and to members in the free account groups.

    Right now if you try to access a member only page without the correct permissions, you just get the Unauthorized page.

    The only thread I was able to find about this topic is here http://forums.modx.com/thread/6668/article-previews, but it's over 4 years old. So I thought it might be worth exploring again.

    My PHP knowledge is limited, but I was thinking I might try to adapt the snippet I was given in this thread: http://forums.modx.com/thread/28489/hiding-chunks-based-on-a-user-s-associated-groups to show or hide the introtext field and content fields based on user groups, assuming I set all of the page permissions to be public.

    Though before I spend too much time on it, I figured I would ask. Just in case there is a better method now, or if there really isn't any method at all.

    Thanks! [ed. note: firebot6 last edited this post 12 years, 7 months ago.]
      • 3749
      • 24,544 Posts
      I think the simplest method is to unprotect the pages in the front end (remove any Resource Group Access ACL entries with a context of 'web') and use a fairly simple snippet to present the content you want to show.

      This, for example, would show the summary field to users who are not logged in and the content field to those who are. It assumes that anyone who is logged in is a member.

      First, in the page template, replace this:

      [[*content]]


      with this:

      [[!ShowContent]]


      Then create a snippet called ShowContent with this code (untested):

      <?php
      /* ShowContent snippet */
      if ($modx->user->hasSessionContext('web')) {
         $output = $modx->resource->getContent();
      } else {
         $output = $modx->resource->get('introtext');
         /* replace 99 with the ID of the "become a member" page */
         $output .= '
      <a href="[[~99]]>Become a member to see more</a>';
      }
      return $output;
        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
        • 36760
        • 136 Posts
        That's working as you said it would.

        To make it show the full content based on user group (then show the preview to anyone not logged in or not in the user groups listed), would I change "hasSessionContext" to "isMember" ? Then add a list of user groups instead of web. Or, is there more to it than that?

        So it would look something like this:

        <?php
        /* ShowContent snippet */
        if ($modx->user->isMember('members-free,members-paid')) {
           $output = $modx->resource->getContent();
        } else {
           $output = $modx->resource->get('introtext');
           /* replace 99 with the ID of the "become a member" page */
           $output .= '
        <a href="[[~99]]">Become a member to see more</a>';
        }
        return $output;


        This is actually part of the same project you helped me with for the hiding/showing chunks. So I appreciate your continued help! And on a side note, I'm looking forward to getting your Modx book smiley
          • 3749
          • 24,544 Posts
          Yes, that looks right, although you wouldn't need the change unless there are logged-in members who aren't in either group or you want to show different content to different groups.
            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
            • 36760
            • 136 Posts
            Yeah, I have several other member groups besides those two, so being able to set it based on user group will be beneficial. At least with what I'm thinking right now.

            With the modified code I gave above, when I login as a test user in the members-free group on a separate browser I still see the introtext content, and not the full page content as intended.

            Any ideas?

            I tried clearing the cache and flushing permissions, but with no luck.
              • 3749
              • 24,544 Posts
              Sorry, I forgot that you need to use an array for multiple groups:

              <?php
              $groups = array(
                  'members-paid',
                  'members-free',
              );
              
              if ($modx->user->isMember($groups)) {
              }
              


              BTW, if you want to send the group names as a snippet property and/or put the "please join" stuff in a chunk:

              [[snippetName? &groups=`members-free,members-paid`]]


              <?php
              $groups = explode(',', $scriptProperties['groups']);
              
              if ($modx->user->isMember($groups)) {
                 $output =  $modx->resource->getContent();
              } else {
                 $output = $modx->getChunk('join');
              }
              
              return $output;
              

              [ed. note: BobRay last edited this post 12 years, 7 months ago.]
                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
                • 36760
                • 136 Posts
                Thank you, that did the trick. If I think of anything else crazy I want to do with it that I can't figure out, I'll check back.

                Just for the sake of sharing, in case anyone else looks through here, this is what I ended up with:

                <?php
                $groups = array(
                    'members-paid',
                    'members-free',
                );
                 
                if ($modx->user->isMember($groups)) {
                   $output =  $modx->resource->getContent();
                } else {
                   $output = $modx->resource->get('introtext');
                   $output .= $modx->getChunk(joinChunk);
                }
                 
                return $output;
                  • 3749
                  • 24,544 Posts
                  I'm glad it worked. Thanks for reporting back. smiley
                    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
                    • 36760
                    • 136 Posts
                    Okay, I've been trying to build off of this some. What I want to happen is to redirect users who are not logged in to the Unauthorized page.

                    The snippet below works, in that it displays nothing on the page when the user isn't logged in and displays the correct content based on the other user groups. I'm just not sure what the code is to redirect, and I've been unable to find it (is there a resource that has snippet related things like that listed out?).

                    Here's what I have (I shortened my groups array just for the sake of readability):

                    <?php
                    $groups = array(
                    'group1',
                    'group2'
                    );
                     
                    if ($modx->user->isMember($groups)) {
                       $output =  $modx->resource->getContent();
                    } elseif ($modx->user->isMember('members-free') ){
                       $output = $modx->resource->get('introtext');
                       $output .= $modx->getChunk(joinChunk);
                    } else {
                       /*redirect to Unauthorized page if they are not logged in on any account*/
                    }
                    
                    return $output;


                    Thanks for any help!
                      • 36760
                      • 136 Posts
                      Nevermind, I found it! Unless there's a better way to do it.

                      <?php
                      $groups = array(
                      'group1',
                      'group2'
                      );
                        
                      if ($modx->user->isMember($groups)) {
                         $output =  $modx->resource->getContent();
                      } elseif ($modx->user->isMember('members-free') ){
                         $output = $modx->resource->get('introtext');
                         $output .= $modx->getChunk(joinChunk);
                      } else {
                        $modx->sendRedirect('614');
                      }
                       
                      return $output;