Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * | |
38 | */ | |
39 | class CRM_Core_Permission_DrupalBase extends CRM_Core_Permission_Base { | |
42762e35 DL |
40 | |
41 | /** | |
42 | * is this user someone with access for the entire system | |
43 | * | |
44 | * @var boolean | |
45 | */ | |
46 | protected $_viewAdminUser = FALSE; | |
47 | protected $_editAdminUser = FALSE; | |
48 | ||
49 | /** | |
50 | * am in in view permission or edit permission? | |
51 | * @var boolean | |
52 | */ | |
53 | protected $_viewPermission = FALSE; | |
54 | protected $_editPermission = FALSE; | |
55 | ||
56 | /** | |
57 | * the current set of permissioned groups for the user | |
58 | * | |
59 | * @var array | |
60 | */ | |
61 | protected $_viewPermissionedGroups; | |
62 | protected $_editPermissionedGroups; | |
63 | ||
64 | /** | |
65 | * Get all groups from database, filtered by permissions | |
66 | * for this user | |
67 | * | |
77b97be7 | 68 | * @param string $groupType type of group(Access/Mailing) |
da3c7979 | 69 | * @param bool $excludeHidden exclude hidden groups. |
42762e35 DL |
70 | * |
71 | * @access public | |
72 | * | |
73 | * @return array - array reference of all groups. | |
42762e35 DL |
74 | */ |
75 | public function group($groupType = NULL, $excludeHidden = TRUE) { | |
76 | if (!isset($this->_viewPermissionedGroups)) { | |
77 | $this->_viewPermissionedGroups = $this->_editPermissionedGroups = array(); | |
78 | } | |
79 | ||
80 | $groupKey = $groupType ? $groupType : 'all'; | |
81 | ||
82 | if (!isset($this->_viewPermissionedGroups[$groupKey])) { | |
83 | $this->_viewPermissionedGroups[$groupKey] = $this->_editPermissionedGroups[$groupKey] = array(); | |
84 | ||
85 | $groups = CRM_Core_PseudoConstant::allGroup($groupType, $excludeHidden); | |
86 | ||
87 | if ($this->check('edit all contacts')) { | |
88 | // this is the most powerful permission, so we return | |
89 | // immediately rather than dilute it further | |
90 | $this->_editAdminUser = $this->_viewAdminUser = TRUE; | |
91 | $this->_editPermission = $this->_viewPermission = TRUE; | |
92 | $this->_editPermissionedGroups[$groupKey] = $groups; | |
93 | $this->_viewPermissionedGroups[$groupKey] = $groups; | |
94 | return $this->_viewPermissionedGroups[$groupKey]; | |
95 | } | |
96 | elseif ($this->check('view all contacts')) { | |
97 | $this->_viewAdminUser = TRUE; | |
98 | $this->_viewPermission = TRUE; | |
99 | $this->_viewPermissionedGroups[$groupKey] = $groups; | |
100 | } | |
101 | ||
102 | ||
103 | $ids = CRM_ACL_API::group(CRM_Core_Permission::VIEW, NULL, 'civicrm_saved_search', $groups); | |
104 | if (!empty($ids)) { | |
105 | foreach (array_values($ids) as $id) { | |
106 | $title = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $id, 'title'); | |
107 | $this->_viewPermissionedGroups[$groupKey][$id] = $title; | |
108 | $this->_viewPermission = TRUE; | |
109 | } | |
110 | } | |
111 | ||
112 | $ids = CRM_ACL_API::group(CRM_Core_Permission::EDIT, NULL, 'civicrm_saved_search', $groups); | |
113 | if (!empty($ids)) { | |
114 | foreach (array_values($ids) as $id) { | |
115 | $title = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $id, 'title'); | |
116 | $this->_editPermissionedGroups[$groupKey][$id] = $title; | |
117 | $this->_viewPermissionedGroups[$groupKey][$id] = $title; | |
118 | $this->_editPermission = TRUE; | |
119 | $this->_viewPermission = TRUE; | |
120 | } | |
121 | } | |
122 | } | |
123 | ||
124 | return $this->_viewPermissionedGroups[$groupKey]; | |
125 | } | |
126 | ||
127 | /** | |
128 | * Get group clause for this user. The group Clause filters the | |
129 | * list of groups that the user is permitted to see in a group listing. | |
130 | * For example it will filter both the list on the 'Manage Groups' page | |
131 | * and on the contact 'Groups' tab | |
132 | * | |
133 | * the aclGroup hook & configured ACLs contribute to this data. | |
134 | * If the contact is allowed to see all contacts the function will return ( 1 ) | |
135 | * | |
136 | * @todo the history of this function is that there was some confusion as to | |
137 | * whether it was filtering contacts or groups & some cruft may remain | |
138 | * | |
139 | * @param int $type the type of permission needed | |
140 | * @param array $tables (reference) add the tables that are needed for the select clause | |
141 | * @param array $whereTables (reference) add the tables that are needed for the where clause | |
142 | * | |
143 | * @return string the clause to add to the query retrieving viewable groups | |
144 | * @access public | |
145 | */ | |
146 | public function groupClause($type, &$tables, &$whereTables) { | |
147 | if (!isset($this->_viewPermissionedGroups)) { | |
148 | $this->group(); | |
149 | } | |
150 | ||
151 | // we basically get all the groups here | |
152 | $groupKey = 'all'; | |
153 | if ($type == CRM_Core_Permission::EDIT) { | |
154 | if ($this->_editAdminUser) { | |
155 | $clause = ' ( 1 ) '; | |
156 | } | |
157 | elseif (empty($this->_editPermissionedGroups[$groupKey])) { | |
158 | $clause = ' ( 0 ) '; | |
159 | } | |
160 | else { | |
161 | $clauses = array(); | |
162 | $groups = implode(', ', $this->_editPermissionedGroups[$groupKey]); | |
163 | $clauses[] = ' ( civicrm_group_contact.group_id IN ( ' . implode(', ', array_keys($this->_editPermissionedGroups[$groupKey])) . " ) AND civicrm_group_contact.status = 'Added' ) "; | |
164 | $tables['civicrm_group_contact'] = 1; | |
165 | $whereTables['civicrm_group_contact'] = 1; | |
166 | ||
167 | // foreach group that is potentially a saved search, add the saved search clause | |
168 | foreach (array_keys($this->_editPermissionedGroups[$groupKey]) as $id) { | |
169 | $group = new CRM_Contact_DAO_Group(); | |
170 | $group->id = $id; | |
171 | if ($group->find(TRUE) && $group->saved_search_id) { | |
172 | $clause = CRM_Contact_BAO_SavedSearch::whereClause($group->saved_search_id, | |
173 | $tables, | |
174 | $whereTables | |
175 | ); | |
176 | if (trim($clause)) { | |
177 | $clauses[] = $clause; | |
178 | } | |
179 | } | |
180 | } | |
181 | $clause = ' ( ' . implode(' OR ', $clauses) . ' ) '; | |
182 | } | |
183 | } | |
184 | else { | |
185 | if ($this->_viewAdminUser) { | |
186 | $clause = ' ( 1 ) '; | |
187 | } | |
188 | elseif (empty($this->_viewPermissionedGroups[$groupKey])) { | |
189 | $clause = ' ( 0 ) '; | |
190 | } | |
191 | else { | |
192 | $clauses = array(); | |
193 | $groups = implode(', ', $this->_viewPermissionedGroups[$groupKey]); | |
194 | $clauses[] = ' civicrm_group.id IN (' . implode(', ', array_keys($this->_viewPermissionedGroups[$groupKey])) . " ) "; | |
195 | $tables['civicrm_group'] = 1; | |
196 | $whereTables['civicrm_group'] = 1; | |
197 | $clause = ' ( ' . implode(' OR ', $clauses) . ' ) '; | |
198 | } | |
199 | } | |
200 | ||
201 | return $clause; | |
202 | } | |
203 | ||
204 | /** | |
205 | * get the current permission of this user | |
206 | * | |
207 | * @return string the permission of the user (edit or view or null) | |
208 | */ | |
209 | public function getPermission() { | |
210 | $this->group(); | |
211 | ||
212 | if ($this->_editPermission) { | |
213 | return CRM_Core_Permission::EDIT; | |
214 | } | |
215 | elseif ($this->_viewPermission) { | |
216 | return CRM_Core_Permission::VIEW; | |
217 | } | |
218 | return NULL; | |
219 | } | |
220 | ||
a0ee3941 EM |
221 | /** |
222 | * @param $uids | |
223 | * | |
224 | * @return string | |
225 | */ | |
42762e35 DL |
226 | function getContactEmails($uids) { |
227 | if (empty($uids)) { | |
228 | return ''; | |
229 | } | |
230 | $uidString = implode(',', $uids); | |
231 | $sql = " | |
232 | SELECT e.email | |
233 | FROM civicrm_contact c | |
234 | INNER JOIN civicrm_email e ON ( c.id = e.contact_id AND e.is_primary = 1 ) | |
235 | INNER JOIN civicrm_uf_match uf ON ( c.id = uf.contact_id ) | |
236 | WHERE c.is_deceased = 0 | |
237 | AND c.is_deleted = 0 | |
238 | AND uf.uf_id IN ( $uidString ) | |
239 | "; | |
240 | ||
241 | $dao = CRM_Core_DAO::executeQuery($sql); | |
242 | ||
243 | $emails = array(); | |
244 | while ($dao->fetch()) { | |
245 | $emails[] = $dao->email; | |
246 | } | |
247 | ||
248 | return implode(', ', $emails); | |
249 | } | |
232624b1 | 250 | } |