Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
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 | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * | |
38 | */ | |
39 | class CRM_Core_Permission_Drupal6 extends CRM_Core_Permission_DrupalBase { | |
40 | ||
41 | /** | |
d09edf64 | 42 | * Is this user someone with access for the entire system. |
6a488035 TO |
43 | * |
44 | * @var boolean | |
45 | */ | |
46 | protected $_viewAdminUser = FALSE; | |
47 | protected $_editAdminUser = FALSE; | |
48 | ||
49 | /** | |
100fef9d | 50 | * Am in in view permission or edit permission? |
6a488035 TO |
51 | * @var boolean |
52 | */ | |
53 | protected $_viewPermission = FALSE; | |
54 | protected $_editPermission = FALSE; | |
55 | ||
56 | /** | |
d09edf64 | 57 | * The current set of permissioned groups for the user. |
6a488035 TO |
58 | * |
59 | * @var array | |
60 | */ | |
61 | protected $_viewPermissionedGroups; | |
62 | protected $_editPermissionedGroups; | |
63 | ||
085823c1 | 64 | /** |
100fef9d | 65 | * Given a permission string, check for access requirements |
085823c1 | 66 | * |
6a0b768e TO |
67 | * @param string $str |
68 | * The permission to check. | |
085823c1 | 69 | * |
100fef9d | 70 | * @param int $contactID |
77b97be7 | 71 | * |
317fceb4 | 72 | * @return bool |
a6c01b45 | 73 | * true if yes, else false |
085823c1 | 74 | */ |
00be9182 | 75 | public function check($str, $contactID = NULL) { |
085823c1 TO |
76 | $str = $this->translatePermission($str, 'Drupal6', array( |
77 | 'view user account' => 'access user profiles', | |
ccd74e76 | 78 | 'administer users' => 'administer users', |
085823c1 TO |
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 | ||
6a488035 TO |
92 | /** |
93 | * Given a roles array, check for access requirements | |
94 | * | |
6a0b768e TO |
95 | * @param array $array |
96 | * The roles to check. | |
6a488035 | 97 | * |
317fceb4 | 98 | * @return bool |
a6c01b45 | 99 | * true if yes, else false |
6a488035 | 100 | */ |
00be9182 | 101 | public function checkGroupRole($array) { |
6a488035 TO |
102 | if (function_exists('user_load') && isset($array)) { |
103 | $user = user_load(array('uid' => $GLOBALS['user']->uid)); | |
104 | //if giver roles found in user roles - return true | |
105 | foreach ($array as $key => $value) { | |
106 | if (in_array($value, $user->roles)) { | |
107 | return TRUE; | |
108 | } | |
109 | } | |
110 | } | |
111 | return FALSE; | |
112 | } | |
113 | ||
114 | /** | |
d09edf64 | 115 | * Get all the contact emails for users that have a specific role. |
6a488035 | 116 | * |
6a0b768e TO |
117 | * @param string $roleName |
118 | * Name of the role we are interested in. | |
6a488035 | 119 | * |
a6c01b45 CW |
120 | * @return string |
121 | * a comma separated list of email addresses | |
6a488035 TO |
122 | */ |
123 | public function roleEmails($roleName) { | |
124 | static $_cache = array(); | |
125 | ||
126 | if (isset($_cache[$roleName])) { | |
127 | return $_cache[$roleName]; | |
128 | } | |
129 | ||
130 | $uids = array(); | |
131 | $sql = " | |
132 | SELECT {users}.uid | |
133 | FROM {users} | |
134 | LEFT JOIN {users_roles} ON {users}.uid = {users_roles}.uid | |
135 | INNER JOIN {role} ON ( {role}.rid = {users_roles}.rid OR {role}.rid = 2 ) | |
136 | WHERE {role}. name LIKE '%%{$roleName}%%' | |
137 | AND {users}.status = 1 | |
138 | "; | |
139 | ||
140 | $query = db_query($sql); | |
141 | while ($result = db_fetch_object($query)) { | |
2aa397bc TO |
142 | $uids[] = $result->uid; |
143 | } | |
6a488035 TO |
144 | |
145 | $_cache[$roleName] = self::getContactEmails($uids); | |
146 | return $_cache[$roleName]; | |
147 | } | |
148 | ||
149 | /** | |
d09edf64 | 150 | * Get all the contact emails for users that have a specific permission. |
6a488035 | 151 | * |
6a0b768e TO |
152 | * @param string $permissionName |
153 | * Name of the permission we are interested in. | |
6a488035 | 154 | * |
a6c01b45 CW |
155 | * @return string |
156 | * a comma separated list of email addresses | |
6a488035 TO |
157 | */ |
158 | public function permissionEmails($permissionName) { | |
159 | static $_cache = array(); | |
160 | ||
161 | if (isset($_cache[$permissionName])) { | |
162 | return $_cache[$permissionName]; | |
163 | } | |
164 | ||
165 | $uids = array(); | |
166 | $sql = " | |
167 | SELECT {users}.uid, {permission}.perm | |
168 | FROM {users} | |
169 | LEFT JOIN {users_roles} ON {users}.uid = {users_roles}.uid | |
170 | INNER JOIN {permission} ON ( {permission}.rid = {users_roles}.rid OR {permission}.rid = 2 ) | |
171 | WHERE {permission}.perm LIKE '%%{$permissionName}%%' | |
172 | AND {users}.status = 1 | |
173 | "; | |
174 | ||
175 | $query = db_query($sql); | |
176 | while ($result = db_fetch_object($query)) { | |
2aa397bc TO |
177 | $uids[] = $result->uid; |
178 | } | |
6a488035 | 179 | |
2aa397bc TO |
180 | $_cache[$permissionName] = self::getContactEmails($uids); |
181 | return $_cache[$permissionName]; | |
6a488035 TO |
182 | } |
183 | ||
dea68872 | 184 | /** |
e7c15cb6 | 185 | * @inheritDoc |
dea68872 TO |
186 | */ |
187 | public function isModulePermissionSupported() { | |
188 | return TRUE; | |
189 | } | |
190 | ||
6a488035 | 191 | /** |
e7c15cb6 | 192 | * @inheritDoc |
6a488035 TO |
193 | * |
194 | * Does nothing in Drupal 6. | |
195 | */ | |
00be9182 | 196 | public function upgradePermissions($permissions) { |
dea68872 | 197 | // D6 allows us to be really lazy... things get cleaned up when the admin form is next submitted... |
6a488035 TO |
198 | } |
199 | ||
200 | /** | |
201 | * Get the permissions defined in the hook_civicrm_permission implementation | |
202 | * of the given module. | |
203 | * | |
da6b46f4 EM |
204 | * @param $module |
205 | * | |
16b10e64 CW |
206 | * @return array |
207 | * Array of permissions, in the same format as CRM_Core_Permission::getCorePermissions(). | |
6a488035 | 208 | */ |
00be9182 | 209 | public static function getModulePermissions($module) { |
6a488035 TO |
210 | $return_permissions = array(); |
211 | $fn_name = "{$module}_civicrm_permission"; | |
212 | if (function_exists($fn_name)) { | |
213 | $fn_name($return_permissions); | |
214 | } | |
215 | return $return_permissions; | |
216 | } | |
96025800 | 217 | |
6a488035 | 218 | } |