/**
  * Logs a user in, using the local Craft user-base if possible, or falling back to legacy user-base data.
  *
  * @param string $username   The user’s username.
  * @param string $password   The user’s submitted password.
  * @param bool   $rememberMe Whether the user should be remembered.
  *
  * @throws Exception
  * @return bool Whether the user was logged in successfully.
  */
 public function login($username, $password, $rememberMe = false)
 {
     // First, try logging in a native User.
     $nativeSuccess = craft()->userSession->login($username, $password, $rememberMe);
     if ($nativeSuccess === true) {
         return LegacyLoginPlugin::NativeUserType;
     }
     // Okay, we'll try to match and validate a legacy user...
     // First, validate the provided username/password.
     $usernameModel = new UsernameModel(['username' => $username]);
     $passwordModel = new PasswordModel(['password' => $password]);
     if (!$usernameModel->validate() || !$passwordModel->validate()) {
         LegacyLoginPlugin::log($username . ' tried to log in unsuccessfully, but there was a validation issue with the username or password.', LogLevel::Warning);
         return false;
     }
     // Okay, we have a valid username and password... Can we authenticate a legacy user?
     $allowedServices = craft()->config->get('allowedServices', 'legacylogin');
     // Bail if we're mis-configured
     if (!is_array($allowedServices)) {
         return false;
     }
     // Try each service in sequence...
     foreach ($allowedServices as $service) {
         switch ($service) {
             case LegacyLoginPlugin::BigCommerceLegacyUserType:
                 if (LegacyLogin_BigCommerceHelper::login($username, $password, $rememberMe)) {
                     return LegacyLoginPlugin::BigCommerceLegacyUserType;
                 }
                 break;
             case LegacyLoginPlugin::EE2LegacyUserType:
                 if (LegacyLogin_Ee2Helper::login($username, $password, $rememberMe)) {
                     return LegacyLoginPlugin::EE2LegacyUserType;
                 }
                 break;
             case LegacyLoginPlugin::WellspringLegacyUserType:
                 if (LegacyLogin_WellspringHelper::login($username, $password, $rememberMe)) {
                     return LegacyLoginPlugin::WellspringLegacyUserType;
                 }
                 break;
             case LegacyLoginPlugin::WordPressLegacyUserType:
                 if (LegacyLogin_WordPressHelper::login($username, $password, $rememberMe)) {
                     return LegacyLoginPlugin::WordPressLegacyUserType;
                 }
                 break;
         }
     }
     // Alas, it just wasn't meant to be.
     LegacyLoginPlugin::log($username . ' could not be authenticated as a legacy user.', LogLevel::Warning);
     return false;
 }
 /**
  * @param mixed $legacyData
  * @param string $password
  *
  * @return LegacyLogin_MatchedUserModel|false
  */
 public static function makeMatchedUser($legacyData = null, $password = null)
 {
     // Validate legacyData and password
     if (empty($legacyData) || empty($password)) {
         return false;
     }
     // Grab settings from Craft config
     $setPassword = craft()->config->get('setPassword', 'legacylogin');
     $requirePasswordReset = craft()->config->get('requirePasswordReset', 'legacylogin');
     $matchBy = craft()->config->get('matchBy', 'legacylogin');
     // Try to find a matching Craft user: by username, email, or both, according to config
     if (empty($craftUser) && in_array($matchBy, ['email', 'both'])) {
         // See if a matching Craft user exists, by email.
         $craftUser = craft()->users->getUserByUsernameOrEmail($legacyData->email);
     }
     if (empty($craftUser) && in_array($matchBy, ['username', 'both'])) {
         // See if a matching Craft user exists, by username.
         $craftUser = craft()->users->getUserByUsernameOrEmail($legacyData->username);
     }
     // Start a transaction
     $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;
     // If we still don't have a Craft user, make one.
     if (empty($craftUser)) {
         $craftUser = new UserModel();
         $craftUser->username = $legacyData->username;
         $craftUser->email = $legacyData->email;
         $success = craft()->users->saveUser($craftUser);
         if (!$success) {
             LegacyLoginPlugin::log("Couldn't save the user {$craftUser->username}.", LogLevel::Error);
         } else {
             craft()->userGroups->assignUserToDefaultGroup($craftUser);
         }
     } else {
         $success = true;
     }
     // Set the new password and/or password reset flag
     if ($success) {
         if ($setPassword) {
             LegacyLoginPlugin::log("Password was set from legacy data for {$legacyData->email}.", LogLevel::Info, true);
             $craftUser->newPassword = $password;
         }
         $success = craft()->users->saveUser($craftUser);
         if ($requirePasswordReset) {
             LegacyLoginPlugin::log("{$legacyData->email} will need to reset their password.", LogLevel::Info, true);
             $craftUser->passwordResetRequired = $requirePasswordReset;
         }
         $success = $success && craft()->users->saveUser($craftUser);
         if (!$success) {
             LegacyLoginPlugin::log("There was an error processing new password profile for {$craftUser->username}.", LogLevel::Error);
         }
     }
     // Make a new matched user, with Craft user attached
     if ($success) {
         $matchedUser = new LegacyLogin_MatchedUserRecord();
         $matchedUser->craftUserId = $craftUser->id;
         $matchedUser->legacyUserType = LegacyLoginPlugin::EE2LegacyUserType;
         $matchedUser->legacyRecordId = $legacyData->id;
         $matchedUser->legacyUserId = $legacyData->member_id;
         $matchedUser->legacyUsername = $legacyData->username;
         $matchedUser->legacyEmail = $legacyData->email;
         $matchedUser->passwordSet = $setPassword;
         $success = $matchedUser->save();
         if (!$success) {
             LegacyLoginPlugin::log("There was an error saving the new Matched User for {$craftUser->username}.", LogLevel::Error);
         }
     }
     if ($success) {
         if ($transaction !== null) {
             $transaction->commit();
         }
         return LegacyLogin_MatchedUserModel::populateModel($matchedUser);
     } else {
         if ($transaction !== null) {
             $transaction->rollback();
         }
         return false;
     }
 }