We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • discuss.answer
    • 10405
    • 288 Posts
    Quote from: BobRay at Sep 27, 2016, 02:34 PM
    Michael's method is probably a better choice, but I think it would also work to protect the page with a snippet rather than access control.The snippet would forward unauthorized users to the login page. I think the redirect to prior would then work.
    Thanks, well I gave Load access to the Anonymous group, and have set up a basic snippet on one of the pages as a test, but when I browse to it (thinking it would forward me to the Login page as I'm not logged in) it just brings up a blank page.

    Probably something simple that's wrong with the snippet but not sure what. This is the snippet I added to the top of the page (where 10 is the ID of the login page):

    if (!$modx->user->hasSessionContext($modx->context->get('key'))) 
    	{
    		$modx->sendForward(10);
    	}
    


    Update - got it working for this page- but not sure if this snippet is the best method, so any other ideas welcome. The RedirectToPrior still doesn't work though unfortunately- after login I'm just sent to the home page of the site. [ed. note: galahad5 last edited this post 7 years, 6 months ago.]
      • 10405
      • 288 Posts
      Quote from: m.engel at Sep 19, 2016, 12:01 PM
      This is the default behavior when configured correctly.

      To get this:

      anonymous Users need to have "Load" priviliges to this resource group. So When you click em, you get an 301 Unauthorized instead of an 404 Not Found.
      You need to configure the unauthorized_page to your Login-Page
      The Login Page Form, should not have an action
      The Login Snippet should have no loginResourceId

      Then you just Link to the member Pages, or users click them out of their favorites, history, etc.
      Modx uses an sendForward to display the login Page, with the url of the previous Page. As Soon as the Users is logged in, they will stay on the url, seeing the member content.
      I've done exactly as suggested, including using sendForward in the snippet. The URL of the requested page displays in the browser bar. But after login, I get sent to the home page- even though my unauthoriased page is set to the Login page, I've removed the loginResourceID from the login snippet and the action from the login tpl....
        • 3749
        • 24,544 Posts
        That usually means that either the user doesn't have permission to see the page, the page isn't published, or the page doesn't exist. In all those cases, the user is sent to the page specified in the error_page System Setting, not to the page set as the unauthorized_page.

        Sending users to the unauthorized_page is tricky, but I'd recommend getting things working before worrying about that.
          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
          • 10405
          • 288 Posts
          Quote from: BobRay at Sep 29, 2016, 04:02 PM
          That usually means that either the user doesn't have permission to see the page, the page isn't published, or the page doesn't exist. In all those cases, the user is sent to the page specified in the error_page System Setting, not to the page set as the unauthorized_page.

          Sending users to the unauthorized_page is tricky, but I'd recommend getting things working before worrying about that.

          Hmm, well I checked the system settings and the Error Page value is also set to 10 which is the ID of the login page. Page is definitely published and the user definitely has full access to it (I can browse to it after login without any problem). So pretty stuck with this one.
            • 3749
            • 24,544 Posts
            What you're describing seems impossible. MODX would only send users to the site_start page ("home page") if the requested page is unavailable.

            Also, maybe you're already doing this, but when testing the availability of a page for a specific user, make sure you're testing in another browser where you're not logged in to the Manager.

              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
              • 5160
              • 118 Posts
              @galahad5 - is this any use? I wrote if for someone in a similar situation a while ago.

              Note - you need to manually set which events will trigger the plugin. Also the site_start id is hard coded near the end (2 in this case).

              <?php
              
              /**
               *
               * redirectToRequestedOnLogin
               * 
               * Plugin detects unauthorised access attempts and redirects
               * to requested URL on successful login.
               *
               * Requirements
               *
               * Use in conjunction with Login Extra.  Do not define the following properties in the Login Snippet call:
               *
               *  - loginResourceId - this overrides the resource the Plugin tries to redirect to
               *  - redirectToPrior - not needed
               *
               * Example Login shippet call:
               *
               * [[!Login? &logoutResourceId=`87` ]]
               *
               *
               * Manually set Plugin to fire on events:
               *		- OnPageUnauthorized
               *		- OnWebLogin
               *
               * Credits
               *		- http://forums.modx.com/thread/44577/want-to-be-able-to-send-not-autorized-user-back-to-the-page-they-requested
               *		- http://forums.modx.com/thread/16338/return-the-current-page-s-full-url
               *		- http://forums.modx.com/thread/44828/redirect-to-login-page?page=2#dis-post-397761
               *
               */
              
              if (!isset($modx)) return '';
              
              // don't process plugin on manager context, add multiple contxts to the array if required
              $excludeContext = array('mgr');
              
              if (in_array($modx->context->get('key'), $excludeContext)) return;
              
              // figure the event that triggered the plugin
              $eventName = $modx->event->name;
              
              // carry out actions depending on event that triggered plugin
              switch($eventName) {
                  case 'OnPageUnauthorized':
              
                  	// grab the id of the restricted page requested by the user
                  	$id = $modx->resourceIdentifier; //$modx->resource->get('id'); - fails
              
                      // set session to hold requested id
                  	$_SESSION['unauthorisedRequestID'] = $id;
              
                  	// option to log session value for unauthorisedRequestID
                  	// $modx->log(modX::LOG_LEVEL_ERROR, '[redirectToRequested] unauthorisedRequestID set to: ' . $_SESSION['unauthorisedRequestID']  );
              
                      break;
                  
                  case 'OnWebLogin':
              
                      // handle redirect to requested id that forced login
              
                  	// continue only if session set
                      if ( !empty( $_SESSION['unauthorisedRequestID'] ) ) {
              
                      	$redirectUrl = $modx->makeUrl( $_SESSION['unauthorisedRequestID'], "", "", "full" );
              
                      	// option to log the full url user will be redirected to
                      	// $modx->log(modX::LOG_LEVEL_ERROR, '[redirectToRequested] redirecting to ' . $redirectUrl  );
              
                      	// clean up
                      	if ( !empty( $_SESSION['unauthorisedRequestID'] ) ) {
              
                      		unset( $_SESSION['unauthorisedRequestID'] );
              
                      	}
              
                      	// send the redirect
                      	$modx->sendRedirect( $redirectUrl );
              
                      } else {
                          
                          // redirect user to login home resource, in this case #2
                          // should change this to a system setting so that it can be edited easily through the manager settings
                          $redirectUrl = $modx->makeUrl( 2, "", "", "full" );
              
                          // $modx->log(modX::LOG_LEVEL_ERROR, '[redirectToRequested] sending to #2' );
              
                          // send the redirect
                          $modx->sendRedirect( $redirectUrl );
                      }
              
                      break;
              }