We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14883 ☆ A M B ☆
    • 450 Posts
    What would be the simplest way to automatically log a user in, without the explicit passing of a username and password? I'm looking to take an anonymous user session and promote it to a generic user with rights to view certain resources. For example:

    if ( internal IP address) {
    log in user "internalUser"
    }

    Is there a way to do this in a snippet? [ed. note: jrotering last edited this post 14 years, 11 months ago.]
      • 3749
      • 24,544 Posts
      I've never tried it, but I think you could just put the proper values in the $_POST and $scriptProperties arrays and do $modx->runSnippet('Login', $scriptProperties);
        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
        • 14883 ☆ A M B ☆
        • 450 Posts
        After a LOT of meticulous debugging, I finally got this to work as follows:
        $_POST['username'] = $user;
        $_POST['password'] = $pass;
        $_POST['service'] = 'login';
        $_REQUEST['service'] = 'login';
        $scriptProperties['loginResourceId'] = 499;

        $modx->runSnippet('Login', $scriptProperties);

        Because of the way /login/controllers/web/Login.php is written (at least as of 2.1.3), you have to explicitly set BOTH $_POST['service'] *and* $_REQUEST['service'] in order for this to work.

        But now it's working great.

          • 18373 ☆ A M B ☆
          • 3,141 Posts
          Why not just use the login (not snippet, but core) processor?

          $properties = array('username' => 'asdf', 'password' => 'asd');
          $response = $modx->runProcessor('security/login',$properties);
          if ($response->hasError()) return 'Something failed. I didnt test this code, so dont look at me!!';
            Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

            Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
            • 3749
            • 24,544 Posts
            Nice work. I didn't know about the "service" requirement.

            I think there's also a way to just set the user's *status* as logged in, but I don't remember what it is (and your method might be more secure).

            BTW, if you change the subject line to "Log in a user in code" or something similar, this will be easier to find for others. I read the current subject as meaning that you wanted to force users to log in before viewing a page.
              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