-
☆ 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.]
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);
-
☆ 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.
-
☆ 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!!';
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.