/** * @param string $username * @param array $data */ protected function createUser($username, array $data) { $password = isset($data['password']) ? $data['password'] : $username; $firstName = isset($data['first_name']) ? $data['first_name'] : ucfirst($username); $lastName = isset($data['last_name']) ? $data['last_name'] : 'Doe'; $email = isset($data['email']) ? $data['email'] : $username . '@example.com'; $apiKey = isset($data['api_key']) ? $data['api_key'] : $username . '_api_key'; $roles = isset($data['roles']) ? $data['roles'] : array('ROLE_ADMINISTRATOR'); $groups = isset($data['groups']) ? $data['groups'] : array('all'); $user = $this->getUserManager()->createUser(); $api = new UserApi(); $api->setApiKey($apiKey)->setUser($user); $user->setUsername($username)->setPlainPassword($password)->setFirstname($firstName)->setLastname($lastName)->setEmail($email)->setApi($api); foreach ($roles as $role) { $user->addRole($this->getOrCreateRole($role)); } foreach ($groups as $group) { $user->addGroup($this->getOrCreateGroup($group)); } $user->setCatalogLocale($this->getLocale($data['catalogLocale'])); $user->setCatalogScope($this->getChannel($data['catalogScope'])); $user->setDefaultTree($this->getTree($data['defaultTree'])); $this->getUserManager()->updateUser($user); // Following to fix a cascade persist issue on UserApi occuring only during Behat Execution $this->getUserManager()->getStorageManager()->clear('Pim\\Bundle\\UserBundle\\Entity\\User'); $this->getUserManager()->getStorageManager()->clear('Oro\\Bundle\\UserBundle\\Entity\\UserApi'); }
/** * @param string $username * @param array $data */ protected function createUser($username, array $data) { $password = isset($data['password']) ? $data['password'] : $username; $firstName = isset($data['first_name']) ? $data['first_name'] : ucfirst($username); $lastName = isset($data['last_name']) ? $data['last_name'] : 'Doe'; $email = isset($data['email']) ? $data['email'] : $username . '@example.com'; $apiKey = isset($data['api_key']) ? $data['api_key'] : $username . '_api_key'; $roles = isset($data['roles']) ? $data['roles'] : array('ROLE_ADMINISTRATOR'); $groups = isset($data['groups']) ? $data['groups'] : array('all'); $user = $this->getUserManager()->createUser(); $api = new UserApi(); $api->setApiKey($apiKey)->setUser($user); $user->setUsername($username)->setPlainPassword($password)->setFirstname($firstName)->setLastname($lastName)->setEmail($email)->setApi($api); foreach ($roles as $role) { $user->addRole($this->getOrCreateRole($role)); } foreach ($groups as $group) { $user->addGroup($this->getOrCreateGroup($group)); } $user->setCatalogLocale($this->getLocale($data['catalogLocale'])); $user->setUiLocale($this->getLocale($data['uiLocale'])); $user->setCatalogScope($this->getChannel($data['catalogScope'])); $user->setDefaultTree($this->getTree($data['defaultTree'])); $this->getUserManager()->updateUser($user); }
/** * @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)); }
public function load(ObjectManager $manager) { /** @var \Oro\Bundle\UserBundle\Entity\UserManager $userManager */ $userManager = $this->container->get('oro_user.manager'); $role = $userManager->getStorageManager()->getRepository('OroUserBundle:Role')->findBy(array('role' => 'IS_AUTHENTICATED_ANONYMOUSLY')); $user = $userManager->createUser(); $api = new UserApi(); $api->setApiKey('user_api_key')->setUser($user); $user->setUsername(self::USER_NAME)->setPlainPassword(self::USER_PASSWORD)->setFirstName('Simple')->setLastName('User')->addRole($role[0])->setEmail('*****@*****.**')->setApi($api); $userManager->updateUser($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)); }
public function load(ObjectManager $manager) { /** @var \Oro\Bundle\UserBundle\Entity\UserManager $userManager */ $userManager = $this->container->get('oro_user.manager'); $role = $userManager->getStorageManager()->getRepository('OroUserBundle:Role')->findOneBy(array('role' => 'IS_AUTHENTICATED_ANONYMOUSLY')); $group = $userManager->getStorageManager()->getRepository('OroUserBundle:Group')->findOneBy(array('name' => 'Administrators')); $user = $userManager->createUser(); $organization = $manager->getRepository('OroOrganizationBundle:Organization')->getFirst(); $api = new UserApi(); $api->setApiKey('user_api_key')->setOrganization($organization)->setUser($user); $user->setUsername(self::USER_NAME)->setPlainPassword(self::USER_PASSWORD)->setFirstName('Simple')->setLastName('User')->addRole($role)->addGroup($group)->setEmail('*****@*****.**')->setOrganization($organization)->setOrganizations(new ArrayCollection([$organization]))->addApiKey($api)->setSalt(''); $userManager->updateUser($user); }
/** * @param ObjectManager $manager */ public function load(ObjectManager $manager) { /** @var UserManager $userManager */ $userManager = $this->container->get('oro_user.manager'); $role = $userManager->getStorageManager()->getRepository('OroUserBundle:Role')->findBy(array('role' => 'IS_AUTHENTICATED_ANONYMOUSLY')); $user = $userManager->createUser(); /** @var OrganizationManager $organizationManager */ $organizationManager = $this->container->get('oro_organization.organization_manager'); $org = $organizationManager->getOrganizationRepo()->getFirst(); $apiKey = new UserApi(); $apiKey->setApiKey(self::USER_PASSWORD)->setUser($user)->setOrganization($org); $user->setUsername(self::USER_NAME)->setPlainPassword(self::USER_PASSWORD)->setFirstName('Simple')->setLastName('User')->addRole($role[0])->setEmail('*****@*****.**')->addApiKey($apiKey)->setOrganization($org)->addOrganization($org)->setSalt(''); $userManager->updateUser($user); }
/** * @return array */ public function userProvider() { $regularUser = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface'); $regularUser->expects($this->exactly(2))->method('getPassword')->will($this->returnValue(self::TEST_PASSWORD)); $regularUser->expects($this->once())->method('getSalt')->will($this->returnValue(self::TEST_SALT)); $regularUser->expects($this->any())->method('getRoles')->will($this->returnValue([])); $userApiKey = new UserApi(); $userApiKey->setApiKey(self::TEST_API_KEY); $userApiKeys = new ArrayCollection([$userApiKey]); $advancedUser = $this->getMock('Oro\\Bundle\\UserBundle\\Entity\\User'); $advancedUser->expects($this->exactly(2))->method('getApiKeys')->will($this->returnValue($userApiKeys)); $advancedUser->expects($this->never())->method('getPassword'); $advancedUser->expects($this->never())->method('getSalt'); $advancedUser->expects($this->any())->method('getRoles')->will($this->returnValue([])); return ['regular user given, should use password and salt' => [$regularUser, self::TEST_PASSWORD, self::TEST_SALT], 'advanced user given, should take API key only' => [$advancedUser, $userApiKeys]]; }
public function load(ObjectManager $manager) { /** @var UserManager $userManager */ $userManager = $this->container->get('oro_user.manager'); $admin = $manager->getRepository('OroUserBundle:User')->findOneBy(array('username' => 'admin')); $role = $manager->getRepository('OroUserBundle:Role')->findOneBy(array('role' => 'ROLE_ADMINISTRATOR')); $group = $manager->getRepository('OroUserBundle:Group')->findOneBy(array('name' => 'Administrators')); $unit = $manager->getRepository('OroOrganizationBundle:BusinessUnit')->findOneBy(array('name' => 'Main')); if (!$admin) { $admin = $userManager->createUser(); $admin->setUsername('admin')->addRole($role)->addGroup($group); } $admin->setPlainPassword('admin')->setFirstname('John')->setLastname('Doe')->setEmail('*****@*****.**')->setOwner($unit)->setBusinessUnits(new ArrayCollection(array($unit))); $api = new UserApi(); if (!$admin->getApi()) { $api->setApiKey('admin_api_key')->setUser($admin); $admin->setApi($api); } $this->addReference('default_user', $admin); $userManager->updateUser($admin); }
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()); }
/** * @param UserInterface $user * @param string $field * @param mixed $data * * @throws \InvalidArgumentException */ protected function setData(UserInterface $user, $field, $data) { switch ($field) { case 'username': $user->setUsername($data); break; case 'name_prefix': $user->setNamePrefix($data); break; case 'first_name': $user->setFirstName($data); break; case 'middle_name': $user->setMiddleName($data); break; case 'last_name': $user->setLastName($data); break; case 'name_suffix': $user->setNameSuffix($data); break; case 'email': $user->setEmail($data); break; case 'password': $user->setPlainPassword($data); $this->userManager->updatePassword($user); break; case 'birthday': $user->setBirthday(new \DateTime($data, \DateTime::ISO8601)); break; case 'email_notifications': $user->setEmailNotifications($data); break; case 'catalog_locale': $user->setCatalogLocale($this->findLocale($data)); break; case 'user_locale': $user->setUiLocale($this->findLocale($data)); break; case 'catalog_scope': $user->setCatalogScope($this->findChannel($data)); break; case 'default_tree': $user->setDefaultTree($this->findCategory($data)); break; case 'roles': foreach ($data as $code) { $role = $this->findRole($code); $user->addRole($role); } break; case 'groups': foreach ($data as $code) { $role = $this->findGroup($code); $user->addGroup($role); } break; case 'api_key': if (null === ($api = $user->getApi())) { $api = new UserApi(); } $api->setApiKey($data)->setUser($user); $user->setApi($api); break; } }
/** * {@inheritDoc} */ public function setApiKey($apiKey) { $this->__initializer__ && $this->__initializer__->__invoke($this, 'setApiKey', array($apiKey)); return parent::setApiKey($apiKey); }
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()); }