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