We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6433
    • 25 Posts
    I’m working with a MODx intranet and we’re looking into avoiding a currently generic login by allowing access by IP - if outside the IP range then normal login is required. Being within the IP range would give you access AS IF logged in. Using WebLogin (or PE?) is there a clean way to do this?

    I was considering just modding the WebLogin snippet, but does anyone know of anything already set up?

    Any help appreciated.
      • 25483
      • 741 Posts
      I think you should use a plugin that runs "OnWebPagePrerender" and then checks the IP of the visitor. Then you could invoke the login api of MODx to login the user when the IP is on the list you specify.
        with regards,

        Ronald Lokers
        'Front-end developer' @ h2o Media

      • It might be better to use one of the Web Access Service Events; onBeforeWebLogin comes to mind.
          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
          • 10472
          • 22 Posts
          I’m look at using similar functionality to show or hide pages based on location (US/UK - determined by the IP). What I’m thinking is this:

          - If a user is visiting from the US, they’re automatically logged on as ’USuser’ and can see all pages in the group ’USpages’
          - If a user is visiting from the UK, they’re automatically logged on as ’UKuser’ and can see all pages in the group ’UKpages’

          Some pages will be available to both the ’UKpages’ and ’USpages’ group.

          I’ve worked out how to determine their location, but I don’t know how to automatically log them on using the login API. Can anybody help?


            • 7455
            • 2,204 Posts
            onBeforeWebLogin is not triggerd until you hit the login button (I think)
            what you need to do is use the OnWebPagePrerender and set a logged in session for that user. something like that

            Dimmy
              follow me on twitter: @dimmy01
              • 29696
              • 4 Posts
              Does anyone have some more details they worked out? I’m trying a similiar idea. (Intranet, users inside company get logged on as a guest user)
                • 3749
                • 24,544 Posts
                I’m not sure about 0.9.6, but in Revo, the plugin events for login are geared toward rejecting a user rather than accepting them. I think it might work to have a plugin that listens for one of these:

                OnBeforeManagerLogin
                OnManagerFormRender

                In the plugin, you’d test the IP. If the IP doesn’t check out - do nothing. If the IP checks out, log the user in Manually in the plugin code and redirect to the main manager page.

                I was just working on this function to get the user’s IP earlier today (for a different use) and thought I’d toss it in:

                /**
                
                $allowReverseProxy = false;
                $allowedReverseProxyAddresses = array();
                
                * Returns the user's IP number.
                *  @returns string  - user's IP number or false
                *  @access protected
                */
                function _getAuthorIp() {
                
                       if (!empty($_SERVER['HTTP_CLIENT_IP'])) {  //check ip from proxy
                           return($_SERVER['HTTP_CLIENT_IP']);
                        }
                        if(!isset($_SERVER['REMOTE_ADDR'])) {
                             return null;
                        }
                
                        $ip = $_SERVER['REMOTE_ADDR'];
                
                        if($allowReverseProxy)   {
                
                            if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))  {
                
                                if(!empty($allowedReverseProxyAddresses) && in_array($ip, $allowedReverseProxyAddresses, true))   {
                                     return array_pop(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']));
                                }
                            }
                
                               // running in a cluster environment
                            if(isset($_SERVER['HTTP_X_CLUSTER_CLIENT_IP'])) return $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
                        }
                
                
                        return $ip;
                
                }


                Someone else may have a better version.
                  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