INFRA-132 - Remove white space after an opening "(" or before a closing ")"
[civicrm-core.git] / CRM / Core / Permission / Drupal.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_Drupal extends CRM_Core_Permission_DrupalBase{
40
41 /**
100fef9d 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 /**
100fef9d 57 * The current set of permissioned groups for the user
6a488035
TO
58 *
59 * @var array
60 */
61 protected $_viewPermissionedGroups;
62 protected $_editPermissionedGroups;
63
64
085823c1 65 /**
100fef9d 66 * Given a permission string, check for access requirements
085823c1 67 *
6a0b768e
TO
68 * @param string $str
69 * The permission to check.
085823c1 70 *
100fef9d 71 * @param int $contactID
77b97be7 72 *
085823c1 73 * @return boolean true if yes, else false
085823c1 74 */
00be9182 75 public function check($str, $contactID = NULL) {
085823c1
TO
76 $str = $this->translatePermission($str, 'Drupal', 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 }
6a488035
TO
91
92 /**
93 * Given a roles array, check for access requirements
94 *
6a0b768e
TO
95 * @param array $array
96 * The roles to check.
6a488035
TO
97 *
98 * @return boolean true if yes, else false
6a488035 99 */
00be9182 100 public function checkGroupRole($array) {
6a488035 101 if (function_exists('user_load') && isset($array)) {
481a74f4 102 $user = user_load($GLOBALS['user']->uid);
6a488035
TO
103 //if giver roles found in user roles - return true
104 foreach ($array as $key => $value) {
105 if (in_array($value, $user->roles)) {
106 return TRUE;
107 }
108 }
109 }
110 return FALSE;
111 }
112
7fccad46
TO
113 /**
114 * {@inheritDoc}
115 */
116 public function isModulePermissionSupported() {
117 return TRUE;
118 }
119
6a488035 120 /**
c4bc14ed 121 * {@inheritdoc}
6a488035 122 */
00be9182 123 public function upgradePermissions($permissions) {
0d8fc497
TO
124 if (empty($permissions)) {
125 throw new CRM_Core_Exception("Cannot upgrade permissions: permission list missing");
6a488035 126 }
0d8fc497
TO
127 $query = db_delete('role_permission')
128 ->condition('module', 'civicrm')
129 ->condition('permission', array_keys($permissions), 'NOT IN');
6a488035
TO
130 $query->execute();
131 }
d0c9a093
BS
132
133 /**
134 * Get all the contact emails for users that have a specific permission
135 *
6a0b768e
TO
136 * @param string $permissionName
137 * Name of the permission we are interested in.
d0c9a093
BS
138 *
139 * @return string a comma separated list of email addresses
140 */
141 public function permissionEmails($permissionName) {
142 static $_cache = array();
143
144 if (isset($_cache[$permissionName])) {
145 return $_cache[$permissionName];
146 }
147
148 $uids = array();
149 $sql = "
150 SELECT {users}.uid, {role_permission}.permission
151 FROM {users}
152 JOIN {users_roles}
153 ON {users}.uid = {users_roles}.uid
154 JOIN {role_permission}
155 ON {role_permission}.rid = {users_roles}.rid
156 WHERE {role_permission}.permission = '{$permissionName}'
157 AND {users}.status = 1
158 ";
159
160 $result = db_query($sql);
481a74f4 161 foreach ($result as $record) {
d0c9a093
BS
162 $uids[] = $record->uid;
163 }
164
165 $_cache[$permissionName] = self::getContactEmails($uids);
166 return $_cache[$permissionName];
167 }
6a488035 168}