Ejemplo n.º 1
0
 /**
  * @param mixed $organization
  * @param OrganizationContextTokenInterface $token
  * @throws \InvalidArgumentException
  */
 protected function setOrganization($organization, OrganizationContextTokenInterface $token)
 {
     if (!$organization) {
         return;
     }
     $organizationRepository = $this->registry->getRepository('OroOrganizationBundle:Organization');
     $organizationId = filter_var($organization, FILTER_VALIDATE_INT);
     if ($organizationId) {
         $organizationEntity = $organizationRepository->find($organizationId);
     } else {
         $organizationEntity = $organizationRepository->findOneBy(['name' => $organization]);
     }
     if ($organizationEntity) {
         // organization must be enabled
         if (!$organizationEntity->isEnabled()) {
             throw new \InvalidArgumentException(sprintf('Organization %s is not enabled', $organizationEntity->getName()));
         }
         $user = $token->getUser();
         if ($user && $user instanceof User && !$user->hasOrganization($organizationEntity)) {
             throw new \InvalidArgumentException(sprintf('User %s is not in organization %s', $user->getUsername(), $organizationEntity->getName()));
         }
         $token->setOrganizationContext($organizationEntity);
     } else {
         throw new \InvalidArgumentException(sprintf('Can\'t find organization with identifier %s', $organization));
     }
 }
 public function testSerialize()
 {
     $newToken = unserialize(serialize($this->token));
     $this->assertEquals($newToken->getUser()->getId(), $this->token->getUser()->getId());
     $this->assertEquals($newToken->getOrganizationContext()->getId(), $this->token->getOrganizationContext()->getId());
 }