]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ACLActions/ACLAction.php
Release 6.5.0
[Github/sugarcrm.git] / modules / ACLActions / ACLAction.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2012 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     var $disable_custom_fields = true;
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 - 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 acl_actions.category = '$category' ";
244         }
245         if(!empty($action)){
246             $additional_where .= " AND acl_actions.name = '$action' ";
247         }
248         if(!empty($type)){
249             $additional_where .= " AND acl_actions.acltype = '$type' ";
250         }
251         $query = "SELECT acl_actions .*, acl_roles_actions.access_override
252                     FROM acl_actions
253                     LEFT JOIN acl_roles_users ON acl_roles_users.user_id = '$user_id' AND  acl_roles_users.deleted = 0
254                     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
255                     WHERE acl_actions.deleted=0 $additional_where ORDER BY category,name";
256         $result = $db->query($query);
257         $selected_actions = array();
258         while($row = $db->fetchByAssoc($result, FALSE) ){
259             $acl = new ACLAction();
260             $isOverride  = false;
261             $acl->populateFromRow($row);
262             if(!empty($row['access_override'])){
263                 $acl->aclaccess = $row['access_override'];
264                 $isOverride = true;
265             }
266             if(!isset($selected_actions[$acl->category])){
267                 $selected_actions[$acl->category] = array();
268
269             }
270             if(!isset($selected_actions[$acl->category][$acl->acltype][$acl->name])
271                 || ($selected_actions[$acl->category][$acl->acltype][$acl->name]['aclaccess'] > $acl->aclaccess
272                     && $isOverride
273                     )
274                 ||
275                     (!empty($selected_actions[$acl->category][$acl->acltype][$acl->name]['isDefault'])
276                     && $isOverride
277                     )
278                 )
279             {
280
281
282                 $selected_actions[$acl->category][$acl->acltype][$acl->name] = $acl->toArray();
283                 $selected_actions[$acl->category][$acl->acltype][$acl->name]['isDefault'] = !$isOverride;
284             }
285
286         }
287
288         //only set the session variable if it was a full list;
289         if(empty($category) && empty($action)){
290             if(!isset($_SESSION['ACL'])){
291                 $_SESSION['ACL'] = array();
292             }
293             $_SESSION['ACL'][$user_id] = $selected_actions;
294         }else{
295             if(empty($action) && !empty($category)){
296                 if(!empty($type)){
297                     $_SESSION['ACL'][$user_id][$category][$type] = $selected_actions[$category][$type];}
298                 $_SESSION['ACL'][$user_id][$category] = $selected_actions[$category];
299             }else{
300                 if(!empty($action) && !empty($category) && !empty($type)){
301                 $_SESSION['ACL'][$user_id][$category][$type][$action] = $selected_actions[$category][$action];
302
303             }
304             }
305         }
306         return $selected_actions;
307     }
308     /**
309     * (static/ non-static)function hasAccess($is_owner= false , $access = 0)
310     * checks if a user has access to this acl if the user is an owner it will check if owners have access
311     *
312     * This function may either be used statically or not. If used staticlly a user must pass in an access level not equal to zero
313     * @param boolean $is_owner
314     * @param int $access
315     * @return true or false
316     */
317     function hasAccess($is_owner=false, $access = 0){
318
319         if($access != 0 && $access == ACL_ALLOW_ALL || ($is_owner && $access == ACL_ALLOW_OWNER))return true;
320         if(isset($this) && isset($this->aclaccess)){
321             if($this->aclaccess == ACL_ALLOW_ALL || ($is_owner && $this->aclaccess == ACL_ALLOW_OWNER))
322             return true;
323         }
324         return false;
325     }
326
327
328
329
330
331
332
333
334
335     /**
336     * static function userHasAccess($user_id, $category, $action, $is_owner = false)
337     *
338     * @param GUID $user_id the user id who you want to check access for
339     * @param STRING $category the category you would like to check access for
340     * @param STRING $action the action of that category you would like to check access for
341     * @param BOOLEAN OPTIONAL $is_owner if the object is owned by the user you are checking access for
342     */
343     function userHasAccess($user_id, $category, $action,$type='module', $is_owner = false){
344        global $current_user;
345        if($current_user->isAdminForModule($category)&& !isset($_SESSION['ACL'][$user_id][$category][$type][$action]['aclaccess'])){
346         return true;
347         }
348         //check if we don't have it set in the cache if not lets reload the cache
349         if(ACLAction::getUserAccessLevel($user_id, $category, 'access', $type) < ACL_ALLOW_ENABLED) return false;
350         if(empty($_SESSION['ACL'][$user_id][$category][$type][$action])){
351             ACLAction::getUserActions($user_id, false);
352
353         }
354
355         if(!empty($_SESSION['ACL'][$user_id][$category][$type][$action])){
356             return ACLAction::hasAccess($is_owner, $_SESSION['ACL'][$user_id][$category][$type][$action]['aclaccess']);
357         }
358         return false;
359
360     }
361     /**
362     * function getUserAccessLevel($user_id, $category, $action,$type='module')
363     * returns the access level for a given category and action
364     *
365     * @param GUID  $user_id
366     * @param STRING $category
367     * @param STRING $action
368     * @param STRING $type
369     * @return INT (ACCESS LEVEL)
370     */
371     function getUserAccessLevel($user_id, $category, $action,$type='module'){
372         if(empty($_SESSION['ACL'][$user_id][$category][$type][$action])){
373             ACLAction::getUserActions($user_id, false);
374
375         }
376         if(!empty($_SESSION['ACL'][$user_id][$category][$type][$action])){
377             return  $_SESSION['ACL'][$user_id][$category][$type][$action]['aclaccess'];
378         }
379     }
380
381     /**
382     * STATIC function userNeedsOwnership($user_id, $category, $action,$type='module')
383     * checks if a user should have ownership to do an action
384     *
385     * @param GUID $user_id
386     * @param STRING $category
387     * @param STRING $action
388     * @param STRING $type
389     * @return boolean
390     */
391     function userNeedsOwnership($user_id, $category, $action,$type='module'){
392         //check if we don't have it set in the cache if not lets reload the cache
393
394         if(empty($_SESSION['ACL'][$user_id][$category][$type][$action])){
395             ACLAction::getUserActions($user_id, false);
396
397         }
398
399
400         if(!empty($_SESSION['ACL'][$user_id][$category][$type][$action])){
401             return $_SESSION['ACL'][$user_id][$category][$type][$action]['aclaccess'] == ACL_ALLOW_OWNER;
402         }
403         return false;
404
405     }
406     /**
407     *
408     * static pass by ref setupCategoriesMatrix(&$categories)
409     * takes in an array of categories and modifes them adding display information
410     *
411     * @param unknown_type $categories
412     */
413     function setupCategoriesMatrix(&$categories){
414         global $ACLActions, $current_user;
415         $names = array();
416         $disabled = array();
417         foreach($categories as $cat_name=>$category){
418             foreach($category as $type_name=>$type){
419                 foreach($type as $act_name=>$action){
420                     $names[$act_name] = translate($ACLActions[$type_name]['actions'][$act_name]['label'], 'ACLActions');
421                     $categories[$cat_name][$type_name][$act_name]['accessColor'] = ACLAction::AccessColor($action['aclaccess']);
422                     if($type_name== 'module'){
423
424                         if($act_name != 'aclaccess' && $categories[$cat_name]['module']['access']['aclaccess'] == ACL_ALLOW_DISABLED){
425                             $categories[$cat_name][$type_name][$act_name]['accessColor'] = 'darkgray';
426                             $disabled[] = $cat_name;
427                         }
428
429                     }
430                     $categories[$cat_name][$type_name][$act_name]['accessName'] = ACLAction::AccessName($action['aclaccess']);
431                     $categories[$cat_name][$type_name][$act_name]['accessLabel'] = ACLAction::AccessLabel($action['aclaccess']);
432
433                     if($cat_name=='Users'&& $act_name=='admin'){
434                         $categories[$cat_name][$type_name][$act_name]['accessOptions'][ACL_ALLOW_DEFAULT]=ACLAction::AccessName(ACL_ALLOW_DEFAULT);;
435                         $categories[$cat_name][$type_name][$act_name]['accessOptions'][ACL_ALLOW_DEV]=ACLAction::AccessName(ACL_ALLOW_DEV);;
436                     }
437                     else{
438                     $categories[$cat_name][$type_name][$act_name]['accessOptions'] =  ACLAction::getAccessOptions($act_name, $type_name);
439                     }
440                 }
441             }
442         }
443
444         if(!is_admin($current_user)){
445             foreach($disabled as $cat_name){
446                 unset($categories[$cat_name]);
447             }
448         }
449         return $names;
450     }
451
452
453
454     /**
455     * function toArray()
456     * returns this acl as an array
457     *
458     * @return array of fields with id, name, access and category
459     */
460     function toArray(){
461         $array_fields = array('id', 'aclaccess');
462         $arr = array();
463         foreach($array_fields as $field){
464             $arr[$field] = $this->$field;
465         }
466         return $arr;
467     }
468
469     /**
470     * function fromArray($arr)
471     * converts an array into an acl mapping name value pairs into files
472     *
473     * @param Array $arr
474     */
475     function fromArray($arr){
476         foreach($arr as $name=>$value){
477             $this->$name = $value;
478         }
479     }
480
481     /**
482     * function clearSessionCache()
483     * clears the session variable storing the cache information for acls
484     *
485     */
486     function clearSessionCache(){
487         unset($_SESSION['ACL']);
488     }
489
490
491
492
493
494
495
496
497
498
499
500
501
502 }
503
504
505 ?>