Quote from: stevesunderland at Oct 20, 2010, 07:31 PM
Bob, thanks so much for your reply. My confusion at this point is whether this $user object needs to be an actual user in the database, or if I can just create a random modUser "session". Any insight you can provide is greatly appreciated.
That’s a great question. I’m not actually sure. If your plugin creates a valid user object and returns it, that might suffice, but I suspect that other snippets or parts of the Manager might look for the user in the DB (e.g., to update a user group). It would only take one line of code to add the user to the DB, so that would make sure everything was copacetic unless you have a reason not to put the user into the DB. The catch is that on future logins, the user would be found in the DB and your code wouldn’t execute (not necessarily bad).
Your plugin would have to create the user in the database using something like the code below.
<?php
/* validate the user here */
if (!valid) {
return false;
}
/* ***************** */
/* if we're here, the user is not in the DB, but is valid */
$user = $modx->newObject('modUser', array('username'=>$username));
$user->set('password',md5($password);
$user->set('active','1');
/* optional -- if you have info on the user, you can populate the user profile */
$profile = $modx->newObject('modUserProfile');
$profile->set('fullname',$fullname);
$profile->set('email', $userEmail);
$user->addOne($profile);
/*********************/
/* save user to DB */
$user->save();
/* could quit here */
/* optionally add user to user group */
$role = $modx->getObject('modUserGroupRole', array('name'=>'PhotoEditor'));
$group = $modx->getObject('modUserGroup', array('name'=>'PhotoEditors'));
$intersect = $modx->newObject('modUserGroupMember');
$intersect->addOne($user);
$intersect->addOne($group);
$intersect->addOne($role);
$modx->beginTransaction();
if (! $intersect->save()) {
$modx->rollback();
$output .= 'An error occurred — user, role, and group not saved';
} else {
$modx->commit();
}