]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ACLRoles/ACLRole.php
Release 6.1.4
[Github/sugarcrm.git] / modules / ACLRoles / ACLRole.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
39 class ACLRole extends SugarBean{
40         var $module_dir = 'ACLRoles';
41         var $object_name = 'ACLRole';
42         var $table_name = 'acl_roles';
43         var $new_schema = true;
44         var $disable_row_level_security = true;
45         var $relationship_fields = array(
46                                                                         'user_id'=>'users'
47                                                                 );
48         
49         var $created_by;
50         
51         function ACLRole(){
52                 parent::SugarBean();
53         }
54         
55         // bug 16790 - missing get_summary_text method led Tracker to display SugarBean's "base implementation"
56         function get_summary_text()
57         {
58                 return "$this->name";
59         }       
60
61
62 /**
63  * function setAction($role_id, $action_id, $access)
64  * 
65  * Sets the relationship between a role and an action and sets the access level of that relationship
66  *
67  * @param GUID $role_id - the role id
68  * @param GUID $action_id - the ACL Action id
69  * @param int $access - the access level ACL_ALLOW_ALL ACL_ALLOW_NONE ACL_ALLOW_OWNER...
70  */
71 function setAction($role_id, $action_id, $access){
72         $relationship_data = array('role_id'=>$role_id, 'action_id'=>$action_id,);
73         $additional_data = array('access_override'=>$access);
74         $this->set_relationship('acl_roles_actions',$relationship_data,true, true,$additional_data);
75 }
76
77
78 /**
79  * static  getUserRoles($user_id)
80  * returns a list of ACLRoles for a given user id
81  *
82  * @param GUID $user_id
83  * @return a list of ACLRole objects
84  */
85 function getUserRoles($user_id, $getAsNameArray = true){
86
87                 //if we don't have it loaded then lets check against the db
88                 $additional_where = '';
89                 $query = "SELECT acl_roles.* ".
90                         "FROM acl_roles ".
91                         "INNER JOIN acl_roles_users ON acl_roles_users.user_id = '$user_id' ".
92                                 "AND acl_roles_users.role_id = acl_roles.id AND acl_roles_users.deleted = 0 ".
93                         "WHERE acl_roles.deleted=0 ";
94         
95                 $result = $GLOBALS['db']->query($query);
96                 $user_roles = array();
97                 
98                 while($row = $GLOBALS['db']->fetchByAssoc($result) ){
99                         $role = new ACLRole();
100                         $role->populateFromRow($row);
101                         if($getAsNameArray)
102                                 $user_roles[] = $role->name;
103                         else
104                                 $user_roles[] = $role;
105                 }
106
107                 return $user_roles;
108 }
109         
110 /**
111  * static  getUserRoleNames($user_id)
112  * returns a list of Role names for a given user id
113  *
114  * @param GUID $user_id
115  * @return a list of ACLRole Names
116  */
117 function getUserRoleNames($user_id){
118
119                 $user_roles = sugar_cache_retrieve("RoleMembershipNames_".$user_id);
120
121                 if(!$user_roles){
122                         //if we don't have it loaded then lets check against the db
123                         $additional_where = '';
124                         $query = "SELECT acl_roles.* ".
125                                 "FROM acl_roles ".
126                                 "INNER JOIN acl_roles_users ON acl_roles_users.user_id = '$user_id' ".
127                                         "AND acl_roles_users.role_id = acl_roles.id AND acl_roles_users.deleted = 0 ".
128                                 "WHERE acl_roles.deleted=0 ";
129
130                         $result = $GLOBALS['db']->query($query);
131                         $user_roles = array();
132
133                         while($row = $GLOBALS['db']->fetchByAssoc($result) ){
134                                 $user_roles[] = $row['name'];
135                         }
136
137                         sugar_cache_put("RoleMembershipNames_".$user_id, $user_roles);
138                 }
139
140                 return $user_roles;
141 }
142         
143         
144 /**
145  * static getAllRoles($returnAsArray = false)
146  *
147  * @param boolean $returnAsArray - should it return the results as an array of arrays or as an array of ACLRoles
148  * @return either an array of array representations of acl roles or an array of ACLRoles
149  */
150 function getAllRoles($returnAsArray = false){
151                 $db = DBManagerFactory::getInstance();
152                 $query = "SELECT acl_roles.* FROM acl_roles
153                                         WHERE acl_roles.deleted=0 ORDER BY name";
154         
155                 $result = $db->query($query);
156                 $roles = array();
157                 
158                 while($row = $db->fetchByAssoc($result) ){
159                         $role = new ACLRole();
160                         $role->populateFromRow($row);
161                         if($returnAsArray){
162                                 $roles[] = $role->toArray();
163                         }else{
164                                 $roles[] = $role;
165                         }
166                 
167                 }
168                 return $roles;
169                 
170
171 }
172         
173 /**
174  * static getRoleActions($role_id)
175  * 
176  * gets the actions of a given role
177  *
178  * @param GUID $role_id
179  * @return array of actions 
180  */
181 function getRoleActions($role_id, $type='module'){
182                 global $beanList;
183                 //if we don't have it loaded then lets check against the db
184                 $additional_where = '';
185                 $db = DBManagerFactory::getInstance();
186                 $query = "SELECT acl_actions.*";
187                 //only if we have a role id do we need to join the table otherwise lets use the ones defined in acl_actions as the defaults
188                 if(!empty($role_id)){
189                                 $query .=" ,acl_roles_actions.access_override ";
190                 }
191                 $query .=" FROM acl_actions ";
192                 
193                 if(!empty($role_id)){
194                         $query .=               " LEFT JOIN acl_roles_actions ON acl_roles_actions.role_id = '$role_id' AND  acl_roles_actions.action_id = acl_actions.id AND acl_roles_actions.deleted = 0";
195                 }
196                 $query .= " WHERE acl_actions.deleted=0 ORDER BY acl_actions.category, acl_actions.name";
197                 $result = $db->query($query);
198                 $role_actions = array();
199                 
200                 while($row = $db->fetchByAssoc($result) ){
201                         $action = new ACLAction();
202                         $action->populateFromRow($row);
203                         if(!empty($row['access_override'])){
204                                 $action->aclaccess = $row['access_override'];
205                         }else{
206                                 $action->aclaccess = ACL_ALLOW_DEFAULT; 
207                                 
208                         }
209                         //#27877 . If  there is no this module in beanlist , we will not show them in UI, no matter this module was deleted or not in ACL_ACTIONS table.
210                         if(empty($beanList[$action->category])){
211                                 continue;
212                         }
213                         //end
214                         
215                         if(!isset($role_actions[$action->category])){
216                                 $role_actions[$action->category] = array();
217                         }
218         
219                         $role_actions[$action->category][$action->acltype][$action->name] = $action->toArray(); 
220                         
221                 
222                 }
223                 return $role_actions;
224         
225         }
226 /**
227  * function mark_relationships_deleted($id)
228  * 
229  * special case to delete acl_roles_actions relationship 
230  *
231  * @param ACLRole GUID $id
232  */
233 function mark_relationships_deleted($id){
234                 //we need to delete the actions relationship by hand (special case)
235                 $date_modified = db_convert("'".gmdate($GLOBALS['timedate']->get_db_date_time_format())."'", 'datetime');
236                 $query =  "UPDATE acl_roles_actions SET deleted=1 , date_modified=$date_modified WHERE role_id = '$id' AND deleted=0";
237                 $this->db->query($query);
238                 parent::mark_relationships_deleted($id);
239 }
240
241 /**
242  *  toArray()
243          * returns this role as an array
244          *
245          * @return array of fields with id, name, description
246          */
247         function toArray(){
248                 $array_fields = array('id', 'name', 'description');
249                 $arr = array();
250                 foreach($array_fields as $field){
251                         if(isset($this->$field)){
252                                 $arr[$field] = $this->$field;
253                         }else{
254                                 $arr[$field] = '';
255                         }
256                 }
257                 return $arr;
258         }
259         
260         /**
261          * fromArray($arr)
262          * converts an array into an role mapping name value pairs into files
263          *
264          * @param Array $arr
265          */
266         function fromArray($arr){
267                 foreach($arr as $name=>$value){
268                         $this->$name = $value;
269                 }
270         }
271 }
272
273 ?>