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.
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.
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
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
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.
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.
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 ********************************************************************************/
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(
55 // bug 16790 - missing get_summary_text method led Tracker to display SugarBean's "base implementation"
56 function get_summary_text()
63 * function setAction($role_id, $action_id, $access)
65 * Sets the relationship between a role and an action and sets the access level of that relationship
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...
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);
79 * static getUserRoles($user_id)
80 * returns a list of ACLRoles for a given user id
82 * @param GUID $user_id
83 * @return a list of ACLRole objects
85 function getUserRoles($user_id, $getAsNameArray = true){
87 //if we don't have it loaded then lets check against the db
88 $additional_where = '';
89 $query = "SELECT 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 ";
95 $result = $GLOBALS['db']->query($query);
96 $user_roles = array();
98 while($row = $GLOBALS['db']->fetchByAssoc($result) ){
99 $role = new ACLRole();
100 $role->populateFromRow($row);
102 $user_roles[] = $role->name;
104 $user_roles[] = $role;
111 * static getUserRoleNames($user_id)
112 * returns a list of Role names for a given user id
114 * @param GUID $user_id
115 * @return a list of ACLRole Names
117 function getUserRoleNames($user_id){
119 $user_roles = sugar_cache_retrieve("RoleMembershipNames_".$user_id);
122 //if we don't have it loaded then lets check against the db
123 $additional_where = '';
124 $query = "SELECT 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 ";
130 $result = $GLOBALS['db']->query($query);
131 $user_roles = array();
133 while($row = $GLOBALS['db']->fetchByAssoc($result) ){
134 $user_roles[] = $row['name'];
137 sugar_cache_put("RoleMembershipNames_".$user_id, $user_roles);
145 * static getAllRoles($returnAsArray = false)
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
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";
155 $result = $db->query($query);
158 while($row = $db->fetchByAssoc($result) ){
159 $role = new ACLRole();
160 $role->populateFromRow($row);
162 $roles[] = $role->toArray();
174 * static getRoleActions($role_id)
176 * gets the actions of a given role
178 * @param GUID $role_id
179 * @return array of actions
181 function getRoleActions($role_id, $type='module'){
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 ";
191 $query .=" FROM acl_actions ";
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";
196 $query .= " WHERE acl_actions.deleted=0 ORDER BY acl_actions.category, acl_actions.name";
197 $result = $db->query($query);
198 $role_actions = array();
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'];
206 $action->aclaccess = ACL_ALLOW_DEFAULT;
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])){
215 if(!isset($role_actions[$action->category])){
216 $role_actions[$action->category] = array();
219 $role_actions[$action->category][$action->acltype][$action->name] = $action->toArray();
223 return $role_actions;
227 * function mark_relationships_deleted($id)
229 * special case to delete acl_roles_actions relationship
231 * @param ACLRole GUID $id
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);
243 * returns this role as an array
245 * @return array of fields with id, name, description
248 $array_fields = array('id', 'name', 'description');
250 foreach($array_fields as $field){
251 if(isset($this->$field)){
252 $arr[$field] = $this->$field;
262 * converts an array into an role mapping name value pairs into files
266 function fromArray($arr){
267 foreach($arr as $name=>$value){
268 $this->$name = $value;