accessStorage = $accessStorage; $this->nodeAccessRecordFactory = $nodeAccessRecordFactory; $this->entityTypeManager = $entityTypeManager; $this->userEntityStorage = $this->entityTypeManager->getStorage('user'); $this->node = $this->entityTypeManager->getStorage('node'); $this->accessCheck = $accessCheck; $this->database = $database; } /** * @return NodeAccessRecordModel */ public function createGrant($nid, $gid) { return $this->nodeAccessRecordFactory->create( AccessStorage::NODE_ACCESS_REALM, $gid, $nid, $this->accessStorage->getLangCode($nid), 0, 0 ); } /** * @return int */ public function getUniqueGid() { return $this->uniqueGid; } /** * @param int $uniqueGid */ public function setUniqueGid($uniqueGid) { $this->uniqueGid = $uniqueGid; } /** * @param $uid * @param $nodeType * @param $nid * * @return bool */ public function canUserUpdateNode($uid, $nodeType, $nid) { $user = $this->getUserInstance($uid); $this->setLoadedUid($uid); if ($user->hasPermission('edit any ' . $nodeType . ' content')) { return TRUE; } if ($this->isNodeOwner($nid, $uid) && $this->canUpdateOwnNode($uid, $nodeType)) { return TRUE; } return FALSE; } public function canUserBypassNodeAccess($uid) { $user = $this->getUserInstance($uid); if ($user->hasPermission('bypass node access')) { return TRUE; } return FALSE; } /** * @param $uid * @param $nodeType * @param $nid * * @return bool */ public function canUserDeleteNode($uid, $nodeType, $nid) { $user = $this->getUserInstance($uid); if ($user->hasPermission('delete any ' . $nodeType . ' content')) { return TRUE; } if ($this->isNodeOwner($nid, $uid) && $this->canDeleteOwnNode($uid, $nodeType)) { return TRUE; } return FALSE; } private function getGrantDelete($uid, $nodeType, $nid) { if ($this->canUserBypassNodeAccess($uid)) { return 1; } if ($this->canUserDeleteNode($uid, $nodeType, $nid)) { return 1; } return 0; } private function getGrantUpdate($uid, $nodeType, $nid) { if ($this->canUserBypassNodeAccess($uid)) { return 1; } if ($this->canUserUpdateNode($uid, $nodeType, $nid)) { return 1; } return 0; } /** * @param $nid * @param $uid * * @return bool */ public function isNodeOwner($nid, $uid) { $node = $this->node->load($nid); if (intval($node->getOwnerId()) == intval($uid)) { return TRUE; } return FALSE; } private function canUpdateOwnNode($uid, $nodeType) { $user = $this->getUserInstance($uid); if ($user->hasPermission('edit own ' . $nodeType . ' content')) { return 1; } return 0; } private function canDeleteOwnNode($uid, $nodeType) { $user = $this->getUserInstance($uid); if ($user->hasPermission('delete own ' . $nodeType . ' content')) { return 1; } return 0; } /** * @param $nid * * @return array */ public function getGrantsByNid($nid) { $grants = []; foreach ($this->grants as $grant) { if ($grant->nid == $nid) { $grants[] = $grant; } } return $grants; } /** * @return int */ public function getLoadedUid() { return $this->loadedUid; } /** * @param int $loadedUid */ public function setLoadedUid($loadedUid) { $this->loadedUid = $loadedUid; } /** * @return User */ public function getUserInstance($uid) { if ($this->getLoadedUid() !== $uid) { $user = $this->userEntityStorage->load($uid); $this->setUserInstance($user); return $user; } return $this->userInstance; } /** * @param User $userInstance */ public function setUserInstance($userInstance) { $this->userInstance = $userInstance; } /** * @param int $nid * * @return bool */ public function isAccessRecordExisting($nid) { $query = $this->database->select('node_access', 'na') ->fields('na', ['nid']) ->condition('na.nid', $nid) ->condition('na.realm', AccessStorage::NODE_ACCESS_REALM); $result = $query->execute() ->fetchCol(); if (empty($result)) { return FALSE; } return TRUE; } }