SugarCRM Users Logic Hooks

Custom Logic Hooks are a great way of adding functionality to SugarCRM. The logic hook below was used to establish a default role for new users. This was required for the automatic LDAP Users functionality that is built into SugarCRM. This logic hook was used with SugarCRM CE 5.2 in a production environment.

<?php
// custom/modules/Users/logic_hooks.php

// Do not store anything in this file that is not part of the array or the hook version.  This file will
// be automatically rebuilt in the future.
$hook_version = 1;
$hook_array = Array();
// position, file, function
$hook_array['after_login'] = Array();
$hook_array['after_login'][] = Array(1, 'SugarFeed old feed entry remover', 'modules/SugarFeed/SugarFeedFlush.php','SugarFeedFlush', 'flushStaleEntries');
$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(1, 'addInProfile', 'custom/modules/Users/UserLogicHook.php', 'UserLogicHook', 'addInProfile');
?>
<?php
// custom/modules/Users/UserLogicHook.php

if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

class UserLogicHook {

    function addInProfile(&$bean, $event, $arguments) {

        global $currentModule;
        $role_name = "Sales Manager";

        $query = "SELECT id FROM users WHERE id = '{$bean->id}'";
        $result = $bean->db->query($query, true);

        // Only update role when creating a new user.
        if ($bean->db->getRowCount($result) == 0 && $_REQUEST['module'] == 'Users' && $_REQUEST['action'] == 'Save') {

            $query = "SELECT id FROM acl_roles WHERE name = '$role_name'";
            $result = $bean->db->query($query, true);

            if($bean->db->getRowCount($result) == 1) {

                $row = $bean->db->fetchByAssoc($result);
                $bean->load_relationship('aclroles');
                $bean->aclroles->add($row['id']);
                require_once('modules/ACL/install_actions.php');

            } else {
                sugar_die("Role does not exist! Contact the system administrator");
            }
        }
    }

}
?>

You must be logged in to post a comment.