]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - service/v4_1/SugarWebServiceUtilv4_1.php
Release 6.5.0
[Github/sugarcrm.git] / service / v4_1 / SugarWebServiceUtilv4_1.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37 require_once('service/v4/SugarWebServiceUtilv4.php');
38
39 class SugarWebServiceUtilv4_1 extends SugarWebServiceUtilv4
40 {
41     /**
42          * Validate the provided session information is correct and current.  Load the session.
43          *
44          * @param String $session_id -- The session ID that was returned by a call to login.
45          * @return true -- If the session is valid and loaded.
46          * @return false -- if the session is not valid.
47          */
48         function validate_authenticated($session_id)
49     {
50                 $GLOBALS['log']->info('Begin: SoapHelperWebServices->validate_authenticated');
51                 if(!empty($session_id)){
52
53                         // only initialize session once in case this method is called multiple times
54                         if(!session_id()) {
55                            session_id($session_id);
56                            session_start();
57                         }
58
59                         if(!empty($_SESSION['is_valid_session']) && $this->is_valid_ip_address('ip_address') && $_SESSION['type'] == 'user'){
60
61                                 global $current_user;
62                                 require_once('modules/Users/User.php');
63                                 $current_user = new User();
64                                 $current_user->retrieve($_SESSION['user_id']);
65                                 $this->login_success();
66                                 $GLOBALS['log']->info('Begin: SoapHelperWebServices->validate_authenticated - passed');
67                                 $GLOBALS['log']->info('End: SoapHelperWebServices->validate_authenticated');
68                                 return true;
69                         }
70
71                         $GLOBALS['log']->debug("calling destroy");
72                         session_destroy();
73                 }
74                 LogicHook::initialize();
75                 $GLOBALS['logic_hook']->call_custom_logic('Users', 'login_failed');
76                 $GLOBALS['log']->info('End: SoapHelperWebServices->validate_authenticated - validation failed');
77                 return false;
78         }
79
80
81     function check_modules_access($user, $module_name, $action='write'){
82         if(!isset($_SESSION['avail_modules'])){
83             $_SESSION['avail_modules'] = get_user_module_list($user);
84         }
85         if(isset($_SESSION['avail_modules'][$module_name])){
86             if($action == 'write' && $_SESSION['avail_modules'][$module_name] == 'read_only'){
87                 if(is_admin($user))return true;
88                 return false;
89             }elseif($action == 'write' && strcmp(strtolower($module_name), 'users') == 0 && !$user->isAdminForModule($module_name)){
90                 //rrs bug: 46000 - If the client is trying to write to the Users module and is not an admin then we need to stop them
91                 return false;
92             }
93             return true;
94         }
95         return false;
96
97     }
98
99     /**
100      * getRelationshipResults
101      * Returns the
102      *
103      * @param Mixed $bean The SugarBean instance to retrieve relationships from
104      * @param String $link_field_name The name of the relationship entry to fetch relationships for
105      * @param Array $link_module_fields Array of fields of relationship entries to return
106      * @param string $optional_where String containing an optional WHERE select clause
107      * @param string $order_by String containing field to order results by
108      * @param Number $offset -- where to start in the return (defaults to 0)
109      * @param Number $limit -- number of results to return (defaults to all)
110      * @return array|bool Returns an Array of relationship results; false if relationship could not be retrieved
111      */
112     function getRelationshipResults($bean, $link_field_name, $link_module_fields, $optional_where = '', $order_by = '', $offset = 0, $limit = '') {
113         $GLOBALS['log']->info('Begin: SoapHelperWebServices->getRelationshipResults');
114                 require_once('include/TimeDate.php');
115                 global $beanList, $beanFiles, $current_user;
116                 global $disable_date_format, $timedate;
117
118                 $bean->load_relationship($link_field_name);
119
120                 if (isset($bean->$link_field_name)) {
121                         //First get all the related beans
122             $params = array();
123             $params['offset'] = $offset;
124             $params['limit'] = $limit;
125
126             if (!empty($optional_where))
127             {
128                 $params['where'] = $optional_where;
129             }
130
131             $related_beans = $bean->$link_field_name->getBeans($params);
132             //Create a list of field/value rows based on $link_module_fields
133                         $list = array();
134             $filterFields = array();
135             if (!empty($order_by) && !empty($related_beans))
136             {
137                 $related_beans = order_beans($related_beans, $order_by);
138             }
139             foreach($related_beans as $id => $bean)
140             {
141                 if (empty($filterFields) && !empty($link_module_fields))
142                 {
143                     $filterFields = $this->filter_fields($bean, $link_module_fields);
144                 }
145                 $row = array();
146                 foreach ($filterFields as $field) {
147                     if (isset($bean->$field))
148                     {
149                         if (isset($bean->field_defs[$field]['type']) && $bean->field_defs[$field]['type'] == 'date') {
150                             $row[$field] = $timedate->to_display_date_time($bean->$field);
151                         } else {
152                             $row[$field] = $bean->$field;
153                         }
154                     }
155                     else
156                     {
157                         $row[$field] = "";
158                     }
159                 }
160                 //Users can't see other user's hashes
161                 if(is_a($bean, 'User') && $current_user->id != $bean->id && isset($row['user_hash'])) {
162                     $row['user_hash'] = "";
163                 }
164                 $list[] = $row;
165             }
166             $GLOBALS['log']->info('End: SoapHelperWebServices->getRelationshipResults');
167             return array('rows' => $list, 'fields_set_on_rows' => $filterFields);
168                 } else {
169                         $GLOBALS['log']->info('End: SoapHelperWebServices->getRelationshipResults - ' . $link_field_name . ' relationship does not exists');
170                         return false;
171                 } // else
172
173         } // fn
174 }