Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
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. | |
6a488035 | 160 | * |
6a488035 | 161 | */ |
00be9182 | 162 | public function check($str) { |
42762e35 | 163 | //no default behaviour |
6a488035 TO |
164 | } |
165 | ||
166 | /** | |
167 | * Given a roles array, check for access requirements | |
168 | * | |
6a0b768e TO |
169 | * @param array $array |
170 | * The roles to check. | |
6a488035 | 171 | * |
7c550ca0 | 172 | * @return bool |
a6c01b45 | 173 | * true if yes, else false |
6a488035 | 174 | */ |
00be9182 | 175 | public function checkGroupRole($array) { |
6a488035 TO |
176 | return FALSE; |
177 | } | |
178 | ||
179 | /** | |
d09edf64 | 180 | * Get all the contact emails for users that have a specific permission. |
6a488035 | 181 | * |
6a0b768e TO |
182 | * @param string $permissionName |
183 | * Name of the permission we are interested in. | |
6a488035 | 184 | * |
6a488035 TO |
185 | */ |
186 | public function permissionEmails($permissionName) { | |
187 | CRM_Core_Error::fatal("this function only works in Drupal 6 at the moment"); | |
188 | } | |
189 | ||
190 | /** | |
d09edf64 | 191 | * Get all the contact emails for users that have a specific role. |
6a488035 | 192 | * |
6a0b768e TO |
193 | * @param string $roleName |
194 | * Name of the role we are interested in. | |
6a488035 | 195 | * |
6a488035 TO |
196 | */ |
197 | public function roleEmails($roleName) { | |
198 | CRM_Core_Error::fatal("this function only works in Drupal 6 at the moment"); | |
199 | } | |
200 | ||
7fccad46 TO |
201 | /** |
202 | * Determine whether the permission store allows us to store | |
203 | * a list of permissions generated dynamically (eg by | |
204 | * hook_civicrm_permissions.) | |
205 | * | |
206 | * @return bool | |
207 | */ | |
208 | public function isModulePermissionSupported() { | |
209 | return FALSE; | |
210 | } | |
211 | ||
6a488035 | 212 | /** |
0d8fc497 TO |
213 | * Ensure that the CMS supports all the permissions defined by CiviCRM |
214 | * and its extensions. If there are stale permissions, they should be | |
215 | * deleted. This is useful during module upgrade when the newer module | |
216 | * version has removed permission that were defined in the older version. | |
c4bc14ed | 217 | * |
6a0b768e TO |
218 | * @param array $permissions |
219 | * Same format as CRM_Core_Permission::getCorePermissions(). | |
77b97be7 EM |
220 | * |
221 | * @throws CRM_Core_Exception | |
c4bc14ed | 222 | * @see CRM_Core_Permission::getCorePermissions |
6a488035 | 223 | */ |
00be9182 | 224 | public function upgradePermissions($permissions) { |
7fccad46 | 225 | throw new CRM_Core_Exception("Unimplemented method: CRM_Core_Permission_*::upgradePermissions"); |
6a488035 TO |
226 | } |
227 | ||
228 | /** | |
229 | * Get the permissions defined in the hook_civicrm_permission implementation | |
230 | * of the given module. | |
231 | * | |
789565bf TO |
232 | * Note: At time of writing, this is only used with native extension-modules, so |
233 | * there's one, predictable calling convention (regardless of CMS). | |
234 | * | |
77b97be7 EM |
235 | * @param $module |
236 | * | |
16b10e64 CW |
237 | * @return array |
238 | * Array of permissions, in the same format as CRM_Core_Permission::getCorePermissions(). | |
b57fc2e3 | 239 | * @see CRM_Core_Permission::getCorePermissions |
6a488035 | 240 | */ |
00be9182 | 241 | public static function getModulePermissions($module) { |
789565bf TO |
242 | $return_permissions = array(); |
243 | $fn_name = "{$module}_civicrm_permission"; | |
244 | if (function_exists($fn_name)) { | |
245 | $module_permissions = array(); | |
246 | $fn_name($module_permissions); | |
247 | $return_permissions = $module_permissions; | |
248 | } | |
249 | return $return_permissions; | |
6a488035 TO |
250 | } |
251 | ||
252 | /** | |
253 | * Get the permissions defined in the hook_civicrm_permission implementation | |
254 | * in all enabled CiviCRM module extensions. | |
255 | * | |
54957108 | 256 | * @param bool $descriptions |
257 | * | |
16b10e64 CW |
258 | * @return array |
259 | * Array of permissions, in the same format as CRM_Core_Permission::getCorePermissions(). | |
6a488035 | 260 | */ |
f1db52b9 | 261 | public function getAllModulePermissions($descriptions = FALSE) { |
6a488035 TO |
262 | $permissions = array(); |
263 | CRM_Utils_Hook::permission($permissions); | |
f1db52b9 AH |
264 | |
265 | if ($descriptions) { | |
266 | foreach ($permissions as $permission => $label) { | |
267 | $permissions[$permission] = (is_array($label)) ? $label : array($label); | |
268 | } | |
269 | } | |
270 | else { | |
271 | foreach ($permissions as $permission => $label) { | |
272 | $permissions[$permission] = (is_array($label)) ? array_shift($label) : $label; | |
273 | } | |
274 | } | |
6a488035 TO |
275 | return $permissions; |
276 | } | |
96025800 | 277 | |
6a488035 | 278 | } |