We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38783
    • 571 Posts
    I wish to display different content on a resource depending on whether the user is logged in and the creator of the resource, or any other site visitor, logged in or not. ie:


    • If user is logged in and the creator of the resource they see Text A
    • If user is an anonymous website visitor, or a logged in user who is not the creator of the resource they see Text B

    I have written this snippet which seems to work as I would like it to, but I suspect that I should be able to do this more easily and/or securely using the Personalize snippet: https://modx.com/extras/package/personalize

    I am concerned that my code might be either insecure or inefficient compared to Personalize.

    However Personalize does not appear to offer this without modification and don't seem to be able to modify it in a way that doesn't break it altogether!

    Does anyone have any advice they can offer regarding this?

    <?php
    
    /* get id of user resource 'created by' */
    $createdBy = $modx->resource->getOne('CreatedBy');
    $resourceCreatedBy = $createdBy->get('id');
    
    /* get id of current Logged In User */
    $user = $modx->getUser();
    $userid = $user->get('id');
    
    /* If the CreatedBy ID and Logged In User id match then display Text A, otherwise display Text B */
    if (($userid == $resourceCreatedBy))
    { 
       echo "user id and CreatedBy match so display Text A";
    }
    else 
    {
       echo "user id and CreatedBy do not match so display Text B";
    }

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

      If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

      email: [email protected] | website: https://andytough.com
      • 17301
      • 932 Posts
      I don't personally see anything wrong with the code you've written. My only suggestion would be to use chunks instead or echos if its a lengthy content change
        ■ email: [email protected] | ■ website: https://alienbuild.uk

        The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
        • 38783
        • 571 Posts
        Thanks Liam. I am still quite new to PHP and am constantly worried that I might know just enough to be dangerous!
          If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

          email: [email protected] | website: https://andytough.com
        • discuss.answer
          • 4172
          • 5,888 Posts
          this should also work:

          if ($modx->resource->get('createdby') == $modx->user->get('id')){
              $output = $modx->getChunk($ownerchunk);
          } else {
             $output = $modx->getChunk($elsechunk);
          }
          
          return $output;
          
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 38783
            • 571 Posts
            That looks much more elegant! Thank you Bruno.
              If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

              email: [email protected] | website: https://andytough.com