/** * {@inheritdoc} */ public function validate($items, Constraint $constraint) { if (!isset($items)) { return; } /* @var \Drupal\Core\Field\FieldItemListInterface $items */ $field = $items->getFieldDefinition(); /* @var \Drupal\user\UserInterface $account */ $account = $items->getEntity(); if (!isset($account) || !empty($account->_skipProtectedUserFieldConstraint)) { // Looks like we are validating a field not being part of a user, or the // constraint should be skipped, so do nothing. return; } // Only validate for existing entities and if this is the current user. if (!$account->isNew() && $account->id() == $this->currentUser->id()) { /* @var \Drupal\user\UserInterface $account_unchanged */ $account_unchanged = $this->userStorage->loadUnchanged($account->id()); $changed = FALSE; // Special case for the password, it being empty means that the existing // password should not be changed, ignore empty password fields. $value = $items->value; if ($field->getName() != 'pass' || !empty($value)) { // Compare the values of the field this is being validated on. $changed = $items->getValue() != $account_unchanged->get($field->getName())->getValue(); } if ($changed && !$account->checkExistingPassword($account_unchanged)) { $this->context->addViolation($constraint->message, array('%name' => $field->getLabel())); } } }
/** * Adds in the current user as a context. * * @param \Drupal\page_manager\Event\PageManagerContextEvent $event * The page entity context event. */ public function onPageContext(PageManagerContextEvent $event) { $id = $this->account->id(); $current_user = $this->userStorage->load($id); $context = new Context(new ContextDefinition('entity:user', $this->t('Current user'))); $context->setContextValue($current_user); $event->getPageExecutable()->addContext('current_user', $context); }
/** * {@inheritdoc} */ protected function getId($create = TRUE) { if ($this->currentUser->isAuthenticated()) { return $this->currentUser->id(); } elseif (!$this->session->has('uc_cart_id') && $create) { $this->session->set('uc_cart_id', md5(uniqid(rand(), TRUE))); } return $this->session->has('uc_cart_id') ? $this->session->get('uc_cart_id') : FALSE; }
/** * Adds in the current user as a context. * * @param \Drupal\page_manager\Event\PageManagerContextEvent $event * The page entity context event. */ public function onPageContext(PageManagerContextEvent $event) { $id = $this->account->id(); $current_user = $this->userStorage->load($id); $context = new Context(new ContextDefinition('entity:user', $this->t('Current user')), $current_user); $cacheability = new CacheableMetadata(); $cacheability->setCacheContexts(['user']); $context->addCacheableDependency($cacheability); $event->getPage()->addContext('current_user', $context); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->keyValue = $this->getMock('Drupal\\Core\\KeyValueStore\\KeyValueStoreExpirableInterface'); $this->lock = $this->getMock('Drupal\\Core\\Lock\\LockBackendInterface'); $this->currentUser = $this->getMock('Drupal\\Core\\Session\\AccountProxyInterface'); $this->currentUser->expects($this->any())->method('id')->willReturn(1); $this->requestStack = new RequestStack(); $this->tempStore = new PrivateTempStore($this->keyValue, $this->lock, $this->currentUser, $this->requestStack, 604800); $this->ownObject = (object) array('data' => 'test_data', 'owner' => $this->currentUser->id(), 'updated' => REQUEST_TIME); // Clone the object but change the owner. $this->otherObject = clone $this->ownObject; $this->otherObject->owner = 2; }
/** * Gets the current owner based on the current user or the session ID. * * @return string * The owner. */ protected function getOwner() { return $this->currentUser->id() ?: $this->requestStack->getCurrentRequest()->getSession()->getId(); }
/** * {@inheritdoc} */ public function fileEntityFromUri($uri, AccountProxyInterface $user) { $uri = file_stream_wrapper_uri_normalize($uri); $file_info = new \SplFileInfo($uri); // Begin building file entity. $values = ['uid' => $user->id(), 'status' => 0, 'filename' => $file_info->getFilename(), 'uri' => $uri, 'filesize' => $file_info->getSize(), 'filemime' => $this->mimeTypeGuesser->guess($uri)]; /** @var \Drupal\file\FileInterface $file */ $file = $this->entityManager->getStorage('file')->create($values); return $file; }