Get Email address of a user; fallback to contact / account if not defined

@param UserInterface $user
@param bool $useFallback

@return null|string
488269
public function getEmailAddressOfUser(UserInterface $user, $useFallback = true)
    {
        // take users email address
        $userEmail = $user->getEmail();
        if ($userEmail) {
            return $userEmail;
        }

        // fallback: get contacts / accounts main-email
        $contact = $user->getContact();
        if ($useFallback && $contact) {
            return $this->getEmailAddressOfContact($contact);
        }

        return null;
    }
1271813
protected function loadUserThrottles(UserInterface $user)
    {
        $queryBuilder = $this->getEntityManager()->createQueryBuilder();
        $queryBuilder
            ->select('t')
            ->from($this->entityName('user'), 't')
            ->where('t.createdAt > :interval')
            ->andWhere('t.user = :user')
            ->setParameters([
                'interval' => Carbon::now()->subSeconds($this->userInterval),
                'user'     => $user
            ]);

        return new Collection($queryBuilder->getQuery()->getResult());
    }
886724
private function getCurrentUser($required)
    {
        $result = null;
        $id = $this->getCurrentUserID($required);
        if ($id !== null) {
            $result = $this->getEntityManager()->find(UserEntity::class, $id);
        }

        if ($result === null && $required) {
            throw new UserMessageException(t('No logged in user'));
        }

        return $result;
    }
729722
public function isAdmitted(UserInterface $user = null)
    {
        if (is_null($user)) {
            return !$this->currentUser() instanceof GuestUser;
        }

        return $user->getIdentifier() === $this->currentUser()->getIdentifier();
    }
183203
protected function getEmail( UserResponseInterface $response )
    {
        $responseArray = $response->getResponse();
        switch ( $response->getResourceOwner()->getName() )
        {
            case UserIdentity::getReadableType( UserIdentity::TYPE_TWITTER ):
                return NULL;
            case UserIdentity::getReadableType( UserIdentity::TYPE_YAHOO ):
                if ( array_key_exists( 'emails', $responseArray['profile'] ))
                {
                    if ( count( $responseArray['profile']['emails'] ) > 0 )
                    {
                        return $responseArray['profile']['emails'][0]['handle'];
                    }
                }
            default:
                if ( array_key_exists( 'email', $responseArray ))
                {
                    return $responseArray['email'];
                }
        }
    }
579356
private function resetEmail(UserInterface $user)
    {
        $ttl = $this->container->getParameter('fos_user.resetting.token_ttl');
        if ($user->isPasswordRequestNonExpired($ttl)) {
            return self::RESET_EMAIL_ALREADY_SENT;
        }

        if (null === $user->getConfirmationToken()) {
            /**
             * @var $tokenGenerator \FOS\UserBundle\Util\TokenGeneratorInterface
             */
            $tokenGenerator = $this->container->get('fos_user.util.token_generator');
            $user->setConfirmationToken($tokenGenerator->generateToken());
        }

        $this->container->get('session')->set(
            static::SESSION_EMAIL,
            $this->getObfuscatedEmail($user)
        );
        $this->container->get('fos_user.mailer')->sendResettingEmailMessage($user);
        $user->setPasswordRequestedAt(new \DateTime());
        $this->container->get('fos_user.user_manager')->updateUser($user);

        return self::RESET_EMAIL_OK;
    }
547019
public function sendCreationMessage(User $user)
    {
        $locale = $this->container->get('claroline.manager.locale_manager')->getLocale($user);
        $url = $this->router->generate(
            'claro_security_validate_email',
            ['hash' => $user->getEmailValidationHash()],
            UrlGeneratorInterface::ABSOLUTE_URL
        );
        $placeholders = [
            'first_name' => $user->getFirstName(),
            'last_name' => $user->getLastName(),
            'username' => $user->getUsername(),
            'password' => $user->getPlainPassword(),
            'validation_mail' => $url,
        ];
        $subject = $this->templateManager->getTemplate('claro_mail_registration', $placeholders, $locale, 'title');
        $body = $this->templateManager->getTemplate('claro_mail_registration', $placeholders, $locale);

        return $this->send($subject, $body, [$user], null, [], true);
    }
