Neos\Neos\Domain\Service\UserService::getUser PHP Метод

getUser() публичный Метод

Retrieves an existing user by the given username
public getUser ( string $username, string $authenticationProviderName = null ) : User
$username string The username
$authenticationProviderName string Name of the authentication provider to use. Example: "Typo3BackendProvider"
Результат Neos\Neos\Domain\Model\User The user, or null if the user does not exist
    public function getUser($username, $authenticationProviderName = null)
    {
        if ($authenticationProviderName !== null && isset($this->runtimeUserCache['a_' . $authenticationProviderName][$username])) {
            return $this->runtimeUserCache['a_' . $authenticationProviderName][$username];
        } elseif (isset($this->runtimeUserCache['u_' . $username])) {
            return $this->runtimeUserCache['u_' . $username];
        }
        $account = $this->accountRepository->findByAccountIdentifierAndAuthenticationProviderName($username, $authenticationProviderName ?: $this->defaultAuthenticationProviderName);
        if (!$account instanceof Account) {
            return null;
        }
        $user = $this->partyService->getAssignedPartyOfAccount($account);
        if (!$user instanceof User) {
            throw new Exception(sprintf('Unexpected user type "%s". An account with the identifier "%s" exists, but the corresponding party is not a Neos User.', get_class($user), $username), 1422270948);
        }
        if ($authenticationProviderName !== null) {
            if (!isset($this->runtimeUserCache['a_' . $authenticationProviderName])) {
                $this->runtimeUserCache['a_' . $authenticationProviderName] = [];
            }
            $this->runtimeUserCache['a_' . $authenticationProviderName][$username] = $user;
        } else {
            $this->runtimeUserCache['u_' . $username] = $user;
        }
        return $user;
    }

Usage Example

 /**
  * Render user initials or an abbreviated name for a given username. If the account was deleted, use the username as fallback.
  *
  * @param string $format Supported are "fullFirstName" and "initials"
  * @return string
  */
 public function render($format = 'initials')
 {
     if (!in_array($format, array('fullFirstName', 'initials', 'fullName'))) {
         throw new \InvalidArgumentException(sprintf('Format "%s" given to history:userInitials(), only supporting "fullFirstName", "initials" and "fullName".', $format), 1415705861);
     }
     $username = $this->renderChildren();
     /* @var $requestedUser Person */
     $requestedUser = $this->domainUserService->getUser($username);
     if ($requestedUser === null || $requestedUser->getName() === null) {
         return $username;
     }
     $currentUser = $this->userService->getBackendUser();
     if ($currentUser) {
         if ($currentUser === $requestedUser) {
             $translationHelper = new TranslationHelper();
             $you = $translationHelper->translate('you', null, [], 'Main', 'Neos.Neos');
         }
     }
     switch ($format) {
         case 'initials':
             return mb_substr($requestedUser->getName()->getFirstName(), 0, 1) . mb_substr($requestedUser->getName()->getLastName(), 0, 1);
         case 'fullFirstName':
             return isset($you) ? $you : $requestedUser->getName()->getFirstName() . ' ' . mb_substr($requestedUser->getName()->getLastName(), 0, 1) . '.';
         case 'fullName':
             return isset($you) ? $you : $requestedUser->getName()->getFullName();
     }
 }
All Usage Examples Of Neos\Neos\Domain\Service\UserService::getUser