View source
<?php
namespace Drupal\permissions_by_term\Service;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\node\Entity\Node;
use Drupal\permissions_by_term\Factory\NodeAccessRecordFactory;
use Drupal\permissions_by_term\Model\NodeAccessRecordModel;
use Drupal\user\Entity\User;
class NodeAccess {
private $uniqueGid = 0;
private $accessStorage;
private $userEntityStorage;
private $node;
private $entityTypeManager;
private $accessCheck;
private $loadedUid;
private $userInstance;
private $database;
public function __construct(AccessStorage $accessStorage, NodeAccessRecordFactory $nodeAccessRecordFactory, EntityTypeManagerInterface $entityTypeManager, AccessCheck $accessCheck, Connection $database) {
$this->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;
}
public function createGrant($nid, $gid) {
return $this->nodeAccessRecordFactory
->create(AccessStorage::NODE_ACCESS_REALM, $gid, $nid, $this->accessStorage
->getLangCode($nid), 0, 0);
}
public function getUniqueGid() {
return $this->uniqueGid;
}
public function setUniqueGid($uniqueGid) {
$this->uniqueGid = $uniqueGid;
}
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;
}
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;
}
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;
}
public function getGrantsByNid($nid) {
$grants = [];
foreach ($this->grants as $grant) {
if ($grant->nid == $nid) {
$grants[] = $grant;
}
}
return $grants;
}
public function getLoadedUid() {
return $this->loadedUid;
}
public function setLoadedUid($loadedUid) {
$this->loadedUid = $loadedUid;
}
public function getUserInstance($uid) {
if ($this
->getLoadedUid() !== $uid) {
$user = $this->userEntityStorage
->load($uid);
$this
->setUserInstance($user);
return $user;
}
return $this->userInstance;
}
public function setUserInstance($userInstance) {
$this->userInstance = $userInstance;
}
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;
}
}