We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 24657
    • 6 Posts
    Hi guys.

    I’m trying to figure out the "best" way for doing this without undermining a lot of what MODx does for me.

    I have two types of webusers

    guests
    regular users
    investors

    both have to be logged in to get to the page within the site, however investors have additional pages they can get access to. The way we have it setup right now where the unauthorized page includes a login form.

    What I want to do is :

    1) if a user who is not logged tries to access a secured page, they are redirected to a login page.
    2) If a "regular user" tries to access an "investor page", they are redirected to a "you don’t have access to investor pages" - i.e. I want a specific message not just a general access denied
    3) If a guest user tries to access a regular user page , I want to encourage them to register (our guest users have a minimal profile, a regular user has a full profile and we want to encourage them to register) so I want to redirect guest users to a "please register" page.

    The way it works right now is if ANYONE hits a page they are not authorized for they get the page with the login form. EVEN if they are already logged in. If I could tell what page them came from I could do a decision on that referring page, i.e. say "hey the page you came from was for investors so display this chunk, if for regular users, display this other chunk based on the page they "tried" to access,

    but I tried :

    $url = $_REQUEST["refurl"];
     if (isset($url))
     echo "url is " . $url;
    else
     echo "refurl is not set";


    and the refurl never seems to be set .

    Any ideas on the BEST way to do this? One thing was to insecure the special pages and figure out what groups the user is in and redirect if they shouldn’t have access but that undermines the modx security model and make a bunch of unnecessary work.

    Any ideas would be a great help, thankyou.

    thanks
    Grant

      • 22303 MODX Staff
      • 10,725 Posts
      As usual, lot’s of approaches, but a simple snippet on a main landing page would do the trick...
      <?php
      if ($modx->isMemberOfWebGroup(array ('Group1'))) {
          $modx->sendRedirect($modx->makeUrl(11));
      } elseif ($modx->isMemberOfWebGroup(array ('Group2'))) {
          $modx->sendRedirect($modx->makeUrl(22));
      } elseif ($modx->isMemberofWebGroup(array ('Group3'))) {
          $modx->sendRedirect($modx->makeUrl(33));
      } else {
          $modx->sendUnauthorizedPage();
      }
      ?>

      ...replacing Group1/11, Group2/22, Group3/33 with your own group names and target documents.
        • 24657
        • 6 Posts
        Thankyou, you rock \m/ smiley
          • 24657
          • 6 Posts
          oops one more extension..

          If a guest user hits an investor page OR a registered user page I need to do a different response for each..

          e.g.

          if they hit a registered user page, I need to ask them to register,
          if they hit a investor page, I need to tell them access denied.

          So the decision is based on the page they tried to access not just the group they are in.

          I looked to see how I figure out what page they were last at and I see the "q" parameter in the $_REQUEST is the document alias, but then when tried to figure out the referrer using


          $referer = $modx->getDocumentIdentifier($modx->getDocumentMethod());  and then 
          


          however I think this is the current page and not the page they *where* trying to access.

          then I wanted to get the document to figure out the "groups" that are allowed to access it..

          getDocumentObject($modx->getDocumentMethod(), $referer);
          


          I of course can’t do this since the getDocumentObject method checks what documents the "current" user is ALLOWED to see, so it just redirects me to the not authorized page...


          What I want to do is say

          1) what page where was the user trying to accesss
          2) What document groups does that page belong to ?
          3) If the group is investors and the user is a guest then redirect to access denied
          4) If the group is regular users and the user is a guest then redirect them to please register.


          Thanks
          Grant.
            • 22303 MODX Staff
            • 10,725 Posts
            Simplify -- if a guest user attempts to access a protected page, the user is forwarded automatically to the page you have specified as your unauthorized_page. You’ll need to implement a plugin for the OnPageUnauthorized event to intercept this before it occurs. At that point you can check what document groups the document is in etc, but don’t use those methods. You can simply get the page id from $modx->documentIdentifier (or $_REQUEST[’refurl’] in the latest release, 0.9.6.1) and go from there.

            BTW, those methods you used are not public API methods and you should really never call them directly; that’s what sendRedirect and sendForward are for.
              • 24657
              • 6 Posts
              Thankyou, this is exactly what I was looking for, how to work within-the-framework instead of "hacking" something to work.