Backup of database 9 Nov 17
[yaffs-website] / web / modules / contrib / permissions_by_term / src / AccessCheck.php
1 <?php
2
3 namespace Drupal\permissions_by_term;
4
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\user\Entity\User;
7
8 /**
9  * AccessCheckService class.
10  */
11 class AccessCheck implements AccessCheckInterface{
12
13   /**
14    * AccessCheckService constructor.
15    */
16   public function __construct(EntityManagerInterface $entity_manager) {
17     $this->entityManager = $entity_manager;
18   }
19
20   /**
21    * {@inheritdoc}
22    */
23   public function canUserAccessByNodeId($iNid, $uid = FALSE) {
24     $node = $this->entityManager->getStorage('node')->load($iNid);
25
26     $access_allowed = TRUE;
27
28     foreach ($node->getFields() as $field) {
29       if ($field->getFieldDefinition()->getType() == 'entity_reference' && $field->getFieldDefinition()->getSetting('target_type') == 'taxonomy_term') {
30         $aReferencedTaxonomyTerms = $field->getValue();
31         if (!empty($aReferencedTaxonomyTerms)) {
32           foreach ($aReferencedTaxonomyTerms as $aReferencedTerm) {
33             if (isset($aReferencedTerm['target_id']) && !$this->isAccessAllowedByDatabase($aReferencedTerm['target_id'], $uid)) {
34               $access_allowed = FALSE;
35             }
36           }
37         }
38       }
39     }
40
41     return $access_allowed;
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public function viewContainsNode($view) {
48     $bViewContainsNodes = FALSE;
49
50     foreach ($view->result as $view_result) {
51       if (array_key_exists('nid', $view_result) === TRUE) {
52         $bViewContainsNodes = TRUE;
53         break;
54       }
55     }
56     return $bViewContainsNodes;
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function removeForbiddenNodesFromView(&$view) {
63     $aNodesToHideInView = [];
64
65     // Iterate over all nodes in view.
66     foreach ($view->result as $v) {
67
68       if ($this->canUserAccessByNodeId($v->nid) === FALSE) {
69         $aNodesToHideInView[] = $v->nid;
70       }
71
72     }
73
74     $counter = 0;
75
76     foreach ($view->result as $v) {
77       if (in_array($v->nid, $aNodesToHideInView)) {
78         unset($view->result[$counter]);
79       }
80       $counter++;
81     }
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function isAccessAllowedByDatabase($tid, $uid = FALSE) {
88
89     if ($uid === FALSE) {
90       $user = \Drupal::currentUser();
91     } elseif (is_numeric($uid)) {
92       $user = User::load($uid);
93     }
94
95     // Admin can access everything (user id "1").
96     if ($user->id() == 1) {
97       return TRUE;
98     }
99
100     $tid = intval($tid);
101
102     if (!$this->isAnyPermissionSetForTerm($tid)) {
103       return TRUE;
104     }
105
106     /* At this point permissions are enabled, check to see if this user or one
107      * of their roles is allowed.
108      */
109     $aUserRoles = $user->getRoles();
110
111     foreach ($aUserRoles as $sUserRole) {
112
113       if ($this->isTermAllowedByUserRole($tid, $sUserRole)) {
114         return TRUE;
115       }
116
117     }
118
119     $iUid = intval($user->id());
120
121     if ($this->isTermAllowedByUserId($tid, $iUid)) {
122       return TRUE;
123     }
124
125     return FALSE;
126
127   }
128
129   /**
130    * {@inheritdoc}
131    */
132   public function isTermAllowedByUserId($tid, $iUid) {
133
134     $query_result = db_query("SELECT uid FROM {permissions_by_term_user} WHERE tid = :tid AND uid = :uid",
135       [':tid' => $tid, ':uid' => $iUid])->fetchField();
136
137     if (!empty($query_result)) {
138       return TRUE;
139     }
140     else {
141       return FALSE;
142     }
143
144   }
145
146   /**
147    * {@inheritdoc}
148    */
149   public function isTermAllowedByUserRole($tid, $sUserRole) {
150     $query_result = db_query("SELECT rid FROM {permissions_by_term_role} WHERE tid = :tid AND rid IN (:user_roles)",
151       [':tid' => $tid, ':user_roles' => $sUserRole])->fetchField();
152
153     if (!empty($query_result)) {
154       return TRUE;
155     }
156     else {
157       return FALSE;
158     }
159
160   }
161
162   /**
163    * {@inheritdoc}
164    */
165   public function isAnyPermissionSetForTerm($tid) {
166
167     $iUserTableResults = intval(db_query("SELECT COUNT(1) FROM {permissions_by_term_user} WHERE tid = :tid",
168       [':tid' => $tid])->fetchField());
169
170     $iRoleTableResults = intval(db_query("SELECT COUNT(1) FROM {permissions_by_term_role} WHERE tid = :tid",
171       [':tid' => $tid])->fetchField());
172
173     if ($iUserTableResults > 0 ||
174       $iRoleTableResults > 0) {
175       return TRUE;
176     }
177
178   }
179
180 }