Using after_relationship_add/delete in Users

Hi guys,

I was trying to use the after_relationship_add hook to catch when a Role or a Team is added to an User. I had no problem with Roles, but with Teams, I couldn't make it work.

Here is an example of what I have:

  • The logic_hooks.php file
<?php
$hook_version = 1;
$hook_array = Array();

$hook_array['after_relationship_add'] = Array();
$hook_array['after_relationship_add'][] = Array(1, 'Example', 'custom/modules/Users/UsersRelationshipAdd/UsersRelationshipAdd.php','AuditRolesTeamsUsersAdd', 'createAuditRegistry');

$hook_array['after_relationship_delete'] = Array();
$hook_array['after_relationship_delete'][] = Array(1, 'Example', 'custom/modules/Users/UsersRelationshipDelete/UsersRelationshipDelete.php','AuditRolesTeamsUsersDelete', 'createAuditRegistry');


?>
  • And an example of the hook file for the relationship_add event:
<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

class AuditRolesTeamsUsersAdd{ 
    
     function createAuditRegistry(&$bean, $event, $args){
         
          $GLOBALS['log']->fatal("It works!");
         
          if($args['related_module'] == 'ACLRoles'){
               $GLOBALS['log']->fatal("It works for Roles");
              
          }
         
          if($args['related_module'] == 'Teams'){
               $GLOBALS['log']->fatal("It works for Teams");
          }
     }
}
?>

As I said before, it works for Roles but not for Teams. So, can you tell me if this works in Sugar 6.7?. If not, what other alternative I have for catching what Team was added to an User?

Thanks!

Matt Marum

Tevfik Tümer

