Returns the user with the given id.
public findUserById ( integer $id ) : Sulu\Component\Security\Authentication\UserInterface | ||
$id | integer | The user to find |
리턴 | Sulu\Component\Security\Authentication\UserInterface |
/** * {@inheritdoc} */ public function save($data, $userId, $id = null) { $name = $data['name']; try { // load existing tag if id is given and create a new one otherwise if ($id) { $tag = $this->tagRepository->findTagById($id); if (!$tag) { throw new TagNotFoundException($id); } } else { $tag = new Tag(); } $user = $this->userRepository->findUserById($userId); // update data $tag->setName($name); $tag->setChanger($user); if (!$id) { $tag->setCreator($user); $this->em->persist($tag); } $this->em->flush(); return $tag; } catch (UniqueConstraintViolationException $exc) { throw new TagAlreadyExistsException($name); } }
/** * {@inheritdoc} */ public function save($data, $userId, $id = null) { $name = $data['name']; try { // load existing tag if id is given and create a new one otherwise if ($id) { $tag = $this->tagRepository->findTagById($id); if (!$tag) { throw new TagNotFoundException($id); } } else { $tag = new Tag(); } $user = $this->userRepository->findUserById($userId); // update data $tag->setName($name); $tag->setChanger($user); if (!$id) { $tag->setCreator($user); $this->em->persist($tag); } $this->em->flush(); return $tag; } catch (DBALException $exc) { if ($exc->getPrevious()->getCode() == 23000) { // Check if unique constraint fails throw new TagAlreadyExistsException($name); } else { throw $exc; } } }
/** * {@inheritdoc} */ public function save(array $data, $locale, $userId, $id = null) { $user = $this->userRepository->findUserById($userId); // TODO SECURITY: Only the user which is referenced by the filter should be allowed to // to change the filter - or the administrator if ($id) { $filter = $this->filterRepository->findByIdAndLocale($id, $locale); if (!$filter) { throw new FilterNotFoundException($id); } $filter = new Filter($filter, $locale); } else { $this->checkData($data, true); $filter = new Filter(new FilterEntity(), $locale); $filter->setCreated(new \DateTime()); $filter->setCreator($user); $this->em->persist($filter->getEntity()); } // set direct properties and translation $filter->setChanged(new \DateTime()); $filter->setChanger($user); $filter->setName($this->getProperty($data, 'name', $filter->getName())); if (array_key_exists('context', $data)) { if ($this->hasContext($data['context'])) { $filter->setContext($data['context']); } else { throw new UnknownContextException($data['context']); } } if (array_key_exists('private', $data) && $data['private'] === true) { $filter->setPrivate($data['private']); $filter->setUser($user); } else { $filter->setPrivate(false); $filter->setUser(null); } $filter->setConjunction($this->getProperty($data, 'conjunction', $filter->getConjunction())); $filter->setChanger($user); $filter->setChanged(new \DateTime()); // update condition groups and conditions if (isset($data['conditionGroups'])) { $get = function (ConditionGroupEntity $conditionGroup) { return $conditionGroup->getId(); }; $add = function ($conditionGroupData) use($filter) { return $this->addConditionGroup($filter, $conditionGroupData); }; $delete = function (ConditionGroupEntity $conditionGroup) use($filter) { $this->em->remove($conditionGroup); return true; }; $update = function (ConditionGroupEntity $conditionGroup, $matchedEntry) { return $this->updateConditionGroup($conditionGroup, $matchedEntry); }; $this->processSubEntities($filter->getEntity()->getConditionGroups(), $data['conditionGroups'], $get, $add, $update, $delete); } $this->em->flush(); return $filter; }
/** * @param int $id * * @return UserInterface */ public function enableUser($id) { /** @var UserInterface $user */ $user = $this->userRepository->findUserById($id); $user->setEnabled(true); $this->em->persist($user); $this->em->flush(); return $user; }
/** * {@inheritdoc} */ public function save($data, $userId, $locale, $patch = false) { if ($this->getProperty($data, 'id')) { $categoryEntity = $this->findById($this->getProperty($data, 'id')); } else { $categoryEntity = $this->categoryRepository->createNew(); } // set user properties if userId is set. else these properties are set by the UserBlameSubscriber on save. if ($user = $userId ? $this->userRepository->findUserById($userId) : null) { if (!$categoryEntity->getCreator()) { $categoryEntity->setCreator($user); } $categoryEntity->setChanger($user); } $categoryWrapper = $this->getApiObject($categoryEntity, $locale); if (!$patch || $this->getProperty($data, 'name')) { $translationEntity = $this->findOrCreateCategoryTranslation($categoryEntity, $categoryWrapper, $locale); $translationEntity->setTranslation($this->getProperty($data, 'name', null)); } if (!$patch || $this->getProperty($data, 'description')) { $translationEntity = $this->findOrCreateCategoryTranslation($categoryEntity, $categoryWrapper, $locale); $translationEntity->setDescription($this->getProperty($data, 'description', null)); } if (!$patch || $this->getProperty($data, 'medias')) { $translationEntity = $this->findOrCreateCategoryTranslation($categoryEntity, $categoryWrapper, $locale); $translationEntity->setMedias(array_map(function ($item) { return $this->em->getReference(Media::class, $item); }, $this->getProperty($data, 'medias', []))); } $key = $this->getProperty($data, 'key'); if (!$patch || $key) { $categoryWrapper->setKey($key); } if (!$patch || $this->getProperty($data, 'meta')) { $metaData = is_array($this->getProperty($data, 'meta')) ? $this->getProperty($data, 'meta') : []; $metaEntities = []; foreach ($metaData as $meta) { $metaEntity = $this->categoryMetaRepository->createNew(); $metaEntity->setId($this->getProperty($meta, 'id')); $metaEntity->setKey($this->getProperty($meta, 'key')); $metaEntity->setValue($this->getProperty($meta, 'value')); $metaEntity->setLocale($this->getProperty($meta, 'locale')); $metaEntities[] = $metaEntity; } $categoryWrapper->setMeta($metaEntities); } if (!$patch || $this->getProperty($data, 'parent')) { if ($this->getProperty($data, 'parent')) { $parentEntity = $this->findById($this->getProperty($data, 'parent')); } else { $parentEntity = null; } $categoryWrapper->setParent($parentEntity); } if (!$categoryWrapper->getName()) { throw new CategoryNameMissingException(); } $categoryEntity = $categoryWrapper->getEntity(); $this->em->persist($categoryEntity); try { $this->em->flush(); } catch (UniqueConstraintViolationException $e) { throw new CategoryKeyNotUniqueException($key); } return $categoryEntity; }
/** * Returns a user for a given user-id. * * @param $userId * * @return UserInterface */ protected function getUser($userId) { return $this->userRepository->findUserById($userId); }
/** * Returns a user for a given user-id. * * @param $userId * * @return \Sulu\Component\Security\Authentication\UserInterface */ private function getUser($userId) { return $this->userRepository->findUserById($userId); }
/** * Creates a new Order Entity. * * @param array $data The data array, which will be used for setting the orders data * @param string $locale Locale * @param int $userId Id of the User, which is is saved as creator / changer * @param int|null $id If defined, the Order with the given ID will be updated * @param int|null $statusId if defined, the status will be set to the given value * @param bool $flush Defines if a flush should be performed * @param bool $patch * * @throws EntityNotFoundException * @throws MissingOrderAttributeException * @throws OrderDependencyNotFoundException * @throws OrderException * @throws OrderNotFoundException * * @return null|Order */ public function save(array $data, $locale, $userId = null, $id = null, $statusId = null, $flush = true, $patch = true) { $isNewOrder = !$id; if (!$isNewOrder) { $order = $this->findByIdAndLocale($id, $locale); if (!$order) { throw new OrderNotFoundException($id); } } else { $order = $this->orderFactory->createApiEntity($this->orderFactory->createEntity(), $locale); $this->checkRequiredData($data, $id === null); } $user = $userId ? $this->userRepository->findUserById($userId) : null; $order->setOrderNumber($this->getPropertyBasedOnPatch($data, 'orderNumber', $order->getOrderNumber(), $patch)); $order->setCurrencyCode($this->getPropertyBasedOnPatch($data, 'currencyCode', $order->getCurrencyCode(), $patch)); $order->setCostCentre($this->getPropertyBasedOnPatch($data, 'costCentre', $order->getCostCentre(), $patch)); $order->setCommission($this->getPropertyBasedOnPatch($data, 'commission', $order->getCommission(), $patch)); $order->setTaxfree($this->getPropertyBasedOnPatch($data, 'taxfree', $order->getTaxfree(), $patch)); $order->setNetShippingCosts($this->getPropertyBasedOnPatch($data, 'netShippingCosts', $order->getNetShippingCosts(), $patch)); $order->setInternalNote($this->getPropertyBasedOnPatch($data, 'internalNote', $order->getInternalNote(), $patch)); // Set type of order (if set). $this->setOrderType($data, $order, $patch); $this->setDate($data, 'desiredDeliveryDate', $order->getDesiredDeliveryDate(), array($order, 'setDesiredDeliveryDate')); $this->setDate($data, 'orderDate', $order->getOrderDate(), array($order, 'setOrderDate')); $this->setTermsOfDelivery($data, $order, $patch); $this->setTermsOfPayment($data, $order, $patch); $account = $this->setCustomerAccount($data, $order, $patch); // Set session - id. $sessionId = $this->session->getId(); $order->setSessionId($sessionId); // Add contact. $contact = $this->addContactRelation($data, 'customerContact', function ($contact) use($order) { $order->setCustomerContact($contact); }); // Add contact. $this->addContactRelation($data, 'responsibleContact', function ($contact) use($order) { $order->setResponsibleContact($contact); }); // Create order (POST). if ($order->getId() == null) { $order->setCreated(new DateTime()); $order->setCreator($user); $this->em->persist($order->getEntity()); // Set status to created if not defined. if ($statusId === null) { $statusId = OrderStatus::STATUS_CREATED; } // Create OrderAddress. $deliveryAddress = new OrderAddress(); $invoiceAddress = new OrderAddress(); // Persist entities. $this->em->persist($deliveryAddress); $this->em->persist($invoiceAddress); // Assign to order. $order->setDeliveryAddress($deliveryAddress); $order->setInvoiceAddress($invoiceAddress); } // Set order status. if ($statusId !== null) { $this->convertStatus($order, $statusId); } // If not new and contact is not set, use old contact. if (!$isNewOrder && !$contact) { $contact = $order->getEntity()->getCustomerContact(); } $contactFullName = null; if ($contact) { $contactFullName = $contact->getFullName(); } if (isset($data['invoiceAddress'])) { // Set customer name to account if set, otherwise to contact. $contactFullName = $this->orderAddressManager->getContactData($data['invoiceAddress'], $contact)['fullName']; // Set OrderAddress data. $this->orderAddressManager->setOrderAddress($order->getEntity()->getInvoiceAddress(), $data['invoiceAddress'], $contact, $account); } if (isset($data['deliveryAddress'])) { $this->orderAddressManager->setOrderAddress($order->getEntity()->getDeliveryAddress(), $data['deliveryAddress'], $contact, $account); } // Set customer name. $customerName = $account !== null ? $account->getName() : $contactFullName; if ($customerName) { $order->setCustomerName($customerName); } // Handle items. if (!$this->processItems($data, $order, $locale, $userId)) { throw new OrderException('Error while processing items'); } $order->setChanged(new DateTime()); $order->setChanger($user); $this->updateApiEntity($order, $locale); if ($flush) { $this->em->flush(); } return $order; }
/** * Saves a shipping * * @param array $data array of data to be set * @param $locale locale in which the data should be set * @param $userId * @param null|int $id * @param null|int $statusId * @param bool $flush * * @return null|Shipping * @throws Exception\ShippingNotFoundException * @throws Exception\ShippingException */ public function save(array $data, $locale, $userId, $id = null, $statusId = null, $flush = true) { if ($id) { $shipping = $this->findByIdAndLocale($id, $locale); if (!$shipping) { throw new ShippingNotFoundException($id); } } else { $shipping = $this->shippingFactory->createApiEntity($this->shippingFactory->createEntity(), $locale); } // check for data $this->checkRequiredData($data, $id === null); $user = $this->userRepository->findUserById($userId); $shipping->setShippingNumber($this->getProperty($data, 'shippingNumber', $shipping->getShippingNumber())); $shipping->setWidth($this->getProperty($data, 'width', $shipping->getWidth())); $shipping->setHeight($this->getProperty($data, 'height', $shipping->getHeight())); $shipping->setLength($this->getProperty($data, 'length', $shipping->getLength())); $shipping->setWeight($this->getProperty($data, 'weight', $shipping->getWeight())); $shipping->setCommission($this->getProperty($data, 'commission', $shipping->getCommission())); $shipping->setTrackingId($this->getProperty($data, 'trackingId', $shipping->getTrackingId())); $shipping->setTrackingUrl($this->getProperty($data, 'trackingUrl', $shipping->getTrackingUrl())); // internal note $shipping->setInternalNote($this->getProperty($data, 'internalNote', $shipping->getInternalNote())); $this->setShippingOrder($data, $shipping); // TODO: check if empty string overrides note $shipping->setNote($this->getProperty($data, 'note', $shipping->getNote())); // set expected delivery date if (($expectedDeliveryDate = $this->getProperty($data, 'expectedDeliveryDate', $shipping->getExpectedDeliveryDate())) !== null) { if (is_string($expectedDeliveryDate)) { $expectedDeliveryDate = new DateTime($data['expectedDeliveryDate']); } $shipping->setExpectedDeliveryDate($expectedDeliveryDate); } // set terms of delivery $shipping->setTermsOfDeliveryContent($this->getProperty($data, 'termsOfDeliveryContent', null)); // set terms of payment $shipping->setTermsOfPaymentContent($this->getProperty($data, 'termsOfPaymentContent', null)); // create shipping (POST) if ($shipping->getId() == null) { $shipping->setCreated(new DateTime()); $shipping->setCreator($user); $this->em->persist($shipping->getEntity()); // set status to created if not defined if ($statusId === null) { $statusId = ShippingStatusEntity::STATUS_CREATED; } // create OrderAddress $deliveryAddress = new OrderAddress(); // persist entities $this->em->persist($deliveryAddress); // assign $shipping->setDeliveryAddress($deliveryAddress); } // set order status if ($statusId !== null) { $this->convertStatus($shipping, $statusId); } // set order address $deliveryAddress = $shipping->getDeliveryAddress(); if ($addressData = $this->getProperty($data, 'deliveryAddress')) { $deliveryAddress->setStreet($this->getProperty($addressData, 'street', $deliveryAddress->getStreet())); $deliveryAddress->setAddition($this->getProperty($addressData, 'addition', $deliveryAddress->getAddition())); $deliveryAddress->setNumber($this->getProperty($addressData, 'number', $deliveryAddress->getNumber())); $deliveryAddress->setCity($this->getProperty($addressData, 'city', $deliveryAddress->getCity())); $deliveryAddress->setZip($this->getProperty($addressData, 'zip', $deliveryAddress->getZip())); $deliveryAddress->setState($this->getProperty($addressData, 'state', $deliveryAddress->getState())); $deliveryAddress->setCountry($this->getProperty($addressData, 'country', $deliveryAddress->getCountry())); $deliveryAddress->setPostboxNumber($this->getProperty($addressData, 'postboxNumber', $deliveryAddress->getPostboxNumber())); $deliveryAddress->setPostboxCity($this->getProperty($addressData, 'postboxCity', $deliveryAddress->getPostboxCity())); $deliveryAddress->setPostboxPostcode($this->getProperty($addressData, 'postboxPostcode', $deliveryAddress->getPostboxPostcode())); $deliveryAddress->setTitle($this->getProperty($addressData, 'title', $deliveryAddress->getTitle())); $deliveryAddress->setFirstName($this->getProperty($addressData, 'firstName', $deliveryAddress->getFirstName())); $deliveryAddress->setLastName($this->getProperty($addressData, 'lastName', $deliveryAddress->getLastName())); $deliveryAddress->setAccountName($this->getProperty($addressData, 'accountName', $deliveryAddress->getAccountName())); $deliveryAddress->setUid($this->getProperty($addressData, 'uid', $deliveryAddress->getUid())); $deliveryAddress->setPhone($this->getProperty($addressData, 'phone', $deliveryAddress->getPhone())); $deliveryAddress->setPhoneMobile($this->getProperty($addressData, 'phoneMobile', $deliveryAddress->getPhoneMobile())); } // handle items if (!$this->processItems($data, $shipping, $locale, $userId)) { throw new ShippingException('Error while processing items'); } $shipping->setChanged(new DateTime()); $shipping->setChanger($user); if ($flush) { $this->em->flush(); } return $shipping; }
/** * Sends a keep message, to tell the server that the connection is still working. * * @param ConnectionInterface $conn * @param MessageHandlerContext $context * @param array $msg * * @return array */ private function keep(ConnectionInterface $conn, MessageHandlerContext $context, array $msg) { $user = $this->userRepository->findUserById($msg['userId']); $this->updateCollaboration($conn, $msg['type'], $msg['id'], $context->getId(), $user); return ['type' => $msg['type'], 'id' => $msg['id']]; }
/** * Creates an item, but does not flush. * * @param array $data * @param string $locale * @param int|null $userId * @param ApiItemInterface|ItemInterface|null $item * @param int|null $itemStatusId * @param ContactInterface|null $contact The contact that should be used for order-address * @param ItemInterface|null $lastProcessedProductItem * * @return ApiItemInterface * * @throws ItemException * @throws ProductException * @throws ProductNotFoundException * @throws \Exception */ public function save(array $data, $locale, $userId = null, $item = null, $itemStatusId = null, ContactInterface $contact = null, ItemInterface $lastProcessedProductItem = null) { $itemEntity = $this->itemFactory->createEntity(); $isNewItem = !$item; // Check if required data for creating an item is set. if ($isNewItem) { $this->checkRequiredData($data, true); $item = $this->itemFactory->createApiEntity($this->itemFactory->createEntity(), $locale); } if ($item instanceof ItemInterface) { $item = $this->itemFactory->createApiEntity($item, $locale); } $user = $userId ? $this->userRepository->findUserById($userId) : null; $account = null; // If no contact is given take user as fallback. if (!$contact && !!$user) { $contact = $user->getContact(); } if ($contact) { $account = $contact->getMainAccount(); } $item->setQuantity($this->getProperty($data, 'quantity')); $item->setUseProductsPrice($this->getProperty($data, 'useProductsPrice', true)); $this->setDate($data, 'deliveryDate', null, [$item, 'setDeliveryDate']); $item->setType($this->getProperty($data, 'type', BaseItem::TYPE_PRODUCT)); // Check if the product or addon relation on a saved item still exists, else set type custom. if (!$isNewItem) { if ($item->getType() === BaseItem::TYPE_PRODUCT && $item->getProduct() === null) { $item->setType(BaseItem::TYPE_CUSTOM); } elseif ($item->getType() === BaseItem::TYPE_ADDON && $item->getAddon() === null) { $item->setType(BaseItem::TYPE_CUSTOM); } } switch ($item->getType()) { case BaseItem::TYPE_PRODUCT: if ($isNewItem) { $productData = $this->getProperty($data, 'product'); if ($productData) { // Set Product's data to item. $this->setItemByProductData($productData, $item, $locale); } } break; case BaseItem::TYPE_CUSTOM: if ($isNewItem) { $item->setUseProductsPrice(false); } $item->setName($this->getProperty($data, 'name', $item->getName())); $item->setTax($this->getProperty($data, 'tax', $item->getTax())); $item->setNumber($this->getProperty($data, 'number', $item->getNumber())); $item->setDescription($this->getProperty($data, 'description', $item->getDescription())); $item->setQuantityUnit($this->getProperty($data, 'quantityUnit', $item->getQuantityUnit())); break; case BaseItem::TYPE_ADDON: if ($isNewItem) { $productData = $this->getProperty($data, 'product'); $parent = null; if ($productData) { // Set Product's data to item. $product = $this->setItemByProductData($productData, $item, $locale); // Check if product has a parent. $parent = $product->getParent(); } if ($lastProcessedProductItem) { $this->setAddonData($lastProcessedProductItem, $item->getEntity(), $parent); } } break; default: throw new ItemException('Unhandled item type found'); break; } if (method_exists($item, 'getSupplierName')) { $item->setSupplierName($this->getProperty($data, 'supplierName', $item->getSupplierName())); } // Update prices. $this->updatePrices($item, $data); $item->setDiscount($this->getProperty($data, 'discount', $item->getDiscount())); $item->setIsRecurringPrice($this->getProperty($data, 'isRecurringPrice', $item->isRecurringPrice())); $item->setCostCentre($this->getProperty($data, 'costCentre')); // Set delivery-address for item. if (isset($data['deliveryAddress'])) { $this->setItemDeliveryAddress($data['deliveryAddress'], $item, $contact, $account); } // Create new item. if ($item->getId() == null) { $item->setCreated(new DateTime()); $item->setCreator($user); $this->em->persist($item->getEntity()); if (!($itemStatusId = null)) { $itemStatusId = $itemEntity::STATUS_CREATED; } } if ($itemStatusId) { $this->addStatus($item, $itemStatusId); } // Handle attributes. $this->processAttributes($data, $item, $locale); $item->setChanged(new DateTime()); $item->setChanger($user); return $item; }