/** * @param User $user * @param array $options * @throws InvalidArgumentException */ protected function updateUser(User $user, $options) { if (!empty($options['user-role'])) { $role = $this->getEntityManager()->getRepository('OroUserBundle:Role')->findOneBy(array('role' => $options['user-role'])); if (!$role) { throw new InvalidArgumentException('Invalid Role'); } $user->addRole($role); } if (!empty($options['user-business-unit'])) { $businessUnit = $this->getEntityManager()->getRepository('OroOrganizationBundle:BusinessUnit')->findOneBy(array('name' => $options['user-business-unit'])); if (!$businessUnit) { throw new InvalidArgumentException('Invalid Business Unit'); } $user->setOwner($businessUnit)->addBusinessUnit($businessUnit); } if (!empty($options['user-name'])) { $user->setUsername($options['user-name']); } if (!empty($options['user-password'])) { $user->setPlainPassword($options['user-password']); } $properties = ['email', 'firstname', 'lastname']; foreach ($properties as $property) { if (!empty($options['user-' . $property])) { $user->{'set' . str_replace('-', '', $property)}($options['user-' . $property]); } } $this->getUserManager()->updateUser($user); }
/** * {@inheritdoc} */ public function load(ObjectManager $manager) { /** @var UserManager $userManager */ $userManager = $this->container->get('oro_user.manager'); $role = $manager->getRepository('OroUserBundle:Role')->findOneByRole('ROLE_ADMINISTRATOR'); $group = $manager->getRepository('OroUserBundle:Group')->findOneByName('Administrators'); $unit = $manager->getRepository('OroOrganizationBundle:BusinessUnit')->findOneByName('Main'); $organization = $manager->getRepository('OroOrganizationBundle:Organization')->getFirst(); $user = new User(); $user->setUsername('owner_User'); $user->addGroup($group); $user->addRole($role); $user->addBusinessUnit($unit); $user->setFirstname('Test Owner FirstName'); $user->setLastname('Test Owner LastName'); $user->setEmail('*****@*****.**'); $user->setOwner($unit); $user->addGroup($group); $user->setPlainPassword('test password'); $user->setSalt(md5(mt_rand(1, 222))); $user->setOrganization($organization); $userManager->updateUser($user); $this->setReference('owner_user', $user); $manager->flush(); }
public function testOwners() { $entity = new User(); $businessUnit = new BusinessUnit(); $this->assertEmpty($entity->getOwner()); $entity->setOwner($businessUnit); $this->assertEquals($businessUnit, $entity->getOwner()); }
/** * @param User $user * @param array $options * @throws InvalidArgumentException * @return $this */ protected function setBusinessUnit(User $user, $options) { if (!empty($options['user-business-unit'])) { $businessUnit = $this->getEntityManager()->getRepository('OroOrganizationBundle:BusinessUnit')->findOneBy(['name' => $options['user-business-unit']]); if (!$businessUnit) { throw new InvalidArgumentException('Invalid Business Unit'); } $user->setOwner($businessUnit)->setOrganization($businessUnit->getOrganization())->addBusinessUnit($businessUnit); } return $this; }