3 namespace Drupal\permissions_by_term;
5 use Drupal\Core\Entity\EntityManager;
6 use Drupal\permissions_by_term\Factory\NodeAccessRecordFactory;
7 use Drupal\user\Entity\User;
8 use Drupal\node\Entity\Node;
9 use Drupal\Core\Database\Driver\mysql\Connection;
14 * @var array $uniqueGid
16 private $uniqueGid = 0;
19 * @var AccessStorage $accessStorage
21 private $accessStorage;
24 * @var User $userEntityStorage
26 private $userEntityStorage;
34 * @var EntityManager $entityManager
36 private $entityManager;
39 * @var AccessCheck $accessCheck
49 * @var User $userInstance
51 private $userInstance;
54 * @var Connection $database
58 public function __construct(AccessStorage $accessStorage, NodeAccessRecordFactory $nodeAccessRecordFactory, EntityManager $entityManager, AccessCheck $accessCheck, Connection $database) {
59 $this->accessStorage = $accessStorage;
60 $this->nodeAccessRecordFactory = $nodeAccessRecordFactory;
61 $this->entityManager = $entityManager;
62 $this->userEntityStorage = $this->entityManager->getStorage('user');
63 $this->node = $this->entityManager->getStorage('node');
64 $this->accessCheck = $accessCheck;
65 $this->database = $database;
68 public function createRealm($uid) {
69 return 'permissions_by_term__uid_' . $uid;
72 public function createGrants($nid, $uid = FALSE) {
74 $uids = $this->accessStorage->getAllUids();
80 foreach ($uids as $uid) {
81 if ($this->accessCheck->canUserAccessByNodeId($nid, $uid)) {
82 $realm = $this->createRealm($uid);
83 $nodeType = $this->accessStorage->getNodeType($nid);
84 $langcode = $this->accessStorage->getLangCode($nid);
85 $grants[] = $this->nodeAccessRecordFactory->create($realm, $this->createUniqueGid(), $nid, $langcode, $this->getGrantUpdate($uid, $nodeType, $nid), $this->getGrantDelete($uid, $nodeType, $nid));
92 public function createUniqueGid() {
93 $uniqueGid = $this->getUniqueGid();
95 $this->setUniqueGid($uniqueGid);
96 return $this->getUniqueGid();
102 public function getUniqueGid() {
103 return $this->uniqueGid;
107 * @param array $uniqueGid
109 public function setUniqueGid($uniqueGid) {
110 $this->uniqueGid = $uniqueGid;
113 public function canUserUpdateNode($uid, $nodeType, $nid) {
114 $user = $this->getUserInstance($uid);
116 $this->setLoadedUid($uid);
118 if ($user->hasPermission('edit any ' . $nodeType . ' content')) {
122 if ($this->isNodeOwner($nid, $uid) && $this->canUpdateOwnNode($uid, $nodeType)) {
129 public function canUserBypassNodeAccess($uid) {
130 $user = $this->getUserInstance($uid);
131 if ($user->hasPermission('bypass node access')) {
138 public function canUserDeleteNode($uid, $nodeType, $nid) {
139 $user = $this->getUserInstance($uid);
140 if ($user->hasPermission('delete any ' . $nodeType . ' content')) {
144 if ($this->isNodeOwner($nid, $uid) && $this->canDeleteOwnNode($uid, $nodeType)) {
151 private function getGrantDelete($uid, $nodeType, $nid) {
152 if ($this->canUserBypassNodeAccess($uid)) {
156 if ($this->canUserDeleteNode($uid, $nodeType, $nid)) {
163 private function getGrantUpdate($uid, $nodeType, $nid) {
164 if ($this->canUserBypassNodeAccess($uid)) {
168 if ($this->canUserUpdateNode($uid, $nodeType, $nid)) {
175 public function isNodeOwner($nid, $uid) {
176 $node = $this->node->load($nid);
177 if (intval($node->getOwnerId()) == intval($uid)) {
184 private function canUpdateOwnNode($uid, $nodeType) {
185 $user = $this->getUserInstance($uid);
186 if ($user->hasPermission('edit own ' . $nodeType . ' content')) {
193 private function canDeleteOwnNode($uid, $nodeType) {
194 $user = $this->getUserInstance($uid);
195 if ($user->hasPermission('delete own ' . $nodeType . ' content')) {
202 public function getGrantsByNid($nid) {
204 foreach ($this->grants as $grant) {
205 if ($grant->nid == $nid) {
216 public function getLoadedUid() {
217 return $this->loadedUid;
221 * @param int $loadedUid
223 public function setLoadedUid($loadedUid) {
224 $this->loadedUid = $loadedUid;
230 public function getUserInstance($uid) {
231 if ($this->getLoadedUid() !== $uid) {
232 $user = $this->userEntityStorage->load($uid);
233 $this->setUserInstance($user);
237 return $this->userInstance;
241 * @param User $userInstance
243 public function setUserInstance($userInstance) {
244 $this->userInstance = $userInstance;
247 public function rebuildByTid($tid, $formState) {
248 $nids = $this->accessStorage->getNidsByTid($tid);
250 $this->dropRecordsByNids($nids);
253 if (empty($this->accessStorage->getSubmittedUserIds()) && empty($this->accessStorage->getSubmittedRolesGrantedAccess($formState))) {
257 foreach ($nids as $nid) {
258 $grants = $this->createGrants($nid);
259 foreach ($grants as $grant) {
260 $this->database->insert('node_access')
262 ['nid', 'langcode', 'fallback', 'gid', 'realm', 'grant_view', 'grant_update', 'grant_delete'],
263 [$nid, $grant->langcode, 1, $grant->gid, $grant->realm, $grant->grant_view, $grant->grant_update, $grant->grant_delete]
271 private function dropRecordsByNids($nids) {
272 $this->database->delete('node_access')
273 ->condition('nid', $nids, 'IN')
277 private function dropRecordsByUid($uid) {
278 $this->database->delete('node_access')
279 ->condition('realm', 'permissions_by_term__uid_' . $uid)
283 public function rebuildByUid($uid, $noDrop = FALSE) {
284 if ($noDrop === FALSE) {
285 $this->dropRecordsByUid($uid);
288 $nids = $this->accessStorage->getAllNids();
290 foreach ($nids as $nid) {
291 $grants = $this->createGrants($nid, $uid);
292 foreach ($grants as $grant) {
293 $this->database->insert('node_access')
295 ['nid', 'langcode', 'fallback', 'gid', 'realm', 'grant_view', 'grant_update', 'grant_delete'],
296 [$nid, $grant->langcode, 1, $grant->gid, $grant->realm, $grant->grant_view, $grant->grant_update, $grant->grant_delete]
304 public function rebuildByNid($nid) {
305 $grants = $this->createGrants($nid);
306 foreach ($grants as $grant) {
307 $this->database->insert('node_access')
309 ['nid', 'langcode', 'fallback', 'gid', 'realm', 'grant_view', 'grant_update', 'grant_delete'],
310 [$nid, $grant->langcode, 1, $grant->gid, $grant->realm, $grant->grant_view, $grant->grant_update, $grant->grant_delete]