After_relationship_delete logic hook for SecurityGroup module

Hi SuiteCRM community

I recently migrated my sugar CE installation to suitecrm and have a problem setting up an after_relationship_delete logic hook for the SecurityGroups module.
I would like to execute a custom function whenever a relationship between a security group and a user is added or removed. The after_relationship_add logic hook is working perfect, the after_relationship_delete logic hook isn’t fireing at all.

I added a logic_hooks.php file in custom/modules/SecurityGroups with the following code:


    $hook_version = 1;
    if (!isset($hook_array) || !is_array($hook_array)) $hook_array = array();

    $hook_array['after_relationship_add'] = Array();
    $hook_array['after_relationship_add'][] = Array(
        1,
        'updateMailman after_relationship_add',
        'custom/modules/SecurityGroups/logic_hooks_updateMailman.php',
        'logic_hooks_updateMailman',
        'updateMailman'
    );
	
    $hook_array['before_relationship_delete'] = Array();
    $hook_array['before_relationship_delete'][] = Array(
        2,
        'updateMailman after_relationship_delete',
        'custom/modules/SecurityGroups/logic_hooks_updateMailman.php',
        'logic_hooks_updateMailman',
        'updateMailman'
    );

My custom function is located in the same directory in the file logic_hooks_updateMailman.php:


    if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
   
    class logic_hooks_updateMailman
    {
        function updateMailman ($bean, $event, $arguments)
        {
			 if ($arguments['related_module'] =='Users'){
				$group_bean = BeanFactory::getBean('SecurityGroups', $arguments['id']);
              	  	        $user_bean  = BeanFactory::getBean('Users', $arguments['related_id']);

				...

			 }
		}
    }

Do you have any idea, why the “add” logic hook is working well, while the delete logic hook isn’t firing at all, when I remove the relationship in one of the subpanels on either the User’s detailed view or the Security Group’s detailed view?

Or: Do you have an idea where to add my custom function update safely elsewhere?

Kind regards and many thanks!
Max

In you logic_hooks.php file your delete logic hook is set to “before_relationship_delete”. Shouldn’t this be after_relationship_delete?

Also you may need to set file permission and run a repair and rebuild.

You’re right! I tried both after_relationship_delete and before_relationship_delete, but none is firing. :frowning:
I ran a repair and rebuild and checked the file permissions, which are ok. This is moreover proven, since the after_relationship_add logic hook is firing perfectly and they both call the same function within the same file.

Thanks for your help!
Max

I can’t see anything that could be the issue. Can you please check your apache log? Or your web server error log? Search for updateMailman?

Maybe there is something in the code you omitted that is causing the script to fail.

Also

It could be that you need a logic hook on the User rather than the Security Group? Or perhaps both.

I think it depends which side of the relationship you are relating the user with a security group. For example, if you go into security groups and add the the user that way…does your logic hook fire? Or does it fire if you go into users and add the security group.

I suspect that this might actually be your problem.

Thanks for your ideas!
I added the logic hooks to the Users module as well. The after_relationship_add logic hook is firing twice, but the after_relationship_delete logic hooks is still not firing…

To test the setup I removed the custom code from the logic hook function and replaced it a simple log command:


    class logic_hooks_updateMailman
    {
        function updateMailman ($bean, $event, $arguments)
        {
			$GLOBALS['log']->fatal("UpdateMailman fired: module: " . $arguments['module'] . ", related module: " . $arguments['related_module'] . ", action: " . $event . ".");
	}
    }

As a result I got:


Mon Mar 28 19:22:24 2016 [14822][1][FATAL] UpdateMailman fired: module: SecurityGroups, related module: Users, action: after_relationship_add.
Mon Mar 28 19:22:24 2016 [14822][1][FATAL] UpdateMailman fired: module: Users, related module: SecurityGroups, action: after_relationship_add.

Both after_relationship_add logic hooks are fired. But no after_relationship_delete is fired, neither when the relationship is deleted in the User’s SecurityGroup Subpanel, nor when it is deleted in the SecurityGroup’s User Subpanel…

Is this a known bug or missing feature that the after_relationship_delete logic hook is not firing?

Moreover, it might be interesting that in both cases the after_relationship_add logic hook from the SecurityGroups module seems to be fired before the User module’s logic hook. The order is the same, independently of whether the user is added to the group on the groups page or on the users page…

Thank!
Max

The problem is in data/Relationships/M2MRelationship.php in lines 265 - 361. It has been deliberately prevented.

If you copy data/Relationships/M2MRelationship.php to custom/data/Relationships/M2MRelationship.php and then change lines 265-361 so that calls $this->callBeforeDelete or $this->callAfterDelete. Then run a repair and rebuild. This should fix your problem in an upgrade safe way.

A word of caution: This could have untold side effects.

1 Like

Many thanks for your hint! I found the problem in data/Relationships/M2MRelationship.php line 278 and 284. Both if statements always fail, because $lhs->$lhsLinkName isn’t loaded yet. In order to solve the problem replace

// old line 278:
if (get_class($lhs) != 'SecurityGroup' && $lhs->$lhsLinkName instanceof Link2)
// new line 278:
if (get_class($lhs) != 'SecurityGroup' && (!empty($lhs->$lhsLinkName) || $lhs->load_relationship($lhsLinkName)) && $lhs->$lhsLinkName instanceof Link2)

and

// old line 284:
if (get_class($rhs) != 'SecurityGroup' && $rhs->$rhsLinkName instanceof Link2)
// new line 284:
if (get_class($rhs) != 'SecurityGroup' && (!empty($rhs->$rhsLinkName) || $rhs->load_relationship($rhsLinkName)) && $rhs->$rhsLinkName instanceof Link2)

Maybe this should generally be changed in one of the next SuiteCRM releases?

However, I wasn’t able the fix this within the custom folder as described above. The file in custom/data/Relationships/M2MRelationship.php wasn’t executed even after a quick repair and rebuild. I wasn’t able to find an update save solution so far…

It is likely to be file permissions that is preventing it from being executed. I always reset the file permission before running any of the repair features.

If you have managed to fix it. You could commit the code back to our repo. https://github.com/salesagility/SuiteCRM by creating a pull request into the hotfix branch. You need to sign our contribution agreement and then fork the repo. Please create an issue detailing the problem first and include the issue number on your pull request comment and other git comments.

Make sure that you only include the necessary changes not every file in SuiteCRM.

I had exactly the same problem whereby I wanted to implement custom bean for a custom module / with related module. I tried overriding the function mark_relationships_deleted but it does not fire when the relationship for the child record is deleted using the “remove” button in a subpanel. The custom bean DOES fire however when you delete the record, it fires (as expected) the mark_deleted override function and then the mark_relationships_deleted function as it should do, but not when just the relationship is removed.

And I tried adding the mark_relationships_deleted override (with a simple suitecrm log write entry) to both the child and parent record, but neither fire. I even tried overriding the delete_linked function as well and that didnt work.

However I am merely wanting an event to fire when relationships are deleted, nothing to do with securitygroups or users. Any thoughts?