576860
public function sendDigestMail(UserInterface $user, array $messages)
    {
        $template = $this->parameters['digest']['template'];
        $from = $this->parameters['digest']['from'];

        $content = $this->templating->render(
            $template,
            [
                'date' => date('Y-m-d H:i:s'),
                'messages' => $messages,
            ]
        );

        return $this->sendEmailMessage($content, $from, $user->getEmail());
    }
1221239
protected function gatherPermissions(UserInterface $user = null)
    {
        $user = $user ?: $this->currentUser();
        $roles = $user->getRoles();
        $permissions = $user->getPermissions();

        foreach ($roles as $role) {
            if (isset($this->roleDictionary[$role])) {
                $permissions = array_merge($permissions, $this->roleDictionary[$role]);
            }
        }

        return $permissions;
    }
1088606
protected function checkActivation(UserInterface $user)
    {
        $completed = $this->activations->completed($user);

        if (! $completed) {
            $exception = new NotActivatedException('Your account has not been activated yet.');

            $exception->setUser($user);

            throw $exception;
        }
    }
1052277
public function doAuthentication(UserInterface $user)
    {
        /** @var \PServerCore\Entity\Repository\User $repository */
        $repository = $this->entityManager->getRepository($this->getUserEntityClassName());

        // fix if we have a proxy we don´t have a valid entity, so we have to clear before we can create a new select
        $username = $user->getUsername();
        $repository->clear();

        $userNew = $repository->getUser4UserName($username);

        $authService = $this->getAuthService();

        $authService->getStorage()->write($userNew);
    }
1017795
public function getUser(ConnectionInterface $con = null)
    {
        if ($this->aUser === null && ($this->user_id !== null)) {
            $this->aUser = ChildUserQuery::create()->findPk($this->user_id, $con);
            /* The following can be used additionally to
                guarantee the related object contains a reference
                to this object.  This level of coupling may, however, be
                undesirable since it could result in an only partially populated collection
                in the referenced object.
                $this->aUser->addUserRoles($this);
             */
        }

        return $this->aUser;
    }
297774
function setCurrentUser(UserInterface $user)
    {
        $this->currentUser = $user;

        // accessing the current user from the
        // DI container is deprecated
        $this->app['user'] = $user;

        return $this;
    }
254082
private function getEmail(UserInterface $user)
    {
        if (null !== $user->getEmail()) {
            return $user->getEmail();
        }

        return $this->container->getParameter('sulu_admin.email');
    }
188256
protected function sendReactivationEmail(User $user)
    {
        $forum = Podium::getInstance()->podiumConfig->get('name');
        $email = Content::fill(Content::EMAIL_REACTIVATION);
        if ($email !== false) {
            $link = Url::to(['account/activate', 'token' => $user->activation_token], true);
            return Email::queue(
                $user->email,
                str_replace('{forum}', $forum, $email->topic),
                str_replace('{forum}', $forum, str_replace('{link}',
                    Html::a($link, $link), $email->content)),
                !empty($user->id) ? $user->id : null
            );
        }
        return false;
    }
317763
protected function getCoinsEntry(UserInterface $user)
    {
        $class = $this->getGameOptions()->getEntityBillingClass();
        $accEntityManager = $this->getBillingEntityManager();

        /** @var \GameBackend\Entity\CrossFire\Repository\Billing $repository */
        $repository = $accEntityManager->getRepository($class);
        return $repository->getEntry4User($user);
    }
438232
public function getFavouriteAccountsForUser(UserInterface $user)
    {
        $criteria = new Criteria();
        $criteria
            ->where($criteria->expr()->in(
                'id',
                $user->getMyFavouriteAccountIds()
            ))
        ;

        return $this->oh->getOrganization()
            ->getAccounts()
            ->matching($criteria)
        ;
    }