X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fpermissions_by_term%2Fsrc%2FService%2FNodeAccess.php;fp=web%2Fmodules%2Fcontrib%2Fpermissions_by_term%2Fsrc%2FNodeAccess.php;h=42e0798c647f3ff1bdbedfc479c28f0e8119bb36;hp=4015c65952b84475e6c7b81733cb5396fee25eba;hb=1270d9129ce8f27c9b28b10518e32132c58e0aca;hpb=c27c0f0cdaa3f354b1fe54a56ae7e854be6e3f68 diff --git a/web/modules/contrib/permissions_by_term/src/NodeAccess.php b/web/modules/contrib/permissions_by_term/src/Service/NodeAccess.php similarity index 50% rename from web/modules/contrib/permissions_by_term/src/NodeAccess.php rename to web/modules/contrib/permissions_by_term/src/Service/NodeAccess.php index 4015c6595..42e0798c6 100644 --- a/web/modules/contrib/permissions_by_term/src/NodeAccess.php +++ b/web/modules/contrib/permissions_by_term/src/Service/NodeAccess.php @@ -1,17 +1,23 @@ accessStorage = $accessStorage; $this->nodeAccessRecordFactory = $nodeAccessRecordFactory; - $this->entityManager = $entityManager; - $this->userEntityStorage = $this->entityManager->getStorage('user'); - $this->node = $this->entityManager->getStorage('node'); + $this->entityTypeManager = $entityTypeManager; + $this->userEntityStorage = $this->entityTypeManager->getStorage('user'); + $this->node = $this->entityTypeManager->getStorage('node'); $this->accessCheck = $accessCheck; $this->database = $database; } - public function createRealm($uid) { - return 'permissions_by_term__uid_' . $uid; - } - - public function createGrants($nid, $uid = FALSE) { - if (empty($uid)) { - $uids = $this->accessStorage->getAllUids(); - } else { - $uids[] = $uid; - } - - $grants = []; - foreach ($uids as $uid) { - if ($this->accessCheck->canUserAccessByNodeId($nid, $uid)) { - $realm = $this->createRealm($uid); - $nodeType = $this->accessStorage->getNodeType($nid); - $langcode = $this->accessStorage->getLangCode($nid); - $grants[] = $this->nodeAccessRecordFactory->create($realm, $this->createUniqueGid(), $nid, $langcode, $this->getGrantUpdate($uid, $nodeType, $nid), $this->getGrantDelete($uid, $nodeType, $nid)); - } - } - - return $grants; - } - - public function createUniqueGid() { - $uniqueGid = $this->getUniqueGid(); - $uniqueGid++; - $this->setUniqueGid($uniqueGid); - return $this->getUniqueGid(); + /** + * @return NodeAccessRecordModel + */ + public function createGrant($nid, $gid) { + return $this->nodeAccessRecordFactory->create( + AccessStorage::NODE_ACCESS_REALM, + $gid, + $nid, + $this->accessStorage->getLangCode($nid), + 0, + 0 + ); } /** - * @return array + * @return int */ public function getUniqueGid() { return $this->uniqueGid; } /** - * @param array $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); @@ -135,6 +148,13 @@ class NodeAccess { 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')) { @@ -172,6 +192,12 @@ class NodeAccess { return 0; } + /** + * @param $nid + * @param $uid + * + * @return bool + */ public function isNodeOwner($nid, $uid) { $node = $this->node->load($nid); if (intval($node->getOwnerId()) == intval($uid)) { @@ -199,6 +225,11 @@ class NodeAccess { return 0; } + /** + * @param $nid + * + * @return array + */ public function getGrantsByNid($nid) { $grants = []; foreach ($this->grants as $grant) { @@ -244,73 +275,25 @@ class NodeAccess { $this->userInstance = $userInstance; } - public function rebuildByTid($tid, $formState) { - $nids = $this->accessStorage->getNidsByTid($tid); - if (!empty($nids)) { - $this->dropRecordsByNids($nids); - } - - if (empty($this->accessStorage->getSubmittedUserIds()) && empty($this->accessStorage->getSubmittedRolesGrantedAccess($formState))) { - return; - } - - foreach ($nids as $nid) { - $grants = $this->createGrants($nid); - foreach ($grants as $grant) { - $this->database->insert('node_access') - ->fields( - ['nid', 'langcode', 'fallback', 'gid', 'realm', 'grant_view', 'grant_update', 'grant_delete'], - [$nid, $grant->langcode, 1, $grant->gid, $grant->realm, $grant->grant_view, $grant->grant_update, $grant->grant_delete] - ) - ->execute(); - } - } - - } - - private function dropRecordsByNids($nids) { - $this->database->delete('node_access') - ->condition('nid', $nids, 'IN') - ->execute(); - } + /** + * @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); - private function dropRecordsByUid($uid) { - $this->database->delete('node_access') - ->condition('realm', 'permissions_by_term__uid_' . $uid) - ->execute(); - } + $result = $query->execute() + ->fetchCol(); - public function rebuildByUid($uid, $noDrop = FALSE) { - if ($noDrop === FALSE) { - $this->dropRecordsByUid($uid); + if (empty($result)) { + return FALSE; } - $nids = $this->accessStorage->getAllNids(); - - foreach ($nids as $nid) { - $grants = $this->createGrants($nid, $uid); - foreach ($grants as $grant) { - $this->database->insert('node_access') - ->fields( - ['nid', 'langcode', 'fallback', 'gid', 'realm', 'grant_view', 'grant_update', 'grant_delete'], - [$nid, $grant->langcode, 1, $grant->gid, $grant->realm, $grant->grant_view, $grant->grant_update, $grant->grant_delete] - ) - ->execute(); - } - } - - } - - public function rebuildByNid($nid) { - $grants = $this->createGrants($nid); - foreach ($grants as $grant) { - $this->database->insert('node_access') - ->fields( - ['nid', 'langcode', 'fallback', 'gid', 'realm', 'grant_view', 'grant_update', 'grant_delete'], - [$nid, $grant->langcode, 1, $grant->gid, $grant->realm, $grant->grant_view, $grant->grant_update, $grant->grant_delete] - ) - ->execute(); - } + return TRUE; } -} \ No newline at end of file +}