]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ACLActions/ACLAction.php
Release 6.1.4
[Github/sugarcrm.git] / modules / ACLActions / ACLAction.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38 require_once('modules/ACLActions/actiondefs.php');
39 class ACLAction  extends SugarBean{
40         var $module_dir = 'ACLActions';
41         var $object_name = 'ACLAction';
42         var $table_name = 'acl_actions';
43         var $new_schema = true;
44
45         function ACLAction(){
46                 parent::SugarBean();
47         }
48         
49         /**
50          * static addActions($category, $type='module')
51          * Adds all default actions for a category/type
52          *
53          * @param STRING $category - the category (e.g module name - Accounts, Contacts)
54          * @param STRING $type - the type (e.g. 'module', 'field')
55          */
56         function addActions($category, $type='module'){
57                 global $ACLActions;
58                 $db = DBManagerFactory::getInstance();
59                 if(isset($ACLActions[$type])){
60                         foreach($ACLActions[$type]['actions'] as $action_name =>$action_def){
61                                 
62                                 $action = new ACLAction();
63                                 $query = "SELECT * FROM " . $action->table_name . " WHERE name='$action_name' AND category = '$category' AND acltype='$type' AND deleted=0 ";
64                                 $result = $db->query($query);
65                                 //only add if an action with that name and category don't exist
66                                 $row=$db->fetchByAssoc($result);
67                                 if ($row == null) {
68                                         $action->name = $action_name;
69                                         $action->category = $category;
70                                         $action->aclaccess = $action_def['default'];
71                                         $action->acltype = $type;
72                                         $action->modified_user_id = 1;
73                                         $action->created_by = 1;
74                                         $action->save();
75                                         
76                                 }
77                         }
78                         
79                 }else{
80                         sugar_die("FAILED TO ADD: $category : $name - TYPE $type NOT DEFINED IN modules/ACLActions/actiondefs.php");
81                 }
82                 
83         }
84
85         /**
86         * static removeActions($category, $type='module')
87         * Removes all default actions for a category/type
88         *
89         * @param STRING $category - the category (e.g module name - Accounts, Contacts)
90         * @param STRING $type - the type (e.g. 'module', 'field')
91         */
92         function removeActions($category, $type='module'){
93                 global $ACLActions;
94                 $db = DBManagerFactory::getInstance();
95                 if(isset($ACLActions[$type])){
96                         foreach($ACLActions[$type]['actions'] as $action_name =>$action_def){
97
98                                 $action = new ACLAction();
99                                 $query = "SELECT * FROM " . $action->table_name . " WHERE name='$action_name' AND category = '$category' AND acltype='$type' and deleted=0";
100                                 $result = $db->query($query);
101                                 //only add if an action with that name and category don't exist
102                                 $row=$db->fetchByAssoc($result);
103                                 if ($row != null) {
104                                         $action->mark_deleted($row['id']);
105                                 }
106                         }
107                 }else{
108                         sugar_die("FAILED TO REMOVE: $category : $name - TYPE $type NOT DEFINED IN modules/ACLActions/actiondefs.php");
109                 }
110         }
111         
112         /**
113          * static AccessColor($access)
114          *
115          * returns the color associated with an access level 
116          * these colors exist in the definitions in modules/ACLActions/actiondefs.php
117          * @param INT $access - the access level you want the color for
118          * @return the color either name or hex representation or false if the level does not exist
119          */
120         function AccessColor($access){
121                 global $ACLActionAccessLevels;
122                 if(isset($ACLActionAccessLevels[$access])){
123                         
124                         return $ACLActionAccessLevels[$access]['color'];
125                 }
126                 return false;
127                 
128         }
129         
130         /**
131          * static AccessName($access)
132          *
133          * returns the translated name  associated with an access level 
134          * these label definitions  exist in the definitions in modules/ACLActions/actiondefs.php
135          * @param INT $access - the access level you want the color for
136          * @return the translated access level name or false if the level does not exist
137          */
138         function AccessName($access){
139                 global $ACLActionAccessLevels;
140                 if(isset($ACLActionAccessLevels[$access])){
141                         return translate($ACLActionAccessLevels[$access]['label'], 'ACLActions');
142                 }
143                 return false;
144                 
145         }
146         
147         /**
148      * static AccessLabel($access)
149      *
150      * returns the label  associated with an access level 
151      * these label definitions  exist in the definitions in modules/ACLActions/actiondefs.php
152      * @param INT $access - the access level you want the color for
153      * @return the access level label or false if the level does not exist
154      */
155     function AccessLabel($access){
156         global $ACLActionAccessLevels;
157         if(isset($ACLActionAccessLevels[$access])){
158             $label=preg_replace('/(LBL_ACCESS_)(.*)/', '$2', $ACLActionAccessLevels[$access]['label']);
159             return strtolower($label);
160             
161         }
162         return false;
163         
164     }
165         
166         /**
167          * static getAccessOptions()
168          * this is used for building select boxes 
169          * @return array containg access levels (ints) as keys and access names as values
170          */
171         function getAccessOptions( $action, $type='module'){
172                 global $ACLActions;
173                 $options = array();
174                 
175                 if(empty($ACLActions[$type]['actions'][$action]['aclaccess']))return $options;
176                 foreach($ACLActions[$type]['actions'][$action]['aclaccess'] as $action){
177                         $options[$action] = ACLAction::AccessName($action);
178                 }
179                 return $options;
180                 
181         }
182         
183         /**
184          * function static getDefaultActions()
185          * This function will return a list of acl actions with their default access levels
186          *
187          *
188          */
189         function getDefaultActions($type='module', $action=''){
190                 $query = "SELECT * FROM acl_actions WHERE deleted=0 ";
191                 if(!empty($type)){
192                         $query .= " AND acltype='$type'";
193                 }
194                 if(!empty($action)){
195                         $query .= "AND name='$action'";
196                 }
197                 $query .= " ORDER BY category";
198                 
199                 $db = DBManagerFactory::getInstance();
200                 $result = $db->query($query);
201                 $default_actions = array();
202                 while($row = $db->fetchByAssoc($result) ){
203                         $acl = new ACLAction();
204                         $acl->populateFromRow($row);
205                         $default_actions[] = $acl;
206                 }
207                 return $default_actions;
208         }
209         
210         
211         /**
212          * static getUserActions($user_id,$refresh=false, $category='', $action='')
213          * returns a list of user actions
214          * @param GUID $user_id
215          * @param BOOLEAN $refresh
216          * @param STRING $category
217          * @param STRING $action
218          * @return ARRAY of ACLActionsArray
219          */
220         
221         function getUserActions($user_id,$refresh=false, $category='',$type='', $action=''){
222                 //check in the session if we already have it loaded
223                 if(!$refresh && !empty($_SESSION['ACL'][$user_id])){
224                         if(empty($category) && empty($action)){
225                                 return $_SESSION['ACL'][$user_id];
226                         }else{
227                                 if(!empty($category) && isset($_SESSION['ACL'][$user_id][$category])){
228                                         if(empty($action)){
229                                                 if(empty($type)){
230                                                         return $_SESSION['ACL'][$user_id][$category];
231                                                 }
232                                                 return $_SESSION['ACL'][$user_id][$category][$type];
233                                         }else if(!empty($type) && isset($_SESSION['ACL'][$user_id][$category][$type][$action])){
234                                                 return $_SESSION['ACL'][$user_id][$category][$type][$action];
235                                         }
236                                 }
237                         }
238                 }
239                 //if we don't have it loaded then lets check against the db
240                 $additional_where = '';
241                 $db = DBManagerFactory::getInstance();
242                 if(!empty($category)){
243                         $additional_where .= " AND $this->table_name.category = '$category' ";
244                 }
245                 if(!empty($action)){
246                         $additional_where .= " AND $this->table_name.name = '$action' ";
247                 }
248                 if(!empty($type)){
249                         $additional_where .= " AND $this->table_name.acltype = '$type' ";
250                 }
251         $query=null;
252         if ($db->dbType == 'oci8') {
253         }
254         if (empty($query)) {
255             $query = "SELECT acl_actions .*, acl_roles_actions.access_override 
256                     FROM acl_actions 
257                     LEFT JOIN acl_roles_users ON acl_roles_users.user_id = '$user_id' AND  acl_roles_users.deleted = 0
258                     LEFT JOIN acl_roles_actions ON acl_roles_actions.role_id = acl_roles_users.role_id AND acl_roles_actions.action_id = acl_actions.id AND acl_roles_actions.deleted=0
259                     WHERE acl_actions.deleted=0 $additional_where ORDER BY category,name";
260         }
261                 $result = $db->query($query);
262                 $selected_actions = array();
263                 while($row = $db->fetchByAssoc($result) ){
264                         $acl = new ACLAction();
265                         $isOverride  = false;
266                         $acl->populateFromRow($row);
267                         if(!empty($row['access_override'])){
268                                 $acl->aclaccess = $row['access_override'];
269                                 $isOverride = true;
270                         }
271                         if(!isset($selected_actions[$acl->category])){
272                                 $selected_actions[$acl->category] = array();
273                                 
274                         }
275                         if(!isset($selected_actions[$acl->category][$acl->acltype][$acl->name]) 
276                                 || ($selected_actions[$acl->category][$acl->acltype][$acl->name]['aclaccess'] > $acl->aclaccess 
277                                          && $isOverride
278                                         ) 
279                                 || 
280                                         (!empty($selected_actions[$acl->category][$acl->acltype][$acl->name]['isDefault']) 
281                                         && $isOverride
282                                         )
283                                 ) 
284                         {
285                                 
286                                 
287                                 $selected_actions[$acl->category][$acl->acltype][$acl->name] = $acl->toArray();
288                                 $selected_actions[$acl->category][$acl->acltype][$acl->name]['isDefault'] = !$isOverride;
289                         }
290                         
291                 }
292                 
293                 //only set the session variable if it was a full list;
294                 if(empty($category) && empty($action)){
295                         if(!isset($_SESSION['ACL'])){
296                                 $_SESSION['ACL'] = array();
297                         }
298                         $_SESSION['ACL'][$user_id] = $selected_actions;
299                 }else{
300                         if(empty($action) && !empty($category)){
301                                 if(!empty($type)){
302                                         $_SESSION['ACL'][$user_id][$category][$type] = $selected_actions[$category][$type];}
303                                 $_SESSION['ACL'][$user_id][$category] = $selected_actions[$category];
304                         }else{
305                                 if(!empty($action) && !empty($category) && !empty($type)){
306                                 $_SESSION['ACL'][$user_id][$category][$type][$action] = $selected_actions[$category][$action];
307                                 
308                         }
309                         }
310                 }
311                 return $selected_actions;
312         }
313         /**
314          * (static/ non-static)function hasAccess($is_owner= false , $access = 0)
315          * checks if a user has access to this acl if the user is an owner it will check if owners have access
316          *
317          * This function may either be used statically or not. If used staticlly a user must pass in an access level not equal to zero
318          * @param boolean $is_owner
319          * @param int $access
320          * @return true or false
321          */
322         function hasAccess($is_owner=false, $access = 0){
323                 
324                 if($access != 0 && $access == ACL_ALLOW_ALL || ($is_owner && $access == ACL_ALLOW_OWNER))return true;
325                 if(isset($this) && isset($this->aclaccess)){
326                         if($this->aclaccess == ACL_ALLOW_ALL || ($is_owner && $this->aclaccess == ACL_ALLOW_OWNER))
327                         return true;
328                 }
329                 return false;
330         }
331         
332         
333         
334         
335         
336
337         
338         
339         
340         /**
341          * static function userHasAccess($user_id, $category, $action, $is_owner = false)
342          *
343          * @param GUID $user_id the user id who you want to check access for
344          * @param STRING $category the category you would like to check access for
345          * @param STRING $action the action of that category you would like to check access for
346          * @param BOOLEAN OPTIONAL $is_owner if the object is owned by the user you are checking access for
347          */
348         function userHasAccess($user_id, $category, $action,$type='module', $is_owner = false){
349             global $current_user;
350             if(is_admin_for_module($current_user,$category)&& !isset($_SESSION['ACL'][$user_id][$category][$type][$action]['aclaccess'])){          
351         return true;   
352         }
353         //check if we don't have it set in the cache if not lets reload the cache
354                 if(ACLAction::getUserAccessLevel($user_id, $category, 'access', $type) < ACL_ALLOW_ENABLED) return false;
355                 if(empty($_SESSION['ACL'][$user_id][$category][$type][$action])){
356                         ACLAction::getUserActions($user_id, false);
357                         
358                 }
359                 
360                 if(!empty($_SESSION['ACL'][$user_id][$category][$type][$action])){
361                         return ACLAction::hasAccess($is_owner, $_SESSION['ACL'][$user_id][$category][$type][$action]['aclaccess']);
362                 }
363                 return false;
364                 
365         }
366         /**
367          * function getUserAccessLevel($user_id, $category, $action,$type='module')
368          * returns the access level for a given category and action
369          *
370          * @param GUID  $user_id
371          * @param STRING $category
372          * @param STRING $action
373          * @param STRING $type
374          * @return INT (ACCESS LEVEL)
375          */
376         function getUserAccessLevel($user_id, $category, $action,$type='module'){
377                 if(empty($_SESSION['ACL'][$user_id][$category][$type][$action])){
378                         ACLAction::getUserActions($user_id, false);
379                         
380                 }
381                 if(!empty($_SESSION['ACL'][$user_id][$category][$type][$action])){
382                         return  $_SESSION['ACL'][$user_id][$category][$type][$action]['aclaccess'];
383                 }
384         }
385         
386         /**
387          * STATIC function userNeedsOwnership($user_id, $category, $action,$type='module')
388          * checks if a user should have ownership to do an action
389          *
390          * @param GUID $user_id
391          * @param STRING $category
392          * @param STRING $action
393          * @param STRING $type
394          * @return boolean
395          */
396         function userNeedsOwnership($user_id, $category, $action,$type='module'){
397                 //check if we don't have it set in the cache if not lets reload the cache
398                 
399                 if(empty($_SESSION['ACL'][$user_id][$category][$type][$action])){
400                         ACLAction::getUserActions($user_id, false);
401                         
402                 }
403                 
404                 
405                 if(!empty($_SESSION['ACL'][$user_id][$category][$type][$action])){
406                         return $_SESSION['ACL'][$user_id][$category][$type][$action]['aclaccess'] == ACL_ALLOW_OWNER;
407                 }
408                 return false;
409                 
410         }
411         /**
412          * 
413          * static pass by ref setupCategoriesMatrix(&$categories)
414          * takes in an array of categories and modifes them adding display information
415          *
416          * @param unknown_type $categories
417          */
418         function setupCategoriesMatrix(&$categories){
419                 global $ACLActions, $current_user;
420                 $names = array();
421                 $disabled = array();
422                 foreach($categories as $cat_name=>$category){
423             foreach($category as $type_name=>$type){
424                 foreach($type as $act_name=>$action){
425                     $names[$act_name] = translate($ACLActions[$type_name]['actions'][$act_name]['label'], 'ACLActions');
426                     $categories[$cat_name][$type_name][$act_name]['accessColor'] = ACLAction::AccessColor($action['aclaccess']);
427                     if($type_name== 'module'){
428                         
429                         if($act_name != 'aclaccess' && $categories[$cat_name]['module']['access']['aclaccess'] == ACL_ALLOW_DISABLED){
430                                 $categories[$cat_name][$type_name][$act_name]['accessColor'] = 'darkgray';
431                                 $disabled[] = $cat_name;
432                         }
433                         
434                     }
435                     $categories[$cat_name][$type_name][$act_name]['accessName'] = ACLAction::AccessName($action['aclaccess']);
436                     $categories[$cat_name][$type_name][$act_name]['accessLabel'] = ACLAction::AccessLabel($action['aclaccess']);
437                     
438                     if($cat_name=='Users'&& $act_name=='admin'){
439                         $categories[$cat_name][$type_name][$act_name]['accessOptions'][ACL_ALLOW_DEFAULT]=ACLAction::AccessName(ACL_ALLOW_DEFAULT);;
440                         $categories[$cat_name][$type_name][$act_name]['accessOptions'][ACL_ALLOW_DEV]=ACLAction::AccessName(ACL_ALLOW_DEV);;
441                     }
442                     else{
443                     $categories[$cat_name][$type_name][$act_name]['accessOptions'] =  ACLAction::getAccessOptions($act_name, $type_name);
444                     }
445                 }
446             }
447         }
448
449         if(!is_admin($current_user)){
450                 foreach($disabled as $cat_name){
451                         unset($categories[$cat_name]);
452                 }
453         }
454         return $names;  
455         }
456         
457         
458         
459         /**
460          * function toArray()
461          * returns this acl as an array
462          *
463          * @return array of fields with id, name, access and category
464          */
465         function toArray(){
466                 $array_fields = array('id', 'aclaccess');
467                 $arr = array();
468                 foreach($array_fields as $field){
469                         $arr[$field] = $this->$field;
470                 }
471                 return $arr;
472         }
473         
474         /**
475          * function fromArray($arr)
476          * converts an array into an acl mapping name value pairs into files
477          *
478          * @param Array $arr
479          */
480         function fromArray($arr){
481                 foreach($arr as $name=>$value){
482                         $this->$name = $value;
483                 }
484         }
485         
486         /**
487          * function clearSessionCache()
488          * clears the session variable storing the cache information for acls
489          *
490          */
491         function clearSessionCache(){
492                 unset($_SESSION['ACL']);
493         }
494         
495         
496         
497         
498         
499         
500         
501
502         
503         
504         
505         
506
507 }
508
509
510 ?>