We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14536
    • 22 Posts
    I’ve been trying to figure out how to direct a user to a specific access denied page based on the page they are coming from. There are multiple templates in use for various private sections of the site I am working on, and I want the template of the denied page to match the page they are coming from, like this:

    • private a -> access denied a
    • private b -> access denied b

    Is there a way to get this information from $modx, or any ideas of the best way to go about it?
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You could use the OnPageUnauthorized event to specify which page to redirect to in a plugin. The plugin code would just need to set (re-set) the $modx->config[’unauthorized_page’] value to the page you want, depending on the page being denied. For example, you could check the $modx->documentObject[’template’] which will hold the template id of the page being processed (the one being denied) and set the unauthorized_page value accordingly.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 14536
        • 22 Posts
        Oooh, very nice. Thanks!
          • 14536
          • 22 Posts
          Here’s what I’ve got... and it isn’t working. Thoughts?

          <?
          global $modx;
          switch($modx->documentObject['template']){
          	case 0:
          		$modx->config['unauthorized_page'] = 8;
          	break;
          	case 3:
          		$modx->config['unauthorized_page'] = 7;
          	break;
          	default:
          		$modx->config['unauthorized_page'] = 8;
          	break;
          }
          ?>
            • 14536
            • 22 Posts
            I’ve tried it with multiple system events as triggers, and none seems to work... Am I missing something?
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              No, I am. The documentObject is not yet available. This will take a query to determine the document’s template. I’m working with it; should have a resolution shortly.
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                This does the job, using the OnPageUnauthorized event:
                $rs = $modx->db->select('template', $modx->getFullTableName('site_content'), 'id = ' . $modx->documentIdentifier);
                $tpl = $modx->db->getValue($rs);
                if($tpl == 3) {
                    $modx->config['unauthorized_page'] = 5; 
                }
                

                adjust the if (or make a switch out of it) as desired.
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                  • 14536
                  • 22 Posts
                  Thanks so much! I’m all kinds of new to plugins, this was super helpful.
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    I need to add this to my chapter on plugins. http://www.sottwell.com/how-plugins-work.html
                      Studying MODX in the desert - http://sottwell.com
                      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                      Join the Slack Community - http://modx.org
                      • 14536
                      • 22 Posts
                      I’ve been playing with this and it doesn’t seem to be working for me... What is the best way to debug plugins?