Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | class CRM_ACL_API { | |
36 | ||
37 | /** | |
38 | * The various type of permissions | |
39 | * | |
40 | * @var int | |
41 | */ | |
42 | CONST EDIT = 1; | |
43 | CONST VIEW = 2; | |
44 | CONST DELETE = 3; | |
45 | CONST CREATE = 4; | |
46 | CONST SEARCH = 5; | |
47 | CONST ALL = 6; | |
48 | ||
49 | /** | |
50 | * given a permission string, check for access requirements | |
51 | * | |
52 | * @param string $str the permission to check | |
53 | * @param int $contactID the contactID for whom the check is made | |
54 | * | |
55 | * @return boolean true if yes, else false | |
56 | * @static | |
57 | * @access public | |
58 | */ | |
59 | static function check($str, $contactID = NULL) { | |
60 | if ($contactID == NULL) { | |
61 | $session = CRM_Core_Session::singleton(); | |
62 | $contactID = $session->get('userID'); | |
63 | } | |
64 | ||
65 | if (!$contactID) { | |
66 | // anonymous user | |
67 | $contactID = 0; | |
68 | } | |
69 | ||
70 | return CRM_ACL_BAO_ACL::check($str, $contactID); | |
71 | } | |
72 | ||
73 | /** | |
74 | * Get the permissioned where clause for the user | |
75 | * | |
76 | * @param int $type the type of permission needed | |
77 | * @param array $tables (reference ) add the tables that are needed for the select clause | |
78 | * @param array $whereTables (reference ) add the tables that are needed for the where clause | |
79 | * @param int $contactID the contactID for whom the check is made | |
80 | * @param bool $onlyDeleted whether to include only deleted contacts | |
81 | * @param bool $skipDeleteClause don't add delete clause if this is true, | |
82 | * this means it is handled by generating query | |
83 | * | |
84 | * @return string the group where clause for this user | |
85 | * @access public | |
86 | */ | |
87 | public static function whereClause($type, | |
88 | &$tables, | |
89 | &$whereTables, | |
90 | $contactID = NULL, | |
91 | $onlyDeleted = FALSE, | |
92 | $skipDeleteClause = FALSE | |
93 | ) { | |
94 | // the default value which is valid for rhe final AND | |
95 | $deleteClause = ' ( 1 ) '; | |
96 | if (!$skipDeleteClause) { | |
97 | if (CRM_Core_Permission::check('access deleted contacts') and $onlyDeleted) { | |
98 | $deleteClause = '(contact_a.is_deleted)'; | |
99 | } | |
100 | else { | |
101 | // CRM-6181 | |
102 | $deleteClause = '(contact_a.is_deleted = 0)'; | |
103 | } | |
104 | } | |
105 | ||
106 | // first see if the contact has edit / view all contacts | |
107 | if (CRM_Core_Permission::check('edit all contacts') || | |
108 | ($type == self::VIEW && | |
109 | CRM_Core_Permission::check('view all contacts') | |
110 | ) | |
111 | ) { | |
112 | return $skipDeleteClause ? ' ( 1 ) ' : $deleteClause; | |
113 | } | |
114 | ||
115 | if ($contactID == NULL) { | |
116 | $session = CRM_Core_Session::singleton(); | |
117 | $contactID = $session->get('userID'); | |
118 | } | |
119 | ||
120 | if (!$contactID) { | |
121 | // anonymous user | |
122 | $contactID = 0; | |
123 | } | |
124 | ||
125 | return implode(' AND ', | |
126 | array( | |
127 | CRM_ACL_BAO_ACL::whereClause($type, | |
128 | $tables, | |
129 | $whereTables, | |
130 | $contactID | |
131 | ), | |
132 | $deleteClause, | |
133 | ) | |
134 | ); | |
135 | } | |
136 | ||
137 | /** | |
138 | * get all the groups the user has access to for the given operation | |
139 | * | |
140 | * @param int $type the type of permission needed | |
141 | * @param int $contactID the contactID for whom the check is made | |
142 | * | |
143 | * @return array the ids of the groups for which the user has permissions | |
144 | * @access public | |
145 | */ | |
146 | public static function group( | |
147 | $type, | |
148 | $contactID = NULL, | |
149 | $tableName = 'civicrm_saved_search', | |
150 | $allGroups = NULL, | |
151 | $includedGroups = NULL | |
152 | ) { | |
153 | if ($contactID == NULL) { | |
154 | $session = CRM_Core_Session::singleton(); | |
155 | $contactID = $session->get('userID'); | |
156 | } | |
157 | ||
158 | if (!$contactID) { | |
159 | // anonymous user | |
160 | $contactID = 0; | |
161 | } | |
162 | ||
163 | return CRM_ACL_BAO_ACL::group($type, $contactID, $tableName, $allGroups, $includedGroups); | |
164 | } | |
165 | ||
166 | /** | |
167 | * check if the user has access to this group for operation $type | |
168 | * | |
169 | * @param int $type the type of permission needed | |
170 | * @param int $contactID the contactID for whom the check is made | |
171 | * | |
172 | * @return array the ids of the groups for which the user has permissions | |
173 | * @access public | |
174 | */ | |
175 | public static function groupPermission( | |
176 | $type, | |
177 | $groupID, | |
178 | $contactID = NULL, | |
179 | $tableName = 'civicrm_saved_search', | |
180 | $allGroups = NULL, | |
181 | $includedGroups = NULL | |
182 | ) { | |
183 | static $cache = array(); | |
184 | ||
185 | if (!$contactID) { | |
186 | $session = CRM_Core_Session::singleton(); | |
187 | $contactID = NULL; | |
188 | if ($session->get('userID')) { | |
189 | $contactID = $session->get('userID'); | |
190 | } | |
191 | } | |
192 | ||
193 | $key = "{$tableName}_{$type}_{$contactID}"; | |
194 | if (array_key_exists($key, $cache)) { | |
195 | $groups = &$cache[$key]; | |
196 | } | |
197 | else { | |
198 | $groups = self::group($type, $contactID, $tableName, $allGroups, $includedGroups); | |
199 | $cache[$key] = $groups; | |
200 | } | |
201 | ||
202 | return in_array($groupID, $groups) ? TRUE : FALSE; | |
203 | } | |
204 | } | |
205 |