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);
 }
 /**
  * @param string $validationKey
  * @param array $params
  * @param Website $website
  * @throws \Exception
  * @return string $userId
  */
 public static function updateFromRegistration($validationKey, $params, $website)
 {
     $user = new UserModelWithPassword();
     if (!$user->readByProperty('validationKey', $validationKey)) {
         return false;
     }
     if (!$user->validate()) {
         throw new \Exception("Sorry, your registration link has expired.");
     }
     $params['id'] = $user->id->asString();
     JsonDecoder::decode($user, $params);
     $user->setPassword($params['password']);
     $user->validate();
     $user->role = SystemRoles::USER;
     $user->siteRole[$website->domain] = $website->userDefaultSiteRole;
     $user->active = true;
     return $user->write();
 }