Backup of database 9 Nov 17
[yaffs-website] / web / modules / contrib / permissions_by_term / src / NodeAccess.php
1 <?php
2
3 namespace Drupal\permissions_by_term;
4
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;
10
11 class NodeAccess {
12
13   /**
14    * @var array $uniqueGid
15    */
16   private $uniqueGid = 0;
17
18   /**
19    * @var AccessStorage $accessStorage
20    */
21   private $accessStorage;
22
23   /**
24    * @var User $userEntityStorage
25    */
26   private $userEntityStorage;
27
28   /**
29    * @var Node $node
30    */
31   private $node;
32
33   /**
34    * @var EntityManager $entityManager
35    */
36   private $entityManager;
37
38   /**
39    * @var AccessCheck $accessCheck
40    */
41   private $accessCheck;
42
43   /**
44    * @var int $loadedUid
45    */
46   private $loadedUid;
47
48   /**
49    * @var User $userInstance
50    */
51   private $userInstance;
52
53   /**
54    * @var Connection $database
55    */
56   private $database;
57
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;
66   }
67
68   public function createRealm($uid) {
69     return 'permissions_by_term__uid_' . $uid;
70   }
71
72   public function createGrants($nid, $uid = FALSE) {
73     if (empty($uid)) {
74       $uids = $this->accessStorage->getAllUids();
75     } else {
76       $uids[] = $uid;
77     }
78
79     $grants = [];
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));
86       }
87     }
88
89     return $grants;
90   }
91
92   public function createUniqueGid() {
93     $uniqueGid = $this->getUniqueGid();
94     $uniqueGid++;
95     $this->setUniqueGid($uniqueGid);
96     return $this->getUniqueGid();
97   }
98
99   /**
100    * @return array
101    */
102   public function getUniqueGid() {
103     return $this->uniqueGid;
104   }
105
106   /**
107    * @param array $uniqueGid
108    */
109   public function setUniqueGid($uniqueGid) {
110     $this->uniqueGid = $uniqueGid;
111   }
112
113   public function canUserUpdateNode($uid, $nodeType, $nid) {
114     $user = $this->getUserInstance($uid);
115
116     $this->setLoadedUid($uid);
117
118     if ($user->hasPermission('edit any ' . $nodeType . ' content')) {
119       return TRUE;
120     }
121
122     if ($this->isNodeOwner($nid, $uid) && $this->canUpdateOwnNode($uid, $nodeType)) {
123       return TRUE;
124     }
125
126     return FALSE;
127   }
128
129   public function canUserBypassNodeAccess($uid) {
130     $user = $this->getUserInstance($uid);
131     if ($user->hasPermission('bypass node access')) {
132       return TRUE;
133     }
134
135     return FALSE;
136   }
137
138   public function canUserDeleteNode($uid, $nodeType, $nid) {
139     $user = $this->getUserInstance($uid);
140     if ($user->hasPermission('delete any ' . $nodeType . ' content')) {
141       return TRUE;
142     }
143
144     if ($this->isNodeOwner($nid, $uid) && $this->canDeleteOwnNode($uid, $nodeType)) {
145       return TRUE;
146     }
147
148     return FALSE;
149   }
150
151   private function getGrantDelete($uid, $nodeType, $nid) {
152     if ($this->canUserBypassNodeAccess($uid)) {
153       return 1;
154     }
155
156     if ($this->canUserDeleteNode($uid, $nodeType, $nid)) {
157       return 1;
158     }
159
160     return 0;
161   }
162
163   private function getGrantUpdate($uid, $nodeType, $nid) {
164     if ($this->canUserBypassNodeAccess($uid)) {
165       return 1;
166     }
167
168     if ($this->canUserUpdateNode($uid, $nodeType, $nid)) {
169       return 1;
170     }
171
172     return 0;
173   }
174
175   public function isNodeOwner($nid, $uid) {
176     $node = $this->node->load($nid);
177     if (intval($node->getOwnerId()) == intval($uid)) {
178       return TRUE;
179     }
180
181     return FALSE;
182   }
183
184   private function canUpdateOwnNode($uid, $nodeType) {
185     $user = $this->getUserInstance($uid);
186     if ($user->hasPermission('edit own ' . $nodeType . ' content')) {
187       return 1;
188     }
189
190     return 0;
191   }
192
193   private function canDeleteOwnNode($uid, $nodeType) {
194     $user = $this->getUserInstance($uid);
195     if ($user->hasPermission('delete own ' . $nodeType . ' content')) {
196       return 1;
197     }
198
199     return 0;
200   }
201
202   public function getGrantsByNid($nid) {
203     $grants = [];
204     foreach ($this->grants as $grant) {
205       if ($grant->nid == $nid) {
206         $grants[] = $grant;
207       }
208     }
209
210     return $grants;
211   }
212
213   /**
214    * @return int
215    */
216   public function getLoadedUid() {
217     return $this->loadedUid;
218   }
219
220   /**
221    * @param int $loadedUid
222    */
223   public function setLoadedUid($loadedUid) {
224     $this->loadedUid = $loadedUid;
225   }
226
227   /**
228    * @return User
229    */
230   public function getUserInstance($uid) {
231     if ($this->getLoadedUid() !== $uid) {
232       $user = $this->userEntityStorage->load($uid);
233       $this->setUserInstance($user);
234       return $user;
235     }
236
237     return $this->userInstance;
238   }
239
240   /**
241    * @param User $userInstance
242    */
243   public function setUserInstance($userInstance) {
244     $this->userInstance = $userInstance;
245   }
246
247   public function rebuildByTid($tid, $formState) {
248     $nids = $this->accessStorage->getNidsByTid($tid);
249     if (!empty($nids)) {
250       $this->dropRecordsByNids($nids);
251     }
252
253     if (empty($this->accessStorage->getSubmittedUserIds()) && empty($this->accessStorage->getSubmittedRolesGrantedAccess($formState))) {
254       return;
255     }
256
257     foreach ($nids as $nid) {
258       $grants = $this->createGrants($nid);
259       foreach ($grants as $grant) {
260         $this->database->insert('node_access')
261           ->fields(
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]
264           )
265           ->execute();
266       }
267     }
268
269   }
270
271   private function dropRecordsByNids($nids) {
272     $this->database->delete('node_access')
273       ->condition('nid', $nids, 'IN')
274       ->execute();
275   }
276
277   private function dropRecordsByUid($uid) {
278     $this->database->delete('node_access')
279       ->condition('realm', 'permissions_by_term__uid_' . $uid)
280       ->execute();
281   }
282
283   public function rebuildByUid($uid, $noDrop = FALSE) {
284     if ($noDrop === FALSE) {
285       $this->dropRecordsByUid($uid);
286     }
287
288     $nids = $this->accessStorage->getAllNids();
289
290     foreach ($nids as $nid) {
291       $grants = $this->createGrants($nid, $uid);
292       foreach ($grants as $grant) {
293         $this->database->insert('node_access')
294           ->fields(
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]
297           )
298           ->execute();
299       }
300     }
301
302   }
303
304   public function rebuildByNid($nid) {
305     $grants = $this->createGrants($nid);
306     foreach ($grants as $grant) {
307       $this->database->insert('node_access')
308         ->fields(
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]
311         )
312         ->execute();
313     }
314   }
315
316 }