/**
  * {@inheritdoc}
  */
 public function reverseTransform($value)
 {
     if (null === $value) {
         return null;
     } elseif (is_array($value)) {
         return $this->manager->getBusinessUnitRepo()->findBy(['id' => $value]);
     }
     return $this->manager->getBusinessUnitRepo()->find($value);
 }
 /**
  * @dataProvider dataProvider
  */
 public function testCanUserBeSetAsOwner($currentUser, $newUser, $accessLevel, $organizationContext, $isCanBeSet)
 {
     $tree = new OwnerTree();
     $this->addUserInfoToTree($tree, $currentUser);
     $this->addUserInfoToTree($tree, $newUser);
     $treeProvider = $this->getMockBuilder('Oro\\Bundle\\SecurityBundle\\Owner\\OwnerTreeProvider')->disableOriginalConstructor()->getMock();
     $treeProvider->expects($this->any())->method('getTree')->will($this->returnValue($tree));
     $result = $this->businessUnitManager->canUserBeSetAsOwner($currentUser, $newUser, $accessLevel, $treeProvider, $organizationContext);
     $this->assertEquals($isCanBeSet, $result);
 }
 /**
  * @param mixed $id
  * @return null|BusinessUnit
  * @throws TransformationFailedException
  */
 public function reverseTransform($id)
 {
     if (!$id) {
         return null;
     }
     $businessUnit = $this->manager->getBusinessUnitRepo()->findOneBy(array('id' => $id));
     if (null === $businessUnit) {
         throw new TransformationFailedException(sprintf('Business Unit with id "%s" does not exist.', $id));
     }
     return $businessUnit;
 }
Beispiel #4
0
 /**
  * @param FormEvent $event
  */
 public function onPreSetData(FormEvent $event)
 {
     $form = $event->getForm();
     $data = $event->getData();
     if ($data) {
         if ($data->getId()) {
             $form->remove('businessUnit');
             $form->add('businessUnit', 'oro_business_unit_tree_select', ['label' => 'oro.organization.businessunit.parent.label', 'empty_value' => 'oro.business_unit.form.none_business_user', 'property_path' => 'owner', 'required' => false, 'choices' => $this->getBusinessUnitChoices($this->businessUnitManager->getBusinessUnitsTree(null, $this->securityFacade->getOrganizationId())), 'forbidden_business_unit_ids' => $this->businessUnitManager->getChildBusinessUnitIds($data->getId(), $this->securityFacade->getOrganizationId())]);
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function buildView(FormView $view, FormInterface $form, array $options)
 {
     $buTree = $this->buManager->getBusinessUnitRepo()->getOrganizationBusinessUnitsTree($this->securityFacade->getOrganizationId());
     $view->vars['organization_tree_ids'] = $buTree;
     /** @var PersistentCollection $businessUnitData */
     $businessUnitData = $view->vars['data']->getBusinessUnits();
     if ($businessUnitData) {
         $businessUnitData = $businessUnitData->map(function ($item) {
             return $item->getId();
         })->getValues();
     }
     $view->vars['selected_organizations'] = [$this->securityFacade->getOrganizationId()];
     $view->vars['selected_business_units'] = $businessUnitData;
 }
 /**
  * {@inheritdoc}
  */
 public function process(User $user)
 {
     $this->form->setData($user);
     if (in_array($this->request->getMethod(), array('POST', 'PUT'))) {
         $this->form->submit($this->request);
         if ($this->form->isValid()) {
             $businessUnits = $this->request->get('businessUnits', array());
             if ($businessUnits) {
                 $businessUnits = array_keys($businessUnits);
             }
             if ($this->businessUnitManager) {
                 $this->businessUnitManager->assignBusinessUnits($user, $businessUnits);
             }
             $this->onSuccess($user);
             return true;
         }
     }
     return false;
 }
 /**
  * Get business units ids for current user for current access level
  *
  * @return array
  *  value -> business unit id
  */
 protected function getBusinessUnitIds()
 {
     if (AccessLevel::SYSTEM_LEVEL == $this->accessLevel) {
         return $this->businessUnitManager->getBusinessUnitIds();
     } elseif (AccessLevel::LOCAL_LEVEL == $this->accessLevel) {
         return $this->treeProvider->getTree()->getUserBusinessUnitIds($this->currentUser->getId(), $this->getOrganizationContextId());
     } elseif (AccessLevel::DEEP_LEVEL === $this->accessLevel) {
         return $this->treeProvider->getTree()->getUserSubordinateBusinessUnitIds($this->currentUser->getId(), $this->getOrganizationContextId());
     } elseif (AccessLevel::GLOBAL_LEVEL === $this->accessLevel) {
         return $this->businessUnitManager->getBusinessUnitIds($this->getOrganizationContextId());
     }
     return [];
 }
 /**
  * @return null|BusinessUnit
  */
 protected function getCurrentBusinessUnit()
 {
     $user = $this->getCurrentUser();
     if (!$user) {
         return null;
     }
     $businessUnits = $user->getBusinessUnits();
     if (!$this->isAssignGranted) {
         return $businessUnits->first();
     }
     // if assign is granted then only allowed business units can be used
     $allowedBusinessUnits = array_keys($this->getTreeOptions($this->businessUnitManager->getBusinessUnitsTree()));
     /** @var BusinessUnit $businessUnit */
     foreach ($businessUnits as $businessUnit) {
         if (in_array($businessUnit->getId(), $allowedBusinessUnits)) {
             return $businessUnit;
         }
     }
     return null;
 }
 /**
  * {@inheritdoc}
  */
 public function setDefaultOptions(OptionsResolverInterface $resolver)
 {
     $resolver->setDefaults(array('choices' => $this->businessUnitManager->getTreeOptions($this->businessUnitManager->getBusinessUnitsTree())));
 }
 public function getBusinessUnitCount()
 {
     return $this->manager->getBusinessUnitRepo()->getBusinessUnitsCount();
 }