/**
  * @AclAncestor("pim_user_user_edit")
  */
 public function apigenAction(User $user)
 {
     if (!($api = $user->getApi())) {
         $api = new UserApi();
     }
     $api->setApiKey($api->generateKey())->setUser($user);
     $em = $this->getDoctrine()->getManager();
     $em->persist($api);
     $em->flush();
     return $this->getRequest()->isXmlHttpRequest() ? new JsonResponse($api->getApiKey()) : $this->forward('OroUserBundle:User:view', array('user' => $user));
 }
 /**
  * @AclAncestor("pim_user_user_edit")
  *
  * @param int $id
  *
  * @return JsonResponse|Response
  */
 public function apigenAction($id)
 {
     $userRepository = $this->container->get('pim_user.repository.user');
     $user = $userRepository->findOneBy(['id' => $id]);
     if (!($api = $user->getApi())) {
         $api = new UserApi();
     }
     $api->setApiKey($api->generateKey())->setUser($user);
     $em = $this->getDoctrine()->getManager();
     $em->persist($api);
     $em->flush();
     return $this->getRequest()->isXmlHttpRequest() ? new JsonResponse($api->getApiKey()) : $this->forward('OroUserBundle:User:view', array('user' => $user));
 }
Пример #3
0
 public function testGetApiKey()
 {
     $entity = $this->getUser();
     $this->assertEmpty($entity->getApiKeys(), 'Should return some key, even if is not present');
     $organization1 = new Organization();
     $organization1->setName('test1');
     $organization2 = new Organization();
     $organization2->setName('test2');
     $apiKey1 = new UserApi();
     $apiKey1->setApiKey($apiKey1->generateKey());
     $apiKey1->setOrganization($organization1);
     $apiKey2 = new UserApi();
     $apiKey2->setApiKey($apiKey2->generateKey());
     $apiKey2->setOrganization($organization2);
     $entity->addApiKey($apiKey1);
     $entity->addApiKey($apiKey2);
     $this->assertSame($apiKey1->getApiKey(), $entity->getApiKeys()[0]->getApiKey(), 'Should delegate call to userApi entity');
     $this->assertEquals(new ArrayCollection([$apiKey1, $apiKey2]), $entity->getApiKeys());
     $entity->removeApiKey($apiKey2);
     $this->assertEquals(new ArrayCollection([$apiKey1]), $entity->getApiKeys());
 }
 /**
  * {@inheritDoc}
  */
 public function generateKey()
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'generateKey', array());
     return parent::generateKey();
 }
Пример #5
0
 public function testGetApiKey()
 {
     /** @var User $entity */
     $entity = new User();
     $this->assertNotEmpty($entity->getApiKeys(), 'Should return some key, even if is not present');
     $key1 = $entity->getApiKeys();
     usleep(1);
     // need because 'uniqid' generates a unique identifier based on the current time in microseconds
     $this->assertNotSame($key1, $entity->getApiKeys(), 'Should return unique random string');
     $organization1 = new Organization();
     $organization1->setName('test1');
     $organization2 = new Organization();
     $organization2->setName('test2');
     $apiKey1 = new UserApi();
     $apiKey1->setApiKey($apiKey1->generateKey());
     $apiKey1->setOrganization($organization1);
     $apiKey2 = new UserApi();
     $apiKey2->setApiKey($apiKey2->generateKey());
     $apiKey2->setOrganization($organization2);
     $entity->addApiKey($apiKey1);
     $entity->addApiKey($apiKey2);
     $this->assertSame($apiKey1->getApiKey(), $entity->getApiKeys()[0]->getApiKey(), 'Should delegate call to userApi entity');
     $this->assertEquals(new ArrayCollection([$apiKey1, $apiKey2]), $entity->getApiKeys());
 }