public function loadUserByUsername($usernameOrEmail)
 {
     $user = new UserModelWithPassword();
     // try to load user by email address
     if (strpos($usernameOrEmail, '@') !== false) {
         $user->readByEmail($usernameOrEmail);
     } else {
         $user->readByUserName($usernameOrEmail);
     }
     if ($user->id->asString() == '') {
         throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $usernameOrEmail));
     }
     if (!$user->hasRoleOnSite($this->website) and $user->role != SystemRoles::SYSTEM_ADMIN) {
         $shouldThrowException = true;
         // special case: if known user on languageforge.org logs in on scriptureforge.org and vice versa, we automatically add them to the site.
         // This is because scriptureforge and languageforge are sister sites where cross-login is expected and allowed.
         $sisterSiteMap = array('scriptureforge.org' => 'languageforge.org', 'scriptureforge.local' => 'languageforge.local', 'dev.scriptureforge.org' => 'dev.languageforge.org');
         $sisterSiteMap = array_merge($sisterSiteMap, array_flip($sisterSiteMap));
         if (array_key_exists($this->website->domain, $sisterSiteMap)) {
             $otherWebsite = Website::get($sisterSiteMap[$this->website->domain]);
             if ($user->hasRoleOnSite($otherWebsite)) {
                 $shouldThrowException = false;
                 $user->siteRole[$this->website->domain] = $this->website->userDefaultSiteRole;
                 $user->write();
             }
         }
         if ($shouldThrowException) {
             throw new AccessDeniedException(sprintf('Username "%s" not available on "%s". Use "Create an Account".', $usernameOrEmail, $this->website->domain));
         }
     }
     /*
     $identityCheck = UserCommands::checkIdentity($usernameOrEmail, '', $this->website);
     if (! $identityCheck->usernameExists) {
         throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $usernameOrEmail));
     }
     
     $user->readByUserName($usernameOrEmail);
     
     if (! $identityCheck->usernameExistsOnThisSite and $user->role != SystemRoles::SYSTEM_ADMIN) {
         throw new AccessDeniedException(sprintf('Username "%s" not available on "%s". Use "Create an Account".', $usernameOrEmail, $this->website->domain));
     }
     */
     $roles = array('ROLE_' . $user->role);
     if ($user->siteRole and $user->siteRole->offsetExists($this->website->domain) and $user->siteRole[$this->website->domain] !== SiteRoles::NONE) {
         $roles[] = 'ROLE_SITE_' . $user->siteRole[$this->website->domain];
     }
     return new UserWithId($user->username, $user->password, $user->id->asString(), $roles);
 }
 /**
  * Register a new user
  * @param array $params
  * @param string $captcha_info
  * @param Website $website
  * @param DeliveryInterface $delivery
  * @throws \Exception
  * @return string $userId
  */
 public static function register($params, $captcha_info, $website, DeliveryInterface $delivery = null)
 {
     if (strtolower($captcha_info['code']) != strtolower($params['captcha'])) {
         return false;
         // captcha does not match
     }
     $user = new UserModel();
     JsonDecoder::decode($user, $params);
     UserCommands::assertUniqueIdentity($user, $params['username'], $params['email'], $website);
     $user->active = false;
     $user->role = SystemRoles::USER;
     $user->siteRole[$website->domain] = $website->userDefaultSiteRole;
     if (!$user->emailPending) {
         if (!$user->email) {
             throw new \Exception("Error: no email set for user signup.");
         }
         $user->emailPending = $user->email;
         $user->email = '';
     }
     $userId = $user->write();
     // Write the password
     $userPassword = new UserModelWithPassword($userId);
     $userPassword->setPassword($params['password']);
     $userPassword->write();
     // if website has a default project then add them to that project
     $project = ProjectModel::getDefaultProject($website);
     if ($project) {
         $project->addUser($user->id->asString(), ProjectRoles::CONTRIBUTOR);
         $user->addProject($project->id->asString());
         $project->write();
         $user->write();
     }
     Communicate::sendSignup($user, $website, $delivery);
     return $userId;
 }