]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Roles/Role.php
Release 6.5.0
[Github/sugarcrm.git] / modules / Roles / Role.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 /*********************************************************************************
39
40  * Description:
41  ********************************************************************************/
42
43
44
45
46
47
48
49
50 class Role extends SugarBean {
51
52         var $field_name_map;
53         
54         var $id;
55         var $deleted;
56         var $date_entered;
57         var $date_modified;
58         var $modified_user_id;
59         var $created_by;
60         var $name;
61         var $description;
62         var $modules;
63         var $disable_row_level_security = true;
64
65         var $table_name = 'roles';
66         var $rel_module_table = 'roles_modules';
67         var $object_name = 'Role';
68         var $module_dir = 'Roles';
69         var $new_schema = true;
70
71         function Role()
72         {
73                 parent::SugarBean();
74         }
75         
76         function get_summary_text()
77         {
78                 return $this->name;
79         }
80
81         function create_export_query($order_by, $where)
82         {
83                 return $this->create_new_list_query($order_by, $where);
84         }       
85         
86         function query_modules($allow = 1)
87         {
88                 $query = "SELECT module_id FROM roles_modules WHERE ";
89                 $query .= "role_id = '$this->id' AND allow = '$allow' AND deleted=0";
90                 $result = $this->db->query($query);
91                 
92                 $return_array = array();
93                 
94                 while($row = $this->db->fetchByAssoc($result))
95                 {
96                         array_push($return_array, $row['module_id']);
97                 }
98                 
99                 return $return_array;
100         }
101         function set_module_relationship($role_id, &$mod_ids, $allow)
102         {
103                 foreach($mod_ids as $mod_id)
104                 {
105                         if($mod_id != '')
106                                 $this->set_relationship('roles_modules', array( 'module_id'=>$mod_id, 'role_id'=>$role_id, 'allow'=>$allow ));
107                 }
108         }
109         
110         function clear_module_relationship($role_id)
111         {
112                 $query = "DELETE FROM roles_modules WHERE role_id='$role_id'";
113                 $this->db->query($query);
114         }
115
116         function set_user_relationship($role_id, &$user_ids)
117         {
118                 foreach($user_ids as $user_id)
119                 {
120                         if($user_id != '')
121                                 $this->set_relationship('roles_users', array( 'user_id'=>$user_id, 'role_id'=>$role_id ));
122                 }
123         }
124
125         function clear_user_relationship($role_id, $user_id)
126         {
127                 $query = "DELETE FROM roles_users WHERE role_id='$role_id' AND user_id='$user_id'";
128                 $this->db->query($query);
129         }
130
131         function query_user_allowed_modules($user_id)
132         {
133                 $userArray = array();
134                 global $app_list_strings;
135                 
136                 
137         
138                 $sql = "SELECT role_id FROM roles_users WHERE user_id='$user_id'";
139                 
140                 $result = $this->db->query($sql);
141                 
142                 while($row = $this->db->fetchByAssoc($result))
143                 {
144                         $role_id = $row["role_id"];
145                         $sql = "SELECT module_id FROM roles_modules WHERE role_id='$role_id' AND allow='1'";
146                         $res = $this->db->query($sql);
147                         
148                         while($col = $this->db->fetchByAssoc($res))
149                         {
150                                 $key = $col['module_id'];
151                                 if(!(array_key_exists($key, $userArray)))
152                                 {
153                                         $userArray[$key] = $app_list_strings['moduleList'][$key];
154                                 }
155                         }
156                 }
157         
158                 return $userArray;
159         }
160         
161         function query_user_disallowed_modules($user_id, &$allowed)
162         {
163                 global $moduleList;
164                 
165                 $returnArray = array();
166                 
167                 foreach($moduleList as $key=>$val)
168                 {
169                         if(array_key_exists($val, $allowed))
170                                 continue;
171                         $returnArray[$val] = $val;
172                 }
173                 
174                 return $returnArray;
175
176         }
177
178         function get_users()
179         {
180                 // First, get the list of IDs.
181
182                 
183                 
184                 $query = "SELECT user_id as id FROM roles_users WHERE role_id='$this->id' AND deleted=0";
185                 
186                 return $this->build_related_list($query, new User());
187         }
188
189         function check_user_role_count($user_id)
190         {
191                 $query =  "SELECT count(*) AS num FROM roles_users WHERE ";
192                 $query .= "user_id='$user_id' AND deleted=0";
193                 $result = $this->db->query($query);
194                 
195                 $row = $this->db->fetchByAssoc($result);
196                 
197                 return $row['num'];
198         }               
199                 
200 }
201
202 ?>