2

I have written a custom PHP file to be called from AJAX request for SugarCRM. However, when I try to print

global $current_user;

print_r($current_user);

It prints blank values with all the fields listed in the array like below.

User Object
(
    [name] => 
    [full_name] => 
    [id] => 
    [user_name] => 
    [user_hash] => 
    [salutation] => 
    [first_name] => 
    [last_name] => 
    [date_entered] => 
    [date_modified] => 
    [modified_user_id] => 
    [created_by] => 
    [created_by_name] => 
    [modified_by_name] => 
    [description] => 
    [phone_home] => 
    [phone_mobile] => 
    [phone_work] => 
    [phone_other] => 
    [phone_fax] => 
    [email1] => 
    [email2] => 
    [address_street] => 
    [address_city] => 
    [address_state] => 
    [address_postalcode] => 
    [address_country] => 
    [status] => 
    [title] => 
    [portal_only] => 0
    [department] => 
    [authenticated] => 
    [error_string] => 
    [is_admin] => 0
    [employee_status] => 
    [messenger_id] => 
    [messenger_type] => 
    [is_group] => 
    [accept_status] => 
    [team_id] => 
    [receive_notifications] => 1
    [default_team] => 
    [reports_to_name] => 
    [reports_to_id] => 
    [team_exists] => 
    [table_name] => users
    [module_dir] => Users
    [object_name] => User
    [user_preferences] => 
    [importable] => 1

.....

I have searched in SugarCRM Forums but no use. I suspect the reason could be valid entry point is not defined in my custom page, as don't understand how to define except the following methods

if(!defined('sugarEntry'))define('sugarEntry', true);
  if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); 
  require_once('include/entryPoint.php');
4

2 回答 2

1

我知道这个问题可以追溯到 2013 年,想到分享:)

当你打电话时会发生这种情况

global $current_user;

在 sugarcrm 之外,我做的一个选项是在自定义用户模块中创建一个钩子并在那里访问 $current_user 变量,并且只获取那些需要的值,也许保存在会话中?,取决于你。

希望这对任何人都有帮助:)

我所做的示例

创建文件custom_external_session.php

<?php if(!defined('sugarEntry') ||!sugarEntry) die('Not A Valid Entry Point');
class custom_external_session {
     function get_current_user(& $focus, $event, $arguments){
          global $current_user;
          $user_id = $current_user->id;
          session_start();
          $_SESSION["custom_current_user"] =  $user_id;
     }
}
  • 将此文件上传到 /var/www/html/sugarcrm/custom/modules/Users/

注意:/var/www/html/sugarcrm/ -> 这取决于您放置 sugarcrm 的位置

  • 然后在该文件夹(/custom/modules/Users/)上,编辑 logic_hooks.php 然后添加此代码

    $hook_array['after_login'][] = Array(5, 'custom_external_session', 'custom/modules/Users/custom_external_session.php', 'custom_external_session', 'get_current_user');
    
  • 你现在可以使用这个:$_SESSION["custom_current_user"]来获取当前的 user_id,然后你可以用它来记录,也可以编辑或添加更多关于保存会话的信息,比如你想要全名等,就做它在 custom_external_session 下。我不确定是否有更好的方法使用外部代码

  • 请记住在编辑custom_external_session时始终重新登录,以便更新值。

希望这对你来说更清楚。

于 2014-04-08T02:43:33.333 回答
0

另一种方法是从js端加载

var userBean = app.data.createBean("Users", { id: app.user.id });
userBean.fetch({
         success:function(userData){
          //everything you need will be on userData
          }
});
于 2021-09-30T16:17:44.313 回答