Merge pull request #4875 from civicrm/minor-fix
[civicrm-core.git] / CRM / Core / Permission / Drupal6.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_Drupal6 extends CRM_Core_Permission_DrupalBase {
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 * Given a permission string, check for access requirements
66 *
67 * @param string $str
68 * The permission to check.
69 *
70 * @param int $contactID
71 *
72 * @return boolean
73 * true if yes, else false
74 */
75 public function check($str, $contactID = NULL) {
76 $str = $this->translatePermission($str, 'Drupal6', array(
77 'view user account' => 'access user profiles',
78 'administer users' => 'administer users',
79 ));
80 if ($str == CRM_Core_Permission::ALWAYS_DENY_PERMISSION) {
81 return FALSE;
82 }
83 if ($str == CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION) {
84 return TRUE;
85 }
86 if (function_exists('user_access')) {
87 return user_access($str) ? TRUE : FALSE;
88 }
89 return TRUE;
90 }
91
92 /**
93 * Given a roles array, check for access requirements
94 *
95 * @param array $array
96 * The roles to check.
97 *
98 * @return boolean
99 * true if yes, else false
100 */
101
102 public function checkGroupRole($array) {
103 if (function_exists('user_load') && isset($array)) {
104 $user = user_load(array('uid' => $GLOBALS['user']->uid));
105 //if giver roles found in user roles - return true
106 foreach ($array as $key => $value) {
107 if (in_array($value, $user->roles)) {
108 return TRUE;
109 }
110 }
111 }
112 return FALSE;
113 }
114
115 /**
116 * Get all the contact emails for users that have a specific role
117 *
118 * @param string $roleName
119 * Name of the role we are interested in.
120 *
121 * @return string
122 * a comma separated list of email addresses
123 */
124 public function roleEmails($roleName) {
125 static $_cache = array();
126
127 if (isset($_cache[$roleName])) {
128 return $_cache[$roleName];
129 }
130
131 $uids = array();
132 $sql = "
133 SELECT {users}.uid
134 FROM {users}
135 LEFT JOIN {users_roles} ON {users}.uid = {users_roles}.uid
136 INNER JOIN {role} ON ( {role}.rid = {users_roles}.rid OR {role}.rid = 2 )
137 WHERE {role}. name LIKE '%%{$roleName}%%'
138 AND {users}.status = 1
139 ";
140
141 $query = db_query($sql);
142 while ($result = db_fetch_object($query)) {
143 $uids[] = $result->uid;
144 }
145
146 $_cache[$roleName] = self::getContactEmails($uids);
147 return $_cache[$roleName];
148 }
149
150 /**
151 * Get all the contact emails for users that have a specific permission
152 *
153 * @param string $permissionName
154 * Name of the permission we are interested in.
155 *
156 * @return string
157 * a comma separated list of email addresses
158 */
159 public function permissionEmails($permissionName) {
160 static $_cache = array();
161
162 if (isset($_cache[$permissionName])) {
163 return $_cache[$permissionName];
164 }
165
166 $uids = array();
167 $sql = "
168 SELECT {users}.uid, {permission}.perm
169 FROM {users}
170 LEFT JOIN {users_roles} ON {users}.uid = {users_roles}.uid
171 INNER JOIN {permission} ON ( {permission}.rid = {users_roles}.rid OR {permission}.rid = 2 )
172 WHERE {permission}.perm LIKE '%%{$permissionName}%%'
173 AND {users}.status = 1
174 ";
175
176 $query = db_query($sql);
177 while ($result = db_fetch_object($query)) {
178 $uids[] = $result->uid;
179 }
180
181 $_cache[$permissionName] = self::getContactEmails($uids);
182 return $_cache[$permissionName];
183 }
184
185 /**
186 * {@inheritDoc}
187 */
188 public function isModulePermissionSupported() {
189 return TRUE;
190 }
191
192 /**
193 * {@inheritdoc}
194 *
195 * Does nothing in Drupal 6.
196 */
197 public function upgradePermissions($permissions) {
198 // D6 allows us to be really lazy... things get cleaned up when the admin form is next submitted...
199 }
200
201 /**
202 * Get the permissions defined in the hook_civicrm_permission implementation
203 * of the given module.
204 *
205 * @param $module
206 *
207 * @return Array
208 * of permissions, in the same format as CRM_Core_Permission::getCorePermissions().
209 */
210 public static function getModulePermissions($module) {
211 $return_permissions = array();
212 $fn_name = "{$module}_civicrm_permission";
213 if (function_exists($fn_name)) {
214 $fn_name($return_permissions);
215 }
216 return $return_permissions;
217 }
218 }