Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * | |
38 | */ | |
39 | class CRM_Core_Permission_Base { | |
40 | ||
dcbec360 EM |
41 | // permission mapping to stub check() calls |
42 | public $permissions = NULL; | |
43 | ||
085823c1 | 44 | /** |
d09edf64 | 45 | * Translate permission. |
085823c1 | 46 | * |
6ad55d91 EM |
47 | * @param string $perm |
48 | * Permission string e.g "administer CiviCRM", "cms:access user record", "Drupal:administer content", | |
49 | * "Joomla:action:com_asset" | |
50 | * | |
085823c1 | 51 | * @param string $nativePrefix |
6a0b768e TO |
52 | * @param array $map |
53 | * Array($portableName => $nativeName). | |
77b97be7 | 54 | * |
72b3a70c CW |
55 | * @return NULL|string |
56 | * a permission name | |
085823c1 TO |
57 | */ |
58 | public function translatePermission($perm, $nativePrefix, $map) { | |
59 | list ($civiPrefix, $name) = CRM_Utils_String::parsePrefix(':', $perm, NULL); | |
60 | switch ($civiPrefix) { | |
61 | case $nativePrefix: | |
2aa397bc TO |
62 | return $name; |
63 | ||
64 | // pass through | |
085823c1 TO |
65 | case 'cms': |
66 | return CRM_Utils_Array::value($name, $map, CRM_Core_Permission::ALWAYS_DENY_PERMISSION); | |
2aa397bc | 67 | |
085823c1 TO |
68 | case NULL: |
69 | return $name; | |
2aa397bc | 70 | |
085823c1 TO |
71 | default: |
72 | return CRM_Core_Permission::ALWAYS_DENY_PERMISSION; | |
73 | } | |
74 | } | |
75 | ||
42762e35 | 76 | /** |
d09edf64 | 77 | * Get the current permission of this user. |
42762e35 | 78 | * |
a6c01b45 CW |
79 | * @return string |
80 | * the permission of the user (edit or view or null) | |
42762e35 DL |
81 | */ |
82 | public function getPermission() { | |
83 | return CRM_Core_Permission::EDIT; | |
84 | } | |
85 | ||
6a488035 | 86 | /** |
d09edf64 | 87 | * Get the permissioned where clause for the user. |
6a488035 | 88 | * |
6a0b768e TO |
89 | * @param int $type |
90 | * The type of permission needed. | |
91 | * @param array $tables | |
92 | * (reference ) add the tables that are needed for the select clause. | |
93 | * @param array $whereTables | |
94 | * (reference ) add the tables that are needed for the where clause. | |
6a488035 | 95 | * |
a6c01b45 CW |
96 | * @return string |
97 | * the group where clause for this user | |
6a488035 TO |
98 | */ |
99 | public function whereClause($type, &$tables, &$whereTables) { | |
100 | return '( 1 )'; | |
101 | } | |
353ffa53 | 102 | |
6a488035 | 103 | /** |
d09edf64 | 104 | * Get the permissioned where clause for the user when trying to see groups. |
6a488035 | 105 | * |
6a0b768e TO |
106 | * @param int $type |
107 | * The type of permission needed. | |
108 | * @param array $tables | |
109 | * (reference ) add the tables that are needed for the select clause. | |
110 | * @param array $whereTables | |
111 | * (reference ) add the tables that are needed for the where clause. | |
6a488035 | 112 | * |
a6c01b45 CW |
113 | * @return string |
114 | * the group where clause for this user | |
6a488035 TO |
115 | */ |
116 | public function getPermissionedStaticGroupClause($type, &$tables, &$whereTables) { | |
117 | $this->group(); | |
118 | return $this->groupClause($type, $tables, $whereTables); | |
119 | } | |
77b97be7 | 120 | |
6a488035 TO |
121 | /** |
122 | * Get all groups from database, filtered by permissions | |
123 | * for this user | |
124 | * | |
6a0b768e TO |
125 | * @param string $groupType |
126 | * Type of group(Access/Mailing). | |
3f8d2862 CW |
127 | * @param bool $excludeHidden |
128 | * exclude hidden groups. | |
6a488035 | 129 | * |
6a488035 | 130 | * |
a6c01b45 CW |
131 | * @return array |
132 | * array reference of all groups. | |
6a488035 TO |
133 | */ |
134 | public function group($groupType = NULL, $excludeHidden = TRUE) { | |
42762e35 | 135 | return CRM_Core_PseudoConstant::allGroup($groupType, $excludeHidden); |
6a488035 TO |
136 | } |
137 | ||
138 | /** | |
d09edf64 | 139 | * Get group clause for this user. |
6a488035 | 140 | * |
6a0b768e TO |
141 | * @param int $type |
142 | * The type of permission needed. | |
143 | * @param array $tables | |
144 | * (reference ) add the tables that are needed for the select clause. | |
145 | * @param array $whereTables | |
146 | * (reference ) add the tables that are needed for the where clause. | |
6a488035 | 147 | * |
a6c01b45 CW |
148 | * @return string |
149 | * the group where clause for this user | |
6a488035 TO |
150 | */ |
151 | public function groupClause($type, &$tables, &$whereTables) { | |
42762e35 | 152 | return ' (1) '; |
6a488035 TO |
153 | } |
154 | ||
155 | /** | |
100fef9d | 156 | * Given a permission string, check for access requirements |
6a488035 | 157 | * |
6a0b768e TO |
158 | * @param string $str |
159 | * The permission to check. | |
18be3201 | 160 | * @param int $userId |
6a488035 | 161 | * |
6a488035 | 162 | */ |
18be3201 | 163 | public function check($str, $userId = NULL) { |
42762e35 | 164 | //no default behaviour |
6a488035 TO |
165 | } |
166 | ||
167 | /** | |
168 | * Given a roles array, check for access requirements | |
169 | * | |
6a0b768e TO |
170 | * @param array $array |
171 | * The roles to check. | |
6a488035 | 172 | * |
7c550ca0 | 173 | * @return bool |
a6c01b45 | 174 | * true if yes, else false |
6a488035 | 175 | */ |
00be9182 | 176 | public function checkGroupRole($array) { |
6a488035 TO |
177 | return FALSE; |
178 | } | |
179 | ||
180 | /** | |
d09edf64 | 181 | * Get all the contact emails for users that have a specific permission. |
6a488035 | 182 | * |
6a0b768e TO |
183 | * @param string $permissionName |
184 | * Name of the permission we are interested in. | |
6a488035 | 185 | * |
6a488035 TO |
186 | */ |
187 | public function permissionEmails($permissionName) { | |
188 | CRM_Core_Error::fatal("this function only works in Drupal 6 at the moment"); | |
189 | } | |
190 | ||
191 | /** | |
d09edf64 | 192 | * Get all the contact emails for users that have a specific role. |
6a488035 | 193 | * |
6a0b768e TO |
194 | * @param string $roleName |
195 | * Name of the role we are interested in. | |
6a488035 | 196 | * |
6a488035 TO |
197 | */ |
198 | public function roleEmails($roleName) { | |
199 | CRM_Core_Error::fatal("this function only works in Drupal 6 at the moment"); | |
200 | } | |
201 | ||
7fccad46 TO |
202 | /** |
203 | * Determine whether the permission store allows us to store | |
204 | * a list of permissions generated dynamically (eg by | |
205 | * hook_civicrm_permissions.) | |
206 | * | |
207 | * @return bool | |
208 | */ | |
209 | public function isModulePermissionSupported() { | |
210 | return FALSE; | |
211 | } | |
212 | ||
6a488035 | 213 | /** |
0d8fc497 TO |
214 | * Ensure that the CMS supports all the permissions defined by CiviCRM |
215 | * and its extensions. If there are stale permissions, they should be | |
216 | * deleted. This is useful during module upgrade when the newer module | |
217 | * version has removed permission that were defined in the older version. | |
c4bc14ed | 218 | * |
6a0b768e TO |
219 | * @param array $permissions |
220 | * Same format as CRM_Core_Permission::getCorePermissions(). | |
77b97be7 EM |
221 | * |
222 | * @throws CRM_Core_Exception | |
c4bc14ed | 223 | * @see CRM_Core_Permission::getCorePermissions |
6a488035 | 224 | */ |
00be9182 | 225 | public function upgradePermissions($permissions) { |
7fccad46 | 226 | throw new CRM_Core_Exception("Unimplemented method: CRM_Core_Permission_*::upgradePermissions"); |
6a488035 TO |
227 | } |
228 | ||
229 | /** | |
230 | * Get the permissions defined in the hook_civicrm_permission implementation | |
231 | * of the given module. | |
232 | * | |
789565bf TO |
233 | * Note: At time of writing, this is only used with native extension-modules, so |
234 | * there's one, predictable calling convention (regardless of CMS). | |
235 | * | |
77b97be7 EM |
236 | * @param $module |
237 | * | |
16b10e64 CW |
238 | * @return array |
239 | * Array of permissions, in the same format as CRM_Core_Permission::getCorePermissions(). | |
b57fc2e3 | 240 | * @see CRM_Core_Permission::getCorePermissions |
6a488035 | 241 | */ |
00be9182 | 242 | public static function getModulePermissions($module) { |
789565bf TO |
243 | $return_permissions = array(); |
244 | $fn_name = "{$module}_civicrm_permission"; | |
245 | if (function_exists($fn_name)) { | |
246 | $module_permissions = array(); | |
247 | $fn_name($module_permissions); | |
248 | $return_permissions = $module_permissions; | |
249 | } | |
250 | return $return_permissions; | |
6a488035 TO |
251 | } |
252 | ||
253 | /** | |
254 | * Get the permissions defined in the hook_civicrm_permission implementation | |
255 | * in all enabled CiviCRM module extensions. | |
256 | * | |
54957108 | 257 | * @param bool $descriptions |
258 | * | |
16b10e64 CW |
259 | * @return array |
260 | * Array of permissions, in the same format as CRM_Core_Permission::getCorePermissions(). | |
6a488035 | 261 | */ |
f1db52b9 | 262 | public function getAllModulePermissions($descriptions = FALSE) { |
6a488035 TO |
263 | $permissions = array(); |
264 | CRM_Utils_Hook::permission($permissions); | |
f1db52b9 AH |
265 | |
266 | if ($descriptions) { | |
267 | foreach ($permissions as $permission => $label) { | |
268 | $permissions[$permission] = (is_array($label)) ? $label : array($label); | |
269 | } | |
270 | } | |
271 | else { | |
272 | foreach ($permissions as $permission => $label) { | |
273 | $permissions[$permission] = (is_array($label)) ? array_shift($label) : $label; | |
274 | } | |
275 | } | |
6a488035 TO |
276 | return $permissions; |
277 | } | |
96025800 | 278 | |
6a488035 | 279 | } |