]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Users/Save.php
Release 6.4.0
[Github/sugarcrm.git] / modules / Users / Save.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-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
40  * Description:  TODO: To be written.
41  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42  * All Rights Reserved.
43  * Contributor(s): ______________________________________..
44  ********************************************************************************/
45
46
47 require_once('modules/MySettings/TabController.php');
48
49 $display_tabs_def = isset($_REQUEST['display_tabs_def']) ? urldecode($_REQUEST['display_tabs_def']) : '';
50 $hide_tabs_def = isset($_REQUEST['hide_tabs_def']) ? urldecode($_REQUEST['hide_tabs_def']): '';
51 $remove_tabs_def = isset($_REQUEST['remove_tabs_def']) ? urldecode($_REQUEST['remove_tabs_def']): '';
52
53 $DISPLAY_ARR = array();
54 $HIDE_ARR = array();
55 $REMOVE_ARR = array();
56
57 parse_str($display_tabs_def,$DISPLAY_ARR);
58 parse_str($hide_tabs_def,$HIDE_ARR);
59 parse_str($remove_tabs_def,$REMOVE_ARR);
60
61 if (isset($_POST['id']))
62         sugar_die("Unauthorized access to administration.");
63 if (isset($_POST['record']) && !is_admin($current_user)
64      && !$GLOBALS['current_user']->isAdminForModule('Users')
65      && $_POST['record'] != $current_user->id)
66 sugar_die("Unauthorized access to administration.");
67 elseif (!isset($_POST['record']) && !is_admin($current_user)
68      && !$GLOBALS['current_user']->isAdminForModule('Users'))
69 sugar_die ("Unauthorized access to user administration.");
70 $focus = new User();
71 $focus->retrieve($_POST['record']);
72
73 // Flag to determine whether to save a new password or not.
74 // Bug 43241 - Changed $focus->id to $focus->user_name to make sure that a system generated password is made when converting employee to user
75 if(empty($focus->user_name))
76 {
77     $newUser = true;
78     clear_register_value('user_array');
79 } else {
80     $newUser = false;
81 }
82         
83
84 if(!$current_user->is_admin && !$GLOBALS['current_user']->isAdminForModule('Users')
85     && $current_user->id != $focus->id) {
86         $GLOBALS['log']->fatal("SECURITY:Non-Admin ". $current_user->id . " attempted to change settings for user:". $focus->id);
87         header("Location: index.php?module=Users&action=Logout");
88         exit;
89 }
90 if(!$current_user->is_admin  && !$GLOBALS['current_user']->isAdminForModule('Users')
91     && !empty($_POST['is_admin'])) {
92         $GLOBALS['log']->fatal("SECURITY:Non-Admin ". $current_user->id . " attempted to change is_admin settings for user:". $focus->id);
93         header("Location: index.php?module=Users&action=Logout");
94         exit;
95 }
96
97
98     // Populate the custom fields
99     foreach ($focus->field_defs as $fieldName => $field ) {
100         if ( isset($field['source']) && $field['source'] == 'custom_fields' ) {
101             if ( isset($_POST[$fieldName]) ) {
102                 $focus->$field = $_POST[$fieldName];
103             }
104         }
105     }
106     
107
108         $portal=array("user_name","last_name","status","portal_only");
109         $group=array("user_name","last_name","status","is_group");
110         if(isset($_POST['portal_only']) && ($_POST['portal_only']=='1' || $focus->portal_only)){
111                 foreach($portal as $field){
112                         if(isset($_POST[$field]))
113                         {
114                                 $value = $_POST[$field];
115                                 $focus->$field = $value;
116                                 
117                         }
118                 }
119         }
120         
121         if(isset($_POST['is_group']) && ($_POST['is_group']=='1' || $focus->is_group)){
122                 foreach($group as $field){
123                         if(isset($_POST[$field]))
124                         {
125                                 $value = $_POST[$field];
126                                 $focus->$field = $value;
127                                 
128                         }
129                 }
130         }
131
132         
133         // copy the group or portal user name over.  We renamed the field in order to ensure auto-complete would not change the value
134         if(isset($_POST['user_name']))
135         {
136                 $focus->user_name = $_POST['user_name'];
137         }
138         
139         // if the user saved is a Regular User
140         if(!$focus->is_group && !$focus->portal_only){  
141                 
142                 foreach($focus->column_fields as $field)
143                 {
144                         if(isset($_POST[$field]))
145                         {
146                                 $value = $_POST[$field];
147                                 $focus->$field = $value;
148                         }
149                 }
150                 foreach($focus->additional_column_fields as $field)
151                 {
152                         if(isset($_POST[$field]))
153                         {
154                                 $value = $_POST[$field];
155                                 $focus->$field = $value;
156                         }
157                 }
158                 
159                 $focus->is_group=0;
160                 $focus->portal_only=0;
161
162                 if(isset($_POST['status']) && $_POST['status']== "Inactive") $focus->employee_status = "Terminated"; //bug49972
163                 
164                         if(isset($_POST['user_name']))
165                 {
166                         $focus->user_name = $_POST['user_name'];
167                 }       
168                 if(isset($_POST['is_admin']) && ($_POST['is_admin'] == 'on' || $_POST['is_admin'] == '1')) $focus->is_admin = 1;
169                 elseif(empty($_POST['is_admin'])) $focus->is_admin = 0;
170                 //if(empty($_POST['portal_only']) || !empty($_POST['is_admin'])) $focus->portal_only = 0;
171                 //if(empty($_POST['is_group'])    || !empty($_POST['is_admin'])) $focus->is_group = 0;
172                 if(empty($_POST['receive_notifications'])) $focus->receive_notifications = 0;
173                 
174                 if(isset($_POST['mailmerge_on']) && !empty($_POST['mailmerge_on'])) {
175                         $focus->setPreference('mailmerge_on','on', 0, 'global');
176                 } else {
177                         $focus->setPreference('mailmerge_on','off', 0, 'global');
178                 }
179         
180                 if(isset($_POST['user_max_tabs']))
181                 {
182                         $current_theme_max_tabs = SugarThemeRegistry::current()->maxTabs;
183                         if(empty($current_theme_max_tabs) || $_POST['user_max_tabs'] <= SugarThemeRegistry::current()->maxTabs){
184                                 $focus->setPreference('max_tabs', $_POST['user_max_tabs'], 0, 'global');
185                         }
186                 }
187             
188             if(isset($_POST['user_swap_last_viewed']))
189             {
190                 $focus->setPreference('swap_last_viewed', $_POST['user_swap_last_viewed'], 0, 'global');
191             }
192             else
193             {
194                 $focus->setPreference('swap_last_viewed', '', 0, 'global');
195             }
196             
197             if(isset($_POST['user_swap_shortcuts']))
198             {
199                 $focus->setPreference('swap_shortcuts', $_POST['user_swap_shortcuts'], 0, 'global');
200             }
201             else
202             {
203                 $focus->setPreference('swap_shortcuts', '', 0, 'global');
204             }
205             
206             if(isset($_POST['use_group_tabs']))
207             {
208                 $focus->setPreference('navigation_paradigm', $_POST['use_group_tabs'], 0, 'global');
209             }
210             else
211             {
212                 $focus->setPreference('navigation_paradigm', 'gm', 0, 'global');
213             }
214
215             if(isset($_POST['user_subpanel_tabs']))
216             {
217                 $focus->setPreference('subpanel_tabs', $_POST['user_subpanel_tabs'], 0, 'global');
218             }
219             else
220             {
221                 $focus->setPreference('subpanel_tabs', '', 0, 'global');
222             }
223         
224         if(isset($_POST['user_theme']))
225         {
226             $focus->setPreference('user_theme', $_POST['user_theme'], 0, 'global');
227             $_SESSION['authenticated_user_theme'] = $_POST['user_theme'];
228         }
229         
230         if(isset($_POST['user_module_favicon']))
231             {
232                 $focus->setPreference('module_favicon', $_POST['user_module_favicon'], 0, 'global');
233             }
234             else
235             {
236                 $focus->setPreference('module_favicon', '', 0, 'global');
237             }
238             
239                 $tabs = new TabController();
240                 if(isset($_POST['display_tabs']))
241                         $tabs->set_user_tabs($DISPLAY_ARR['display_tabs'], $focus, 'display');
242                 if(isset($HIDE_ARR['hide_tabs'])){
243                         $tabs->set_user_tabs($HIDE_ARR['hide_tabs'], $focus, 'hide');
244                 
245                 }else{
246                         $tabs->set_user_tabs(array(), $focus, 'hide');  
247                 }
248                 if(is_admin($current_user)){
249                         if(isset($REMOVE_ARR['remove_tabs'])){
250                                 $tabs->set_user_tabs($REMOVE_ARR['remove_tabs'], $focus, 'remove');
251                         }else{
252                                 $tabs->set_user_tabs(array(), $focus, 'remove');                
253                         }
254                 }
255         
256             if(isset($_POST['no_opps'])) {
257                 $focus->setPreference('no_opps',$_POST['no_opps'], 0, 'global');
258             }
259             else {
260                 $focus->setPreference('no_opps','off', 0, 'global');
261             }
262             
263                 if(     isset($_POST['should_remind']) 
264                         && $_POST['should_remind'] == '1' 
265                         && isset($_POST['reminder_time'])) {
266                         $focus->setPreference('reminder_time', $_POST['reminder_time'], 0, 'global');
267                 } else {
268                         // cn: bug 5522, need to unset reminder time if unchecked.
269                         $focus->setPreference('reminder_time', -1, 0, 'global');
270                 }
271                 if(isset($_POST['timezone'])) $focus->setPreference('timezone',$_POST['timezone'], 0, 'global');
272                 if(isset($_POST['ut'])) $focus->setPreference('ut', '0', 0, 'global');
273                 else $focus->setPreference('ut', '1', 0, 'global');
274                 if(isset($_POST['currency'])) $focus->setPreference('currency',$_POST['currency'], 0, 'global');
275                 if(isset($_POST['default_currency_significant_digits'])) $focus->setPreference('default_currency_significant_digits',$_POST['default_currency_significant_digits'], 0, 'global');
276                 if(isset($_POST['num_grp_sep'])) $focus->setPreference('num_grp_sep', $_POST['num_grp_sep'], 0, 'global');
277                 if(isset($_POST['dec_sep'])) $focus->setPreference('dec_sep', $_POST['dec_sep'], 0, 'global');
278                 if(isset($_POST['fdow'])) $focus->setPreference('fdow', $_POST['fdow'], 0, 'global');
279                 if(isset($_POST['dateformat'])) $focus->setPreference('datef',$_POST['dateformat'], 0, 'global');
280                 if(isset($_POST['timeformat'])) $focus->setPreference('timef',$_POST['timeformat'], 0, 'global');
281                 if(isset($_POST['timezone'])) $focus->setPreference('timezone',$_POST['timezone'], 0, 'global');
282                 if(isset($_POST['mail_fromname'])) $focus->setPreference('mail_fromname',$_POST['mail_fromname'], 0, 'global');
283                 if(isset($_POST['mail_fromaddress'])) $focus->setPreference('mail_fromaddress',$_POST['mail_fromaddress'], 0, 'global');
284                 if(isset($_POST['mail_sendtype'])) $focus->setPreference('mail_sendtype', $_POST['mail_sendtype'], 0, 'global');
285                 if(isset($_POST['mail_smtpserver'])) $focus->setPreference('mail_smtpserver',$_POST['mail_smtpserver'], 0, 'global');
286                 if(isset($_POST['mail_smtpport'])) $focus->setPreference('mail_smtpport',$_POST['mail_smtpport'], 0, 'global');
287                 if(isset($_POST['mail_smtpuser'])) $focus->setPreference('mail_smtpuser',$_POST['mail_smtpuser'], 0, 'global');
288                 if(isset($_POST['mail_smtppass'])) $focus->setPreference('mail_smtppass',$_POST['mail_smtppass'], 0, 'global');
289                 if(isset($_POST['default_locale_name_format'])) $focus->setPreference('default_locale_name_format',$_POST['default_locale_name_format'], 0, 'global');
290                 if(isset($_POST['export_delimiter'])) $focus->setPreference('export_delimiter', $_POST['export_delimiter'], 0, 'global');
291                 if(isset($_POST['default_export_charset'])) $focus->setPreference('default_export_charset', $_POST['default_export_charset'], 0, 'global');
292                 if(isset($_POST['use_real_names'])) $focus->setPreference('use_real_names', 'on', 0, 'global');
293                 else $focus->setPreference('use_real_names', 'off', 0, 'global');
294         
295                 if(isset($_POST['mail_smtpauth_req'])) {
296                         $focus->setPreference('mail_smtpauth_req',$_POST['mail_smtpauth_req'] , 0, 'global');
297                 } else {
298                         $focus->setPreference('mail_smtpauth_req','', 0, 'global');
299                 }
300                 
301                 // SSL-enabled SMTP connection
302                 if(isset($_POST['mail_smtpssl'])) {
303                         $focus->setPreference('mail_smtpssl', 1, 0, 'global');
304                 } else {
305                         $focus->setPreference('mail_smtpssl', 0, 0, 'global');
306                 }
307             ///////////////////////////////////////////////////////////////////////////
308             ////    PDF SETTINGS
309             foreach($_POST as $k=>$v){
310                 if(strpos($k,"sugarpdf_pdf") !== false){
311                     $focus->setPreference($k, $v, 0, 'global');
312                 }
313             }
314             ////    PDF SETTINGS
315                 ///////////////////////////////////////////////////////////////////////////
316                 
317                 ///////////////////////////////////////////////////////////////////////////
318                 ////    SIGNATURES
319                 if(isset($_POST['signature_id']))
320                         $focus->setPreference('signature_default', $_POST['signature_id'], 0, 'global');        
321                 
322                 if(isset($_POST['signature_prepend'])) $focus->setPreference('signature_prepend',$_POST['signature_prepend'], 0, 'global');
323                 ////    END SIGNATURES
324                 ///////////////////////////////////////////////////////////////////////////
325                 
326                 
327                  if(isset($_POST['email_link_type'])) $focus->setPreference('email_link_type', $_REQUEST['email_link_type']);
328                 if(isset($_REQUEST['email_show_counts'])) {
329                         $focus->setPreference('email_show_counts', $_REQUEST['email_show_counts'], 0, 'global');
330                 } else {
331                         $focus->setPreference('email_show_counts', 0, 0, 'global');
332                 }
333                 if(isset($_REQUEST['email_editor_option']))
334                         $focus->setPreference('email_editor_option', $_REQUEST['email_editor_option'], 0, 'global');
335                 if(isset($_REQUEST['default_email_charset']))
336                         $focus->setPreference('default_email_charset', $_REQUEST['default_email_charset'], 0, 'global');
337         
338                 if(isset($_POST['calendar_publish_key'])) $focus->setPreference('calendar_publish_key',$_POST['calendar_publish_key'], 0, 'global');
339         }
340         
341         if (!$focus->verify_data())
342         {
343                 header("Location: index.php?action=Error&module=Users&error_string=".urlencode($focus->error_string));
344                 exit;
345         }
346         else
347         {       $GLOBALS['sugar_config']['disable_team_access_check'] = true;
348                 $focus->save();
349                 $GLOBALS['sugar_config']['disable_team_access_check'] = false;
350                 $return_id = $focus->id;
351                 $ieVerified = true;
352
353                 global $new_pwd;
354                 $new_pwd='';
355                 if((isset($_POST['old_password']) || $focus->portal_only) && 
356                         (isset($_POST['new_password']) && !empty($_POST['new_password'])) && 
357                         (isset($_POST['password_change']) && $_POST['password_change'] == 'true') ) {
358                         if (!$focus->change_password($_POST['old_password'], $_POST['new_password'])) {
359                            if((isset($_POST['page']) && $_POST['page'] == 'EditView')){
360                                header("Location: index.php?action=EditView&module=Users&record=".$_POST['record']."&error_password=".urlencode($focus->error_string));
361                                exit;
362                            }
363                            if((isset($_POST['page']) && $_POST['page'] == 'Change')){
364                                header("Location: index.php?action=ChangePassword&module=Users&record=".$_POST['record']."&error_password=".urlencode($focus->error_string));
365                                exit;
366                            }
367                    }
368                    else{
369                                 if ($newUser)
370                                         $new_pwd='3';
371                                 else
372                                         $new_pwd='1';
373                    }
374                 }
375                 
376                 ///////////////////////////////////////////////////////////////////////////
377                 ////    OUTBOUND EMAIL SAVES
378                 ///////////////////////////////////////////////////////////////////////////
379
380                 $sysOutboundAccunt = new OutboundEmail();
381                 
382                 //If a user is not alloweed to use the default system outbound account then they will be
383                 //saving their own username/password for the system account
384                 if( ! $sysOutboundAccunt->isAllowUserAccessToSystemDefaultOutbound() )
385         {
386             $userOverrideOE = $sysOutboundAccunt->getUsersMailerForSystemOverride($focus->id);
387             if($userOverrideOE != null)
388             {
389                 //User is alloweed to clear username and pass so no need to check for blanks.
390                 $userOverrideOE->mail_smtpuser = $_REQUEST['mail_smtpuser'];
391                 $userOverrideOE->mail_smtppass = $_REQUEST['mail_smtppass'];
392                 $userOverrideOE->save();
393             }
394             else 
395             {
396                 //If a user name and password for the mail account is set, create the users override account.
397                 if( ! (empty($_REQUEST['mail_smtpuser']) || empty($_REQUEST['mail_smtppass'])) )
398                     $sysOutboundAccunt->createUserSystemOverrideAccount($focus->id,$_REQUEST['mail_smtpuser'],$_REQUEST['mail_smtppass'] );
399             }
400         }
401                 
402                 
403                 ///////////////////////////////////////////////////////////////////////////
404                 ////    INBOUND EMAIL SAVES
405                 if(isset($_REQUEST['server_url']) && !empty($_REQUEST['server_url'])) {
406                         
407                         $ie = new InboundEmail();
408                         if(false === $ie->savePersonalEmailAccount($return_id, $focus->user_name)) {
409                                 header("Location: index.php?action=Error&module=Users&error_string=&ie_error=true&id=".$return_id);
410                                 die(); // die here, else the header redirect below takes over.
411                         }
412                 } elseif(isset($_REQUEST['ie_id']) && !empty($_REQUEST['ie_id']) && empty($_REQUEST['server_url'])) {
413                         // user is deleting their I-E
414                         
415                         $ie = new InboundEmail();
416                         $ie->deletePersonalEmailAccount($_REQUEST['ie_id'], $focus->user_name);
417                 }
418                 ////    END INBOUND EMAIL SAVES 
419                 ///////////////////////////////////////////////////////////////////////////
420                 if(($newUser) && !($focus->is_group) && !($focus->portal_only) && isset($sugar_config['passwordsetting']['SystemGeneratedPasswordON']) && $sugar_config['passwordsetting']['SystemGeneratedPasswordON']){
421                         $new_pwd='2';
422                         require_once('modules/Users/GeneratePassword.php');
423                 }
424                         
425         }
426
427
428     //handle navigation from user wizard
429     if(isset($_REQUEST['whatnext'])){
430         if($_REQUEST['whatnext']== 'import'){
431             header("Location:index.php?module=Import&action=step1&import_module=Administration");
432             return;
433         }elseif($_REQUEST['whatnext']== 'users'){
434             header("Location:index.php?module=Users&action=index");
435             return;
436         }elseif($_REQUEST['whatnext']== 'settings'){
437             header("Location:index.php?module=Configurator&action=EditView");
438             return;
439         }elseif($_REQUEST['whatnext']== 'studio'){
440             header("Location:index.php?module=ModuleBuilder&action=index&type=studio");
441             return;
442         }else{
443             //do nothing, let the navigation continue as normal using code below
444         }
445
446     }
447
448 if(isset($_REQUEST['return_module']) && $_REQUEST['return_module'] != "") $return_module = $_REQUEST['return_module'];
449 else $return_module = "Users";
450 if(isset($_REQUEST['return_action']) && $_REQUEST['return_action'] != "") $return_action = $_REQUEST['return_action'];
451 else $return_action = "DetailView";
452 if(isset($_REQUEST['return_id']) && $_REQUEST['return_id'] != "") $return_id = $_REQUEST['return_id'];
453
454 $GLOBALS['log']->debug("Saved record with id of ".$return_id);
455
456 $redirect = "index.php?action={$return_action}&module={$return_module}&record={$return_id}";
457 $redirect .= isset($_REQUEST['type']) ? "&type={$_REQUEST['type']}" : ''; // cn: bug 6897 - detect redirect to Email compose
458 $redirect .= isset($_REQUEST['return_id']) ? "&return_id={$_REQUEST['return_id']}" : '';
459 $redirect .= ($new_pwd!='') ? "&pwd_set=".$new_pwd : '';
460 header("Location: {$redirect}");
461 ?>