Backup of database 9 Nov 17
[yaffs-website] / web / modules / contrib / permissions_by_term / src / Service / NodeAccess.php
1 <?php
2
3 namespace Drupal\permissions_by_term\Service;
4
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\permissions_by_term\Factory\NodeAccessRecordFactory;
7 use Drupal\node\Entity\Node;
8 use Drupal\Core\Database\Connection;
9 use Drupal\permissions_by_term\Model\NodeAccessRecordModel;
10 use Drupal\user\Entity\User;
11
12 /**
13  * Class NodeAccess
14  *
15  * @package Drupal\permissions_by_term
16  */
17 class NodeAccess {
18
19   /**
20    * @var int $uniqueGid
21    */
22   private $uniqueGid = 0;
23
24   /**
25    * @var AccessStorage $accessStorage
26    */
27   private $accessStorage;
28
29   /**
30    * @var User $userEntityStorage
31    */
32   private $userEntityStorage;
33
34   /**
35    * @var Node $node
36    */
37   private $node;
38
39   /**
40    * @var EntityTypeManagerInterface $entityTypeManager
41    */
42   private $entityTypeManager;
43
44   /**
45    * @var AccessCheck $accessCheck
46    */
47   private $accessCheck;
48
49   /**
50    * @var int $loadedUid
51    */
52   private $loadedUid;
53
54   /**
55    * @var User $userInstance
56    */
57   private $userInstance;
58
59   /**
60    * The database connection.
61    *
62    * @var Connection
63    */
64   private $database;
65
66   /**
67    * NodeAccess constructor.
68    *
69    * @param AccessStorage           $accessStorage
70    * @param NodeAccessRecordFactory $nodeAccessRecordFactory
71    * @param EntityTypeManagerInterface           $entityTypeManager
72    * @param AccessCheck             $accessCheck
73    * @param Connection              $database
74    */
75   public function __construct(
76     AccessStorage $accessStorage,
77     NodeAccessRecordFactory $nodeAccessRecordFactory,
78     EntityTypeManagerInterface $entityTypeManager,
79     AccessCheck $accessCheck,
80     Connection $database
81   ) {
82     $this->accessStorage = $accessStorage;
83     $this->nodeAccessRecordFactory = $nodeAccessRecordFactory;
84     $this->entityTypeManager = $entityTypeManager;
85     $this->userEntityStorage = $this->entityTypeManager->getStorage('user');
86     $this->node = $this->entityTypeManager->getStorage('node');
87     $this->accessCheck = $accessCheck;
88     $this->database = $database;
89   }
90
91   /**
92    * @return NodeAccessRecordModel
93    */
94   public function createGrant($nid, $gid) {
95     return $this->nodeAccessRecordFactory->create(
96       AccessStorage::NODE_ACCESS_REALM,
97       $gid,
98       $nid,
99       $this->accessStorage->getLangCode($nid),
100       0,
101       0
102     );
103   }
104
105   /**
106    * @return int
107    */
108   public function getUniqueGid() {
109     return $this->uniqueGid;
110   }
111
112   /**
113    * @param int $uniqueGid
114    */
115   public function setUniqueGid($uniqueGid) {
116     $this->uniqueGid = $uniqueGid;
117   }
118
119   /**
120    * @param $uid
121    * @param $nodeType
122    * @param $nid
123    *
124    * @return bool
125    */
126   public function canUserUpdateNode($uid, $nodeType, $nid) {
127     $user = $this->getUserInstance($uid);
128
129     $this->setLoadedUid($uid);
130
131     if ($user->hasPermission('edit any ' . $nodeType . ' content')) {
132       return TRUE;
133     }
134
135     if ($this->isNodeOwner($nid, $uid) && $this->canUpdateOwnNode($uid, $nodeType)) {
136       return TRUE;
137     }
138
139     return FALSE;
140   }
141
142   public function canUserBypassNodeAccess($uid) {
143     $user = $this->getUserInstance($uid);
144     if ($user->hasPermission('bypass node access')) {
145       return TRUE;
146     }
147
148     return FALSE;
149   }
150
151   /**
152    * @param $uid
153    * @param $nodeType
154    * @param $nid
155    *
156    * @return bool
157    */
158   public function canUserDeleteNode($uid, $nodeType, $nid) {
159     $user = $this->getUserInstance($uid);
160     if ($user->hasPermission('delete any ' . $nodeType . ' content')) {
161       return TRUE;
162     }
163
164     if ($this->isNodeOwner($nid, $uid) && $this->canDeleteOwnNode($uid, $nodeType)) {
165       return TRUE;
166     }
167
168     return FALSE;
169   }
170
171   private function getGrantDelete($uid, $nodeType, $nid) {
172     if ($this->canUserBypassNodeAccess($uid)) {
173       return 1;
174     }
175
176     if ($this->canUserDeleteNode($uid, $nodeType, $nid)) {
177       return 1;
178     }
179
180     return 0;
181   }
182
183   private function getGrantUpdate($uid, $nodeType, $nid) {
184     if ($this->canUserBypassNodeAccess($uid)) {
185       return 1;
186     }
187
188     if ($this->canUserUpdateNode($uid, $nodeType, $nid)) {
189       return 1;
190     }
191
192     return 0;
193   }
194
195   /**
196    * @param $nid
197    * @param $uid
198    *
199    * @return bool
200    */
201   public function isNodeOwner($nid, $uid) {
202     $node = $this->node->load($nid);
203     if (intval($node->getOwnerId()) == intval($uid)) {
204       return TRUE;
205     }
206
207     return FALSE;
208   }
209
210   private function canUpdateOwnNode($uid, $nodeType) {
211     $user = $this->getUserInstance($uid);
212     if ($user->hasPermission('edit own ' . $nodeType . ' content')) {
213       return 1;
214     }
215
216     return 0;
217   }
218
219   private function canDeleteOwnNode($uid, $nodeType) {
220     $user = $this->getUserInstance($uid);
221     if ($user->hasPermission('delete own ' . $nodeType . ' content')) {
222       return 1;
223     }
224
225     return 0;
226   }
227
228   /**
229    * @param $nid
230    *
231    * @return array
232    */
233   public function getGrantsByNid($nid) {
234     $grants = [];
235     foreach ($this->grants as $grant) {
236       if ($grant->nid == $nid) {
237         $grants[] = $grant;
238       }
239     }
240
241     return $grants;
242   }
243
244   /**
245    * @return int
246    */
247   public function getLoadedUid() {
248     return $this->loadedUid;
249   }
250
251   /**
252    * @param int $loadedUid
253    */
254   public function setLoadedUid($loadedUid) {
255     $this->loadedUid = $loadedUid;
256   }
257
258   /**
259    * @return User
260    */
261   public function getUserInstance($uid) {
262     if ($this->getLoadedUid() !== $uid) {
263       $user = $this->userEntityStorage->load($uid);
264       $this->setUserInstance($user);
265       return $user;
266     }
267
268     return $this->userInstance;
269   }
270
271   /**
272    * @param User $userInstance
273    */
274   public function setUserInstance($userInstance) {
275     $this->userInstance = $userInstance;
276   }
277
278   /**
279    * @param int $nid
280    *
281    * @return bool
282    */
283   public function isAccessRecordExisting($nid) {
284     $query = $this->database->select('node_access', 'na')
285       ->fields('na', ['nid'])
286       ->condition('na.nid', $nid)
287       ->condition('na.realm', AccessStorage::NODE_ACCESS_REALM);
288
289     $result = $query->execute()
290       ->fetchCol();
291
292     if (empty($result)) {
293       return FALSE;
294     }
295
296     return TRUE;
297   }
298
299 }