Angel Magana

  • Hi Cristian Chavez

    Once check what value you are getting in your logic hook $args and based on that you can give the conditions to do so:

    function createAuditRegistry(&$bean, $event, $args){
             
              $GLOBALS['log']->fatal("It works!");
              $GLOBALS['log']->fatal("Hi Friends.. ".print_r($args, true));

    }

    Hope this Helps..!!

    Best Regards

    S Ramana Raju

  • I take it it the first logger:

              $GLOBALS['log']->fatal("It works!");

    isn't working either for teams?

    Can you try the first logger in an after-save logic hook and see if it fires then? Maybe if it does, there's a workaround using after save?

  • Hi Ramana,

    The first line..

    $GLOBALS['log']->fatal("It works!");

    ..it doesn't write in the log file when I add a team to an user. I assume that the logic hook is not firing, so I'm not able to log the $args element.

  • Hi Alan,

    Where do you suggest to include the after_save logic hook?. In Users? In Teams?. I don't think that the after_save event triggers every time I add a Team to an User, because the User doesn't save with that action.

  • I'd try it in both, just to be sure. I don't think it should fire, but you might find unexpected behaviour.   

  • Hi,

    As you said that it works for the roles but not for the teams it means that your link is established with the roles but not established with the teams. First check that your link is established or not. For that go to
    modules/teams/vardefs
    Then check link with relationship name team. If it does not exist then it means that link is not established.

    Regards,

    RT

  • Rolustech, what do you mean about "links"?. I went to vardefs.php in modules/Teams/ as you said, and I have this:

    <?php
    if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
    /*********************************************************************************
    * By installing or using this file, you are confirming on behalf of the entity
    * subscribed to the SugarCRM Inc. product ("Company") that Company is bound by
    * the SugarCRM Inc. Master Subscription Agreement (“MSA”), which is viewable at:
    * http://www.sugarcrm.com/master-subscription-agreement
    *
    * If Company is not bound by the MSA, then by installing or using this file
    * you are agreeing unconditionally that Company will be bound by the MSA and
    * certifying that you have authority to bind Company accordingly.
    *
    * Copyright (C) 2004-2013 SugarCRM Inc.  All rights reserved.
    ********************************************************************************/


    $dictionary['Team'] = array ( 'table' => 'teams'
                                      , 'fields' => array (
      'id' =>
      array (
        'name' => 'id',
        'vname' => 'LBL_ID',
        'type' => 'id',
        'required'=>true,
        'reportable'=>true,
      ),
      'name' =>
      array (
        'name' => 'name',
        'vname' => 'LBL_PRIMARY_TEAM_NAME',
        'type' => 'name',
        'dbType' => 'varchar',
        'len' => '128',
      ),
      'name_2' =>
      array (
        'name' => 'name_2',
        'vname' => 'LBL_NAME_2',
        'type' => 'name',
        'dbType' => 'varchar',
        'len' => '128',
        'reportable' => false,
      ),
      'associated_user_id' =>
       array (
         'name' => 'associated_user_id',
         'type' => 'id',
         'reportable'=>false,          
      ),
      'date_entered' =>
      array (
        'name' => 'date_entered',
        'vname' => 'LBL_DATE_ENTERED',
        'type' => 'datetime',
        'required'=>true,
      ),
      'date_modified' =>
      array (
        'name' => 'date_modified',
        'vname' => 'LBL_DATE_MODIFIED',
        'type' => 'datetime',
        'required'=>true,
      ),
        'modified_user_id' =>
      array (
        'name' => 'modified_user_id',
        'rname' => 'user_name',
        'id_name' => 'modified_user_id',
        'vname' => 'LBL_ASSIGNED_TO',
        'type' => 'assigned_user_name',
        'table' => 'users',
        'isnull' => 'false',
        'dbType' => 'id',
        'reportable'=>true,
      ),
      'created_by' =>
      array (
        'name' => 'created_by',
        'rname' => 'user_name',
        'id_name' => 'modified_user_id',
        'vname' => 'LBL_ASSIGNED_TO',
        'type' => 'assigned_user_name',
        'table' => 'users',
        'isnull' => 'false',
        'dbType' => 'id',
        'reportable'=>true,
      ),
      'private' =>
      array (
        'name' => 'private',
        'vname' => 'LBL_PRIVATE',
        'type' => 'bool',
        'default' => '0',
      ),
      'description' =>
      array (
        'name' => 'description',
        'vname' => 'LBL_DESCRIPTION',
        'type' => 'text',
      ), 
      'deleted' =>
      array (
        'name' => 'deleted',
        'vname' => 'LBL_DELETED',
        'type' => 'bool',
        'reportable'=>false,
        'required'=>false,
      ),        
      'users' =>
      array (
           'name' => 'users',
        'type' => 'link',
        'relationship' => 'team_memberships',
        'source'=>'non-db',
         'vname'=>'LBL_USERS',
      ),
        'teams_sets' =>
        array (
            'name' => 'teams_sets',
            'type' => 'link',
            'relationship' => 'team_sets_teams',
            'link_type' => 'many',
            'side' => 'right',
            'source' => 'non-db',
            'vname' => 'LBL_TEAMS',
            'studio' => false,
            'duplicate_merge'=> 'disabled',
        ),
     
    )   , 'indices' => array (
          array('name' =>'teamspk', 'type' =>'primary', 'fields'=>array('id')),
          array('name' =>'idx_team_del', 'type' =>'index', 'fields'=>array('name')),
          array('name' =>'idx_team_del_name', 'type' =>'index', 'fields'=>array('deleted','name'))
           )
    );

    $dictionary['TeamMembership'] = array(
         'table' => 'team_memberships',
         'fields' => array(
          'id'=>
              array(
                   'name' => 'id',
                   'type' => 'id',
                   'required' => true
              ),
         'team_id'=>
              array(
                   'name' => 'team_id',
                   'type' => 'id',
              ),
        'user_id'=>          
              array(
                   'name' => 'user_id',
                   'type' => 'id',
              ),
         'explicit_assign'=>
              array(
                   'name' => 'explicit_assign',
                   'type' => 'bool',
                   'len' => '1',
                   'default' => 0,
                   'required' => true
              ),
    'implicit_assign'=>          
              array(
                   'name' => 'implicit_assign',
                   'type' => 'bool',
                   'len' => '1',
                   'default' => '0',
                   'required' => true
              ),
    'date_modified'=>          
              array(
                   'name' => 'date_modified',
                   'type' => 'datetime'
              ),
    'deleted'=>          
              array(
                   'name' => 'deleted',
                   'type' => 'bool',
                   'len'=> '1',
                   'default'=> 0,
              ),
         ),
         'indices' => array(
              array(
                   'name' => 'team_membershipspk',
                   'type' => 'primary',
                   'fields' => array('id')
              ),
              array(
                   'name' => 'idx_team_membership',
                   'type' => 'index',
                   'fields' => array('user_id','team_id')
              ),
              array(
                   'name' => 'idx_teammemb_team_user',
                   'type' => 'alternate_key',
                   'fields' => array('team_id','user_id')
              )
         )
          , 'relationships' => array ('team_memberships' => array('lhs_module'=> 'Teams', 'lhs_table'=> 'teams', 'lhs_key' => 'id',
                                         'rhs_module'=> 'Users', 'rhs_table'=> 'users', 'rhs_key' => 'id',
                                         'relationship_type'=>'many-to-many',
                                         'join_table'=> 'team_memberships', 'join_key_lhs'=>'team_id', 'join_key_rhs'=>'user_id'))
         
    );
    $dictionary['TeamSet'] = array(
         'table' => 'team_sets',
         'fields' => array(
          'id'=>
              array(
                   'name' => 'id',
                   'type' => 'id',
                   'required' => true
              ),
         'name' =>
           array (
             'name' => 'name',
             'vname' => 'LBL_NAME',
             'type' => 'name',
             'dbType' => 'varchar',
             'len' => '128',
           ),
         'team_md5' =>
           array (
             'name' => 'team_md5',
             'vname' => 'LBL_NAME',
             'type' => 'name',
             'dbType' => 'varchar',
             'len' => '32',
           ),
           'team_count' =>
           array (
             'name' => 'team_count',
             'type' => 'int',
              'default' => 0,
           ),
          'primary_team_id'=>
              array(
                   'name' => 'primary_team_id',
                   'type' => 'id',
                   'required' => true,
                   'source' => 'non-db',
              ),
         'date_modified'=>          
                   array(
                        'name' => 'date_modified',
                        'type' => 'datetime'
                   ),
         'deleted'=>          
                   array(
                        'name' => 'deleted',
                        'type' => 'bool',
                        'len'=> '1',
                        'default'=> 0,
                   ),
         'created_by' =>
           array (
             'name' => 'created_by',
             'rname' => 'user_name',
             'id_name' => 'modified_user_id',
             'vname' => 'LBL_ASSIGNED_TO',
             'type' => 'assigned_user_name',
             'table' => 'users',
             'isnull' => 'false',
             'dbType' => 'id',
             'reportable'=>true,
           ),
         'teams' =>
              array (
                   'name' => 'teams',
                   'type' => 'link',
                   'relationship' => 'team_sets_teams',
                   'link_type' => 'one',
                   'source' => 'non-db',
                   'vname' => 'LBL_ACCOUNT',
                'duplicate_merge'=> 'disabled',
              ),
         ),
         'indices' => array (
           array(
                     'name' =>'team_setspk',
                     'type' =>'primary',
                     'fields'=>array('id')
           ),
              array(
                   'name' => 'idx_team_sets_md5',
                   'type' => 'index',
                   'fields' => array('team_md5')
              )
        ),
    );

    $dictionary['TeamSetModule'] = array(
         'table' => 'team_sets_modules',
         'fields' => array(
          'id'=>
              array(
                   'name' => 'id',
                   'type' => 'id',
                   'required' => true
              ),
         'team_set_id' =>
           array (
             'name' => 'team_set_id',
             'type' => 'id',
           ),
          'module_table_name' =>
           array (
             'name' => 'module_table_name',
             'vname' => 'LBL_NAME',
             'type' => 'name',
             'dbType' => 'varchar',
             'len' => '128',
           ),
           'deleted'=>          
                   array(
                        'name' => 'deleted',
                        'type' => 'bool',
                        'len'=> '1',
                        'default'=> 0,
                   ),
         ),     
         'indices' => array(
              array(
                   'name' => 'team_sets_modulespk',
                   'type' => 'primary',
                   'fields' => array('id')
              ),
              array(
                   'name' => 'idx_team_sets_modules',
                   'type' => 'index',
                   'fields' => array('team_set_id')
              )
         )
    );
    ?>

    This file can be extended in an upgrade safe to make the logic hook fired?.

  • Hi Cristian,

    Your link seems to be alright. That means the link is established but logic hook is not called properly. May be you havn't defined after relationship hook correctly. Check the spellings of file and path of file. You should place the hook files in

    custom/modules/yourmodule/logic_hooks.php

    You have to define logic hooks for both modules i.e for users and for teams with the above mentioned path.

    Regards,

    RT

  • For best practices, you should NOT put your hook code in 

    custom/modules/yourmodule/logic_hooks.php

    Instead put it inside 

    custom/Extension/modules/<modulename>/Ext/LogicHooks/<yourfile>.php and write logichook code inside yourfile.php

    Check the below link:

    https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.7/Architecture/Logic_Hooks/Module_Hoo… 

  • The relationship between Users and teams is not a default one as this is part of the visibility framework. So when adding a team the normal after_relationship_add logic hook is not triggered. However you can do a before_save or after_save LogicHook on TeamMemberships by adding the LogicHook to the Teams module directory in custom (i.e. compile your hook into custom/modules/Team/logic_hooks.php or custom/Extension/... as mentioned above). This would allow you to track the modifying user by using the global $current_user reference.

    One caveat is since the hook would also apply to Teams, you would want to make sure that the current Bean that triggered your hook was TeamMemberships, in order to assure your audit is correct.

    For monitoring if a team is deleted you can use the normal after_relationship_delete hook on Users module, that will trigger when a Team is deleted from a user. 

    All credits to Mike Russell for that answer.