/** * Guess organization * * @param User $user * @param TokenInterface $token * * @return Organization * * @throws BadCredentialsException */ protected function guessOrganization(User $user, TokenInterface $token) { $organizationGuesser = new UserOrganizationGuesser(); $organization = $organizationGuesser->guess($user, $token); if (!$organization) { throw new BadCredentialsException("You don't have active organization assigned."); } elseif (!$user->getOrganizations(true)->contains($organization)) { throw new BadCredentialsException(sprintf("You don't have access to organization '%s'", $organization->getName())); } return $organization; }
public function guessFromUserActiveOrganizations() { $guesser = new UserOrganizationGuesser(); $user = $this->getMockBuilder('Oro\\Bundle\\UserBundle\\Entity\\User')->disableOriginalConstructor()->getMock(); $creatorOrganization = $this->getMockBuilder('Oro\\Bundle\\OrganizationBundle\\Entity\\Organization')->disableOriginalConstructor()->getMock(); $organization1 = $this->getMockBuilder('Oro\\Bundle\\OrganizationBundle\\Entity\\Organization')->disableOriginalConstructor()->getMock(); $organization2 = $this->getMockBuilder('Oro\\Bundle\\OrganizationBundle\\Entity\\Organization')->disableOriginalConstructor()->getMock(); $token = $this->getMock('Oro\\Bundle\\SecurityBundle\\Authentication\\Token\\OrganizationContextTokenInterface'); $token->expects($this->once())->method('getOrganizationContext')->willReturn(false); $user->expects($this->once())->method('getOrganization')->willReturn($creatorOrganization); $user->expects($this->once())->method('getOrganizations')->with(true)->willReturn(new ArrayCollection([$organization1, $organization2])); $this->assertSame($organization1, $guesser->guess($user, $token)); }
/** * {@inheritdoc} */ public function authenticate(TokenInterface $token) { $guesser = new UserOrganizationGuesser(); /** @var TokenInterface $token */ $authenticatedToken = parent::authenticate($token); /** @var User $user */ $user = $authenticatedToken->getUser(); $organization = $guesser->guess($user, $token); if (!$organization) { throw new BadCredentialsException("You don't have active organization assigned."); } elseif (!$user->getOrganizations(true)->contains($organization)) { throw new BadCredentialsException(sprintf("You don't have access to organization '%s'", $organization->getName())); } $authenticatedToken = new OrganizationRememberMeToken($user, $authenticatedToken->getProviderKey(), $authenticatedToken->getKey(), $organization); return $authenticatedToken; }
/** * {@inheritdoc} */ public function authenticate(TokenInterface $token) { if (null === $this->tokenFactory) { throw new AuthenticationException('Token Factory is not set in UsernamePasswordOrganizationAuthenticationProvider.'); } $guesser = new UserOrganizationGuesser(); /** @var TokenInterface $token */ $authenticatedToken = parent::authenticate($token); /** @var User $user */ $user = $authenticatedToken->getUser(); $organization = $guesser->guess($user, $token); if (!$organization) { throw new BadCredentialsException("You don't have active organization assigned."); } elseif (!$user->getOrganizations(true)->contains($organization)) { throw new BadCredentialsException(sprintf("You don't have access to organization '%s'", $organization->getName())); } $authenticatedToken = $this->tokenFactory->create($authenticatedToken->getUser(), $authenticatedToken->getCredentials(), $authenticatedToken->getProviderKey(), $organization, $authenticatedToken->getRoles()); return $authenticatedToken; }