INFRA-132 add full stops
[civicrm-core.git] / CRM / Core / Permission / Base.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 *
38 */
39 class CRM_Core_Permission_Base {
40
41 // permission mapping to stub check() calls
42 public $permissions = NULL;
43
44 /**
45 * Translate permission.
46 *
47 * @param string $perm
48 * Permission string e.g "administer CiviCRM", "cms:access user record", "Drupal:administer content",
49 * "Joomla:action:com_asset"
50 *
51 * @param string $nativePrefix
52 * @param array $map
53 * Array($portableName => $nativeName).
54 *
55 * @return NULL|string
56 * a permission name
57 */
58 public function translatePermission($perm, $nativePrefix, $map) {
59 list ($civiPrefix, $name) = CRM_Utils_String::parsePrefix(':', $perm, NULL);
60 switch ($civiPrefix) {
61 case $nativePrefix:
62 return $name;
63
64 // pass through
65 case 'cms':
66 return CRM_Utils_Array::value($name, $map, CRM_Core_Permission::ALWAYS_DENY_PERMISSION);
67
68 case NULL:
69 return $name;
70
71 default:
72 return CRM_Core_Permission::ALWAYS_DENY_PERMISSION;
73 }
74 }
75
76 /**
77 * Get the current permission of this user.
78 *
79 * @return string
80 * the permission of the user (edit or view or null)
81 */
82 public function getPermission() {
83 return CRM_Core_Permission::EDIT;
84 }
85
86 /**
87 * Get the permissioned where clause for the user.
88 *
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.
95 *
96 * @return string
97 * the group where clause for this user
98 */
99 public function whereClause($type, &$tables, &$whereTables) {
100 return '( 1 )';
101 }
102
103 /**
104 * Get the permissioned where clause for the user when trying to see groups.
105 *
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.
112 *
113 * @return string
114 * the group where clause for this user
115 */
116 public function getPermissionedStaticGroupClause($type, &$tables, &$whereTables) {
117 $this->group();
118 return $this->groupClause($type, $tables, $whereTables);
119 }
120
121 /**
122 * Get all groups from database, filtered by permissions
123 * for this user
124 *
125 * @param string $groupType
126 * Type of group(Access/Mailing).
127 * @param bool $excludeHidden
128 * exclude hidden groups.
129 *
130 *
131 * @return array
132 * array reference of all groups.
133 */
134 public function group($groupType = NULL, $excludeHidden = TRUE) {
135 return CRM_Core_PseudoConstant::allGroup($groupType, $excludeHidden);
136 }
137
138 /**
139 * Get group clause for this user.
140 *
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.
147 *
148 * @return string
149 * the group where clause for this user
150 */
151 public function groupClause($type, &$tables, &$whereTables) {
152 return ' (1) ';
153 }
154
155 /**
156 * Given a permission string, check for access requirements
157 *
158 * @param string $str
159 * The permission to check.
160 *
161 */
162 public function check($str) {
163 //no default behaviour
164 }
165
166 /**
167 * Given a roles array, check for access requirements
168 *
169 * @param array $array
170 * The roles to check.
171 *
172 * @return bool
173 * true if yes, else false
174 */
175 public function checkGroupRole($array) {
176 return FALSE;
177 }
178
179 /**
180 * Get all the contact emails for users that have a specific permission.
181 *
182 * @param string $permissionName
183 * Name of the permission we are interested in.
184 *
185 */
186 public function permissionEmails($permissionName) {
187 CRM_Core_Error::fatal("this function only works in Drupal 6 at the moment");
188 }
189
190 /**
191 * Get all the contact emails for users that have a specific role.
192 *
193 * @param string $roleName
194 * Name of the role we are interested in.
195 *
196 */
197 public function roleEmails($roleName) {
198 CRM_Core_Error::fatal("this function only works in Drupal 6 at the moment");
199 }
200
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
212 /**
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.
217 *
218 * @param array $permissions
219 * Same format as CRM_Core_Permission::getCorePermissions().
220 *
221 * @throws CRM_Core_Exception
222 * @see CRM_Core_Permission::getCorePermissions
223 */
224 public function upgradePermissions($permissions) {
225 throw new CRM_Core_Exception("Unimplemented method: CRM_Core_Permission_*::upgradePermissions");
226 }
227
228 /**
229 * Get the permissions defined in the hook_civicrm_permission implementation
230 * of the given module.
231 *
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 *
235 * @param $module
236 *
237 * @return array
238 * Array of permissions, in the same format as CRM_Core_Permission::getCorePermissions().
239 * @see CRM_Core_Permission::getCorePermissions
240 */
241 public static function getModulePermissions($module) {
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;
250 }
251
252 /**
253 * Get the permissions defined in the hook_civicrm_permission implementation
254 * in all enabled CiviCRM module extensions.
255 *
256 * @return array
257 * Array of permissions, in the same format as CRM_Core_Permission::getCorePermissions().
258 */
259 public function getAllModulePermissions() {
260 $permissions = array();
261 CRM_Utils_Hook::permission($permissions);
262 return $permissions;
263 }
264
265 }