We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 39583
    • 103 Posts
    I'm still quite new to modx, and now I have someone asking me if its possible to have an external login page to access parts of the site.

    As flexible as modx seems to be I'm sure it would work to do this somehow.

    Lets say I have the modx menu like above with General, Revolution, Evolution, etc. Now I want General to be visible for the public, but Revolution and the rest not, however I still want the menu items to be seen by the public.

    If then Revolution is clicked and the user isn't logged in then I want modx to redirect the user to a login page. And if the user is already logged in then show the correct page of course.

    First of all, is this possible?

    I'm not asking for a full working example (unless someone has it and wants to share it =) ) , just if someone could point me in the right direction.

    My guess is that I need a plugin which uses OnHandleRequest and checks if a user is logged in and if the requested page is private or public.
    Would this be more or less right?

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

      --
      André
      • 10208 ☆ A M B ☆
      • 1,780 Posts
      MODX Documentation - see Security
      MODX Add On's - See Login, Register, DefaultResourceGroup, DefaultUserGroup - and any other registration & login Extras such as Frontpage, NewsPublisher, etc. See also the documentation for these at http://rtfm.modx.com/display/ADDON/Home
      Bob's Guides - http://bobsguides.com - See the MODX Security information

      Beware, don't just start adding context ACL's and user groups till you've got a decent handle on all the documentation. Even then, expect to make mistakes. It's not exactly simple. Post here if you have questions.
        Frogabog- MODX Websites in Portland Oregon
        "Do yourself a favor and get a copy of "MODX - The Official Guide" by Bob Ray. Read it.
        Having server issues? These guys have MODX Hosting perfected - SkyToaster
        • 39583
        • 103 Posts
        Quote from: frogabog at Nov 06, 2012, 11:55 AM
        MODX Documentation - see Security
        MODX Add On's - See Login, Register, DefaultResourceGroup, DefaultUserGroup - and any other registration & login Extras such as Frontpage, NewsPublisher, etc. See also the documentation for these at http://rtfm.modx.com/display/ADDON/Home
        Bob's Guides - http://bobsguides.com - See the MODX Security information

        Beware, don't just start adding context ACL's and user groups till you've got a decent handle on all the documentation. Even then, expect to make mistakes. It's not exactly simple. Post here if you have questions.

        Thanks Frogabog for the suggestions. I can't find exactly what I'm looking for though.
        What should I do to achieve the following?

        I have a full website with two contexts for two different languages. The only public page is the landing page of both languages. The rest should be private/protected. This I have achieved with resource groups and limits on the anonymous users. (except for one problem which I have another question about here: https://forums.modx.com/thread/80467/making-member-pages-works-on-one-context-but-not-the-other#dis-post-443427)
        On the landing page all the menu entries that are protected should be seen by the anonymous user and if clicked a popup with two login-forms should be displayed. These login-form are from other sites and will return if the users has permission or not when they've entered their credentials. And as long as this session exists the user should be able to view all pages if the user was approved of course.

        My guess as a non modx- or php- pro is that I should check if a session exists when the landing page is loaded (and all sub-pages). If no user is logged in all links will point to the popup. The user then logs in, sends info to the external server and is redirected to the private/protected landing page if successful. And this is what I can't find any info about, probably because I'm not entirely sure what to look for.
        I need one snippet that checks if a valid session exists for the protected pages, if not display the logins.
        The other code I would need is something that creates the session for the user if the external login was successful. Should this be a snippet or just a php document on the server. And how can I start a session for the protected pages?

        Many questions now. Hope what i want is clear. Any help would be appreciated. =)

        Thanks
        André


        EDIT:
        Maybe I'm asking the question wrong. Is, how can I create a user session for a specific private resource group on the front-end, a better question? [ed. note: phacer last edited this post 13 years, 11 months ago.]
          --
          André
          • 39583
          • 103 Posts
          Interesting that no one has a clue of how to do this.

          Now I'm skipping modx's security part all along and have added my own script to see if the correct session variable exists or not on each page that is loaded.
          So in the modx manager all pages are public but the ones I want private has the script. Feels like extra work and not safe, but it works.
            --
            André
            • 10378
            • 375 Posts
            Quote from: phacer at Nov 20, 2012, 09:17 AM
            Interesting that no one has a clue of how to do this.

            Now I'm skipping modx's security part all along and have added my own script to see if the correct session variable exists or not on each page that is loaded.
            So in the modx manager all pages are public but the ones I want private has the script. Feels like extra work and not safe, but it works.

            You should have a look at the Login (http://modx.com/extras/package/login) extra - I think it should do what you want!
            Or you could use the MODx API to receive login status of a user:

            http://api.modx.com/revolution/2.2/ - core/model/modx/moduser.class.php

            If you would like to show visitors a menu entry but not let them open the resource without logging in, you have to give the special user group (anonymous) load and list access to the resource group of these menu entries.

            greetings,
            Martin
              Freelancer @bitego http://www.bitego.com
              ---
              GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
              More infos here: http://www.bitego.com/extras/goodnews/
              • 39583
              • 103 Posts
              Quote from: gadgetto at Nov 20, 2012, 03:41 PM
              Quote from: phacer at Nov 20, 2012, 09:17 AM
              Interesting that no one has a clue of how to do this.

              Now I'm skipping modx's security part all along and have added my own script to see if the correct session variable exists or not on each page that is loaded.
              So in the modx manager all pages are public but the ones I want private has the script. Feels like extra work and not safe, but it works.

              You should have a look at the Login (http://modx.com/extras/package/login) extra - I think it should do what you want!
              Or you could use the MODx API to receive login status of a user:

              http://api.modx.com/revolution/2.2/ - core/model/modx/moduser.class.php

              If you would like to show visitors a menu entry but not let them open the resource without logging in, you have to give the special user group (anonymous) load and list access to the resource group of these menu entries.

              greetings,
              Martin

              Thanks Martin,
              much appreciated.

              With Login I can't figure out a way to have a hidden login. Because the login I need to use is an external one and when the user is redirected back to my site should be logged in, in the background without the user realising. (Sorry, I've been asking around in many places about this so I forgot to mention it in this thread.)

              Using the MODx Api seems more like the approach I should take. But I have no idea how to start to make a php script on my server that can be the target of the redirection that then create the login in modx and goes to the protected startpage. I'm a bit of noob when it comes to modx. Do you have any suggestions or a simple script that logs in a user to modx?

              Thanks
              André
                --
                André
              • discuss.answer
                • 10378
                • 375 Posts
                Here is a sample code for loggin in a user via API (I think it's originally from Jason Coward):

                <?php
                $loginContext = isset($_REQUEST['login_context']) && !empty($_REQUEST['login_context']) ? $_REQUEST['login_context'] : $modx->context->get('key');
                $contexts = !empty($scriptProperties['contexts']) ? $scriptProperties['contexts'] : '';
                $contexts = !empty($_REQUEST['add_contexts']) ? $_REQUEST['add_contexts'] : $contexts;
                $authenticated = $modx->user->isAuthenticated($loginContext);
                 
                if (!$authenticated) {
                    // get the username and password in a way that fits your snippet
                    $_POST['username'] = $username;
                    $_POST['password'] = $password;
                 
                    if (!isset($_POST['login_context'])) $_POST['login_context'] = $loginContext;
                    $response = $modx->executeProcessor(array(
                        'action' => 'login',
                        'location' => 'security',
                        'login_context' => $loginContext,
                        'add_contexts' => $contexts,
                        'rememberme' => !empty($_REQUEST['rememberme']) ? true : false,
                    ));
                    if (empty($response['success']) ) {
                        // code to handle unhappy user
                    } else {
                        // code to handle happy user
                    }
                }
                


                If you try to execute this code from "outside" of MODx you first need to get a connection to MODx:
                - this can be done through a snippet which is called in a MODx resource
                - or by including the necessary MODx files in your php file (please have a look at the forums, this was postet many times before)
                  Freelancer @bitego http://www.bitego.com
                  ---
                  GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
                  More infos here: http://www.bitego.com/extras/goodnews/
                  • 39583
                  • 103 Posts
                  Quote from: gadgetto at Nov 21, 2012, 05:56 AM
                  Here is a sample code for loggin in a user via API (I think it's originally from Jason Coward):

                  <!--?php
                  $loginContext = isset($_REQUEST['login_context']) && !empty($_REQUEST['login_context']) ? $_REQUEST['login_context'] : $modx--->context->get('key');
                  $contexts = !empty($scriptProperties['contexts']) ? $scriptProperties['contexts'] : '';
                  $contexts = !empty($_REQUEST['add_contexts']) ? $_REQUEST['add_contexts'] : $contexts;
                  $authenticated = $modx->user->isAuthenticated($loginContext);
                   
                  if (!$authenticated) {
                      // get the username and password in a way that fits your snippet
                      $_POST['username'] = $username;
                      $_POST['password'] = $password;
                   
                      if (!isset($_POST['login_context'])) $_POST['login_context'] = $loginContext;
                      $response = $modx->executeProcessor(array(
                          'action' => 'login',
                          'location' => 'security',
                          'login_context' => $loginContext,
                          'add_contexts' => $contexts,
                          'rememberme' => !empty($_REQUEST['rememberme']) ? true : false,
                      ));
                      if (empty($response['success']) ) {
                          // code to handle unhappy user
                      } else {
                          // code to handle happy user
                      }
                  }
                  


                  If you try to execute this code from "outside" of MODx you first need to get a connection to MODx:
                  - this can be done through a snippet which is called in a MODx resource
                  - or by including the necessary MODx files in your php file (please have a look at the forums, this was postet many times before)


                  Thanks, this is perfect.
                  So I need to have a user created and pass the username and password to this script right?

                  require_once 'config.core.php';  
                  require_once MODX_CORE_PATH.'config/'.MODX_CONFIG_KEY.'.inc.php';  
                  require_once MODX_CORE_PATH.'model/modx/modx.class.php';  
                  
                  $modx = new modX(); 
                  $modx->initialize('web');


                  I added this on the top of the code but nothing happens with this script. And there are no errors if I pass a non-existing user or not. And it never seems to get to happy or unhappy user. Any suggestions?
                    --
                    André